2 messages::{SendComment, SendCommunityRoomMessage, SendPost, SendUserRoomMessage},
6 use lemmy_api_common::{
8 comment::CommentResponse,
9 community::CommunityResponse,
10 person::PrivateMessageResponse,
13 use lemmy_db_queries::DeleteableOrRemoveable;
14 use lemmy_db_schema::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId};
16 comment_view::CommentView,
17 local_user_view::LocalUserView,
19 private_message_view::PrivateMessageView,
21 use lemmy_db_views_actor::community_view::CommunityView;
22 use lemmy_utils::{ConnectionId, LemmyError};
24 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
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)
36 if post_view.post.deleted || post_view.post.removed {
37 post_view.post = post_view.post.blank_out_deleted_or_removed_info();
40 let res = PostResponse { post_view };
42 context.chat_server().do_send(SendPost {
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,
56 context: &LemmyContext,
57 ) -> Result<CommentResponse, LemmyError> {
58 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
61 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
62 comment_id: CommentId,
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)
75 if view.comment.deleted || view.comment.removed {
76 view.comment = view.comment.blank_out_deleted_or_removed_info();
79 let mut res = CommentResponse {
82 // The sent out form id should be null
86 context.chat_server().do_send(SendComment {
92 // The recipient_ids should be empty for returns
93 res.recipient_ids = Vec::new();
94 res.form_id = form_id;
99 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
100 community_id: CommunityId,
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)
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();
115 let res = CommunityResponse { community_view };
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;
121 context.chat_server().do_send(SendCommunityRoomMessage {
124 community_id: res.community_view.community.id,
131 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
132 private_message_id: PrivateMessageId,
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)
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();
147 let res = PrivateMessageResponse {
148 private_message_view: view,
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)
158 context.chat_server().do_send(SendUserRoomMessage {
160 response: res.clone(),
161 local_recipient_id: local_recipient.local_user.id,