]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Adding check to description and body length fields. (#2805)
[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, get_local_user_view_from_jwt, local_site_to_slur_regex},
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 =
41       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
42     let local_site = LocalSite::read(context.pool()).await?;
43
44     let comment_id = data.comment_id;
45     let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
46
47     check_community_ban(
48       local_user_view.person.id,
49       orig_comment.community.id,
50       context.pool(),
51     )
52     .await?;
53
54     // Verify that only the creator can edit
55     if local_user_view.person.id != orig_comment.creator.id {
56       return Err(LemmyError::from_message("no_comment_edit_allowed"));
57     }
58
59     let language_id = self.language_id;
60     CommunityLanguage::is_allowed_community_language(
61       context.pool(),
62       language_id,
63       orig_comment.community.id,
64     )
65     .await?;
66
67     // Update the Content
68     let content_slurs_removed = data
69       .content
70       .as_ref()
71       .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
72
73     is_valid_body_field(&content_slurs_removed)?;
74
75     let comment_id = data.comment_id;
76     let form = CommentUpdateForm::builder()
77       .content(content_slurs_removed)
78       .language_id(data.language_id)
79       .updated(Some(Some(naive_now())))
80       .build();
81     let updated_comment = Comment::update(context.pool(), comment_id, &form)
82       .await
83       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
84
85     // Do the mentions / recipients
86     let updated_comment_content = updated_comment.content.clone();
87     let mentions = scrape_text_for_mentions(&updated_comment_content);
88     let recipient_ids = context
89       .send_local_notifs(
90         mentions,
91         &updated_comment,
92         &local_user_view.person,
93         &orig_comment.post,
94         false,
95       )
96       .await?;
97
98     context
99       .send_comment_ws_message(
100         &UserOperationCrud::EditComment,
101         data.comment_id,
102         websocket_id,
103         data.form_id.clone(),
104         None,
105         recipient_ids,
106       )
107       .await
108   }
109 }