]> Untitled Git - lemmy.git/commitdiff
Only sending private message if its a local user.
authorDessalines <tyhou13@gmx.com>
Fri, 12 Mar 2021 20:18:03 +0000 (15:18 -0500)
committerDessalines <tyhou13@gmx.com>
Fri, 12 Mar 2021 20:18:03 +0000 (15:18 -0500)
crates/api/src/comment.rs
crates/api/src/local_user.rs
crates/api/src/post.rs
crates/api_structs/src/lib.rs
crates/apub/src/activities/receive/private_message.rs
crates/websocket/src/handlers.rs
crates/websocket/src/messages.rs

index aff39e0249ffbb1af50b3b2da3448d14a86fbd85..e59eefd9cfb6de5eeceab86d1436fe8cb5e8ddb2 100644 (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,
     });
 
index 9b5611f2c37ea544082862ce71be34c6c9298741..b3ec928251fc50f8c07badc393bd61cab3a2b0c6 100644 (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,
     });
 
index 5515575e67b088776403d7ce177cebc53651e65c..9911f6722c551c701a1e487156eaccf00ed55b68 100644 (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,
     });
 
index e675d02ae09c3e43ace4c8073b38c50c237cd5ad..e0fab6b24d368ba9b236cbdecf7a4d79ee79b5a2 100644 (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,
index 45d4a689ddf4e0d6f657d93fa7b495d4f31b3470..daae54d912d106a066348a2c9fa3267ce827bbdf 100644 (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,
   });
 
index 7198fcb78d0ea4fd958133eab210547c2dc522a7..ccc2d099caa53135e4291b285dfdbb9c9366e4b2 100644 (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();
   }
 }
index e66ffe11086862bca0a9513b81b0e66f310ea844..675563f37d010f2a9808e31686d2f5a3b00f5b9e 100644 (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>,
 }