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 for Activity {
13 type Form = ActivityForm;
15 fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
16 use lemmy_db_schema::schema::activity::dsl::*;
17 activity.find(activity_id).first::<Self>(conn)
20 fn create(conn: &PgConnection, new_activity: &ActivityForm) -> Result<Self, Error> {
21 use lemmy_db_schema::schema::activity::dsl::*;
24 .get_result::<Self>(conn)
30 new_activity: &ActivityForm,
31 ) -> Result<Self, Error> {
32 use lemmy_db_schema::schema::activity::dsl::*;
33 diesel::update(activity.find(activity_id))
35 .get_result::<Self>(conn)
37 fn delete(conn: &PgConnection, activity_id: i32) -> Result<usize, Error> {
38 use lemmy_db_schema::schema::activity::dsl::*;
39 diesel::delete(activity.find(activity_id)).execute(conn)
50 ) -> Result<Activity, IoError>
54 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Activity, Error>;
55 fn delete_olds(conn: &PgConnection) -> Result<usize, Error>;
57 /// Returns up to 20 activities of type `Announce/Create/Page` from the community
58 fn read_community_outbox(
60 community_actor_id: &DbUrl,
61 ) -> Result<Vec<Value>, Error>;
64 impl Activity_ for Activity {
71 ) -> Result<Activity, IoError>
75 debug!("{}", serde_json::to_string_pretty(&data)?);
76 let activity_form = ActivityForm {
78 data: serde_json::to_value(&data)?,
83 let result = Activity::create(conn, &activity_form);
86 Err(e) => Err(IoError::new(
88 format!("Failed to insert activity into database: {}", e),
93 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Activity, Error> {
94 use lemmy_db_schema::schema::activity::dsl::*;
95 activity.filter(ap_id.eq(object_id)).first::<Self>(conn)
98 fn delete_olds(conn: &PgConnection) -> Result<usize, Error> {
99 use lemmy_db_schema::schema::activity::dsl::*;
100 diesel::delete(activity.filter(published.lt(now - 6.months()))).execute(conn)
103 fn read_community_outbox(
105 community_actor_id: &DbUrl,
106 ) -> Result<Vec<Value>, Error> {
107 use lemmy_db_schema::schema::activity::dsl::*;
108 let res: Vec<Value> = activity
111 sql("activity.data ->> 'type' = 'Announce'")
112 .sql(" AND activity.data -> 'object' ->> 'type' = 'Create'")
113 .sql(" AND activity.data -> 'object' -> 'object' ->> 'type' = 'Page'")
114 .sql(" AND activity.data ->> 'actor' = ")
115 .bind::<Text, _>(community_actor_id)
116 .sql(" ORDER BY activity.published DESC"),
127 use crate::{establish_unpooled_connection, source::activity::Activity_};
128 use lemmy_db_schema::source::{
129 activity::{Activity, ActivityForm},
130 person::{Person, PersonForm},
132 use serde_json::Value;
133 use serial_test::serial;
139 let conn = establish_unpooled_connection();
141 let creator_form = PersonForm {
142 name: "activity_creator_pm".into(),
143 ..PersonForm::default()
146 let inserted_creator = Person::create(&conn, &creator_form).unwrap();
148 let ap_id: DbUrl = Url::parse(
149 "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
153 let test_json: Value = serde_json::from_str(
155 "@context": "https://www.w3.org/ns/activitystreams",
156 "id": "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
158 "actor": "https://enterprise.lemmy.ml/u/riker",
159 "to": "https://www.w3.org/ns/activitystreams#Public",
161 "https://enterprise.lemmy.ml/c/main/"
163 "object": "https://enterprise.lemmy.ml/post/32"
167 let activity_form = ActivityForm {
168 ap_id: ap_id.clone(),
169 data: test_json.to_owned(),
175 let inserted_activity = Activity::create(&conn, &activity_form).unwrap();
177 let expected_activity = Activity {
178 ap_id: Some(ap_id.clone()),
179 id: inserted_activity.id,
182 sensitive: Some(false),
183 published: inserted_activity.published,
187 let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
188 let read_activity_by_apub_id = Activity::read_from_apub_id(&conn, &ap_id).unwrap();
189 Person::delete(&conn, inserted_creator.id).unwrap();
190 Activity::delete(&conn, inserted_activity.id).unwrap();
192 assert_eq!(expected_activity, read_activity);
193 assert_eq!(expected_activity, read_activity_by_apub_id);
194 assert_eq!(expected_activity, inserted_activity);