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 res = CommentResponse {
85 context.chat_server().do_send(SendComment {
94 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
95 community_id: CommunityId,
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)
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();
110 let res = CommunityResponse { community_view };
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;
116 context.chat_server().do_send(SendCommunityRoomMessage {
119 community_id: res.community_view.community.id,
126 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
127 private_message_id: PrivateMessageId,
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)
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();
142 let res = PrivateMessageResponse {
143 private_message_view: view,
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)
153 context.chat_server().do_send(SendUserRoomMessage {
155 response: res.clone(),
156 local_recipient_id: local_recipient.local_user.id,