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