]> Untitled Git - lemmy.git/blob - crates/websocket/src/send.rs
Fixing some comment websocket issues. (#1768)
[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 mut res = CommentResponse {
80     comment_view: view,
81     recipient_ids,
82     // The sent out form id should be null
83     form_id: None,
84   };
85
86   context.chat_server().do_send(SendComment {
87     op,
88     comment: res.clone(),
89     websocket_id,
90   });
91
92   // The recipient_ids should be empty for returns
93   res.recipient_ids = Vec::new();
94   res.form_id = form_id;
95
96   Ok(res)
97 }
98
99 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
100   community_id: CommunityId,
101   op: OP,
102   websocket_id: Option<ConnectionId>,
103   person_id: Option<PersonId>,
104   context: &LemmyContext,
105 ) -> Result<CommunityResponse, LemmyError> {
106   let mut community_view = blocking(context.pool(), move |conn| {
107     CommunityView::read(conn, community_id, person_id)
108   })
109   .await??;
110   // Blank out deleted or removed info
111   if community_view.community.deleted || community_view.community.removed {
112     community_view.community = community_view.community.blank_out_deleted_or_removed_info();
113   }
114
115   let res = CommunityResponse { community_view };
116
117   // Strip out the person id and subscribed when sending to others
118   let mut res_mut = res.clone();
119   res_mut.community_view.subscribed = false;
120
121   context.chat_server().do_send(SendCommunityRoomMessage {
122     op,
123     response: res_mut,
124     community_id: res.community_view.community.id,
125     websocket_id,
126   });
127
128   Ok(res)
129 }
130
131 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
132   private_message_id: PrivateMessageId,
133   op: OP,
134   websocket_id: Option<ConnectionId>,
135   context: &LemmyContext,
136 ) -> Result<PrivateMessageResponse, LemmyError> {
137   let mut view = blocking(context.pool(), move |conn| {
138     PrivateMessageView::read(conn, private_message_id)
139   })
140   .await??;
141
142   // Blank out deleted or removed info
143   if view.private_message.deleted {
144     view.private_message = view.private_message.blank_out_deleted_or_removed_info();
145   }
146
147   let res = PrivateMessageResponse {
148     private_message_view: view,
149   };
150
151   // Send notifications to the local recipient, if one exists
152   if res.private_message_view.recipient.local {
153     let recipient_id = res.private_message_view.recipient.id;
154     let local_recipient = blocking(context.pool(), move |conn| {
155       LocalUserView::read_person(conn, recipient_id)
156     })
157     .await??;
158     context.chat_server().do_send(SendUserRoomMessage {
159       op,
160       response: res.clone(),
161       local_recipient_id: local_recipient.local_user.id,
162       websocket_id,
163     });
164   }
165
166   Ok(res)
167 }