]> Untitled Git - lemmy.git/blob - lemmy_db/src/activity.rs
In activity table, remove `user_id` and add `sensitive` (#127)
[lemmy.git] / lemmy_db / src / activity.rs
1 use crate::{schema::activity, Crud};
2 use diesel::{dsl::*, result::Error, *};
3 use log::debug;
4 use serde::Serialize;
5 use serde_json::Value;
6 use std::{
7   fmt::Debug,
8   io::{Error as IoError, ErrorKind},
9 };
10
11 #[derive(Queryable, Identifiable, PartialEq, Debug)]
12 #[table_name = "activity"]
13 pub struct Activity {
14   pub id: i32,
15   pub ap_id: String,
16   pub data: Value,
17   pub local: bool,
18   pub sensitive: bool,
19   pub published: chrono::NaiveDateTime,
20   pub updated: Option<chrono::NaiveDateTime>,
21 }
22
23 #[derive(Insertable, AsChangeset)]
24 #[table_name = "activity"]
25 pub struct ActivityForm {
26   pub ap_id: String,
27   pub data: Value,
28   pub local: bool,
29   pub sensitive: bool,
30   pub updated: Option<chrono::NaiveDateTime>,
31 }
32
33 impl Crud<ActivityForm> for Activity {
34   fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
35     use crate::schema::activity::dsl::*;
36     activity.find(activity_id).first::<Self>(conn)
37   }
38
39   fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
40     use crate::schema::activity::dsl::*;
41     insert_into(activity)
42       .values(new_activity)
43       .get_result::<Self>(conn)
44   }
45
46   fn update(
47     conn: &PgConnection,
48     activity_id: i32,
49     new_activity: &ActivityForm,
50   ) -> Result<Self, Error> {
51     use crate::schema::activity::dsl::*;
52     diesel::update(activity.find(activity_id))
53       .set(new_activity)
54       .get_result::<Self>(conn)
55   }
56 }
57
58 impl Activity {
59   pub fn insert<T>(
60     conn: &PgConnection,
61     ap_id: String,
62     data: &T,
63     local: bool,
64     sensitive: bool,
65   ) -> Result<Self, IoError>
66   where
67     T: Serialize + Debug,
68   {
69     debug!("{}", serde_json::to_string_pretty(&data)?);
70     let activity_form = ActivityForm {
71       ap_id,
72       data: serde_json::to_value(&data)?,
73       local,
74       sensitive,
75       updated: None,
76     };
77     let result = Activity::create(&conn, &activity_form);
78     match result {
79       Ok(s) => Ok(s),
80       Err(e) => Err(IoError::new(
81         ErrorKind::Other,
82         format!("Failed to insert activity into database: {}", e),
83       )),
84     }
85   }
86
87   pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
88     use crate::schema::activity::dsl::*;
89     activity.filter(ap_id.eq(object_id)).first::<Self>(conn)
90   }
91 }
92
93 #[cfg(test)]
94 mod tests {
95   use crate::{
96     activity::{Activity, ActivityForm},
97     tests::establish_unpooled_connection,
98     user::{UserForm, User_},
99     Crud,
100     ListingType,
101     SortType,
102   };
103   use serde_json::Value;
104
105   #[test]
106   fn test_crud() {
107     let conn = establish_unpooled_connection();
108
109     let creator_form = UserForm {
110       name: "activity_creator_pm".into(),
111       preferred_username: None,
112       password_encrypted: "nope".into(),
113       email: None,
114       matrix_user_id: None,
115       avatar: None,
116       banner: None,
117       admin: false,
118       banned: false,
119       published: None,
120       updated: None,
121       show_nsfw: false,
122       theme: "browser".into(),
123       default_sort_type: SortType::Hot as i16,
124       default_listing_type: ListingType::Subscribed as i16,
125       lang: "browser".into(),
126       show_avatars: true,
127       send_notifications_to_email: false,
128       actor_id: None,
129       bio: None,
130       local: true,
131       private_key: None,
132       public_key: None,
133       last_refreshed_at: None,
134     };
135
136     let inserted_creator = User_::create(&conn, &creator_form).unwrap();
137
138     let ap_id =
139       "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c";
140     let test_json: Value = serde_json::from_str(
141       r#"{
142     "@context": "https://www.w3.org/ns/activitystreams",
143     "id": "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
144     "type": "Delete",
145     "actor": "https://enterprise.lemmy.ml/u/riker",
146     "to": "https://www.w3.org/ns/activitystreams#Public",
147     "cc": [
148         "https://enterprise.lemmy.ml/c/main/"
149     ],
150     "object": "https://enterprise.lemmy.ml/post/32"
151     }"#,
152     )
153     .unwrap();
154     let activity_form = ActivityForm {
155       ap_id: ap_id.to_string(),
156       data: test_json.to_owned(),
157       local: true,
158       sensitive: false,
159       updated: None,
160     };
161
162     let inserted_activity = Activity::create(&conn, &activity_form).unwrap();
163
164     let expected_activity = Activity {
165       ap_id: ap_id.to_string(),
166       id: inserted_activity.id,
167       data: test_json,
168       local: true,
169       sensitive: false,
170       published: inserted_activity.published,
171       updated: None,
172     };
173
174     let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
175     let read_activity_by_apub_id = Activity::read_from_apub_id(&conn, ap_id).unwrap();
176     User_::delete(&conn, inserted_creator.id).unwrap();
177
178     assert_eq!(expected_activity, read_activity);
179     assert_eq!(expected_activity, read_activity_by_apub_id);
180     assert_eq!(expected_activity, inserted_activity);
181   }
182 }