]> Untitled Git - lemmy.git/blob - crates/websocket/src/send.rs
Opentelemetry (#1992)
[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   check_person_block,
9   comment::CommentResponse,
10   community::CommunityResponse,
11   person::PrivateMessageResponse,
12   post::PostResponse,
13   send_email_to_user,
14 };
15 use lemmy_db_schema::{
16   newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
17   source::{
18     comment::Comment,
19     person::Person,
20     person_mention::{PersonMention, PersonMentionForm},
21     post::Post,
22   },
23   traits::{Crud, DeleteableOrRemoveable},
24 };
25 use lemmy_db_views::{
26   comment_view::CommentView,
27   local_user_view::LocalUserView,
28   post_view::PostView,
29   private_message_view::PrivateMessageView,
30 };
31 use lemmy_db_views_actor::community_view::CommunityView;
32 use lemmy_utils::{utils::MentionData, ConnectionId, LemmyError};
33
34 #[tracing::instrument(skip_all)]
35 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
36   post_id: PostId,
37   op: OP,
38   websocket_id: Option<ConnectionId>,
39   person_id: Option<PersonId>,
40   context: &LemmyContext,
41 ) -> Result<PostResponse, LemmyError> {
42   let post_view = blocking(context.pool(), move |conn| {
43     PostView::read(conn, post_id, person_id)
44   })
45   .await??;
46
47   let res = PostResponse { post_view };
48
49   context.chat_server().do_send(SendPost {
50     op,
51     post: res.clone(),
52     websocket_id,
53   });
54
55   Ok(res)
56 }
57
58 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
59 //       we should get the actual recipient actors from somewhere
60 #[tracing::instrument(skip_all)]
61 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
62   comment_id: CommentId,
63   op: OP,
64   context: &LemmyContext,
65 ) -> Result<CommentResponse, LemmyError> {
66   send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
67 }
68
69 #[tracing::instrument(skip_all)]
70 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
71   comment_id: CommentId,
72   op: OP,
73   websocket_id: Option<ConnectionId>,
74   form_id: Option<String>,
75   person_id: Option<PersonId>,
76   recipient_ids: Vec<LocalUserId>,
77   context: &LemmyContext,
78 ) -> Result<CommentResponse, LemmyError> {
79   let mut view = blocking(context.pool(), move |conn| {
80     CommentView::read(conn, comment_id, person_id)
81   })
82   .await??;
83
84   if view.comment.deleted || view.comment.removed {
85     view.comment = view.comment.blank_out_deleted_or_removed_info();
86   }
87
88   let mut res = CommentResponse {
89     comment_view: view,
90     recipient_ids,
91     // The sent out form id should be null
92     form_id: None,
93   };
94
95   context.chat_server().do_send(SendComment {
96     op,
97     comment: res.clone(),
98     websocket_id,
99   });
100
101   // The recipient_ids should be empty for returns
102   res.recipient_ids = Vec::new();
103   res.form_id = form_id;
104
105   Ok(res)
106 }
107
108 #[tracing::instrument(skip_all)]
109 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
110   community_id: CommunityId,
111   op: OP,
112   websocket_id: Option<ConnectionId>,
113   person_id: Option<PersonId>,
114   context: &LemmyContext,
115 ) -> Result<CommunityResponse, LemmyError> {
116   let community_view = blocking(context.pool(), move |conn| {
117     CommunityView::read(conn, community_id, person_id)
118   })
119   .await??;
120
121   let res = CommunityResponse { community_view };
122
123   // Strip out the person id and subscribed when sending to others
124   let mut res_mut = res.clone();
125   res_mut.community_view.subscribed = false;
126
127   context.chat_server().do_send(SendCommunityRoomMessage {
128     op,
129     response: res_mut,
130     community_id: res.community_view.community.id,
131     websocket_id,
132   });
133
134   Ok(res)
135 }
136
137 #[tracing::instrument(skip_all)]
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 #[tracing::instrument(skip_all)]
177 pub async fn send_local_notifs(
178   mentions: Vec<MentionData>,
179   comment: &Comment,
180   person: &Person,
181   post: &Post,
182   do_send_email: bool,
183   context: &LemmyContext,
184 ) -> Result<Vec<LocalUserId>, LemmyError> {
185   let mut recipient_ids = Vec::new();
186
187   // Send the local mentions
188   for mention in mentions
189     .iter()
190     .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
191     .collect::<Vec<&MentionData>>()
192   {
193     let mention_name = mention.name.clone();
194     let user_view = blocking(context.pool(), move |conn| {
195       LocalUserView::read_from_name(conn, &mention_name)
196     })
197     .await?;
198     if let Ok(mention_user_view) = user_view {
199       // TODO
200       // At some point, make it so you can't tag the parent creator either
201       // This can cause two notifications, one for reply and the other for mention
202       recipient_ids.push(mention_user_view.local_user.id);
203
204       let user_mention_form = PersonMentionForm {
205         recipient_id: mention_user_view.person.id,
206         comment_id: comment.id,
207         read: None,
208       };
209
210       // Allow this to fail softly, since comment edits might re-update or replace it
211       // Let the uniqueness handle this fail
212       blocking(context.pool(), move |conn| {
213         PersonMention::create(conn, &user_mention_form)
214       })
215       .await?
216       .ok();
217
218       // Send an email to those local users that have notifications on
219       if do_send_email {
220         send_email_to_user(
221           &mention_user_view,
222           "Mentioned by",
223           "Person Mention",
224           &comment.content,
225           &context.settings(),
226         )
227       }
228     }
229   }
230
231   // Send notifs to the parent commenter / poster
232   match comment.parent_id {
233     Some(parent_id) => {
234       let parent_comment =
235         blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await?;
236       if let Ok(parent_comment) = parent_comment {
237         // Get the parent commenter local_user
238         let parent_creator_id = parent_comment.creator_id;
239
240         // Only add to recipients if that person isn't blocked
241         let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool())
242           .await
243           .is_err();
244
245         // Don't send a notif to yourself
246         if parent_comment.creator_id != person.id && !creator_blocked {
247           let user_view = blocking(context.pool(), move |conn| {
248             LocalUserView::read_person(conn, parent_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     // Don't send a notif to yourself
269     None => {
270       // Only add to recipients if that person isn't blocked
271       let creator_blocked = check_person_block(person.id, post.creator_id, context.pool())
272         .await
273         .is_err();
274
275       if post.creator_id != person.id && !creator_blocked {
276         let creator_id = post.creator_id;
277         let parent_user = blocking(context.pool(), move |conn| {
278           LocalUserView::read_person(conn, creator_id)
279         })
280         .await?;
281         if let Ok(parent_user_view) = parent_user {
282           recipient_ids.push(parent_user_view.local_user.id);
283
284           if do_send_email {
285             send_email_to_user(
286               &parent_user_view,
287               "Reply from",
288               "Post Reply",
289               &comment.content,
290               &context.settings(),
291             )
292           }
293         }
294       }
295     }
296   };
297   Ok(recipient_ids)
298 }