2 messages::{SendComment, SendCommunityRoomMessage, SendPost, SendUserRoomMessage},
6 use lemmy_api_common::{
9 comment::CommentResponse,
10 community::CommunityResponse,
11 person::PrivateMessageResponse,
15 use lemmy_db_schema::{
16 newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
20 person_mention::{PersonMention, PersonMentionForm},
23 traits::{Crud, DeleteableOrRemoveable},
26 comment_view::CommentView,
27 local_user_view::LocalUserView,
29 private_message_view::PrivateMessageView,
31 use lemmy_db_views_actor::community_view::CommunityView;
32 use lemmy_utils::{utils::MentionData, ConnectionId, LemmyError};
34 #[tracing::instrument(skip_all)]
35 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
38 websocket_id: Option<ConnectionId>,
39 person_id: Option<PersonId>,
40 context: &LemmyContext,
41 ) -> Result<PostResponse, LemmyError> {
42 let post_view = blocking(context.pool(), move |conn| {
43 PostView::read(conn, post_id, person_id)
47 let res = PostResponse { post_view };
49 context.chat_server().do_send(SendPost {
58 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
59 // we should get the actual recipient actors from somewhere
60 #[tracing::instrument(skip_all)]
61 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
62 comment_id: CommentId,
64 context: &LemmyContext,
65 ) -> Result<CommentResponse, LemmyError> {
66 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
69 #[tracing::instrument(skip_all)]
70 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
71 comment_id: CommentId,
73 websocket_id: Option<ConnectionId>,
74 form_id: Option<String>,
75 person_id: Option<PersonId>,
76 recipient_ids: Vec<LocalUserId>,
77 context: &LemmyContext,
78 ) -> Result<CommentResponse, LemmyError> {
79 let mut view = blocking(context.pool(), move |conn| {
80 CommentView::read(conn, comment_id, person_id)
84 if view.comment.deleted || view.comment.removed {
85 view.comment = view.comment.blank_out_deleted_or_removed_info();
88 let mut res = CommentResponse {
91 // The sent out form id should be null
95 context.chat_server().do_send(SendComment {
101 // The recipient_ids should be empty for returns
102 res.recipient_ids = Vec::new();
103 res.form_id = form_id;
108 #[tracing::instrument(skip_all)]
109 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
110 community_id: CommunityId,
112 websocket_id: Option<ConnectionId>,
113 person_id: Option<PersonId>,
114 context: &LemmyContext,
115 ) -> Result<CommunityResponse, LemmyError> {
116 let community_view = blocking(context.pool(), move |conn| {
117 CommunityView::read(conn, community_id, person_id)
121 let res = CommunityResponse { community_view };
123 // Strip out the person id and subscribed when sending to others
124 let mut res_mut = res.clone();
125 res_mut.community_view.subscribed = false;
127 context.chat_server().do_send(SendCommunityRoomMessage {
130 community_id: res.community_view.community.id,
137 #[tracing::instrument(skip_all)]
138 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
139 private_message_id: PrivateMessageId,
141 websocket_id: Option<ConnectionId>,
142 context: &LemmyContext,
143 ) -> Result<PrivateMessageResponse, LemmyError> {
144 let mut view = blocking(context.pool(), move |conn| {
145 PrivateMessageView::read(conn, private_message_id)
149 // Blank out deleted or removed info
150 if view.private_message.deleted {
151 view.private_message = view.private_message.blank_out_deleted_or_removed_info();
154 let res = PrivateMessageResponse {
155 private_message_view: view,
158 // Send notifications to the local recipient, if one exists
159 if res.private_message_view.recipient.local {
160 let recipient_id = res.private_message_view.recipient.id;
161 let local_recipient = blocking(context.pool(), move |conn| {
162 LocalUserView::read_person(conn, recipient_id)
165 context.chat_server().do_send(SendUserRoomMessage {
167 response: res.clone(),
168 local_recipient_id: local_recipient.local_user.id,
176 #[tracing::instrument(skip_all)]
177 pub async fn send_local_notifs(
178 mentions: Vec<MentionData>,
183 context: &LemmyContext,
184 ) -> Result<Vec<LocalUserId>, LemmyError> {
185 let mut recipient_ids = Vec::new();
187 // Send the local mentions
188 for mention in mentions
190 .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
191 .collect::<Vec<&MentionData>>()
193 let mention_name = mention.name.clone();
194 let user_view = blocking(context.pool(), move |conn| {
195 LocalUserView::read_from_name(conn, &mention_name)
198 if let Ok(mention_user_view) = user_view {
200 // At some point, make it so you can't tag the parent creator either
201 // This can cause two notifications, one for reply and the other for mention
202 recipient_ids.push(mention_user_view.local_user.id);
204 let user_mention_form = PersonMentionForm {
205 recipient_id: mention_user_view.person.id,
206 comment_id: comment.id,
210 // Allow this to fail softly, since comment edits might re-update or replace it
211 // Let the uniqueness handle this fail
212 blocking(context.pool(), move |conn| {
213 PersonMention::create(conn, &user_mention_form)
218 // Send an email to those local users that have notifications on
231 // Send notifs to the parent commenter / poster
232 match comment.parent_id {
235 blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await?;
236 if let Ok(parent_comment) = parent_comment {
237 // Get the parent commenter local_user
238 let parent_creator_id = parent_comment.creator_id;
240 // Only add to recipients if that person isn't blocked
241 let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool())
245 // Don't send a notif to yourself
246 if parent_comment.creator_id != person.id && !creator_blocked {
247 let user_view = blocking(context.pool(), move |conn| {
248 LocalUserView::read_person(conn, parent_creator_id)
251 if let Ok(parent_user_view) = user_view {
252 recipient_ids.push(parent_user_view.local_user.id);
268 // Don't send a notif to yourself
270 // Only add to recipients if that person isn't blocked
271 let creator_blocked = check_person_block(person.id, post.creator_id, context.pool())
275 if post.creator_id != person.id && !creator_blocked {
276 let creator_id = post.creator_id;
277 let parent_user = blocking(context.pool(), move |conn| {
278 LocalUserView::read_person(conn, creator_id)
281 if let Ok(parent_user_view) = parent_user {
282 recipient_ids.push(parent_user_view.local_user.id);