]> Untitled Git - lemmy.git/blob - lemmy_structs/src/lib.rs
Some API cleanup, adding site_id to site aggregates.
[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
7 use diesel::PgConnection;
8 use lemmy_db::{
9   source::{
10     comment::Comment,
11     post::Post,
12     user::User_,
13     user_mention::{UserMention, UserMentionForm},
14   },
15   Crud,
16   DbPool,
17 };
18 use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
19 use log::error;
20 use serde::{Deserialize, Serialize};
21
22 #[derive(Serialize, Deserialize, Debug)]
23 pub struct WebFingerLink {
24   pub rel: Option<String>,
25   #[serde(rename(serialize = "type", deserialize = "type"))]
26   pub type_: Option<String>,
27   pub href: Option<String>,
28   #[serde(skip_serializing_if = "Option::is_none")]
29   pub template: Option<String>,
30 }
31
32 #[derive(Serialize, Deserialize, Debug)]
33 pub struct WebFingerResponse {
34   pub subject: String,
35   pub aliases: Vec<String>,
36   pub links: Vec<WebFingerLink>,
37 }
38
39 pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
40 where
41   F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,
42   T: Send + 'static,
43 {
44   let pool = pool.clone();
45   let res = actix_web::web::block(move || {
46     let conn = pool.get()?;
47     let res = (f)(&conn);
48     Ok(res) as Result<_, LemmyError>
49   })
50   .await?;
51
52   Ok(res)
53 }
54
55 pub async fn send_local_notifs(
56   mentions: Vec<MentionData>,
57   comment: Comment,
58   user: &User_,
59   post: Post,
60   pool: &DbPool,
61   do_send_email: bool,
62 ) -> Result<Vec<i32>, LemmyError> {
63   let user2 = user.clone();
64   let ids = blocking(pool, move |conn| {
65     do_send_local_notifs(conn, &mentions, &comment, &user2, &post, do_send_email)
66   })
67   .await?;
68
69   Ok(ids)
70 }
71
72 fn do_send_local_notifs(
73   conn: &PgConnection,
74   mentions: &[MentionData],
75   comment: &Comment,
76   user: &User_,
77   post: &Post,
78   do_send_email: bool,
79 ) -> Vec<i32> {
80   let mut recipient_ids = Vec::new();
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       let _ = UserMention::create(&conn, &user_mention_form);
103
104       // Send an email to those users that have notifications on
105       if do_send_email && mention_user.send_notifications_to_email {
106         send_email_to_user(
107           mention_user,
108           "Mentioned by",
109           "User Mention",
110           &comment.content,
111         )
112       }
113     }
114   }
115
116   // Send notifs to the parent commenter / poster
117   match comment.parent_id {
118     Some(parent_id) => {
119       if let Ok(parent_comment) = Comment::read(&conn, parent_id) {
120         if parent_comment.creator_id != user.id {
121           if let Ok(parent_user) = User_::read(&conn, parent_comment.creator_id) {
122             recipient_ids.push(parent_user.id);
123
124             if do_send_email && parent_user.send_notifications_to_email {
125               send_email_to_user(parent_user, "Reply from", "Comment Reply", &comment.content)
126             }
127           }
128         }
129       }
130     }
131     // Its a post
132     None => {
133       if post.creator_id != user.id {
134         if let Ok(parent_user) = User_::read(&conn, post.creator_id) {
135           recipient_ids.push(parent_user.id);
136
137           if do_send_email && parent_user.send_notifications_to_email {
138             send_email_to_user(parent_user, "Reply from", "Post Reply", &comment.content)
139           }
140         }
141       }
142     }
143   };
144   recipient_ids
145 }
146
147 pub fn send_email_to_user(user: User_, subject_text: &str, body_text: &str, comment_content: &str) {
148   if user.banned {
149     return;
150   }
151
152   if let Some(user_email) = user.email {
153     let subject = &format!(
154       "{} - {} {}",
155       subject_text,
156       Settings::get().hostname,
157       user.name,
158     );
159     let html = &format!(
160       "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
161       body_text,
162       user.name,
163       comment_content,
164       Settings::get().get_protocol_and_hostname()
165     );
166     match send_email(subject, &user_email, &user.name, html) {
167       Ok(_o) => _o,
168       Err(e) => error!("{}", e),
169     };
170   }
171 }