]> Untitled Git - lemmy.git/blob - crates/websocket/src/send.rs
Rewrite delete activities (#1699)
[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_queries::DeleteableOrRemoveable;
14 use lemmy_db_schema::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId};
15 use lemmy_db_views::{
16   comment_view::CommentView,
17   local_user_view::LocalUserView,
18   post_view::PostView,
19   private_message_view::PrivateMessageView,
20 };
21 use lemmy_db_views_actor::community_view::CommunityView;
22 use lemmy_utils::{ConnectionId, LemmyError};
23
24 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
25   post_id: PostId,
26   op: OP,
27   websocket_id: Option<ConnectionId>,
28   person_id: Option<PersonId>,
29   context: &LemmyContext,
30 ) -> Result<PostResponse, LemmyError> {
31   let mut post_view = blocking(context.pool(), move |conn| {
32     PostView::read(conn, post_id, person_id)
33   })
34   .await??;
35
36   if post_view.post.deleted || post_view.post.removed {
37     post_view.post = post_view.post.blank_out_deleted_or_removed_info();
38   }
39
40   let res = PostResponse { post_view };
41
42   context.chat_server().do_send(SendPost {
43     op,
44     post: res.clone(),
45     websocket_id,
46   });
47
48   Ok(res)
49 }
50
51 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
52 //       we should get the actual recipient actors from somewhere
53 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
54   comment_id: CommentId,
55   op: OP,
56   context: &LemmyContext,
57 ) -> Result<CommentResponse, LemmyError> {
58   send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
59 }
60
61 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
62   comment_id: CommentId,
63   op: OP,
64   websocket_id: Option<ConnectionId>,
65   form_id: Option<String>,
66   person_id: Option<PersonId>,
67   recipient_ids: Vec<LocalUserId>,
68   context: &LemmyContext,
69 ) -> Result<CommentResponse, LemmyError> {
70   let mut view = blocking(context.pool(), move |conn| {
71     CommentView::read(conn, comment_id, person_id)
72   })
73   .await??;
74
75   if view.comment.deleted || view.comment.removed {
76     view.comment = view.comment.blank_out_deleted_or_removed_info();
77   }
78
79   let res = CommentResponse {
80     comment_view: view,
81     recipient_ids,
82     form_id,
83   };
84
85   context.chat_server().do_send(SendComment {
86     op,
87     comment: res.clone(),
88     websocket_id,
89   });
90
91   Ok(res)
92 }
93
94 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
95   community_id: CommunityId,
96   op: OP,
97   websocket_id: Option<ConnectionId>,
98   person_id: Option<PersonId>,
99   context: &LemmyContext,
100 ) -> Result<CommunityResponse, LemmyError> {
101   let mut community_view = blocking(context.pool(), move |conn| {
102     CommunityView::read(conn, community_id, person_id)
103   })
104   .await??;
105   // Blank out deleted or removed info
106   if community_view.community.deleted || community_view.community.removed {
107     community_view.community = community_view.community.blank_out_deleted_or_removed_info();
108   }
109
110   let res = CommunityResponse { community_view };
111
112   // Strip out the person id and subscribed when sending to others
113   let mut res_mut = res.clone();
114   res_mut.community_view.subscribed = false;
115
116   context.chat_server().do_send(SendCommunityRoomMessage {
117     op,
118     response: res_mut,
119     community_id: res.community_view.community.id,
120     websocket_id,
121   });
122
123   Ok(res)
124 }
125
126 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
127   private_message_id: PrivateMessageId,
128   op: OP,
129   websocket_id: Option<ConnectionId>,
130   context: &LemmyContext,
131 ) -> Result<PrivateMessageResponse, LemmyError> {
132   let mut view = blocking(context.pool(), move |conn| {
133     PrivateMessageView::read(conn, private_message_id)
134   })
135   .await??;
136
137   // Blank out deleted or removed info
138   if view.private_message.deleted {
139     view.private_message = view.private_message.blank_out_deleted_or_removed_info();
140   }
141
142   let res = PrivateMessageResponse {
143     private_message_view: view,
144   };
145
146   // Send notifications to the local recipient, if one exists
147   if res.private_message_view.recipient.local {
148     let recipient_id = res.private_message_view.recipient.id;
149     let local_recipient = blocking(context.pool(), move |conn| {
150       LocalUserView::read_person(conn, recipient_id)
151     })
152     .await??;
153     context.chat_server().do_send(SendUserRoomMessage {
154       op,
155       response: res.clone(),
156       local_recipient_id: local_recipient.local_user.id,
157       websocket_id,
158     });
159   }
160
161   Ok(res)
162 }