diff options
author | Dessalines <tyhou13@gmx.com> | 2020-01-31 20:02:20 -0500 |
---|---|---|
committer | Dessalines <tyhou13@gmx.com> | 2020-01-31 20:02:20 -0500 |
commit | 8036474ddad2f20c27a2fb023395306d6b9e577d (patch) | |
tree | 28f3546c9086931f33d9ee6d910c9aa43a5a1f77 /server/src/api/comment.rs | |
parent | 5188bddd4ddb1d4f4bc4add24db210789054c2a5 (diff) |
Starting to work on user message scope.
Diffstat (limited to 'server/src/api/comment.rs')
-rw-r--r-- | server/src/api/comment.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/server/src/api/comment.rs b/server/src/api/comment.rs index 8efb30fb..775085e9 100644 --- a/server/src/api/comment.rs +++ b/server/src/api/comment.rs @@ -36,6 +36,7 @@ pub struct SaveComment { #[derive(Serialize, Deserialize, Clone)] pub struct CommentResponse { pub comment: CommentView, + pub recipient_ids: Vec<i32>, } #[derive(Serialize, Deserialize)] @@ -88,6 +89,8 @@ impl Perform<CommentResponse> for Oper<CreateComment> { Err(_e) => return Err(APIError::err("couldnt_create_comment").into()), }; + let mut recipient_ids = Vec::new(); + // Scan the comment for user mentions, add those rows let extracted_usernames = extract_usernames(&comment_form.content); @@ -97,6 +100,8 @@ impl Perform<CommentResponse> for Oper<CreateComment> { // At some point, make it so you can't tag the parent creator either // This can cause two notifications, one for reply and the other for mention if mention_user.id != user_id { + recipient_ids.push(mention_user.id); + let user_mention_form = UserMentionForm { recipient_id: mention_user.id, comment_id: inserted_comment.id, @@ -138,6 +143,8 @@ impl Perform<CommentResponse> for Oper<CreateComment> { let parent_comment = Comment::read(&conn, parent_id)?; if parent_comment.creator_id != user_id { let parent_user = User_::read(&conn, parent_comment.creator_id)?; + recipient_ids.push(parent_user.id); + if parent_user.send_notifications_to_email { if let Some(comment_reply_email) = parent_user.email { let subject = &format!( @@ -161,6 +168,8 @@ impl Perform<CommentResponse> for Oper<CreateComment> { None => { if post.creator_id != user_id { let parent_user = User_::read(&conn, post.creator_id)?; + recipient_ids.push(parent_user.id); + if parent_user.send_notifications_to_email { if let Some(post_reply_email) = parent_user.email { let subject = &format!( @@ -199,6 +208,7 @@ impl Perform<CommentResponse> for Oper<CreateComment> { Ok(CommentResponse { comment: comment_view, + recipient_ids, }) } } @@ -265,6 +275,8 @@ impl Perform<CommentResponse> for Oper<EditComment> { Err(_e) => return Err(APIError::err("couldnt_update_comment").into()), }; + let mut recipient_ids = Vec::new(); + // Scan the comment for user mentions, add those rows let extracted_usernames = extract_usernames(&comment_form.content); @@ -278,6 +290,8 @@ impl Perform<CommentResponse> for Oper<EditComment> { // At some point, make it so you can't tag the parent creator either // This can cause two notifications, one for reply and the other for mention if mention_user_id != user_id { + recipient_ids.push(mention_user_id); + let user_mention_form = UserMentionForm { recipient_id: mention_user_id, comment_id: data.edit_id, @@ -294,6 +308,21 @@ impl Perform<CommentResponse> for Oper<EditComment> { } } + // Add to recipient ids + match data.parent_id { + Some(parent_id) => { + let parent_comment = Comment::read(&conn, parent_id)?; + if parent_comment.creator_id != user_id { + let parent_user = User_::read(&conn, parent_comment.creator_id)?; + recipient_ids.push(parent_user.id); + } + } + None => { + let post = Post::read(&conn, data.post_id)?; + recipient_ids.push(post.creator_id); + } + } + // Mod tables if let Some(removed) = data.removed.to_owned() { let form = ModRemoveCommentForm { @@ -309,6 +338,7 @@ impl Perform<CommentResponse> for Oper<EditComment> { Ok(CommentResponse { comment: comment_view, + recipient_ids, }) } } @@ -345,6 +375,7 @@ impl Perform<CommentResponse> for Oper<SaveComment> { Ok(CommentResponse { comment: comment_view, + recipient_ids: Vec::new(), }) } } @@ -360,6 +391,8 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> { let user_id = claims.id; + let mut recipient_ids = Vec::new(); + // Don't do a downvote if site has downvotes disabled if data.score == -1 { let site = SiteView::read(&conn)?; @@ -379,6 +412,22 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> { return Err(APIError::err("site_ban").into()); } + let comment = Comment::read(&conn, data.comment_id)?; + + // Add to recipient ids + match comment.parent_id { + Some(parent_id) => { + let parent_comment = Comment::read(&conn, parent_id)?; + if parent_comment.creator_id != user_id { + let parent_user = User_::read(&conn, parent_comment.creator_id)?; + recipient_ids.push(parent_user.id); + } + } + None => { + recipient_ids.push(post.creator_id); + } + } + let like_form = CommentLikeForm { comment_id: data.comment_id, post_id: data.post_id, @@ -403,6 +452,7 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> { Ok(CommentResponse { comment: liked_comment, + recipient_ids, }) } } |