]> Untitled Git - lemmy.git/blob - crates/websocket/src/send.rs
ccb29879e5d42bf9992adc6d263a7562bccba9df
[lemmy.git] / crates / websocket / src / send.rs
1 use crate::{
2   messages::{SendComment, SendCommunityRoomMessage, SendPost, SendUserRoomMessage},
3   LemmyContext,
4   OperationType,
5 };
6 use lemmy_api_common::{
7   blocking,
8   comment::CommentResponse,
9   community::CommunityResponse,
10   person::PrivateMessageResponse,
11   post::PostResponse,
12 };
13 use lemmy_db_schema::{
14   newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
15   source::{
16     comment::Comment,
17     person::Person,
18     person_mention::{PersonMention, PersonMentionForm},
19     post::Post,
20   },
21   traits::{Crud, DeleteableOrRemoveable},
22 };
23 use lemmy_db_views::{
24   comment_view::CommentView,
25   local_user_view::LocalUserView,
26   post_view::PostView,
27   private_message_view::PrivateMessageView,
28 };
29 use lemmy_db_views_actor::community_view::CommunityView;
30 use lemmy_utils::{
31   email::send_email,
32   settings::structs::Settings,
33   utils::MentionData,
34   ConnectionId,
35   LemmyError,
36 };
37 use tracing::error;
38
39 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
40   post_id: PostId,
41   op: OP,
42   websocket_id: Option<ConnectionId>,
43   person_id: Option<PersonId>,
44   context: &LemmyContext,
45 ) -> Result<PostResponse, LemmyError> {
46   let post_view = blocking(context.pool(), move |conn| {
47     PostView::read(conn, post_id, person_id)
48   })
49   .await??;
50
51   let res = PostResponse { post_view };
52
53   context.chat_server().do_send(SendPost {
54     op,
55     post: res.clone(),
56     websocket_id,
57   });
58
59   Ok(res)
60 }
61
62 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
63 //       we should get the actual recipient actors from somewhere
64 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
65   comment_id: CommentId,
66   op: OP,
67   context: &LemmyContext,
68 ) -> Result<CommentResponse, LemmyError> {
69   send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
70 }
71
72 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
73   comment_id: CommentId,
74   op: OP,
75   websocket_id: Option<ConnectionId>,
76   form_id: Option<String>,
77   person_id: Option<PersonId>,
78   recipient_ids: Vec<LocalUserId>,
79   context: &LemmyContext,
80 ) -> Result<CommentResponse, LemmyError> {
81   let mut view = blocking(context.pool(), move |conn| {
82     CommentView::read(conn, comment_id, person_id)
83   })
84   .await??;
85
86   if view.comment.deleted || view.comment.removed {
87     view.comment = view.comment.blank_out_deleted_or_removed_info();
88   }
89
90   let mut res = CommentResponse {
91     comment_view: view,
92     recipient_ids,
93     // The sent out form id should be null
94     form_id: None,
95   };
96
97   context.chat_server().do_send(SendComment {
98     op,
99     comment: res.clone(),
100     websocket_id,
101   });
102
103   // The recipient_ids should be empty for returns
104   res.recipient_ids = Vec::new();
105   res.form_id = form_id;
106
107   Ok(res)
108 }
109
110 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
111   community_id: CommunityId,
112   op: OP,
113   websocket_id: Option<ConnectionId>,
114   person_id: Option<PersonId>,
115   context: &LemmyContext,
116 ) -> Result<CommunityResponse, LemmyError> {
117   let community_view = blocking(context.pool(), move |conn| {
118     CommunityView::read(conn, community_id, person_id)
119   })
120   .await??;
121
122   let res = CommunityResponse { community_view };
123
124   // Strip out the person id and subscribed when sending to others
125   let mut res_mut = res.clone();
126   res_mut.community_view.subscribed = false;
127
128   context.chat_server().do_send(SendCommunityRoomMessage {
129     op,
130     response: res_mut,
131     community_id: res.community_view.community.id,
132     websocket_id,
133   });
134
135   Ok(res)
136 }
137
138 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
139   private_message_id: PrivateMessageId,
140   op: OP,
141   websocket_id: Option<ConnectionId>,
142   context: &LemmyContext,
143 ) -> Result<PrivateMessageResponse, LemmyError> {
144   let mut view = blocking(context.pool(), move |conn| {
145     PrivateMessageView::read(conn, private_message_id)
146   })
147   .await??;
148
149   // Blank out deleted or removed info
150   if view.private_message.deleted {
151     view.private_message = view.private_message.blank_out_deleted_or_removed_info();
152   }
153
154   let res = PrivateMessageResponse {
155     private_message_view: view,
156   };
157
158   // Send notifications to the local recipient, if one exists
159   if res.private_message_view.recipient.local {
160     let recipient_id = res.private_message_view.recipient.id;
161     let local_recipient = blocking(context.pool(), move |conn| {
162       LocalUserView::read_person(conn, recipient_id)
163     })
164     .await??;
165     context.chat_server().do_send(SendUserRoomMessage {
166       op,
167       response: res.clone(),
168       local_recipient_id: local_recipient.local_user.id,
169       websocket_id,
170     });
171   }
172
173   Ok(res)
174 }
175
176 pub async fn send_local_notifs(
177   mentions: Vec<MentionData>,
178   comment: &Comment,
179   person: &Person,
180   post: &Post,
181   do_send_email: bool,
182   context: &LemmyContext,
183 ) -> Result<Vec<LocalUserId>, LemmyError> {
184   let mut recipient_ids = Vec::new();
185
186   // Send the local mentions
187   for mention in mentions
188     .iter()
189     .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
190     .collect::<Vec<&MentionData>>()
191   {
192     let mention_name = mention.name.clone();
193     let user_view = blocking(context.pool(), move |conn| {
194       LocalUserView::read_from_name(conn, &mention_name)
195     })
196     .await?;
197     if let Ok(mention_user_view) = user_view {
198       // TODO
199       // At some point, make it so you can't tag the parent creator either
200       // This can cause two notifications, one for reply and the other for mention
201       recipient_ids.push(mention_user_view.local_user.id);
202
203       let user_mention_form = PersonMentionForm {
204         recipient_id: mention_user_view.person.id,
205         comment_id: comment.id,
206         read: None,
207       };
208
209       // Allow this to fail softly, since comment edits might re-update or replace it
210       // Let the uniqueness handle this fail
211       blocking(context.pool(), move |conn| {
212         PersonMention::create(conn, &user_mention_form)
213       })
214       .await?
215       .ok();
216
217       // Send an email to those local users that have notifications on
218       if do_send_email {
219         send_email_to_user(
220           &mention_user_view,
221           "Mentioned by",
222           "Person Mention",
223           &comment.content,
224           &context.settings(),
225         )
226       }
227     }
228   }
229
230   // Send notifs to the parent commenter / poster
231   match comment.parent_id {
232     Some(parent_id) => {
233       let parent_comment =
234         blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await?;
235       if let Ok(parent_comment) = parent_comment {
236         // Don't send a notif to yourself
237         if parent_comment.creator_id != person.id {
238           // Get the parent commenter local_user
239           let user_view = blocking(context.pool(), move |conn| {
240             LocalUserView::read_person(conn, parent_comment.creator_id)
241           })
242           .await?;
243           if let Ok(parent_user_view) = user_view {
244             recipient_ids.push(parent_user_view.local_user.id);
245
246             if do_send_email {
247               send_email_to_user(
248                 &parent_user_view,
249                 "Reply from",
250                 "Comment Reply",
251                 &comment.content,
252                 &context.settings(),
253               )
254             }
255           }
256         }
257       }
258     }
259     // Its a post
260     None => {
261       if post.creator_id != person.id {
262         let creator_id = post.creator_id;
263         let parent_user = blocking(context.pool(), move |conn| {
264           LocalUserView::read_person(conn, creator_id)
265         })
266         .await?;
267         if let Ok(parent_user_view) = parent_user {
268           recipient_ids.push(parent_user_view.local_user.id);
269
270           if do_send_email {
271             send_email_to_user(
272               &parent_user_view,
273               "Reply from",
274               "Post Reply",
275               &comment.content,
276               &context.settings(),
277             )
278           }
279         }
280       }
281     }
282   };
283   Ok(recipient_ids)
284 }
285
286 pub fn send_email_to_user(
287   local_user_view: &LocalUserView,
288   subject_text: &str,
289   body_text: &str,
290   comment_content: &str,
291   settings: &Settings,
292 ) {
293   if local_user_view.person.banned || !local_user_view.local_user.send_notifications_to_email {
294     return;
295   }
296
297   if let Some(user_email) = &local_user_view.local_user.email {
298     let subject = &format!(
299       "{} - {} {}",
300       subject_text, settings.hostname, local_user_view.person.name,
301     );
302     let html = &format!(
303       "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
304       body_text,
305       local_user_view.person.name,
306       comment_content,
307       settings.get_protocol_and_hostname()
308     );
309     match send_email(
310       subject,
311       user_email,
312       &local_user_view.person.name,
313       html,
314       settings,
315     ) {
316       Ok(_o) => _o,
317       Err(e) => error!("{}", e),
318     };
319   }
320 }