]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/activity.rs
Support plain `cargo test` and disable unused doctests for speed
[lemmy.git] / crates / db_queries / src / source / activity.rs
1 use crate::Crud;
2 use diesel::{dsl::*, result::Error, sql_types::Text, *};
3 use lemmy_db_schema::{source::activity::*, Url};
4 use log::debug;
5 use serde::Serialize;
6 use serde_json::Value;
7 use std::{
8   fmt::Debug,
9   io::{Error as IoError, ErrorKind},
10 };
11
12 impl Crud<ActivityForm> for Activity {
13   fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
14     use lemmy_db_schema::schema::activity::dsl::*;
15     activity.find(activity_id).first::<Self>(conn)
16   }
17
18   fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
19     use lemmy_db_schema::schema::activity::dsl::*;
20     insert_into(activity)
21       .values(new_activity)
22       .get_result::<Self>(conn)
23   }
24
25   fn update(
26     conn: &PgConnection,
27     activity_id: i32,
28     new_activity: &ActivityForm,
29   ) -> Result<Self, Error> {
30     use lemmy_db_schema::schema::activity::dsl::*;
31     diesel::update(activity.find(activity_id))
32       .set(new_activity)
33       .get_result::<Self>(conn)
34   }
35   fn delete(conn: &PgConnection, activity_id: i32) -> Result<usize, Error> {
36     use lemmy_db_schema::schema::activity::dsl::*;
37     diesel::delete(activity.find(activity_id)).execute(conn)
38   }
39 }
40
41 pub trait Activity_ {
42   fn insert<T>(
43     conn: &PgConnection,
44     ap_id: String,
45     data: &T,
46     local: bool,
47     sensitive: bool,
48   ) -> Result<Activity, IoError>
49   where
50     T: Serialize + Debug;
51
52   fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Activity, Error>;
53   fn delete_olds(conn: &PgConnection) -> Result<usize, Error>;
54
55   /// Returns up to 20 activities of type `Announce/Create/Page` from the community
56   fn read_community_outbox(
57     conn: &PgConnection,
58     community_actor_id: &Url,
59   ) -> Result<Vec<Value>, Error>;
60 }
61
62 impl Activity_ for Activity {
63   fn insert<T>(
64     conn: &PgConnection,
65     ap_id: String,
66     data: &T,
67     local: bool,
68     sensitive: bool,
69   ) -> Result<Activity, IoError>
70   where
71     T: Serialize + Debug,
72   {
73     debug!("{}", serde_json::to_string_pretty(&data)?);
74     let activity_form = ActivityForm {
75       ap_id,
76       data: serde_json::to_value(&data)?,
77       local,
78       sensitive,
79       updated: None,
80     };
81     let result = Activity::create(&conn, &activity_form);
82     match result {
83       Ok(s) => Ok(s),
84       Err(e) => Err(IoError::new(
85         ErrorKind::Other,
86         format!("Failed to insert activity into database: {}", e),
87       )),
88     }
89   }
90
91   fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Activity, Error> {
92     use lemmy_db_schema::schema::activity::dsl::*;
93     activity.filter(ap_id.eq(object_id)).first::<Self>(conn)
94   }
95
96   fn delete_olds(conn: &PgConnection) -> Result<usize, Error> {
97     use lemmy_db_schema::schema::activity::dsl::*;
98     diesel::delete(activity.filter(published.lt(now - 6.months()))).execute(conn)
99   }
100
101   fn read_community_outbox(
102     conn: &PgConnection,
103     community_actor_id: &Url,
104   ) -> Result<Vec<Value>, Error> {
105     use lemmy_db_schema::schema::activity::dsl::*;
106     let res: Vec<Value> = activity
107       .select(data)
108       .filter(
109         sql("activity.data ->> 'type' = 'Announce'")
110           .sql(" AND activity.data -> 'object' ->> 'type' = 'Create'")
111           .sql(" AND activity.data -> 'object' -> 'object' ->> 'type' = 'Page'")
112           .sql(" AND activity.data ->> 'actor' = ")
113           .bind::<Text, _>(community_actor_id)
114           .sql(" ORDER BY activity.published DESC"),
115       )
116       .limit(20)
117       .get_results(conn)?;
118     Ok(res)
119   }
120 }
121
122 #[cfg(test)]
123 mod tests {
124   use crate::{
125     establish_unpooled_connection,
126     source::activity::Activity_,
127     Crud,
128     ListingType,
129     SortType,
130   };
131   use lemmy_db_schema::source::{
132     activity::{Activity, ActivityForm},
133     user::{UserForm, User_},
134   };
135   use serde_json::Value;
136   use serial_test::serial;
137
138   #[test]
139   #[serial]
140   fn test_crud() {
141     let conn = establish_unpooled_connection();
142
143     let creator_form = UserForm {
144       name: "activity_creator_pm".into(),
145       preferred_username: None,
146       password_encrypted: "nope".into(),
147       email: None,
148       matrix_user_id: None,
149       avatar: None,
150       banner: None,
151       admin: false,
152       banned: Some(false),
153       published: None,
154       updated: None,
155       show_nsfw: false,
156       theme: "browser".into(),
157       default_sort_type: SortType::Hot as i16,
158       default_listing_type: ListingType::Subscribed as i16,
159       lang: "browser".into(),
160       show_avatars: true,
161       send_notifications_to_email: false,
162       actor_id: None,
163       bio: None,
164       local: true,
165       private_key: None,
166       public_key: None,
167       last_refreshed_at: None,
168       inbox_url: None,
169       shared_inbox_url: None,
170     };
171
172     let inserted_creator = User_::create(&conn, &creator_form).unwrap();
173
174     let ap_id =
175       "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c";
176     let test_json: Value = serde_json::from_str(
177       r#"{
178     "@context": "https://www.w3.org/ns/activitystreams",
179     "id": "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
180     "type": "Delete",
181     "actor": "https://enterprise.lemmy.ml/u/riker",
182     "to": "https://www.w3.org/ns/activitystreams#Public",
183     "cc": [
184         "https://enterprise.lemmy.ml/c/main/"
185     ],
186     "object": "https://enterprise.lemmy.ml/post/32"
187     }"#,
188     )
189     .unwrap();
190     let activity_form = ActivityForm {
191       ap_id: ap_id.to_string(),
192       data: test_json.to_owned(),
193       local: true,
194       sensitive: false,
195       updated: None,
196     };
197
198     let inserted_activity = Activity::create(&conn, &activity_form).unwrap();
199
200     let expected_activity = Activity {
201       ap_id: Some(ap_id.to_string()),
202       id: inserted_activity.id,
203       data: test_json,
204       local: true,
205       sensitive: Some(false),
206       published: inserted_activity.published,
207       updated: None,
208     };
209
210     let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
211     let read_activity_by_apub_id = Activity::read_from_apub_id(&conn, ap_id).unwrap();
212     User_::delete(&conn, inserted_creator.id).unwrap();
213     Activity::delete(&conn, inserted_activity.id).unwrap();
214
215     assert_eq!(expected_activity, read_activity);
216     assert_eq!(expected_activity, read_activity_by_apub_id);
217     assert_eq!(expected_activity, inserted_activity);
218   }
219 }