Only sending private message if its a local user.

This commit is contained in:
Dessalines 2021-03-12 15:18:03 -05:00
parent 434fb53dd1
commit 5998c83b2a
7 changed files with 120 additions and 60 deletions

View file

@ -754,7 +754,7 @@ impl Perform for CreateCommentReport {
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreateCommentReport, op: UserOperation::CreateCommentReport,
response: res.clone(), response: res.clone(),
recipient_id: local_user_view.person.id, local_recipient_id: local_user_view.person.id,
websocket_id, websocket_id,
}); });
@ -856,7 +856,7 @@ impl Perform for ListCommentReports {
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::ListCommentReports, op: UserOperation::ListCommentReports,
response: res.clone(), response: res.clone(),
recipient_id: local_user_view.person.id, local_recipient_id: local_user_view.person.id,
websocket_id, websocket_id,
}); });

View file

@ -1141,21 +1141,6 @@ impl Perform for CreatePrivateMessage {
.send_create(&local_user_view.person, context) .send_create(&local_user_view.person, context)
.await?; .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| { let private_message_view = blocking(context.pool(), move |conn| {
PrivateMessageView::read(conn, inserted_private_message.id) PrivateMessageView::read(conn, inserted_private_message.id)
}) })
@ -1165,12 +1150,30 @@ impl Perform for CreatePrivateMessage {
private_message_view, private_message_view,
}; };
context.chat_server().do_send(SendUserRoomMessage { // Send notifications to the local recipient, if one exists
op: UserOperation::CreatePrivateMessage, let recipient_id = data.recipient_id;
response: res.clone(), if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
recipient_id, LocalUserView::read_person(conn, recipient_id)
websocket_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) Ok(res)
} }
@ -1220,18 +1223,26 @@ impl Perform for EditPrivateMessage {
PrivateMessageView::read(conn, private_message_id) PrivateMessageView::read(conn, private_message_id)
}) })
.await??; .await??;
let recipient_id = private_message_view.recipient.id;
let res = PrivateMessageResponse { let res = PrivateMessageResponse {
private_message_view, private_message_view,
}; };
context.chat_server().do_send(SendUserRoomMessage { // Send notifications to the local recipient, if one exists
op: UserOperation::EditPrivateMessage, let recipient_id = orig_private_message.recipient_id;
response: res.clone(), if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
recipient_id, LocalUserView::read_person(conn, recipient_id)
websocket_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) Ok(res)
} }
@ -1287,18 +1298,26 @@ impl Perform for DeletePrivateMessage {
PrivateMessageView::read(conn, private_message_id) PrivateMessageView::read(conn, private_message_id)
}) })
.await??; .await??;
let recipient_id = private_message_view.recipient.id;
let res = PrivateMessageResponse { let res = PrivateMessageResponse {
private_message_view, private_message_view,
}; };
context.chat_server().do_send(SendUserRoomMessage { // Send notifications to the local recipient, if one exists
op: UserOperation::DeletePrivateMessage, let recipient_id = orig_private_message.recipient_id;
response: res.clone(), if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
recipient_id, LocalUserView::read_person(conn, recipient_id)
websocket_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) Ok(res)
} }
@ -1339,24 +1358,31 @@ impl Perform for MarkPrivateMessageAsRead {
}; };
// No need to send an apub update // No need to send an apub update
let private_message_id = data.private_message_id; let private_message_id = data.private_message_id;
let private_message_view = blocking(context.pool(), move |conn| { let private_message_view = blocking(context.pool(), move |conn| {
PrivateMessageView::read(conn, private_message_id) PrivateMessageView::read(conn, private_message_id)
}) })
.await??; .await??;
let recipient_id = private_message_view.recipient.id;
let res = PrivateMessageResponse { let res = PrivateMessageResponse {
private_message_view, private_message_view,
}; };
context.chat_server().do_send(SendUserRoomMessage { // Send notifications to the local recipient, if one exists
op: UserOperation::MarkPrivateMessageAsRead, let recipient_id = orig_private_message.recipient_id;
response: res.clone(), if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
recipient_id, LocalUserView::read_person(conn, recipient_id)
websocket_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) Ok(res)
} }
@ -1441,7 +1467,7 @@ impl Perform for GetReportCount {
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::GetReportCount, op: UserOperation::GetReportCount,
response: res.clone(), response: res.clone(),
recipient_id: local_user_view.person.id, local_recipient_id: local_user_view.person.id,
websocket_id, websocket_id,
}); });

View file

@ -844,7 +844,7 @@ impl Perform for CreatePostReport {
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreatePostReport, op: UserOperation::CreatePostReport,
response: res.clone(), response: res.clone(),
recipient_id: local_user_view.person.id, local_recipient_id: local_user_view.local_user.id,
websocket_id, websocket_id,
}); });
@ -945,7 +945,7 @@ impl Perform for ListPostReports {
context.chat_server().do_send(SendUserRoomMessage { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::ListPostReports, op: UserOperation::ListPostReports,
response: res.clone(), response: res.clone(),
recipient_id: local_user_view.person.id, local_recipient_id: local_user_view.local_user.id,
websocket_id, websocket_id,
}); });

View file

@ -105,7 +105,7 @@ fn do_send_local_notifs(
// Send an email to those local users that have notifications on // Send an email to those local users that have notifications on
if do_send_email && mention_user_view.local_user.send_notifications_to_email { if do_send_email && mention_user_view.local_user.send_notifications_to_email {
send_email_to_user( send_email_to_user(
mention_user_view, &mention_user_view,
"Mentioned by", "Mentioned by",
"Person Mention", "Person Mention",
&comment.content, &comment.content,
@ -125,7 +125,7 @@ fn do_send_local_notifs(
if do_send_email && parent_user_view.local_user.send_notifications_to_email { if do_send_email && parent_user_view.local_user.send_notifications_to_email {
send_email_to_user( send_email_to_user(
parent_user_view, &parent_user_view,
"Reply from", "Reply from",
"Comment Reply", "Comment Reply",
&comment.content, &comment.content,
@ -143,7 +143,7 @@ fn do_send_local_notifs(
if do_send_email && parent_user_view.local_user.send_notifications_to_email { if do_send_email && parent_user_view.local_user.send_notifications_to_email {
send_email_to_user( send_email_to_user(
parent_user_view, &parent_user_view,
"Reply from", "Reply from",
"Post Reply", "Post Reply",
&comment.content, &comment.content,
@ -157,7 +157,7 @@ fn do_send_local_notifs(
} }
pub fn send_email_to_user( pub fn send_email_to_user(
local_user_view: LocalUserView, local_user_view: &LocalUserView,
subject_text: &str, subject_text: &str,
body_text: &str, body_text: &str,
comment_content: &str, comment_content: &str,
@ -166,7 +166,7 @@ pub fn send_email_to_user(
return; 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!( let subject = &format!(
"{} - {} {}", "{} - {} {}",
subject_text, subject_text,

View file

@ -16,7 +16,7 @@ use anyhow::{anyhow, Context};
use lemmy_api_structs::{blocking, person::PrivateMessageResponse}; use lemmy_api_structs::{blocking, person::PrivateMessageResponse};
use lemmy_db_queries::source::private_message::PrivateMessage_; use lemmy_db_queries::source::private_message::PrivateMessage_;
use lemmy_db_schema::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_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};
use url::Url; use url::Url;
@ -50,12 +50,19 @@ pub(crate) async fn receive_create_private_message(
private_message_view: message, private_message_view: message,
}; };
// Send notifications to the local recipient, if one exists
let recipient_id = res.private_message_view.recipient.id; 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 { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreatePrivateMessage, op: UserOperation::CreatePrivateMessage,
response: res, response: res,
recipient_id, local_recipient_id,
websocket_id: None, 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 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 { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage, op: UserOperation::EditPrivateMessage,
response: res, response: res,
recipient_id, local_recipient_id,
websocket_id: None, websocket_id: None,
}); });
@ -123,11 +136,19 @@ pub(crate) async fn receive_delete_private_message(
let res = PrivateMessageResponse { let res = PrivateMessageResponse {
private_message_view: message, private_message_view: message,
}; };
let recipient_id = res.private_message_view.recipient.id; 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 { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage, op: UserOperation::EditPrivateMessage,
response: res, response: res,
recipient_id, local_recipient_id,
websocket_id: None, websocket_id: None,
}); });
@ -160,11 +181,19 @@ pub(crate) async fn receive_undo_delete_private_message(
let res = PrivateMessageResponse { let res = PrivateMessageResponse {
private_message_view: message, private_message_view: message,
}; };
let recipient_id = res.private_message_view.recipient.id; 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 { context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage, op: UserOperation::EditPrivateMessage,
response: res, response: res,
recipient_id, local_recipient_id,
websocket_id: None, websocket_id: None,
}); });

View file

@ -102,7 +102,12 @@ where
fn handle(&mut self, msg: SendUserRoomMessage<Response>, _: &mut Context<Self>) { fn handle(&mut self, msg: SendUserRoomMessage<Response>, _: &mut Context<Self>) {
self 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(); .ok();
} }
} }

View file

@ -50,7 +50,7 @@ pub struct SendAllMessage<Response> {
pub struct SendUserRoomMessage<Response> { pub struct SendUserRoomMessage<Response> {
pub op: UserOperation, pub op: UserOperation,
pub response: Response, pub response: Response,
pub recipient_id: LocalUserId, pub local_recipient_id: LocalUserId,
pub websocket_id: Option<ConnectionId>, pub websocket_id: Option<ConnectionId>,
} }