From 008f2b1d71e8ee1f7d1979c33744d98f5508a201 Mon Sep 17 00:00:00 2001 From: Bat Date: Fri, 7 Sep 2018 23:29:50 +0100 Subject: [PATCH] Delete notifications when deleting likes/boosts/follows Fixes #190 --- plume-models/src/comments.rs | 4 ++-- plume-models/src/follows.rs | 5 +++++ plume-models/src/likes.rs | 7 ++++++- plume-models/src/notifications.rs | 7 +++++++ plume-models/src/reshares.rs | 5 +++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/plume-models/src/comments.rs b/plume-models/src/comments.rs index b7511d8..d48b1a7 100644 --- a/plume-models/src/comments.rs +++ b/plume-models/src/comments.rs @@ -57,7 +57,7 @@ impl Comment { } pub fn get_post(&self, conn: &PgConnection) -> Post { - Post::get(conn, self.post_id).unwrap() + Post::get(conn, self.post_id).unwrap() } pub fn count_local(conn: &PgConnection) -> usize { @@ -105,7 +105,7 @@ impl FromActivity for Comment { sensitive: false // "sensitive" is not a standard property, we need to think about how to support it with the activitypub crate }); - // save mentionsd + // save mentions if let Some(serde_json::Value::Array(tags)) = note.object_props.tag.clone() { for tag in tags.into_iter() { serde_json::from_value::(tag) diff --git a/plume-models/src/follows.rs b/plume-models/src/follows.rs index eca39d3..9c29aac 100644 --- a/plume-models/src/follows.rs +++ b/plume-models/src/follows.rs @@ -108,6 +108,11 @@ impl Deletable for Follow { fn delete(&self, conn: &PgConnection) -> Undo { diesel::delete(self).execute(conn).expect("Coudn't delete follow"); + // delete associated notification if any + if let Some(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) { + diesel::delete(¬if).execute(conn).expect("Couldn't delete follow notification"); + } + let mut undo = Undo::default(); undo.undo_props.set_actor_link(User::get(conn, self.follower_id).unwrap().into_id()).expect("Follow::delete: actor error"); undo.object_props.set_id_string(format!("{}/undo", self.ap_url)).expect("Follow::delete: id error"); diff --git a/plume-models/src/likes.rs b/plume-models/src/likes.rs index f0a3973..9bf1bec 100644 --- a/plume-models/src/likes.rs +++ b/plume-models/src/likes.rs @@ -91,6 +91,11 @@ impl Deletable for Like { fn delete(&self, conn: &PgConnection) -> activity::Undo { diesel::delete(self).execute(conn).unwrap(); + // delete associated notification if any + if let Some(notif) = Notification::find(conn, notification_kind::LIKE, self.id) { + diesel::delete(¬if).execute(conn).expect("Couldn't delete like notification"); + } + let mut act = activity::Undo::default(); act.undo_props.set_actor_link(User::get(conn, self.user_id).unwrap().into_id()).expect("Like::delete: actor error"); act.undo_props.set_object_object(self.into_activity(conn)).expect("Like::delete: object error"); @@ -100,7 +105,7 @@ impl Deletable for Like { act } - + fn delete_id(id: String, conn: &PgConnection) { if let Some(like) = Like::find_by_ap_url(conn, id.into()) { like.delete(conn); diff --git a/plume-models/src/notifications.rs b/plume-models/src/notifications.rs index 33b06c1..c30d16d 100644 --- a/plume-models/src/notifications.rs +++ b/plume-models/src/notifications.rs @@ -56,6 +56,13 @@ impl Notification { .expect("Couldn't load user notifications page") } + pub fn find>(conn: &PgConnection, kind: S, obj: i32) -> Option { + notifications::table.filter(notifications::kind.eq(kind.into())) + .filter(notifications::object_id.eq(obj)) + .get_result::(conn) + .ok() + } + pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value { let mut json = json!(self); json["object"] = json!(match self.kind.as_ref() { diff --git a/plume-models/src/reshares.rs b/plume-models/src/reshares.rs index 734da12..1701263 100644 --- a/plume-models/src/reshares.rs +++ b/plume-models/src/reshares.rs @@ -102,6 +102,11 @@ impl Deletable for Reshare { fn delete(&self, conn: &PgConnection) -> Undo { diesel::delete(self).execute(conn).unwrap(); + // delete associated notification if any + if let Some(notif) = Notification::find(conn, notification_kind::RESHARE, self.id) { + diesel::delete(¬if).execute(conn).expect("Couldn't delete reshare notification"); + } + let mut act = Undo::default(); act.undo_props.set_actor_link(User::get(conn, self.user_id).unwrap().into_id()).unwrap(); act.undo_props.set_object_object(self.into_activity(conn)).unwrap();