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 {
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,
});

View File

@ -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,
});

View File

@ -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,
});

View File

@ -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,

View File

@ -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,
});

View File

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

View File

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