From 5998c83b2adf3e92d30b5acf77a43f7d63fa46ed Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 12 Mar 2021 15:18:03 -0500 Subject: [PATCH] Only sending private message if its a local user. --- crates/api/src/comment.rs | 4 +- crates/api/src/local_user.rs | 114 +++++++++++------- crates/api/src/post.rs | 4 +- crates/api_structs/src/lib.rs | 10 +- .../src/activities/receive/private_message.rs | 39 +++++- crates/websocket/src/handlers.rs | 7 +- crates/websocket/src/messages.rs | 2 +- 7 files changed, 120 insertions(+), 60 deletions(-) diff --git a/crates/api/src/comment.rs b/crates/api/src/comment.rs index aff39e02..e59eefd9 100644 --- a/crates/api/src/comment.rs +++ b/crates/api/src/comment.rs @@ -754,7 +754,7 @@ impl Perform for CreateCommentReport { context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::CreateCommentReport, response: res.clone(), - recipient_id: local_user_view.person.id, + local_recipient_id: local_user_view.person.id, websocket_id, }); @@ -856,7 +856,7 @@ impl Perform for ListCommentReports { context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::ListCommentReports, response: res.clone(), - recipient_id: local_user_view.person.id, + local_recipient_id: local_user_view.person.id, websocket_id, }); diff --git a/crates/api/src/local_user.rs b/crates/api/src/local_user.rs index 9b5611f2..b3ec9282 100644 --- a/crates/api/src/local_user.rs +++ b/crates/api/src/local_user.rs @@ -1141,21 +1141,6 @@ impl Perform for CreatePrivateMessage { .send_create(&local_user_view.person, context) .await?; - // Send notifications to the recipient - let recipient_id = data.recipient_id; - let recipient = blocking(context.pool(), move |conn| { - LocalUserView::read_person(conn, recipient_id) - }) - .await??; - if recipient.local_user.send_notifications_to_email { - send_email_to_user( - recipient, - "Private Message from", - "Private Message", - &content_slurs_removed, - ); - } - let private_message_view = blocking(context.pool(), move |conn| { PrivateMessageView::read(conn, inserted_private_message.id) }) @@ -1165,12 +1150,30 @@ impl Perform for CreatePrivateMessage { private_message_view, }; - context.chat_server().do_send(SendUserRoomMessage { - op: UserOperation::CreatePrivateMessage, - response: res.clone(), - recipient_id, - websocket_id, - }); + // Send notifications to the local recipient, if one exists + let recipient_id = data.recipient_id; + if let Ok(local_recipient) = blocking(context.pool(), move |conn| { + LocalUserView::read_person(conn, recipient_id) + }) + .await? + { + if local_recipient.local_user.send_notifications_to_email { + send_email_to_user( + &local_recipient, + "Private Message from", + "Private Message", + &content_slurs_removed, + ); + } + + let local_recipient_id = local_recipient.local_user.id; + context.chat_server().do_send(SendUserRoomMessage { + op: UserOperation::CreatePrivateMessage, + response: res.clone(), + local_recipient_id, + websocket_id, + }); + } Ok(res) } @@ -1220,18 +1223,26 @@ impl Perform for EditPrivateMessage { PrivateMessageView::read(conn, private_message_id) }) .await??; - let recipient_id = private_message_view.recipient.id; let res = PrivateMessageResponse { private_message_view, }; - context.chat_server().do_send(SendUserRoomMessage { - op: UserOperation::EditPrivateMessage, - response: res.clone(), - recipient_id, - websocket_id, - }); + // Send notifications to the local recipient, if one exists + let recipient_id = orig_private_message.recipient_id; + if let Ok(local_recipient) = blocking(context.pool(), move |conn| { + LocalUserView::read_person(conn, recipient_id) + }) + .await? + { + let local_recipient_id = local_recipient.local_user.id; + context.chat_server().do_send(SendUserRoomMessage { + op: UserOperation::EditPrivateMessage, + response: res.clone(), + local_recipient_id, + websocket_id, + }); + } Ok(res) } @@ -1287,18 +1298,26 @@ impl Perform for DeletePrivateMessage { PrivateMessageView::read(conn, private_message_id) }) .await??; - let recipient_id = private_message_view.recipient.id; let res = PrivateMessageResponse { private_message_view, }; - context.chat_server().do_send(SendUserRoomMessage { - op: UserOperation::DeletePrivateMessage, - response: res.clone(), - recipient_id, - websocket_id, - }); + // Send notifications to the local recipient, if one exists + let recipient_id = orig_private_message.recipient_id; + if let Ok(local_recipient) = blocking(context.pool(), move |conn| { + LocalUserView::read_person(conn, recipient_id) + }) + .await? + { + let local_recipient_id = local_recipient.local_user.id; + context.chat_server().do_send(SendUserRoomMessage { + op: UserOperation::DeletePrivateMessage, + response: res.clone(), + local_recipient_id, + websocket_id, + }); + } Ok(res) } @@ -1339,24 +1358,31 @@ impl Perform for MarkPrivateMessageAsRead { }; // No need to send an apub update - let private_message_id = data.private_message_id; let private_message_view = blocking(context.pool(), move |conn| { PrivateMessageView::read(conn, private_message_id) }) .await??; - let recipient_id = private_message_view.recipient.id; let res = PrivateMessageResponse { private_message_view, }; - context.chat_server().do_send(SendUserRoomMessage { - op: UserOperation::MarkPrivateMessageAsRead, - response: res.clone(), - recipient_id, - websocket_id, - }); + // Send notifications to the local recipient, if one exists + let recipient_id = orig_private_message.recipient_id; + if let Ok(local_recipient) = blocking(context.pool(), move |conn| { + LocalUserView::read_person(conn, recipient_id) + }) + .await? + { + let local_recipient_id = local_recipient.local_user.id; + context.chat_server().do_send(SendUserRoomMessage { + op: UserOperation::MarkPrivateMessageAsRead, + response: res.clone(), + local_recipient_id, + websocket_id, + }); + } Ok(res) } @@ -1441,7 +1467,7 @@ impl Perform for GetReportCount { context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::GetReportCount, response: res.clone(), - recipient_id: local_user_view.person.id, + local_recipient_id: local_user_view.person.id, websocket_id, }); diff --git a/crates/api/src/post.rs b/crates/api/src/post.rs index 5515575e..9911f672 100644 --- a/crates/api/src/post.rs +++ b/crates/api/src/post.rs @@ -844,7 +844,7 @@ impl Perform for CreatePostReport { context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::CreatePostReport, response: res.clone(), - recipient_id: local_user_view.person.id, + local_recipient_id: local_user_view.local_user.id, websocket_id, }); @@ -945,7 +945,7 @@ impl Perform for ListPostReports { context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::ListPostReports, response: res.clone(), - recipient_id: local_user_view.person.id, + local_recipient_id: local_user_view.local_user.id, websocket_id, }); diff --git a/crates/api_structs/src/lib.rs b/crates/api_structs/src/lib.rs index e675d02a..e0fab6b2 100644 --- a/crates/api_structs/src/lib.rs +++ b/crates/api_structs/src/lib.rs @@ -105,7 +105,7 @@ fn do_send_local_notifs( // Send an email to those local users that have notifications on if do_send_email && mention_user_view.local_user.send_notifications_to_email { send_email_to_user( - mention_user_view, + &mention_user_view, "Mentioned by", "Person Mention", &comment.content, @@ -125,7 +125,7 @@ fn do_send_local_notifs( if do_send_email && parent_user_view.local_user.send_notifications_to_email { send_email_to_user( - parent_user_view, + &parent_user_view, "Reply from", "Comment Reply", &comment.content, @@ -143,7 +143,7 @@ fn do_send_local_notifs( if do_send_email && parent_user_view.local_user.send_notifications_to_email { send_email_to_user( - parent_user_view, + &parent_user_view, "Reply from", "Post Reply", &comment.content, @@ -157,7 +157,7 @@ fn do_send_local_notifs( } pub fn send_email_to_user( - local_user_view: LocalUserView, + local_user_view: &LocalUserView, subject_text: &str, body_text: &str, comment_content: &str, @@ -166,7 +166,7 @@ pub fn send_email_to_user( return; } - if let Some(user_email) = local_user_view.local_user.email { + if let Some(user_email) = &local_user_view.local_user.email { let subject = &format!( "{} - {} {}", subject_text, diff --git a/crates/apub/src/activities/receive/private_message.rs b/crates/apub/src/activities/receive/private_message.rs index 45d4a689..daae54d9 100644 --- a/crates/apub/src/activities/receive/private_message.rs +++ b/crates/apub/src/activities/receive/private_message.rs @@ -16,7 +16,7 @@ use anyhow::{anyhow, Context}; use lemmy_api_structs::{blocking, person::PrivateMessageResponse}; use lemmy_db_queries::source::private_message::PrivateMessage_; use lemmy_db_schema::source::private_message::PrivateMessage; -use lemmy_db_views::private_message_view::PrivateMessageView; +use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView}; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; use url::Url; @@ -50,12 +50,19 @@ pub(crate) async fn receive_create_private_message( private_message_view: message, }; + // Send notifications to the local recipient, if one exists let recipient_id = res.private_message_view.recipient.id; + let local_recipient_id = blocking(context.pool(), move |conn| { + LocalUserView::read_person(conn, recipient_id) + }) + .await?? + .local_user + .id; context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::CreatePrivateMessage, response: res, - recipient_id, + local_recipient_id, websocket_id: None, }); @@ -91,11 +98,17 @@ pub(crate) async fn receive_update_private_message( }; let recipient_id = res.private_message_view.recipient.id; + let local_recipient_id = blocking(context.pool(), move |conn| { + LocalUserView::read_person(conn, recipient_id) + }) + .await?? + .local_user + .id; context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::EditPrivateMessage, response: res, - recipient_id, + local_recipient_id, websocket_id: None, }); @@ -123,11 +136,19 @@ pub(crate) async fn receive_delete_private_message( let res = PrivateMessageResponse { private_message_view: message, }; + let recipient_id = res.private_message_view.recipient.id; + let local_recipient_id = blocking(context.pool(), move |conn| { + LocalUserView::read_person(conn, recipient_id) + }) + .await?? + .local_user + .id; + context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::EditPrivateMessage, response: res, - recipient_id, + local_recipient_id, websocket_id: None, }); @@ -160,11 +181,19 @@ pub(crate) async fn receive_undo_delete_private_message( let res = PrivateMessageResponse { private_message_view: message, }; + let recipient_id = res.private_message_view.recipient.id; + let local_recipient_id = blocking(context.pool(), move |conn| { + LocalUserView::read_person(conn, recipient_id) + }) + .await?? + .local_user + .id; + context.chat_server().do_send(SendUserRoomMessage { op: UserOperation::EditPrivateMessage, response: res, - recipient_id, + local_recipient_id, websocket_id: None, }); diff --git a/crates/websocket/src/handlers.rs b/crates/websocket/src/handlers.rs index 7198fcb7..ccc2d099 100644 --- a/crates/websocket/src/handlers.rs +++ b/crates/websocket/src/handlers.rs @@ -102,7 +102,12 @@ where fn handle(&mut self, msg: SendUserRoomMessage, _: &mut Context) { self - .send_user_room_message(&msg.op, &msg.response, msg.recipient_id, msg.websocket_id) + .send_user_room_message( + &msg.op, + &msg.response, + msg.local_recipient_id, + msg.websocket_id, + ) .ok(); } } diff --git a/crates/websocket/src/messages.rs b/crates/websocket/src/messages.rs index e66ffe11..675563f3 100644 --- a/crates/websocket/src/messages.rs +++ b/crates/websocket/src/messages.rs @@ -50,7 +50,7 @@ pub struct SendAllMessage { pub struct SendUserRoomMessage { pub op: UserOperation, pub response: Response, - pub recipient_id: LocalUserId, + pub local_recipient_id: LocalUserId, pub websocket_id: Option, } -- 2.44.1