]> Untitled Git - lemmy.git/blob - lemmy_structs/src/lib.rs
f7140205a284ce4478bd9b3ea8d79fbdf25568ac
[lemmy.git] / lemmy_structs / src / lib.rs
1 extern crate actix;
2 extern crate actix_web;
3 extern crate diesel;
4 extern crate log;
5 extern crate serde;
6 #[macro_use]
7 extern crate strum_macros;
8 extern crate chrono;
9
10 pub mod comment;
11 pub mod community;
12 pub mod post;
13 pub mod site;
14 pub mod user;
15 pub mod websocket;
16
17 use diesel::PgConnection;
18 use lemmy_db::{
19   comment::Comment,
20   post::Post,
21   user::User_,
22   user_mention::{UserMention, UserMentionForm},
23   Crud,
24   DbPool,
25 };
26 use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
27 use log::error;
28
29 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
30 where
31   F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
32   T: Send + 'static,
33 {
34   let pool = pool.clone();
35   let res = actix_web::web::block(move || {
36     let conn = pool.get()?;
37     let res = (f)(&conn);
38     Ok(res) as Result<_, LemmyError>
39   })
40   .await?;
41
42   Ok(res)
43 }
44
45 pub async fn send_local_notifs(
46   mentions: Vec<MentionData>,
47   comment: Comment,
48   user: &User_,
49   post: Post,
50   pool: &DbPool,
51   do_send_email: bool,
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)
56   })
57   .await?;
58
59   Ok(ids)
60 }
61
62 fn do_send_local_notifs(
63   conn: &PgConnection,
64   mentions: &[MentionData],
65   comment: &Comment,
66   user: &User_,
67   post: &Post,
68   do_send_email: bool,
69 ) -> Vec<i32> {
70   let mut recipient_ids = Vec::new();
71   let hostname = &format!("https://{}", Settings::get().hostname);
72
73   // Send the local mentions
74   for mention in mentions
75     .iter()
76     .filter(|m| m.is_local() && m.name.ne(&user.name))
77     .collect::<Vec<&MentionData>>()
78   {
79     if let Ok(mention_user) = User_::read_from_name(&conn, &mention.name) {
80       // TODO
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);
84
85       let user_mention_form = UserMentionForm {
86         recipient_id: mention_user.id,
87         comment_id: comment.id,
88         read: None,
89       };
90
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) {
94         Ok(_mention) => (),
95         Err(_e) => error!("{}", &_e),
96       };
97
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,);
102           let html = &format!(
103             "<h1>User Mention</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
104             user.name, comment.content, hostname
105           );
106           match send_email(subject, &mention_email, &mention_user.name, html) {
107             Ok(_o) => _o,
108             Err(e) => error!("{}", e),
109           };
110         }
111       }
112     }
113   }
114
115   // Send notifs to the parent commenter / poster
116   match comment.parent_id {
117     Some(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);
122
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,);
126                 let html = &format!(
127                   "<h1>Comment Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
128                   user.name, comment.content, hostname
129                 );
130                 match send_email(subject, &comment_reply_email, &parent_user.name, html) {
131                   Ok(_o) => _o,
132                   Err(e) => error!("{}", e),
133                 };
134               }
135             }
136           }
137         }
138       }
139     }
140     // Its a post
141     None => {
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);
145
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,);
149               let html = &format!(
150                 "<h1>Post Reply</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
151                 user.name, comment.content, hostname
152               );
153               match send_email(subject, &post_reply_email, &parent_user.name, html) {
154                 Ok(_o) => _o,
155                 Err(e) => error!("{}", e),
156               };
157             }
158           }
159         }
160       }
161     }
162   };
163   recipient_ids
164 }