2 messages::{SendComment, SendCommunityRoomMessage, SendPost, SendUserRoomMessage},
6 use lemmy_api_common::{
9 comment::CommentResponse,
10 community::CommunityResponse,
12 person::PrivateMessageResponse,
16 use lemmy_db_schema::{
17 newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
21 person_mention::{PersonMention, PersonMentionForm},
24 traits::{Crud, DeleteableOrRemoveable},
27 comment_view::CommentView,
28 local_user_view::LocalUserView,
30 private_message_view::PrivateMessageView,
32 use lemmy_db_views_actor::community_view::CommunityView;
33 use lemmy_utils::{utils::MentionData, ConnectionId, LemmyError};
35 #[tracing::instrument(skip_all)]
36 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
39 websocket_id: Option<ConnectionId>,
40 person_id: Option<PersonId>,
41 context: &LemmyContext,
42 ) -> Result<PostResponse, LemmyError> {
43 let post_view = blocking(context.pool(), move |conn| {
44 PostView::read(conn, post_id, person_id)
48 let res = PostResponse { post_view };
50 context.chat_server().do_send(SendPost {
59 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
60 // we should get the actual recipient actors from somewhere
61 #[tracing::instrument(skip_all)]
62 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
63 comment_id: CommentId,
65 context: &LemmyContext,
66 ) -> Result<CommentResponse, LemmyError> {
67 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
70 #[tracing::instrument(skip_all)]
71 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
72 comment_id: CommentId,
74 websocket_id: Option<ConnectionId>,
75 form_id: Option<String>,
76 person_id: Option<PersonId>,
77 recipient_ids: Vec<LocalUserId>,
78 context: &LemmyContext,
79 ) -> Result<CommentResponse, LemmyError> {
80 let mut view = blocking(context.pool(), move |conn| {
81 CommentView::read(conn, comment_id, person_id)
85 if view.comment.deleted || view.comment.removed {
86 view.comment = view.comment.blank_out_deleted_or_removed_info();
89 let mut res = CommentResponse {
92 // The sent out form id should be null
96 context.chat_server().do_send(SendComment {
102 // The recipient_ids should be empty for returns
103 res.recipient_ids = Vec::new();
104 res.form_id = form_id;
109 #[tracing::instrument(skip_all)]
110 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
111 community_id: CommunityId,
113 websocket_id: Option<ConnectionId>,
114 person_id: Option<PersonId>,
115 context: &LemmyContext,
116 ) -> Result<CommunityResponse, LemmyError> {
117 let community_view = blocking(context.pool(), move |conn| {
118 CommunityView::read(conn, community_id, person_id)
122 let res = CommunityResponse { community_view };
124 // Strip out the person id and subscribed when sending to others
125 let mut res_mut = res.clone();
126 res_mut.community_view.subscribed = false;
128 context.chat_server().do_send(SendCommunityRoomMessage {
131 community_id: res.community_view.community.id,
138 #[tracing::instrument(skip_all)]
139 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
140 private_message_id: PrivateMessageId,
142 websocket_id: Option<ConnectionId>,
143 context: &LemmyContext,
144 ) -> Result<PrivateMessageResponse, LemmyError> {
145 let mut view = blocking(context.pool(), move |conn| {
146 PrivateMessageView::read(conn, private_message_id)
150 // Blank out deleted or removed info
151 if view.private_message.deleted {
152 view.private_message = view.private_message.blank_out_deleted_or_removed_info();
155 let res = PrivateMessageResponse {
156 private_message_view: view,
159 // Send notifications to the local recipient, if one exists
160 if res.private_message_view.recipient.local {
161 let recipient_id = res.private_message_view.recipient.id;
162 let local_recipient = blocking(context.pool(), move |conn| {
163 LocalUserView::read_person(conn, recipient_id)
166 context.chat_server().do_send(SendUserRoomMessage {
168 response: res.clone(),
169 local_recipient_id: local_recipient.local_user.id,
177 #[tracing::instrument(skip_all)]
178 pub async fn send_local_notifs(
179 mentions: Vec<MentionData>,
184 context: &LemmyContext,
185 ) -> Result<Vec<LocalUserId>, LemmyError> {
186 let mut recipient_ids = Vec::new();
187 let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
189 // Send the local mentions
190 for mention in mentions
192 .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
193 .collect::<Vec<&MentionData>>()
195 let mention_name = mention.name.clone();
196 let user_view = blocking(context.pool(), move |conn| {
197 LocalUserView::read_from_name(conn, &mention_name)
200 if let Ok(mention_user_view) = user_view {
202 // At some point, make it so you can't tag the parent creator either
203 // This can cause two notifications, one for reply and the other for mention
204 recipient_ids.push(mention_user_view.local_user.id);
206 let user_mention_form = PersonMentionForm {
207 recipient_id: mention_user_view.person.id,
208 comment_id: comment.id,
212 // Allow this to fail softly, since comment edits might re-update or replace it
213 // Let the uniqueness handle this fail
214 blocking(context.pool(), move |conn| {
215 PersonMention::create(conn, &user_mention_form)
220 // Send an email to those local users that have notifications on
222 let lang = get_user_lang(&mention_user_view);
225 &lang.notification_mentioned_by_subject(&person.name),
226 &lang.notification_mentioned_by_body(&person.name, &comment.content, &inbox_link),
233 // Send notifs to the parent commenter / poster
234 match comment.parent_id {
237 blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await?;
238 if let Ok(parent_comment) = parent_comment {
239 // Get the parent commenter local_user
240 let parent_creator_id = parent_comment.creator_id;
242 // Only add to recipients if that person isn't blocked
243 let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool())
247 // Don't send a notif to yourself
248 if parent_comment.creator_id != person.id && !creator_blocked {
249 let user_view = blocking(context.pool(), move |conn| {
250 LocalUserView::read_person(conn, parent_creator_id)
253 if let Ok(parent_user_view) = user_view {
254 recipient_ids.push(parent_user_view.local_user.id);
257 let lang = get_user_lang(&parent_user_view);
260 &lang.notification_post_reply_subject(&person.name),
261 &lang.notification_post_reply_body(&person.name, &comment.content, &inbox_link),
270 // Don't send a notif to yourself
272 // Only add to recipients if that person isn't blocked
273 let creator_blocked = check_person_block(person.id, post.creator_id, context.pool())
277 if post.creator_id != person.id && !creator_blocked {
278 let creator_id = post.creator_id;
279 let parent_user = blocking(context.pool(), move |conn| {
280 LocalUserView::read_person(conn, creator_id)
283 if let Ok(parent_user_view) = parent_user {
284 recipient_ids.push(parent_user_view.local_user.id);
287 let lang = get_user_lang(&parent_user_view);
290 &lang.notification_post_reply_subject(&person.name),
291 &lang.notification_post_reply_body(&person.name, &comment.content, &inbox_link),