2 messages::{SendComment, SendCommunityRoomMessage, SendPost, SendUserRoomMessage},
6 use lemmy_api_common::{
9 comment::CommentResponse,
10 community::CommunityResponse,
11 person::PrivateMessageResponse,
15 use lemmy_db_schema::{
16 newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId, PrivateMessageId},
20 person_mention::{PersonMention, PersonMentionForm},
23 traits::{Crud, DeleteableOrRemoveable},
26 comment_view::CommentView,
27 local_user_view::LocalUserView,
29 private_message_view::PrivateMessageView,
31 use lemmy_db_views_actor::community_view::CommunityView;
32 use lemmy_utils::{utils::MentionData, ConnectionId, LemmyError};
34 pub async fn send_post_ws_message<OP: ToString + Send + OperationType + 'static>(
37 websocket_id: Option<ConnectionId>,
38 person_id: Option<PersonId>,
39 context: &LemmyContext,
40 ) -> Result<PostResponse, LemmyError> {
41 let post_view = blocking(context.pool(), move |conn| {
42 PostView::read(conn, post_id, person_id)
46 let res = PostResponse { post_view };
48 context.chat_server().do_send(SendPost {
57 // TODO: in many call sites in apub crate, we are setting an empty vec for recipient_ids,
58 // we should get the actual recipient actors from somewhere
59 pub async fn send_comment_ws_message_simple<OP: ToString + Send + OperationType + 'static>(
60 comment_id: CommentId,
62 context: &LemmyContext,
63 ) -> Result<CommentResponse, LemmyError> {
64 send_comment_ws_message(comment_id, op, None, None, None, vec![], context).await
67 pub async fn send_comment_ws_message<OP: ToString + Send + OperationType + 'static>(
68 comment_id: CommentId,
70 websocket_id: Option<ConnectionId>,
71 form_id: Option<String>,
72 person_id: Option<PersonId>,
73 recipient_ids: Vec<LocalUserId>,
74 context: &LemmyContext,
75 ) -> Result<CommentResponse, LemmyError> {
76 let mut view = blocking(context.pool(), move |conn| {
77 CommentView::read(conn, comment_id, person_id)
81 if view.comment.deleted || view.comment.removed {
82 view.comment = view.comment.blank_out_deleted_or_removed_info();
85 let mut res = CommentResponse {
88 // The sent out form id should be null
92 context.chat_server().do_send(SendComment {
98 // The recipient_ids should be empty for returns
99 res.recipient_ids = Vec::new();
100 res.form_id = form_id;
105 pub async fn send_community_ws_message<OP: ToString + Send + OperationType + 'static>(
106 community_id: CommunityId,
108 websocket_id: Option<ConnectionId>,
109 person_id: Option<PersonId>,
110 context: &LemmyContext,
111 ) -> Result<CommunityResponse, LemmyError> {
112 let community_view = blocking(context.pool(), move |conn| {
113 CommunityView::read(conn, community_id, person_id)
117 let res = CommunityResponse { community_view };
119 // Strip out the person id and subscribed when sending to others
120 let mut res_mut = res.clone();
121 res_mut.community_view.subscribed = false;
123 context.chat_server().do_send(SendCommunityRoomMessage {
126 community_id: res.community_view.community.id,
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 pub async fn send_local_notifs(
172 mentions: Vec<MentionData>,
177 context: &LemmyContext,
178 ) -> Result<Vec<LocalUserId>, LemmyError> {
179 let mut recipient_ids = Vec::new();
181 // Send the local mentions
182 for mention in mentions
184 .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
185 .collect::<Vec<&MentionData>>()
187 let mention_name = mention.name.clone();
188 let user_view = blocking(context.pool(), move |conn| {
189 LocalUserView::read_from_name(conn, &mention_name)
192 if let Ok(mention_user_view) = user_view {
194 // At some point, make it so you can't tag the parent creator either
195 // This can cause two notifications, one for reply and the other for mention
196 recipient_ids.push(mention_user_view.local_user.id);
198 let user_mention_form = PersonMentionForm {
199 recipient_id: mention_user_view.person.id,
200 comment_id: comment.id,
204 // Allow this to fail softly, since comment edits might re-update or replace it
205 // Let the uniqueness handle this fail
206 blocking(context.pool(), move |conn| {
207 PersonMention::create(conn, &user_mention_form)
212 // Send an email to those local users that have notifications on
225 // Send notifs to the parent commenter / poster
226 match comment.parent_id {
229 blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await?;
230 if let Ok(parent_comment) = parent_comment {
231 // Get the parent commenter local_user
232 let parent_creator_id = parent_comment.creator_id;
234 // Only add to recipients if that person isn't blocked
235 let creator_blocked = check_person_block(person.id, parent_creator_id, context.pool())
239 // Don't send a notif to yourself
240 if parent_comment.creator_id != person.id && !creator_blocked {
241 let user_view = blocking(context.pool(), move |conn| {
242 LocalUserView::read_person(conn, parent_creator_id)
245 if let Ok(parent_user_view) = user_view {
246 recipient_ids.push(parent_user_view.local_user.id);
262 // Don't send a notif to yourself
264 // Only add to recipients if that person isn't blocked
265 let creator_blocked = check_person_block(person.id, post.creator_id, context.pool())
269 if post.creator_id != person.id && !creator_blocked {
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);