4 extern crate actix_web;
12 use lemmy_db::comment::Comment;
13 use lemmy_db::user::User_;
14 use lemmy_db::post::Post;
15 use lemmy_db::user_mention::{UserMentionForm, UserMention};
17 use lemmy_db::{Crud, DbPool};
18 use lemmy_utils::utils::MentionData;
19 use lemmy_utils::settings::Settings;
20 use lemmy_utils::email::send_email;
21 use diesel::PgConnection;
22 use lemmy_utils::LemmyError;
24 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
26 F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
29 let pool = pool.clone();
30 let res = actix_web::web::block(move || {
31 let conn = pool.get()?;
33 Ok(res) as Result<_, LemmyError>
40 pub async fn send_local_notifs(
41 mentions: Vec<MentionData>,
47 ) -> Result<Vec<i32>, LemmyError> {
48 let user2 = user.clone();
49 let ids = blocking(pool, move |conn| {
50 do_send_local_notifs(conn, &mentions, &comment, &user2, &post, do_send_email)
57 fn do_send_local_notifs(
59 mentions: &[MentionData],
65 let mut recipient_ids = Vec::new();
66 let hostname = &format!("https://{}", Settings::get().hostname);
68 // Send the local mentions
69 for mention in mentions
71 .filter(|m| m.is_local() && m.name.ne(&user.name))
72 .collect::<Vec<&MentionData>>()
74 if let Ok(mention_user) = User_::read_from_name(&conn, &mention.name) {
76 // At some point, make it so you can't tag the parent creator either
77 // This can cause two notifications, one for reply and the other for mention
78 recipient_ids.push(mention_user.id);
80 let user_mention_form = UserMentionForm {
81 recipient_id: mention_user.id,
82 comment_id: comment.id,
86 // Allow this to fail softly, since comment edits might re-update or replace it
87 // Let the uniqueness handle this fail
88 match UserMention::create(&conn, &user_mention_form) {
90 Err(_e) => error!("{}", &_e),
93 // Send an email to those users that have notifications on
94 if do_send_email && mention_user.send_notifications_to_email {
95 if let Some(mention_email) = mention_user.email {
96 let subject = &format!("{} - Mentioned by {}", Settings::get().hostname, user.name,);
98 "<h1>User Mention</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
99 user.name, comment.content, hostname
101 match send_email(subject, &mention_email, &mention_user.name, html) {
103 Err(e) => error!("{}", e),
110 // Send notifs to the parent commenter / poster
111 match comment.parent_id {
113 if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
114 if parent_comment.creator_id != user.id {
115 if let Ok(parent_user) = User_::read(&conn, parent_comment.creator_id) {
116 recipient_ids.push(parent_user.id);
118 if do_send_email && parent_user.send_notifications_to_email {
119 if let Some(comment_reply_email) = parent_user.email {
120 let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,);
122 "<h1>Comment Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
123 user.name, comment.content, hostname
125 match send_email(subject, &comment_reply_email, &parent_user.name, html) {
127 Err(e) => error!("{}", e),
137 if post.creator_id != user.id {
138 if let Ok(parent_user) = User_::read(&conn, post.creator_id) {
139 recipient_ids.push(parent_user.id);
141 if do_send_email && parent_user.send_notifications_to_email {
142 if let Some(post_reply_email) = parent_user.email {
143 let subject = &format!("{} - Reply from {}", Settings::get().hostname, user.name,);
145 "<h1>Post Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
146 user.name, comment.content, hostname
148 match send_email(subject, &post_reply_email, &parent_user.name, html) {
150 Err(e) => error!("{}", e),