2 messages::{SendComment, SendCommunityRoomMessage, SendPost, SendUserRoomMessage},
6 use lemmy_api_common::{
8 comment::CommentResponse,
9 community::CommunityResponse,
10 person::PrivateMessageResponse,
13 use lemmy_db_schema::{
14 newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
15 traits::DeleteableOrRemoveable,
18 comment_view::CommentView,
19 local_user_view::LocalUserView,
21 private_message_view::PrivateMessageView,
23 use lemmy_db_views_actor::community_view::CommunityView;
24 use lemmy_utils::{ConnectionId, LemmyError};
26 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
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)
38 if post_view.post.deleted || post_view.post.removed {
39 post_view.post = post_view.post.blank_out_deleted_or_removed_info();
42 let res = PostResponse { post_view };
44 context.chat_server().do_send(SendPost {
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,
58 context: &LemmyContext,
59 ) -> Result<CommentResponse, LemmyError> {
60 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
63 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
64 comment_id: CommentId,
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)
77 if view.comment.deleted || view.comment.removed {
78 view.comment = view.comment.blank_out_deleted_or_removed_info();
81 let mut res = CommentResponse {
84 // The sent out form id should be null
88 context.chat_server().do_send(SendComment {
94 // The recipient_ids should be empty for returns
95 res.recipient_ids = Vec::new();
96 res.form_id = form_id;
101 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
102 community_id: CommunityId,
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)
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();
117 let res = CommunityResponse { community_view };
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;
123 context.chat_server().do_send(SendCommunityRoomMessage {
126 community_id: res.community_view.community.id,
133 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
134 private_message_id: PrivateMessageId,
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)
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();
149 let res = PrivateMessageResponse {
150 private_message_view: view,
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)
160 context.chat_server().do_send(SendUserRoomMessage {
162 response: res.clone(),
163 local_recipient_id: local_recipient.local_user.id,