]> Untitled Git - lemmy.git/blob - crates/websocket/src/send.rs
Merge crates db_schema and db_queries
[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   traits::DeleteableOrRemoveable,
16 };
17 use lemmy_db_views::{
18   comment_view::CommentView,
19   local_user_view::LocalUserView,
20   post_view::PostView,
21   private_message_view::PrivateMessageView,
22 };
23 use lemmy_db_views_actor::community_view::CommunityView;
24 use lemmy_utils::{ConnectionId, LemmyError};
25
26 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
27   post_id: PostId,
28   op: OP,
29   websocket_id: Option<ConnectionId>,
30   person_id: Option<PersonId>,
31   context: &LemmyContext,
32 ) -> Result<PostResponse, LemmyError> {
33   let mut post_view = blocking(context.pool(), move |conn| {
34     PostView::read(conn, post_id, person_id)
35   })
36   .await??;
37
38   if post_view.post.deleted || post_view.post.removed {
39     post_view.post = post_view.post.blank_out_deleted_or_removed_info();
40   }
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 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
56   comment_id: CommentId,
57   op: OP,
58   context: &LemmyContext,
59 ) -> Result<CommentResponse, LemmyError> {
60   send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
61 }
62
63 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
64   comment_id: CommentId,
65   op: OP,
66   websocket_id: Option<ConnectionId>,
67   form_id: Option<String>,
68   person_id: Option<PersonId>,
69   recipient_ids: Vec<LocalUserId>,
70   context: &LemmyContext,
71 ) -> Result<CommentResponse, LemmyError> {
72   let mut view = blocking(context.pool(), move |conn| {
73     CommentView::read(conn, comment_id, person_id)
74   })
75   .await??;
76
77   if view.comment.deleted || view.comment.removed {
78     view.comment = view.comment.blank_out_deleted_or_removed_info();
79   }
80
81   let mut res = CommentResponse {
82     comment_view: view,
83     recipient_ids,
84     // The sent out form id should be null
85     form_id: None,
86   };
87
88   context.chat_server().do_send(SendComment {
89     op,
90     comment: res.clone(),
91     websocket_id,
92   });
93
94   // The recipient_ids should be empty for returns
95   res.recipient_ids = Vec::new();
96   res.form_id = form_id;
97
98   Ok(res)
99 }
100
101 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
102   community_id: CommunityId,
103   op: OP,
104   websocket_id: Option<ConnectionId>,
105   person_id: Option<PersonId>,
106   context: &LemmyContext,
107 ) -> Result<CommunityResponse, LemmyError> {
108   let mut community_view = blocking(context.pool(), move |conn| {
109     CommunityView::read(conn, community_id, person_id)
110   })
111   .await??;
112   // Blank out deleted or removed info
113   if community_view.community.deleted || community_view.community.removed {
114     community_view.community = community_view.community.blank_out_deleted_or_removed_info();
115   }
116
117   let res = CommunityResponse { community_view };
118
119   // Strip out the person id and subscribed when sending to others
120   let mut res_mut = res.clone();
121   res_mut.community_view.subscribed = false;
122
123   context.chat_server().do_send(SendCommunityRoomMessage {
124     op,
125     response: res_mut,
126     community_id: res.community_view.community.id,
127     websocket_id,
128   });
129
130   Ok(res)
131 }
132
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 }