]> Untitled Git - lemmy.git/blob - crates/api_structs/src/lib.rs
Removing some TODOS.
[lemmy.git] / crates / api_structs / src / lib.rs
1 pub mod comment;
2 pub mod community;
3 pub mod person;
4 pub mod post;
5 pub mod site;
6 pub mod websocket;
7
8 use diesel::PgConnection;
9 use lemmy_db_queries::{Crud, DbPool};
10 use lemmy_db_schema::source::{
11   comment::Comment,
12   person::Person,
13   person_mention::{PersonMention, PersonMentionForm},
14   post::Post,
15 };
16 use lemmy_db_views::local_user_view::LocalUserView;
17 use lemmy_utils::{email::send_email, settings::structs::Settings, utils::MentionData, LemmyError};
18 use log::error;
19 use serde::{Deserialize, Serialize};
20 use url::Url;
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<Url>,
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<Url>,
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   person: Person,
59   post: Post,
60   pool: &DbPool,
61   do_send_email: bool,
62 ) -> Result<Vec<i32>, LemmyError> {
63   let ids = blocking(pool, move |conn| {
64     do_send_local_notifs(conn, &mentions, &comment, &person, &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   person: &Person,
76   post: &Post,
77   do_send_email: bool,
78 ) -> Vec<i32> {
79   let mut recipient_ids = Vec::new();
80
81   // Send the local mentions
82   for mention in mentions
83     .iter()
84     .filter(|m| m.is_local() && m.name.ne(&person.name))
85     .collect::<Vec<&MentionData>>()
86   {
87     if let Ok(mention_user_view) = LocalUserView::read_from_name(&conn, &mention.name) {
88       // TODO
89       // At some point, make it so you can't tag the parent creator either
90       // This can cause two notifications, one for reply and the other for mention
91       recipient_ids.push(mention_user_view.local_user.id);
92
93       let user_mention_form = PersonMentionForm {
94         recipient_id: mention_user_view.person.id,
95         comment_id: comment.id,
96         read: None,
97       };
98
99       // Allow this to fail softly, since comment edits might re-update or replace it
100       // Let the uniqueness handle this fail
101       PersonMention::create(&conn, &user_mention_form).ok();
102
103       // Send an email to those local users that have notifications on
104       if do_send_email && mention_user_view.local_user.send_notifications_to_email {
105         send_email_to_user(
106           &mention_user_view,
107           "Mentioned by",
108           "Person Mention",
109           &comment.content,
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 != person.id {
120           if let Ok(parent_user_view) = LocalUserView::read_person(&conn, parent_comment.creator_id)
121           {
122             recipient_ids.push(parent_user_view.local_user.id);
123
124             if do_send_email && parent_user_view.local_user.send_notifications_to_email {
125               send_email_to_user(
126                 &parent_user_view,
127                 "Reply from",
128                 "Comment Reply",
129                 &comment.content,
130               )
131             }
132           }
133         }
134       }
135     }
136     // Its a post
137     None => {
138       if post.creator_id != person.id {
139         if let Ok(parent_user_view) = LocalUserView::read_person(&conn, post.creator_id) {
140           recipient_ids.push(parent_user_view.local_user.id);
141
142           if do_send_email && parent_user_view.local_user.send_notifications_to_email {
143             send_email_to_user(
144               &parent_user_view,
145               "Reply from",
146               "Post Reply",
147               &comment.content,
148             )
149           }
150         }
151       }
152     }
153   };
154   recipient_ids
155 }
156
157 pub fn send_email_to_user(
158   local_user_view: &LocalUserView,
159   subject_text: &str,
160   body_text: &str,
161   comment_content: &str,
162 ) {
163   if local_user_view.person.banned {
164     return;
165   }
166
167   if let Some(user_email) = &local_user_view.local_user.email {
168     let subject = &format!(
169       "{} - {} {}",
170       subject_text,
171       Settings::get().hostname(),
172       local_user_view.person.name,
173     );
174     let html = &format!(
175       "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
176       body_text,
177       local_user_view.person.name,
178       comment_content,
179       Settings::get().get_protocol_and_hostname()
180     );
181     match send_email(subject, &user_email, &local_user_view.person.name, html) {
182       Ok(_o) => _o,
183       Err(e) => error!("{}", e),
184     };
185   }
186 }