Improve notification management (#561)

* Don't notify for comment when mentioned

fix #505

* Don't save notification for remote users

fix #472
This commit is contained in:
fdb-hiroshima 2019-05-04 17:15:41 +02:00 committed by GitHub
parent 918bda14ec
commit c9070930d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 46 deletions

View File

@ -15,7 +15,7 @@ use medias::Media;
use mentions::Mention; use mentions::Mention;
use notifications::*; use notifications::*;
use plume_common::activity_pub::{ use plume_common::activity_pub::{
inbox::{AsObject, FromId}, inbox::{AsActor, AsObject, FromId},
Id, IntoId, PUBLIC_VISIBILITY, Id, IntoId, PUBLIC_VISIBILITY,
}; };
use plume_common::utils; use plume_common::utils;
@ -157,14 +157,20 @@ impl Comment {
pub fn notify(&self, conn: &Connection) -> Result<()> { pub fn notify(&self, conn: &Connection) -> Result<()> {
for author in self.get_post(conn)?.get_authors(conn)? { for author in self.get_post(conn)?.get_authors(conn)? {
Notification::insert( if Mention::list_for_comment(conn, self.id)?
conn, .iter()
NewNotification { .all(|m| m.get_mentioned(conn).map(|u| u != author).unwrap_or(true))
kind: notification_kind::COMMENT.to_string(), && author.is_local()
object_id: self.id, {
user_id: author.id, Notification::insert(
}, conn,
)?; NewNotification {
kind: notification_kind::COMMENT.to_string(),
object_id: self.id,
user_id: author.id,
},
)?;
}
} }
Ok(()) Ok(())
} }

View File

@ -67,15 +67,18 @@ impl Follow {
Ok(act) Ok(act)
} }
pub fn notify(&self, conn: &Connection) -> Result<Notification> { pub fn notify(&self, conn: &Connection) -> Result<()> {
Notification::insert( if User::get(conn, self.following_id)?.is_local() {
conn, Notification::insert(
NewNotification { conn,
kind: notification_kind::FOLLOW.to_string(), NewNotification {
object_id: self.id, kind: notification_kind::FOLLOW.to_string(),
user_id: self.following_id, object_id: self.id,
}, user_id: self.following_id,
) },
)?;
}
Ok(())
} }
/// from -> The one sending the follow request /// from -> The one sending the follow request

View File

@ -4,7 +4,7 @@ use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use notifications::*; use notifications::*;
use plume_common::activity_pub::{ use plume_common::activity_pub::{
inbox::{AsObject, FromId}, inbox::{AsActor, AsObject, FromId},
Id, IntoId, PUBLIC_VISIBILITY, Id, IntoId, PUBLIC_VISIBILITY,
}; };
use posts::Post; use posts::Post;
@ -54,14 +54,16 @@ impl Like {
pub fn notify(&self, conn: &Connection) -> Result<()> { pub fn notify(&self, conn: &Connection) -> Result<()> {
let post = Post::get(conn, self.post_id)?; let post = Post::get(conn, self.post_id)?;
for author in post.get_authors(conn)? { for author in post.get_authors(conn)? {
Notification::insert( if author.is_local() {
conn, Notification::insert(
NewNotification { conn,
kind: notification_kind::LIKE.to_string(), NewNotification {
object_id: self.id, kind: notification_kind::LIKE.to_string(),
user_id: author.id, object_id: self.id,
}, user_id: author.id,
)?; },
)?;
}
} }
Ok(()) Ok(())
} }

View File

@ -3,6 +3,7 @@ use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use comments::Comment; use comments::Comment;
use notifications::*; use notifications::*;
use plume_common::activity_pub::inbox::AsActor;
use posts::Post; use posts::Post;
use schema::mentions; use schema::mentions;
use users::User; use users::User;
@ -129,14 +130,18 @@ impl Mention {
fn notify(&self, conn: &Connection) -> Result<()> { fn notify(&self, conn: &Connection) -> Result<()> {
let m = self.get_mentioned(conn)?; let m = self.get_mentioned(conn)?;
Notification::insert( if m.is_local() {
conn, Notification::insert(
NewNotification { conn,
kind: notification_kind::MENTION.to_string(), NewNotification {
object_id: self.id, kind: notification_kind::MENTION.to_string(),
user_id: m.id, object_id: self.id,
}, user_id: m.id,
) },
.map(|_| ()) )
.map(|_| ())
} else {
Ok(())
}
} }
} }

View File

@ -4,7 +4,7 @@ use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use notifications::*; use notifications::*;
use plume_common::activity_pub::{ use plume_common::activity_pub::{
inbox::{AsObject, FromId}, inbox::{AsActor, AsObject, FromId},
Id, IntoId, PUBLIC_VISIBILITY, Id, IntoId, PUBLIC_VISIBILITY,
}; };
use posts::Post; use posts::Post;
@ -79,14 +79,16 @@ impl Reshare {
pub fn notify(&self, conn: &Connection) -> Result<()> { pub fn notify(&self, conn: &Connection) -> Result<()> {
let post = self.get_post(conn)?; let post = self.get_post(conn)?;
for author in post.get_authors(conn)? { for author in post.get_authors(conn)? {
Notification::insert( if author.is_local() {
conn, Notification::insert(
NewNotification { conn,
kind: notification_kind::RESHARE.to_string(), NewNotification {
object_id: self.id, kind: notification_kind::RESHARE.to_string(),
user_id: author.id, object_id: self.id,
}, user_id: author.id,
)?; },
)?;
}
} }
Ok(()) Ok(())
} }

View File

@ -62,7 +62,6 @@ pub fn create(
}, },
) )
.expect("comments::create: insert error"); .expect("comments::create: insert error");
comm.notify(&*conn).expect("comments::create: notify error");
let new_comment = comm let new_comment = comm
.create_activity(&rockets) .create_activity(&rockets)
.expect("comments::create: activity error"); .expect("comments::create: activity error");
@ -80,6 +79,8 @@ pub fn create(
.expect("comments::create: mention save error"); .expect("comments::create: mention save error");
} }
comm.notify(&*conn).expect("comments::create: notify error");
// federate // federate
let dest = User::one_by_instance(&*conn).expect("comments::create: dest error"); let dest = User::one_by_instance(&*conn).expect("comments::create: dest error");
let user_clone = user.clone(); let user_clone = user.clone();