]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
e74d5c4edd0acff3d3a4a83397c9a9616e027970
[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   comment::{CommentResponse, EditComment},
5   context::LemmyContext,
6   utils::{check_community_ban, local_site_to_slur_regex, local_user_view_from_jwt},
7   websocket::UserOperationCrud,
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,
21   utils::{
22     mention::scrape_text_for_mentions,
23     slurs::remove_slurs,
24     validation::is_valid_body_field,
25   },
26   ConnectionId,
27 };
28
29 #[async_trait::async_trait(?Send)]
30 impl PerformCrud for EditComment {
31   type Response = CommentResponse;
32
33   #[tracing::instrument(skip(context, websocket_id))]
34   async fn perform(
35     &self,
36     context: &Data<LemmyContext>,
37     websocket_id: Option<ConnectionId>,
38   ) -> Result<CommentResponse, LemmyError> {
39     let data: &EditComment = self;
40     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
41     let local_site = LocalSite::read(context.pool()).await?;
42
43     let comment_id = data.comment_id;
44     let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
45
46     check_community_ban(
47       local_user_view.person.id,
48       orig_comment.community.id,
49       context.pool(),
50     )
51     .await?;
52
53     // Verify that only the creator can edit
54     if local_user_view.person.id != orig_comment.creator.id {
55       return Err(LemmyError::from_message("no_comment_edit_allowed"));
56     }
57
58     let language_id = self.language_id;
59     CommunityLanguage::is_allowed_community_language(
60       context.pool(),
61       language_id,
62       orig_comment.community.id,
63     )
64     .await?;
65
66     // Update the Content
67     let content_slurs_removed = data
68       .content
69       .as_ref()
70       .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
71
72     is_valid_body_field(&content_slurs_removed)?;
73
74     let comment_id = data.comment_id;
75     let form = CommentUpdateForm::builder()
76       .content(content_slurs_removed)
77       .language_id(data.language_id)
78       .updated(Some(Some(naive_now())))
79       .build();
80     let updated_comment = Comment::update(context.pool(), comment_id, &form)
81       .await
82       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
83
84     // Do the mentions / recipients
85     let updated_comment_content = updated_comment.content.clone();
86     let mentions = scrape_text_for_mentions(&updated_comment_content);
87     let recipient_ids = context
88       .send_local_notifs(
89         mentions,
90         &updated_comment,
91         &local_user_view.person,
92         &orig_comment.post,
93         false,
94       )
95       .await?;
96
97     context
98       .send_comment_ws_message(
99         &UserOperationCrud::EditComment,
100         data.comment_id,
101         websocket_id,
102         data.form_id.clone(),
103         None,
104         recipient_ids,
105       )
106       .await
107   }
108 }