2 messages::{SendComment, SendCommunityRoomMessage, SendPost, SendUserRoomMessage},
6 use lemmy_api_common::{
7 comment::CommentResponse,
8 community::CommunityResponse,
9 person::PrivateMessageResponse,
11 utils::{blocking, check_person_block, get_user_lang, send_email_to_user},
13 use lemmy_db_schema::{
14 newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
17 comment_reply::{CommentReply, CommentReplyForm},
19 person_mention::{PersonMention, PersonMentionForm},
22 traits::{Crud, DeleteableOrRemoveable},
25 use lemmy_db_views::structs::{CommentView, LocalUserView, PostView, PrivateMessageView};
26 use lemmy_db_views_actor::structs::CommunityView;
27 use lemmy_utils::{error::LemmyError, utils::MentionData, ConnectionId};
29 #[tracing::instrument(skip_all)]
30 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
33 websocket_id: Option<ConnectionId>,
34 person_id: Option<PersonId>,
35 context: &LemmyContext,
36 ) -> Result<PostResponse, LemmyError> {
37 let post_view = blocking(context.pool(), move |conn| {
38 PostView::read(conn, post_id, person_id)
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 #[tracing::instrument(skip_all)]
56 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
57 comment_id: CommentId,
59 context: &LemmyContext,
60 ) -> Result<CommentResponse, LemmyError> {
61 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
64 #[tracing::instrument(skip_all)]
65 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
66 comment_id: CommentId,
68 websocket_id: Option<ConnectionId>,
69 form_id: Option<String>,
70 person_id: Option<PersonId>,
71 recipient_ids: Vec<LocalUserId>,
72 context: &LemmyContext,
73 ) -> Result<CommentResponse, LemmyError> {
74 let mut view = blocking(context.pool(), move |conn| {
75 CommentView::read(conn, comment_id, person_id)
79 if view.comment.deleted || view.comment.removed {
80 view.comment = view.comment.blank_out_deleted_or_removed_info();
83 let mut res = CommentResponse {
86 // The sent out form id should be null
90 context.chat_server().do_send(SendComment {
96 // The recipient_ids should be empty for returns
97 res.recipient_ids = Vec::new();
98 res.form_id = form_id;
103 #[tracing::instrument(skip_all)]
104 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
105 community_id: CommunityId,
107 websocket_id: Option<ConnectionId>,
108 person_id: Option<PersonId>,
109 context: &LemmyContext,
110 ) -> Result<CommunityResponse, LemmyError> {
111 let community_view = blocking(context.pool(), move |conn| {
112 CommunityView::read(conn, community_id, person_id)
116 let res = CommunityResponse { community_view };
118 // Strip out the person id and subscribed when sending to others
119 let mut res_mut = res.clone();
120 res_mut.community_view.subscribed = SubscribedType::NotSubscribed;
122 context.chat_server().do_send(SendCommunityRoomMessage {
125 community_id: res.community_view.community.id,
132 #[tracing::instrument(skip_all)]
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,
171 #[tracing::instrument(skip_all)]
172 pub async fn send_local_notifs(
173 mentions: Vec<MentionData>,
178 context: &LemmyContext,
179 ) -> Result<Vec<LocalUserId>, LemmyError> {
180 let mut recipient_ids = Vec::new();
181 let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
183 // Send the local mentions
184 for mention in mentions
186 .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
187 .collect::<Vec<&MentionData>>()
189 let mention_name = mention.name.clone();
190 let user_view = blocking(context.pool(), move |conn| {
191 LocalUserView::read_from_name(conn, &mention_name)
194 if let Ok(mention_user_view) = user_view {
196 // At some point, make it so you can't tag the parent creator either
197 // This can cause two notifications, one for reply and the other for mention
198 recipient_ids.push(mention_user_view.local_user.id);
200 let user_mention_form = PersonMentionForm {
201 recipient_id: mention_user_view.person.id,
202 comment_id: comment.id,
206 // Allow this to fail softly, since comment edits might re-update or replace it
207 // Let the uniqueness handle this fail
208 blocking(context.pool(), move |conn| {
209 PersonMention::create(conn, &user_mention_form)
214 // Send an email to those local users that have notifications on
216 let lang = get_user_lang(&mention_user_view);
219 &lang.notification_mentioned_by_subject(&person.name),
220 &lang.notification_mentioned_by_body(&comment.content, &inbox_link, &person.name),
227 // Send comment_reply to the parent commenter / poster
228 if let Some(parent_comment_id) = comment.parent_comment_id() {
229 let parent_comment = blocking(context.pool(), move |conn| {
230 Comment::read(conn, parent_comment_id)
234 // Get the parent commenter local_user
235 let parent_creator_id = parent_comment.creator_id;
237 // Only add to recipients if that person isn't blocked
238 let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool())
242 // Don't send a notif to yourself
243 if parent_comment.creator_id != person.id && !creator_blocked {
244 let user_view = blocking(context.pool(), move |conn| {
245 LocalUserView::read_person(conn, parent_creator_id)
248 if let Ok(parent_user_view) = user_view {
249 recipient_ids.push(parent_user_view.local_user.id);
251 let comment_reply_form = CommentReplyForm {
252 recipient_id: parent_user_view.person.id,
253 comment_id: comment.id,
257 // Allow this to fail softly, since comment edits might re-update or replace it
258 // Let the uniqueness handle this fail
259 blocking(context.pool(), move |conn| {
260 CommentReply::create(conn, &comment_reply_form)
266 let lang = get_user_lang(&parent_user_view);
269 &lang.notification_comment_reply_subject(&person.name),
270 &lang.notification_comment_reply_body(&comment.content, &inbox_link, &person.name),
277 // If there's no parent, its the post creator
278 // Only add to recipients if that person isn't blocked
279 let creator_blocked = check_person_block(person.id, post.creator_id, context.pool())
283 if post.creator_id != person.id && !creator_blocked {
284 let creator_id = post.creator_id;
285 let parent_user = blocking(context.pool(), move |conn| {
286 LocalUserView::read_person(conn, creator_id)
289 if let Ok(parent_user_view) = parent_user {
290 recipient_ids.push(parent_user_view.local_user.id);
292 let comment_reply_form = CommentReplyForm {
293 recipient_id: parent_user_view.person.id,
294 comment_id: comment.id,
298 // Allow this to fail softly, since comment edits might re-update or replace it
299 // Let the uniqueness handle this fail
300 blocking(context.pool(), move |conn| {
301 CommentReply::create(conn, &comment_reply_form)
307 let lang = get_user_lang(&parent_user_view);
310 &lang.notification_post_reply_subject(&person.name),
311 &lang.notification_post_reply_body(&comment.content, &inbox_link, &person.name),