2 extern crate actix_web;
7 extern crate strum_macros;
17 use diesel::PgConnection;
22 user_mention::{UserMention, UserMentionForm},
26 use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
29 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
31 F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
34 let pool = pool.clone();
35 let res = actix_web::web::block(move || {
36 let conn = pool.get()?;
38 Ok(res) as Result<_, LemmyError>
45 pub async fn send_local_notifs(
46 mentions: Vec<MentionData>,
52 ) -> Result<Vec<i32>, LemmyError> {
53 let user2 = user.clone();
54 let ids = blocking(pool, move |conn| {
55 do_send_local_notifs(conn, &mentions, &comment, &user2, &post, do_send_email)
62 fn do_send_local_notifs(
64 mentions: &[MentionData],
70 let mut recipient_ids = Vec::new();
71 let hostname = &format!("https://{}", Settings::get().hostname);
73 // Send the local mentions
74 for mention in mentions
76 .filter(|m| m.is_local() && m.name.ne(&user.name))
77 .collect::<Vec<&MentionData>>()
79 if let Ok(mention_user) = User_::read_from_name(&conn, &mention.name) {
81 // At some point, make it so you can't tag the parent creator either
82 // This can cause two notifications, one for reply and the other for mention
83 recipient_ids.push(mention_user.id);
85 let user_mention_form = UserMentionForm {
86 recipient_id: mention_user.id,
87 comment_id: comment.id,
91 // Allow this to fail softly, since comment edits might re-update or replace it
92 // Let the uniqueness handle this fail
93 match UserMention::create(&conn, &user_mention_form) {
95 Err(_e) => error!("{}", &_e),
98 // Send an email to those users that have notifications on
99 if do_send_email && mention_user.send_notifications_to_email {
100 if let Some(mention_email) = mention_user.email {
101 let subject = &format!("{} - Mentioned by {}", Settings::get().hostname, user.name,);
103 "<h1>User Mention</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
104 user.name, comment.content, hostname
106 match send_email(subject, &mention_email, &mention_user.name, html) {
108 Err(e) => error!("{}", e),
115 // Send notifs to the parent commenter / poster
116 match comment.parent_id {
118 if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
119 if parent_comment.creator_id != user.id {
120 if let Ok(parent_user) = User_::read(&conn, parent_comment.creator_id) {
121 recipient_ids.push(parent_user.id);
123 if do_send_email && parent_user.send_notifications_to_email {
124 if let Some(comment_reply_email) = parent_user.email {
125 let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,);
127 "<h1>Comment Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
128 user.name, comment.content, hostname
130 match send_email(subject, &comment_reply_email, &parent_user.name, html) {
132 Err(e) => error!("{}", e),
142 if post.creator_id != user.id {
143 if let Ok(parent_user) = User_::read(&conn, post.creator_id) {
144 recipient_ids.push(parent_user.id);
146 if do_send_email && parent_user.send_notifications_to_email {
147 if let Some(post_reply_email) = parent_user.email {
148 let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,);
150 "<h1>Post Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
151 user.name, comment.content, hostname
153 match send_email(subject, &post_reply_email, &parent_user.name, html) {
155 Err(e) => error!("{}", e),