2 messages::{SendComment, SendCommunityRoomMessage, SendPost, SendUserRoomMessage},
6 use lemmy_api_common::{
9 comment::CommentResponse,
10 community::CommunityResponse,
11 person::PrivateMessageResponse,
14 use lemmy_db_schema::{
15 newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
19 person_mention::{PersonMention, PersonMentionForm},
22 traits::{Crud, DeleteableOrRemoveable},
25 comment_view::CommentView,
26 local_user_view::LocalUserView,
28 private_message_view::PrivateMessageView,
30 use lemmy_db_views_actor::community_view::CommunityView;
33 settings::structs::Settings,
40 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
43 websocket_id: Option<ConnectionId>,
44 person_id: Option<PersonId>,
45 context: &LemmyContext,
46 ) -> Result<PostResponse, LemmyError> {
47 let post_view = blocking(context.pool(), move |conn| {
48 PostView::read(conn, post_id, person_id)
52 let res = PostResponse { post_view };
54 context.chat_server().do_send(SendPost {
63 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
64 // we should get the actual recipient actors from somewhere
65 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
66 comment_id: CommentId,
68 context: &LemmyContext,
69 ) -> Result<CommentResponse, LemmyError> {
70 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
73 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
74 comment_id: CommentId,
76 websocket_id: Option<ConnectionId>,
77 form_id: Option<String>,
78 person_id: Option<PersonId>,
79 recipient_ids: Vec<LocalUserId>,
80 context: &LemmyContext,
81 ) -> Result<CommentResponse, LemmyError> {
82 let mut view = blocking(context.pool(), move |conn| {
83 CommentView::read(conn, comment_id, person_id)
87 if view.comment.deleted || view.comment.removed {
88 view.comment = view.comment.blank_out_deleted_or_removed_info();
91 let mut res = CommentResponse {
94 // The sent out form id should be null
98 context.chat_server().do_send(SendComment {
100 comment: res.clone(),
104 // The recipient_ids should be empty for returns
105 res.recipient_ids = Vec::new();
106 res.form_id = form_id;
111 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
112 community_id: CommunityId,
114 websocket_id: Option<ConnectionId>,
115 person_id: Option<PersonId>,
116 context: &LemmyContext,
117 ) -> Result<CommunityResponse, LemmyError> {
118 let community_view = blocking(context.pool(), move |conn| {
119 CommunityView::read(conn, community_id, person_id)
123 let res = CommunityResponse { community_view };
125 // Strip out the person id and subscribed when sending to others
126 let mut res_mut = res.clone();
127 res_mut.community_view.subscribed = false;
129 context.chat_server().do_send(SendCommunityRoomMessage {
132 community_id: res.community_view.community.id,
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 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);
300 pub fn send_email_to_user(
301 local_user_view: &LocalUserView,
304 comment_content: &str,
307 if local_user_view.person.banned || !local_user_view.local_user.send_notifications_to_email {
311 if let Some(user_email) = &local_user_view.local_user.email {
312 let subject = &format!(
314 subject_text, settings.hostname, local_user_view.person.name,
317 "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
319 local_user_view.person.name,
321 settings.get_protocol_and_hostname()
326 &local_user_view.person.name,
331 Err(e) => error!("{}", e),