]> Untitled Git - lemmy.git/blob - lemmy_structs/src/lib.rs
dc06a40cd1c05415353bf5b08b203998df6c8829
[lemmy.git] / lemmy_structs / src / lib.rs
1 pub mod comment;
2 pub mod community;
3 pub mod post;
4 pub mod site;
5 pub mod user;
6 pub mod websocket;
7
8 use diesel::PgConnection;
9 use lemmy_db::{
10   source::{
11     comment::Comment,
12     post::Post,
13     user::User_,
14     user_mention::{UserMention, UserMentionForm},
15   },
16   Crud,
17   DbPool,
18 };
19 use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
20 use log::error;
21 use serde::{Deserialize, Serialize};
22
23 #[derive(Serialize, Deserialize, Debug)]
24 pub struct WebFingerLink {
25   pub rel: Option<String>,
26   #[serde(rename(serialize = "type", deserialize = "type"))]
27   pub type_: Option<String>,
28   pub href: Option<String>,
29   #[serde(skip_serializing_if = "Option::is_none")]
30   pub template: Option<String>,
31 }
32
33 #[derive(Serialize, Deserialize, Debug)]
34 pub struct WebFingerResponse {
35   pub subject: String,
36   pub aliases: Vec<String>,
37   pub links: Vec<WebFingerLink>,
38 }
39
40 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
41 where
42   F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
43   T: Send + 'static,
44 {
45   let pool = pool.clone();
46   let res = actix_web::web::block(move || {
47     let conn = pool.get()?;
48     let res = (f)(&conn);
49     Ok(res) as Result<_, LemmyError>
50   })
51   .await?;
52
53   Ok(res)
54 }
55
56 pub async fn send_local_notifs(
57   mentions: Vec<MentionData>,
58   comment: Comment,
59   user: &User_,
60   post: Post,
61   pool: &DbPool,
62   do_send_email: bool,
63 ) -> Result<Vec<i32>, LemmyError> {
64   let user2 = user.clone();
65   let ids = blocking(pool, move |conn| {
66     do_send_local_notifs(conn, &mentions, &comment, &user2, &post, do_send_email)
67   })
68   .await?;
69
70   Ok(ids)
71 }
72
73 fn do_send_local_notifs(
74   conn: &PgConnection,
75   mentions: &[MentionData],
76   comment: &Comment,
77   user: &User_,
78   post: &Post,
79   do_send_email: bool,
80 ) -> Vec<i32> {
81   let mut recipient_ids = Vec::new();
82
83   // Send the local mentions
84   for mention in mentions
85     .iter()
86     .filter(|m| m.is_local() && m.name.ne(&user.name))
87     .collect::<Vec<&MentionData>>()
88   {
89     if let Ok(mention_user) = User_::read_from_name(&conn, &mention.name) {
90       // TODO
91       // At some point, make it so you can't tag the parent creator either
92       // This can cause two notifications, one for reply and the other for mention
93       recipient_ids.push(mention_user.id);
94
95       let user_mention_form = UserMentionForm {
96         recipient_id: mention_user.id,
97         comment_id: comment.id,
98         read: None,
99       };
100
101       // Allow this to fail softly, since comment edits might re-update or replace it
102       // Let the uniqueness handle this fail
103       let _ = UserMention::create(&conn, &user_mention_form);
104
105       // Send an email to those users that have notifications on
106       if do_send_email && mention_user.send_notifications_to_email {
107         send_email_to_user(
108           mention_user,
109           "Mentioned by",
110           "User Mention",
111           &comment.content,
112         )
113       }
114     }
115   }
116
117   // Send notifs to the parent commenter / poster
118   match comment.parent_id {
119     Some(parent_id) => {
120       if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
121         if parent_comment.creator_id != user.id {
122           if let Ok(parent_user) = User_::read(&conn, parent_comment.creator_id) {
123             recipient_ids.push(parent_user.id);
124
125             if do_send_email && parent_user.send_notifications_to_email {
126               send_email_to_user(parent_user, "Reply from", "Comment Reply", &comment.content)
127             }
128           }
129         }
130       }
131     }
132     // Its a post
133     None => {
134       if post.creator_id != user.id {
135         if let Ok(parent_user) = User_::read(&conn, post.creator_id) {
136           recipient_ids.push(parent_user.id);
137
138           if do_send_email && parent_user.send_notifications_to_email {
139             send_email_to_user(parent_user, "Reply from", "Post Reply", &comment.content)
140           }
141         }
142       }
143     }
144   };
145   recipient_ids
146 }
147
148 pub fn send_email_to_user(user: User_, subject_text: &str, body_text: &str, comment_content: &str) {
149   if user.banned {
150     return;
151   }
152
153   if let Some(user_email) = user.email {
154     let subject = &format!(
155       "{} - {} {}",
156       subject_text,
157       Settings::get().hostname,
158       user.name,
159     );
160     let html = &format!(
161       "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
162       body_text,
163       user.name,
164       comment_content,
165       Settings::get().get_protocol_and_hostname()
166     );
167     match send_email(subject, &user_email, &user.name, html) {
168       Ok(_o) => _o,
169       Err(e) => error!("{}", e),
170     };
171   }
172 }