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