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},
18 person_mention::{PersonMention, PersonMentionForm},
21 traits::{Crud, DeleteableOrRemoveable},
24 comment_view::CommentView,
25 local_user_view::LocalUserView,
27 private_message_view::PrivateMessageView,
29 use lemmy_db_views_actor::community_view::CommunityView;
32 settings::structs::Settings,
39 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
42 websocket_id: Option<ConnectionId>,
43 person_id: Option<PersonId>,
44 context: &LemmyContext,
45 ) -> Result<PostResponse, LemmyError> {
46 let mut post_view = blocking(context.pool(), move |conn| {
47 PostView::read(conn, post_id, person_id)
51 if post_view.post.deleted || post_view.post.removed {
52 post_view.post = post_view.post.blank_out_deleted_or_removed_info();
55 let res = PostResponse { post_view };
57 context.chat_server().do_send(SendPost {
66 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
67 // we should get the actual recipient actors from somewhere
68 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
69 comment_id: CommentId,
71 context: &LemmyContext,
72 ) -> Result<CommentResponse, LemmyError> {
73 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
76 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
77 comment_id: CommentId,
79 websocket_id: Option<ConnectionId>,
80 form_id: Option<String>,
81 person_id: Option<PersonId>,
82 recipient_ids: Vec<LocalUserId>,
83 context: &LemmyContext,
84 ) -> Result<CommentResponse, LemmyError> {
85 let mut view = blocking(context.pool(), move |conn| {
86 CommentView::read(conn, comment_id, person_id)
90 if view.comment.deleted || view.comment.removed {
91 view.comment = view.comment.blank_out_deleted_or_removed_info();
94 let mut res = CommentResponse {
97 // The sent out form id should be null
101 context.chat_server().do_send(SendComment {
103 comment: res.clone(),
107 // The recipient_ids should be empty for returns
108 res.recipient_ids = Vec::new();
109 res.form_id = form_id;
114 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
115 community_id: CommunityId,
117 websocket_id: Option<ConnectionId>,
118 person_id: Option<PersonId>,
119 context: &LemmyContext,
120 ) -> Result<CommunityResponse, LemmyError> {
121 let mut community_view = blocking(context.pool(), move |conn| {
122 CommunityView::read(conn, community_id, person_id)
125 // Blank out deleted or removed info
126 if community_view.community.deleted || community_view.community.removed {
127 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
130 let res = CommunityResponse { community_view };
132 // Strip out the person id and subscribed when sending to others
133 let mut res_mut = res.clone();
134 res_mut.community_view.subscribed = false;
136 context.chat_server().do_send(SendCommunityRoomMessage {
139 community_id: res.community_view.community.id,
146 pub async fn send_pm_ws_message<OP: ToString + Send + OperationType + 'static>(
147 private_message_id: PrivateMessageId,
149 websocket_id: Option<ConnectionId>,
150 context: &LemmyContext,
151 ) -> Result<PrivateMessageResponse, LemmyError> {
152 let mut view = blocking(context.pool(), move |conn| {
153 PrivateMessageView::read(conn, private_message_id)
157 // Blank out deleted or removed info
158 if view.private_message.deleted {
159 view.private_message = view.private_message.blank_out_deleted_or_removed_info();
162 let res = PrivateMessageResponse {
163 private_message_view: view,
166 // Send notifications to the local recipient, if one exists
167 if res.private_message_view.recipient.local {
168 let recipient_id = res.private_message_view.recipient.id;
169 let local_recipient = blocking(context.pool(), move |conn| {
170 LocalUserView::read_person(conn, recipient_id)
173 context.chat_server().do_send(SendUserRoomMessage {
175 response: res.clone(),
176 local_recipient_id: local_recipient.local_user.id,
184 pub async fn send_local_notifs(
185 mentions: Vec<MentionData>,
190 context: &LemmyContext,
191 ) -> Result<Vec<LocalUserId>, LemmyError> {
192 let mut recipient_ids = Vec::new();
194 // Send the local mentions
195 for mention in mentions
197 .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
198 .collect::<Vec<&MentionData>>()
200 let mention_name = mention.name.clone();
201 let user_view = blocking(context.pool(), move |conn| {
202 LocalUserView::read_from_name(conn, &mention_name)
205 if let Ok(mention_user_view) = user_view {
207 // At some point, make it so you can't tag the parent creator either
208 // This can cause two notifications, one for reply and the other for mention
209 recipient_ids.push(mention_user_view.local_user.id);
211 let user_mention_form = PersonMentionForm {
212 recipient_id: mention_user_view.person.id,
213 comment_id: comment.id,
217 // Allow this to fail softly, since comment edits might re-update or replace it
218 // Let the uniqueness handle this fail
219 blocking(context.pool(), move |conn| {
220 PersonMention::create(conn, &user_mention_form)
225 // Send an email to those local users that have notifications on
238 // Send notifs to the parent commenter / poster
239 match comment.parent_id {
242 blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await?;
243 if let Ok(parent_comment) = parent_comment {
244 // Don't send a notif to yourself
245 if parent_comment.creator_id != person.id {
246 // Get the parent commenter local_user
247 let user_view = blocking(context.pool(), move |conn| {
248 LocalUserView::read_person(conn, parent_comment.creator_id)
251 if let Ok(parent_user_view) = user_view {
252 recipient_ids.push(parent_user_view.local_user.id);
269 if post.creator_id != person.id {
270 let creator_id = post.creator_id;
271 let parent_user = blocking(context.pool(), move |conn| {
272 LocalUserView::read_person(conn, creator_id)
275 if let Ok(parent_user_view) = parent_user {
276 recipient_ids.push(parent_user_view.local_user.id);
294 pub fn send_email_to_user(
295 local_user_view: &LocalUserView,
298 comment_content: &str,
301 if local_user_view.person.banned || !local_user_view.local_user.send_notifications_to_email {
305 if let Some(user_email) = &local_user_view.local_user.email {
306 let subject = &format!(
308 subject_text, settings.hostname, local_user_view.person.name,
311 "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
313 local_user_view.person.name,
315 settings.get_protocol_and_hostname()
320 &local_user_view.person.name,
325 Err(e) => error!("{}", e),