Kibou is a federated social networking server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

activity.rs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. use database::models::{InsertActivity, QueryActivity};
  2. use database::schema::activities;
  3. use database::schema::activities::dsl::*;
  4. use diesel::ExpressionMethods;
  5. use diesel::sql_query;
  6. use diesel::query_dsl::QueryDsl;
  7. use diesel::query_dsl::RunQueryDsl;
  8. use diesel::pg::PgConnection;
  9. use serde_json;
  10. pub struct Activity
  11. {
  12. pub id: i64,
  13. pub data: serde_json::Value,
  14. pub actor: String
  15. }
  16. pub fn get_activity_by_id(db_connection: &PgConnection, activity_id: i64) -> Result<Activity, diesel::result::Error>
  17. {
  18. match activities
  19. .filter(id.eq(activity_id))
  20. .limit(1)
  21. .first::<QueryActivity>(db_connection)
  22. {
  23. Ok(activity) => Ok(serialize_activity(activity)),
  24. Err(e) => Err(e)
  25. }
  26. }
  27. /// # Note
  28. ///
  29. /// [TODO]
  30. /// Originally I did not want any protocol-specific functions in this file, so it might be
  31. /// reasonable to move ActivityPub-specfic database querys into their own module
  32. pub fn get_ap_object_by_id(db_connection: &PgConnection, object_id: &str) -> Result<Activity, diesel::result::Error>
  33. {
  34. match sql_query(format!("SELECT * FROM activities WHERE data->'object'->>'id' = '{}' LIMIT 1;", object_id))
  35. .clone()
  36. .load::<QueryActivity>(db_connection)
  37. {
  38. Ok(activity) => {
  39. if !activity.is_empty()
  40. {
  41. let new_activity = std::borrow::ToOwned::to_owned(&activity[0]);
  42. Ok(serialize_activity(new_activity))
  43. } else { Err(diesel::result::Error::NotFound) }
  44. },
  45. Err(e) => Err(e),
  46. }
  47. }
  48. pub fn get_ap_object_replies_by_id(db_connection: &PgConnection, object_id: &str) -> Result<Vec<Activity>, diesel::result::Error>
  49. {
  50. match sql_query(format!("SELECT * FROM activities WHERE data->'object'->>'inReplyTo' = '{}';", object_id))
  51. .clone()
  52. .load::<QueryActivity>(db_connection)
  53. {
  54. Ok(activity) => {
  55. let mut serialized_activites: Vec<Activity> = vec![];
  56. for object in activity
  57. {
  58. serialized_activites.push(serialize_activity(object));
  59. }
  60. Ok(serialized_activites)
  61. },
  62. Err(e) => Err(e),
  63. }
  64. }
  65. pub fn count_ap_object_replies_by_id(db_connection: &PgConnection, object_id: &str) -> Result<usize, diesel::result::Error>
  66. {
  67. match sql_query(format!("SELECT * FROM activities WHERE data->'object'->>'inReplyTo' = '{}';", object_id))
  68. .clone()
  69. .load::<QueryActivity>(db_connection)
  70. {
  71. Ok(activity) => Ok(activity.len()),
  72. Err(e) => Err(e),
  73. }
  74. }
  75. pub fn count_ap_object_reactions_by_id(db_connection: &PgConnection, object_id: &str, reaction: &str) -> Result<usize, diesel::result::Error>
  76. {
  77. match sql_query(format!("SELECT * FROM activities WHERE data->>'type' = '{reaction_type}' AND data->>'object'= '{id}';",
  78. reaction_type = reaction,
  79. id = object_id))
  80. .clone()
  81. .load::<QueryActivity>(db_connection)
  82. {
  83. Ok(activity) => Ok(activity.len()),
  84. Err(e) => Err(e),
  85. }
  86. }
  87. fn serialize_activity(sql_activity: QueryActivity) -> Activity
  88. {
  89. Activity {id: sql_activity.id, data: sql_activity.data, actor: sql_activity.actor_uri }
  90. }
  91. fn deserialize_activity(activity: &Activity) -> InsertActivity
  92. {
  93. InsertActivity { data: &activity.data, actor_uri: &activity.actor }
  94. }
  95. pub fn insert_activity(db_connection: &PgConnection, activity: Activity)
  96. {
  97. let new_activity = deserialize_activity(&activity);
  98. diesel::insert_into(activities::table)
  99. .values(new_activity)
  100. .execute(db_connection)
  101. .expect("Error creating activity");
  102. }
  103. pub fn delete_ap_object_by_id (db_connection: &PgConnection, object_id: String)
  104. {
  105. sql_query(format!("DELETE FROM activities WHERE data->'object'->>'id' = '{}';", object_id))
  106. .execute(db_connection);
  107. }