diff options
Diffstat (limited to 'server/src/api/comment.rs')
-rw-r--r-- | server/src/api/comment.rs | 79 |
1 files changed, 72 insertions, 7 deletions
diff --git a/server/src/api/comment.rs b/server/src/api/comment.rs index ed658985..5c3417dd 100644 --- a/server/src/api/comment.rs +++ b/server/src/api/comment.rs @@ -1,4 +1,6 @@ use super::*; +use crate::send_email; +use crate::settings::Settings; #[derive(Serialize, Deserialize)] pub struct CreateComment { @@ -56,6 +58,8 @@ impl Perform<CommentResponse> for Oper<CreateComment> { let user_id = claims.id; + let hostname = &format!("https://{}", Settings::get().hostname); + // Check for a community ban let post = Post::read(&conn, data.post_id)?; if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { @@ -89,17 +93,13 @@ impl Perform<CommentResponse> for Oper<CreateComment> { let extracted_usernames = extract_usernames(&comment_form.content); for username_mention in &extracted_usernames { - let mention_user = User_::read_from_name(&conn, (*username_mention).to_string()); - - if mention_user.is_ok() { - let mention_user_id = mention_user?.id; - + if let Ok(mention_user) = User_::read_from_name(&conn, (*username_mention).to_string()) { // You can't mention yourself // 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 { + if mention_user.id != user_id { let user_mention_form = UserMentionForm { - recipient_id: mention_user_id, + recipient_id: mention_user.id, comment_id: inserted_comment.id, read: None, }; @@ -109,11 +109,76 @@ impl Perform<CommentResponse> for Oper<CreateComment> { match UserMention::create(&conn, &user_mention_form) { Ok(_mention) => (), Err(_e) => eprintln!("{}", &_e), + }; + + // Send an email to those users that have notifications on + if mention_user.send_notifications_to_email { + if let Some(mention_email) = mention_user.email { + let subject = &format!( + "{} - Mentioned by {}", + Settings::get().hostname, + claims.username + ); + let html = &format!( + "<h1>User Mention</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>", + claims.username, comment_form.content, hostname + ); + match send_email(subject, &mention_email, &mention_user.name, html) { + Ok(_o) => _o, + Err(e) => eprintln!("{}", e), + }; + } } } } } + // Send notifs to the parent commenter / poster + match data.parent_id { + Some(parent_id) => { + let parent_comment = Comment::read(&conn, parent_id)?; + let parent_user = User_::read(&conn, parent_comment.creator_id)?; + if parent_user.send_notifications_to_email { + if let Some(comment_reply_email) = parent_user.email { + let subject = &format!( + "{} - Reply from {}", + Settings::get().hostname, + claims.username + ); + let html = &format!( + "<h1>Comment Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>", + claims.username, comment_form.content, hostname + ); + match send_email(subject, &comment_reply_email, &parent_user.name, html) { + Ok(_o) => _o, + Err(e) => eprintln!("{}", e), + }; + } + } + } + // Its a post + None => { + let parent_user = User_::read(&conn, post.creator_id)?; + if parent_user.send_notifications_to_email { + if let Some(post_reply_email) = parent_user.email { + let subject = &format!( + "{} - Reply from {}", + Settings::get().hostname, + claims.username + ); + let html = &format!( + "<h1>Post Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>", + claims.username, comment_form.content, hostname + ); + match send_email(subject, &post_reply_email, &parent_user.name, html) { + Ok(_o) => _o, + Err(e) => eprintln!("{}", e), + }; + } + } + } + }; + // You like your own comment by default let like_form = CommentLikeForm { comment_id: inserted_comment.id, |