diff --git a/plume-models/src/notifications.rs b/plume-models/src/notifications.rs index 3e10859d..8aa5a7d2 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 ff9c18cc..695f5112 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(|_| ())