From 12c80f9981c06cb51bfcc10f4f30b6e5d7ead226 Mon Sep 17 00:00:00 2001 From: fdb-hiroshima <35889323+fdb-hiroshima@users.noreply.github.com> Date: Fri, 13 Sep 2019 11:29:52 +0200 Subject: [PATCH] delete notification on user deletion (#658) * delete notification on user deletion fix #651 * use the correct id for deletion * add regression test * push helpers too * revert CI changes --- plume-models/src/notifications.rs | 14 ++++++++++++-- plume-models/src/users.rs | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/plume-models/src/notifications.rs b/plume-models/src/notifications.rs index 3e10859..8aa5a7d 100644 --- a/plume-models/src/notifications.rs +++ b/plume-models/src/notifications.rs @@ -1,5 +1,5 @@ use chrono::NaiveDateTime; -use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; +use diesel::{self, ExpressionMethods, JoinOnDsl, QueryDsl, RunQueryDsl}; use comments::Comment; use follows::Follow; @@ -7,7 +7,7 @@ use likes::Like; use mentions::Mention; use posts::Post; use reshares::Reshare; -use schema::notifications; +use schema::{follows, notifications}; use users::User; use {Connection, Error, Result}; @@ -64,6 +64,16 @@ impl Notification { .map_err(Error::from) } + pub fn find_followed_by(conn: &Connection, user: &User) -> Result> { + notifications::table + .inner_join(follows::table.on(notifications::object_id.eq(follows::id))) + .filter(notifications::kind.eq(notification_kind::FOLLOW)) + .filter(follows::follower_id.eq(user.id)) + .select(notifications::all_columns) + .load::(conn) + .map_err(Error::from) + } + pub fn count_for_user(conn: &Connection, user: &User) -> Result { notifications::table .filter(notifications::user_id.eq(user.id)) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index ff9c18c..695f511 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -42,6 +42,7 @@ use db_conn::DbConn; use follows::Follow; use instance::*; use medias::Media; +use notifications::Notification; use post_authors::PostAuthor; use posts::Post; use safe_string::SafeString; @@ -147,6 +148,10 @@ impl User { } } + for notif in Notification::find_followed_by(conn, self)? { + notif.delete(conn)? + } + diesel::delete(self) .execute(conn) .map(|_| ())