]> Untitled Git - lemmy.git/blob - server/lemmy_api_structs/src/lib.rs
e94a286f770bbfbcb6133a99f8db5b0e0355407c
[lemmy.git] / server / lemmy_api_structs / src / lib.rs
1 extern crate serde;
2 extern crate log;
3 extern crate diesel;
4 extern crate actix_web;
5
6 pub mod comment;
7 pub mod community;
8 pub mod post;
9 pub mod site;
10 pub mod user;
11
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};
16 use log::error;
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;
23
24 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
25   where
26     F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
27     T: Send + 'static,
28 {
29   let pool = pool.clone();
30   let res = actix_web::web::block(move || {
31     let conn = pool.get()?;
32     let res = (f)(&conn);
33     Ok(res) as Result<_, LemmyError>
34   })
35     .await?;
36
37   Ok(res)
38 }
39
40 pub async fn send_local_notifs(
41   mentions: Vec<MentionData>,
42   comment: Comment,
43   user: &User_,
44   post: Post,
45   pool: &DbPool,
46   do_send_email: bool,
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)
51   })
52     .await?;
53
54   Ok(ids)
55 }
56
57 fn do_send_local_notifs(
58   conn: &PgConnection,
59   mentions: &[MentionData],
60   comment: &Comment,
61   user: &User_,
62   post: &Post,
63   do_send_email: bool,
64 ) -> Vec<i32> {
65   let mut recipient_ids = Vec::new();
66   let hostname = &format!("https://{}", Settings::get().hostname);
67
68   // Send the local mentions
69   for mention in mentions
70     .iter()
71     .filter(|m| m.is_local() && m.name.ne(&user.name))
72     .collect::<Vec<&MentionData>>()
73   {
74     if let Ok(mention_user) = User_::read_from_name(&conn, &mention.name) {
75       // TODO
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);
79
80       let user_mention_form = UserMentionForm {
81         recipient_id: mention_user.id,
82         comment_id: comment.id,
83         read: None,
84       };
85
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) {
89         Ok(_mention) => (),
90         Err(_e) => error!("{}", &_e),
91       };
92
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,);
97           let html = &format!(
98             "<h1>User Mention</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
99             user.name, comment.content, hostname
100           );
101           match send_email(subject, &mention_email, &mention_user.name, html) {
102             Ok(_o) => _o,
103             Err(e) => error!("{}", e),
104           };
105         }
106       }
107     }
108   }
109
110   // Send notifs to the parent commenter / poster
111   match comment.parent_id {
112     Some(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);
117
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,);
121                 let html = &format!(
122                   "<h1>Comment Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
123                   user.name, comment.content, hostname
124                 );
125                 match send_email(subject, &comment_reply_email, &parent_user.name, html) {
126                   Ok(_o) => _o,
127                   Err(e) => error!("{}", e),
128                 };
129               }
130             }
131           }
132         }
133       }
134     }
135     // Its a post
136     None => {
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);
140
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,);
144               let html = &format!(
145                 "<h1>Post Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
146                 user.name, comment.content, hostname
147               );
148               match send_email(subject, &post_reply_email, &parent_user.name, html) {
149                 Ok(_o) => _o,
150                 Err(e) => error!("{}", e),
151               };
152             }
153           }
154         }
155       }
156     }
157   };
158   recipient_ids
159 }