]> Untitled Git - lemmy.git/blobdiff - crates/websocket/src/send.rs
First pass at adding comment trees. (#2362)
[lemmy.git] / crates / websocket / src / send.rs
index fbc88962aa16c1d1cbb8695e76e4411c987e817b..f518f4c15322ae6ef1bce7a6609a407fde4707c9 100644 (file)
@@ -14,6 +14,7 @@ use lemmy_db_schema::{
   newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
   source::{
     comment::Comment,
+    comment_reply::{CommentReply, CommentReplyForm},
     person::Person,
     person_mention::{PersonMention, PersonMentionForm},
     post::Post,
@@ -223,71 +224,97 @@ pub async fn send_local_notifs(
     }
   }
 
-  // Send notifs to the parent commenter / poster
-  match comment.parent_id {
-    Some(parent_id) => {
-      let parent_comment =
-        blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await?;
-      if let Ok(parent_comment) = parent_comment {
-        // Get the parent commenter local_user
-        let parent_creator_id = parent_comment.creator_id;
-
-        // Only add to recipients if that person isn't blocked
-        let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool())
-          .await
-          .is_err();
-
-        // Don't send a notif to yourself
-        if parent_comment.creator_id != person.id && !creator_blocked {
-          let user_view = blocking(context.pool(), move |conn| {
-            LocalUserView::read_person(conn, parent_creator_id)
-          })
-          .await?;
-          if let Ok(parent_user_view) = user_view {
-            recipient_ids.push(parent_user_view.local_user.id);
-
-            if do_send_email {
-              let lang = get_user_lang(&parent_user_view);
-              send_email_to_user(
-                &parent_user_view,
-                &lang.notification_comment_reply_subject(&person.name),
-                &lang.notification_comment_reply_body(&comment.content, &inbox_link, &person.name),
-                context.settings(),
-              )
-            }
-          }
+  // Send comment_reply to the parent commenter / poster
+  if let Some(parent_comment_id) = comment.parent_comment_id() {
+    let parent_comment = blocking(context.pool(), move |conn| {
+      Comment::read(conn, parent_comment_id)
+    })
+    .await??;
+
+    // Get the parent commenter local_user
+    let parent_creator_id = parent_comment.creator_id;
+
+    // Only add to recipients if that person isn't blocked
+    let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool())
+      .await
+      .is_err();
+
+    // Don't send a notif to yourself
+    if parent_comment.creator_id != person.id && !creator_blocked {
+      let user_view = blocking(context.pool(), move |conn| {
+        LocalUserView::read_person(conn, parent_creator_id)
+      })
+      .await?;
+      if let Ok(parent_user_view) = user_view {
+        recipient_ids.push(parent_user_view.local_user.id);
+
+        let comment_reply_form = CommentReplyForm {
+          recipient_id: parent_user_view.person.id,
+          comment_id: comment.id,
+          read: None,
+        };
+
+        // Allow this to fail softly, since comment edits might re-update or replace it
+        // Let the uniqueness handle this fail
+        blocking(context.pool(), move |conn| {
+          CommentReply::create(conn, &comment_reply_form)
+        })
+        .await?
+        .ok();
+
+        if do_send_email {
+          let lang = get_user_lang(&parent_user_view);
+          send_email_to_user(
+            &parent_user_view,
+            &lang.notification_comment_reply_subject(&person.name),
+            &lang.notification_comment_reply_body(&comment.content, &inbox_link, &person.name),
+            context.settings(),
+          )
         }
       }
     }
-    // Its a post
-    // Don't send a notif to yourself
-    None => {
-      // Only add to recipients if that person isn't blocked
-      let creator_blocked = check_person_block(person.id, post.creator_id, context.pool())
-        .await
-        .is_err();
-
-      if post.creator_id != person.id && !creator_blocked {
-        let creator_id = post.creator_id;
-        let parent_user = blocking(context.pool(), move |conn| {
-          LocalUserView::read_person(conn, creator_id)
+  } else {
+    // If there's no parent, its the post creator
+    // Only add to recipients if that person isn't blocked
+    let creator_blocked = check_person_block(person.id, post.creator_id, context.pool())
+      .await
+      .is_err();
+
+    if post.creator_id != person.id && !creator_blocked {
+      let creator_id = post.creator_id;
+      let parent_user = blocking(context.pool(), move |conn| {
+        LocalUserView::read_person(conn, creator_id)
+      })
+      .await?;
+      if let Ok(parent_user_view) = parent_user {
+        recipient_ids.push(parent_user_view.local_user.id);
+
+        let comment_reply_form = CommentReplyForm {
+          recipient_id: parent_user_view.person.id,
+          comment_id: comment.id,
+          read: None,
+        };
+
+        // Allow this to fail softly, since comment edits might re-update or replace it
+        // Let the uniqueness handle this fail
+        blocking(context.pool(), move |conn| {
+          CommentReply::create(conn, &comment_reply_form)
         })
-        .await?;
-        if let Ok(parent_user_view) = parent_user {
-          recipient_ids.push(parent_user_view.local_user.id);
-
-          if do_send_email {
-            let lang = get_user_lang(&parent_user_view);
-            send_email_to_user(
-              &parent_user_view,
-              &lang.notification_post_reply_subject(&person.name),
-              &lang.notification_post_reply_body(&comment.content, &inbox_link, &person.name),
-              context.settings(),
-            )
-          }
+        .await?
+        .ok();
+
+        if do_send_email {
+          let lang = get_user_lang(&parent_user_view);
+          send_email_to_user(
+            &parent_user_view,
+            &lang.notification_post_reply_subject(&person.name),
+            &lang.notification_post_reply_body(&comment.content, &inbox_link, &person.name),
+            context.settings(),
+          )
         }
       }
     }
-  };
+  }
+
   Ok(recipient_ids)
 }