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 post_view = blocking(context.pool(), move |conn| {
47 PostView::read(conn, post_id, person_id)
51 let res = PostResponse { post_view };
53 context.chat_server().do_send(SendPost {
62 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
63 // we should get the actual recipient actors from somewhere
64 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
65 comment_id: CommentId,
67 context: &LemmyContext,
68 ) -> Result<CommentResponse, LemmyError> {
69 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
72 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
73 comment_id: CommentId,
75 websocket_id: Option<ConnectionId>,
76 form_id: Option<String>,
77 person_id: Option<PersonId>,
78 recipient_ids: Vec<LocalUserId>,
79 context: &LemmyContext,
80 ) -> Result<CommentResponse, LemmyError> {
81 let mut view = blocking(context.pool(), move |conn| {
82 CommentView::read(conn, comment_id, person_id)
86 if view.comment.deleted || view.comment.removed {
87 view.comment = view.comment.blank_out_deleted_or_removed_info();
90 let mut res = CommentResponse {
93 // The sent out form id should be null
97 context.chat_server().do_send(SendComment {
103 // The recipient_ids should be empty for returns
104 res.recipient_ids = Vec::new();
105 res.form_id = form_id;
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 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 pub async fn send_local_notifs(
177 mentions: Vec<MentionData>,
182 context: &LemmyContext,
183 ) -> Result<Vec<LocalUserId>, LemmyError> {
184 let mut recipient_ids = Vec::new();
186 // Send the local mentions
187 for mention in mentions
189 .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
190 .collect::<Vec<&MentionData>>()
192 let mention_name = mention.name.clone();
193 let user_view = blocking(context.pool(), move |conn| {
194 LocalUserView::read_from_name(conn, &mention_name)
197 if let Ok(mention_user_view) = user_view {
199 // At some point, make it so you can't tag the parent creator either
200 // This can cause two notifications, one for reply and the other for mention
201 recipient_ids.push(mention_user_view.local_user.id);
203 let user_mention_form = PersonMentionForm {
204 recipient_id: mention_user_view.person.id,
205 comment_id: comment.id,
209 // Allow this to fail softly, since comment edits might re-update or replace it
210 // Let the uniqueness handle this fail
211 blocking(context.pool(), move |conn| {
212 PersonMention::create(conn, &user_mention_form)
217 // Send an email to those local users that have notifications on
230 // Send notifs to the parent commenter / poster
231 match comment.parent_id {
234 blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await?;
235 if let Ok(parent_comment) = parent_comment {
236 // Don't send a notif to yourself
237 if parent_comment.creator_id != person.id {
238 // Get the parent commenter local_user
239 let user_view = blocking(context.pool(), move |conn| {
240 LocalUserView::read_person(conn, parent_comment.creator_id)
243 if let Ok(parent_user_view) = user_view {
244 recipient_ids.push(parent_user_view.local_user.id);
261 if post.creator_id != person.id {
262 let creator_id = post.creator_id;
263 let parent_user = blocking(context.pool(), move |conn| {
264 LocalUserView::read_person(conn, creator_id)
267 if let Ok(parent_user_view) = parent_user {
268 recipient_ids.push(parent_user_view.local_user.id);
286 pub fn send_email_to_user(
287 local_user_view: &LocalUserView,
290 comment_content: &str,
293 if local_user_view.person.banned || !local_user_view.local_user.send_notifications_to_email {
297 if let Some(user_email) = &local_user_view.local_user.email {
298 let subject = &format!(
300 subject_text, settings.hostname, local_user_view.person.name,
303 "<h1>{}</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
305 local_user_view.person.name,
307 settings.get_protocol_and_hostname()
312 &local_user_view.person.name,
317 Err(e) => error!("{}", e),