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