3 schema::activity::dsl::{activity, ap_id},
4 source::activity::{Activity, ActivityInsertForm, ActivityUpdateForm},
6 utils::{get_conn, DbPool},
10 result::{DatabaseErrorKind, Error},
14 use diesel_async::RunQueryDsl;
15 use serde_json::Value;
18 impl Crud for Activity {
19 type InsertForm = ActivityInsertForm;
20 type UpdateForm = ActivityUpdateForm;
22 async fn read(pool: &DbPool, activity_id: i32) -> Result<Self, Error> {
23 let conn = &mut get_conn(pool).await?;
24 activity.find(activity_id).first::<Self>(conn).await
27 async fn create(pool: &DbPool, new_activity: &Self::InsertForm) -> Result<Self, Error> {
28 let conn = &mut get_conn(pool).await?;
31 .get_result::<Self>(conn)
38 new_activity: &Self::UpdateForm,
39 ) -> Result<Self, Error> {
40 let conn = &mut get_conn(pool).await?;
41 diesel::update(activity.find(activity_id))
43 .get_result::<Self>(conn)
46 async fn delete(pool: &DbPool, activity_id: i32) -> Result<usize, Error> {
47 let conn = &mut get_conn(pool).await?;
48 diesel::delete(activity.find(activity_id))
55 /// Returns true if the insert was successful
56 // TODO this should probably just be changed to an upsert on_conflict, rather than an error
62 sensitive_: Option<bool>,
63 ) -> Result<bool, Error> {
64 let activity_form = ActivityInsertForm {
68 sensitive: sensitive_,
71 match Activity::create(pool, &activity_form).await {
74 if let Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _) = e {
82 pub async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result<Activity, Error> {
83 let conn = &mut get_conn(pool).await?;
85 .filter(ap_id.eq(object_id))
97 activity::{Activity, ActivityInsertForm},
99 person::{Person, PersonInsertForm},
101 utils::build_db_pool_for_tests,
103 use serde_json::Value;
104 use serial_test::serial;
109 async fn test_crud() {
110 let pool = &build_db_pool_for_tests().await;
112 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
116 let creator_form = PersonInsertForm::builder()
117 .name("activity_creator_ pm".into())
118 .public_key("pubkey".to_string())
119 .instance_id(inserted_instance.id)
122 let inserted_creator = Person::create(pool, &creator_form).await.unwrap();
124 let ap_id_: DbUrl = Url::parse(
125 "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
129 let test_json: Value = serde_json::from_str(
131 "@context": "https://www.w3.org/ns/activitystreams",
132 "id": "https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
134 "actor": "https://enterprise.lemmy.ml/u/riker",
135 "to": "https://www.w3.org/ns/activitystreams#Public",
137 "https://enterprise.lemmy.ml/c/main/"
139 "object": "https://enterprise.lemmy.ml/post/32"
143 let activity_form = ActivityInsertForm {
144 ap_id: ap_id_.clone(),
145 data: test_json.clone(),
147 sensitive: Some(false),
151 let inserted_activity = Activity::create(pool, &activity_form).await.unwrap();
153 let expected_activity = Activity {
154 ap_id: ap_id_.clone(),
155 id: inserted_activity.id,
158 sensitive: Some(false),
159 published: inserted_activity.published,
163 let read_activity = Activity::read(pool, inserted_activity.id).await.unwrap();
164 let read_activity_by_apub_id = Activity::read_from_apub_id(pool, &ap_id_).await.unwrap();
165 Person::delete(pool, inserted_creator.id).await.unwrap();
166 Activity::delete(pool, inserted_activity.id).await.unwrap();
168 assert_eq!(expected_activity, read_activity);
169 assert_eq!(expected_activity, read_activity_by_apub_id);
170 assert_eq!(expected_activity, inserted_activity);