2 use diesel::{dsl::*, result::Error, sql_types::Text, *};
3 use lemmy_db_schema::{source::activity::*, DbUrl};
9 io::{Error as IoError, ErrorKind},
12 impl Crud<ActivityForm, i32> 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)
18 fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
19 use lemmy_db_schema::schema::activity::dsl::*;
22 .get_result::<Self>(conn)
28 new_activity: &ActivityForm,
29 ) -> Result<Self, Error> {
30 use lemmy_db_schema::schema::activity::dsl::*;
31 diesel::update(activity.find(activity_id))
33 .get_result::<Self>(conn)
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)
48 ) -> Result<Activity, IoError>
52 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Activity, Error>;
53 fn delete_olds(conn: &PgConnection) -> Result<usize, Error>;
55 /// Returns up to 20 activities of type `Announce/Create/Page` from the community
56 fn read_community_outbox(
58 community_actor_id: &DbUrl,
59 ) -> Result<Vec<Value>, Error>;
62 impl Activity_ for Activity {
69 ) -> Result<Activity, IoError>
73 debug!("{}", serde_json::to_string_pretty(&data)?);
74 let activity_form = ActivityForm {
76 data: serde_json::to_value(&data)?,
81 let result = Activity::create(&conn, &activity_form);
84 Err(e) => Err(IoError::new(
86 format!("Failed to insert activity into database: {}", e),
91 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Activity, Error> {
92 use lemmy_db_schema::schema::activity::dsl::*;
93 activity.filter(ap_id.eq(object_id)).first::<Self>(conn)
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)
101 fn read_community_outbox(
103 community_actor_id: &DbUrl,
104 ) -> Result<Vec<Value>, Error> {
105 use lemmy_db_schema::schema::activity::dsl::*;
106 let res: Vec<Value> = activity
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"),
125 use crate::{establish_unpooled_connection, source::activity::Activity_};
126 use lemmy_db_schema::source::{
127 activity::{Activity, ActivityForm},
128 person::{Person, PersonForm},
130 use serde_json::Value;
131 use serial_test::serial;
137 let conn = establish_unpooled_connection();
139 let creator_form = PersonForm {
140 name: "activity_creator_pm".into(),
141 preferred_username: None,
153 last_refreshed_at: None,
155 shared_inbox_url: None,
156 matrix_user_id: None,
159 let inserted_creator = Person::create(&conn, &creator_form).unwrap();
161 let ap_id: DbUrl = Url::parse(
162 "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
166 let test_json: Value = serde_json::from_str(
168 "@context": "https://www.w3.org/ns/activitystreams",
169 "id": "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
171 "actor": "https://enterprise.lemmy.ml/u/riker",
172 "to": "https://www.w3.org/ns/activitystreams#Public",
174 "https://enterprise.lemmy.ml/c/main/"
176 "object": "https://enterprise.lemmy.ml/post/32"
180 let activity_form = ActivityForm {
181 ap_id: ap_id.clone(),
182 data: test_json.to_owned(),
188 let inserted_activity = Activity::create(&conn, &activity_form).unwrap();
190 let expected_activity = Activity {
191 ap_id: Some(ap_id.clone()),
192 id: inserted_activity.id,
195 sensitive: Some(false),
196 published: inserted_activity.published,
200 let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
201 let read_activity_by_apub_id = Activity::read_from_apub_id(&conn, &ap_id).unwrap();
202 Person::delete(&conn, inserted_creator.id).unwrap();
203 Activity::delete(&conn, inserted_activity.id).unwrap();
205 assert_eq!(expected_activity, read_activity);
206 assert_eq!(expected_activity, read_activity_by_apub_id);
207 assert_eq!(expected_activity, inserted_activity);