]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
0129e87c2e50ea99183924e815cb3c3a22aa273d
[lemmy.git] / crates / api_crud / src / comment / update.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   build_response::{build_comment_response, send_local_notifs},
5   comment::{CommentResponse, EditComment},
6   context::LemmyContext,
7   utils::{check_community_ban, local_site_to_slur_regex, local_user_view_from_jwt},
8 };
9 use lemmy_db_schema::{
10   source::{
11     actor_language::CommunityLanguage,
12     comment::{Comment, CommentUpdateForm},
13     local_site::LocalSite,
14   },
15   traits::Crud,
16   utils::naive_now,
17 };
18 use lemmy_db_views::structs::CommentView;
19 use lemmy_utils::{
20   error::{LemmyError, LemmyErrorExt, LemmyErrorType},
21   utils::{
22     mention::scrape_text_for_mentions,
23     slurs::remove_slurs,
24     validation::is_valid_body_field,
25   },
26 };
27
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for EditComment {
30   type Response = CommentResponse;
31
32   #[tracing::instrument(skip(context))]
33   async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
34     let data: &EditComment = self;
35     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
36     let local_site = LocalSite::read(&mut context.pool()).await?;
37
38     let comment_id = data.comment_id;
39     let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
40
41     check_community_ban(
42       local_user_view.person.id,
43       orig_comment.community.id,
44       &mut context.pool(),
45     )
46     .await?;
47
48     // Verify that only the creator can edit
49     if local_user_view.person.id != orig_comment.creator.id {
50       return Err(LemmyErrorType::NoCommentEditAllowed)?;
51     }
52
53     let language_id = self.language_id;
54     CommunityLanguage::is_allowed_community_language(
55       &mut context.pool(),
56       language_id,
57       orig_comment.community.id,
58     )
59     .await?;
60
61     // Update the Content
62     let content_slurs_removed = data
63       .content
64       .as_ref()
65       .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
66
67     is_valid_body_field(&content_slurs_removed, false)?;
68
69     let comment_id = data.comment_id;
70     let form = CommentUpdateForm::builder()
71       .content(content_slurs_removed)
72       .language_id(data.language_id)
73       .updated(Some(Some(naive_now())))
74       .build();
75     let updated_comment = Comment::update(&mut context.pool(), comment_id, &form)
76       .await
77       .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
78
79     // Do the mentions / recipients
80     let updated_comment_content = updated_comment.content.clone();
81     let mentions = scrape_text_for_mentions(&updated_comment_content);
82     let recipient_ids = send_local_notifs(
83       mentions,
84       &updated_comment,
85       &local_user_view.person,
86       &orig_comment.post,
87       false,
88       context,
89     )
90     .await?;
91
92     build_comment_response(
93       context,
94       updated_comment.id,
95       Some(local_user_view),
96       self.form_id.clone(),
97       recipient_ids,
98     )
99     .await
100   }
101 }