2 comment::CommentResponse,
3 community::CommunityResponse,
6 utils::{check_person_block, get_interface_language, is_mod_or_admin, send_email_to_user},
8 use actix_web::web::Json;
10 newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId},
12 actor_language::CommunityLanguage,
14 comment_reply::{CommentReply, CommentReplyInsertForm},
16 person_mention::{PersonMention, PersonMentionInsertForm},
21 use lemmy_db_views::structs::{CommentView, LocalUserView, PostView};
22 use lemmy_db_views_actor::structs::CommunityView;
23 use lemmy_utils::{error::LemmyError, utils::mention::MentionData};
25 pub async fn build_comment_response(
26 context: &LemmyContext,
27 comment_id: CommentId,
28 local_user_view: Option<LocalUserView>,
29 form_id: Option<String>,
30 recipient_ids: Vec<LocalUserId>,
31 ) -> Result<CommentResponse, LemmyError> {
32 let person_id = local_user_view.map(|l| l.person.id);
33 let comment_view = CommentView::read(&mut context.pool(), comment_id, person_id).await?;
41 pub async fn build_community_response(
42 context: &LemmyContext,
43 local_user_view: LocalUserView,
44 community_id: CommunityId,
45 ) -> Result<Json<CommunityResponse>, LemmyError> {
47 is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id)
50 let person_id = local_user_view.person.id;
51 let community_view = CommunityView::read(
55 Some(is_mod_or_admin),
58 let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;
60 Ok(Json(CommunityResponse {
66 pub async fn build_post_response(
67 context: &LemmyContext,
68 community_id: CommunityId,
71 ) -> Result<Json<PostResponse>, LemmyError> {
72 let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), person_id, community_id)
75 let post_view = PostView::read(
79 Some(is_mod_or_admin),
82 Ok(Json(PostResponse { post_view }))
85 // TODO: this function is a mess and should be split up to handle email seperately
86 #[tracing::instrument(skip_all)]
87 pub async fn send_local_notifs(
88 mentions: Vec<MentionData>,
93 context: &LemmyContext,
94 ) -> Result<Vec<LocalUserId>, LemmyError> {
95 let mut recipient_ids = Vec::new();
96 let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
98 // Send the local mentions
99 for mention in mentions
101 .filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
103 let mention_name = mention.name.clone();
104 let user_view = LocalUserView::read_from_name(&mut context.pool(), &mention_name).await;
105 if let Ok(mention_user_view) = user_view {
107 // At some point, make it so you can't tag the parent creator either
108 // This can cause two notifications, one for reply and the other for mention
109 recipient_ids.push(mention_user_view.local_user.id);
111 let user_mention_form = PersonMentionInsertForm {
112 recipient_id: mention_user_view.person.id,
113 comment_id: comment.id,
117 // Allow this to fail softly, since comment edits might re-update or replace it
118 // Let the uniqueness handle this fail
119 PersonMention::create(&mut context.pool(), &user_mention_form)
123 // Send an email to those local users that have notifications on
125 let lang = get_interface_language(&mention_user_view);
128 &lang.notification_mentioned_by_subject(&person.name),
129 &lang.notification_mentioned_by_body(&comment.content, &inbox_link, &person.name),
137 // Send comment_reply to the parent commenter / poster
138 if let Some(parent_comment_id) = comment.parent_comment_id() {
139 let parent_comment = Comment::read(&mut context.pool(), parent_comment_id).await?;
141 // Get the parent commenter local_user
142 let parent_creator_id = parent_comment.creator_id;
144 // Only add to recipients if that person isn't blocked
145 let creator_blocked = check_person_block(person.id, parent_creator_id, &mut context.pool())
149 // Don't send a notif to yourself
150 if parent_comment.creator_id != person.id && !creator_blocked {
151 let user_view = LocalUserView::read_person(&mut context.pool(), parent_creator_id).await;
152 if let Ok(parent_user_view) = user_view {
153 recipient_ids.push(parent_user_view.local_user.id);
155 let comment_reply_form = CommentReplyInsertForm {
156 recipient_id: parent_user_view.person.id,
157 comment_id: comment.id,
161 // Allow this to fail softly, since comment edits might re-update or replace it
162 // Let the uniqueness handle this fail
163 CommentReply::create(&mut context.pool(), &comment_reply_form)
168 let lang = get_interface_language(&parent_user_view);
171 &lang.notification_comment_reply_subject(&person.name),
172 &lang.notification_comment_reply_body(&comment.content, &inbox_link, &person.name),
180 // If there's no parent, its the post creator
181 // Only add to recipients if that person isn't blocked
182 let creator_blocked = check_person_block(person.id, post.creator_id, &mut context.pool())
186 if post.creator_id != person.id && !creator_blocked {
187 let creator_id = post.creator_id;
188 let parent_user = LocalUserView::read_person(&mut context.pool(), creator_id).await;
189 if let Ok(parent_user_view) = parent_user {
190 recipient_ids.push(parent_user_view.local_user.id);
192 let comment_reply_form = CommentReplyInsertForm {
193 recipient_id: parent_user_view.person.id,
194 comment_id: comment.id,
198 // Allow this to fail softly, since comment edits might re-update or replace it
199 // Let the uniqueness handle this fail
200 CommentReply::create(&mut context.pool(), &comment_reply_form)
205 let lang = get_interface_language(&parent_user_view);
208 &lang.notification_post_reply_subject(&person.name),
209 &lang.notification_post_reply_body(&comment.content, &inbox_link, &person.name),