]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Fixing updated form fields. Fixes #2724 (#2725)
[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::{
7     check_community_ban,
8     check_community_deleted_or_removed,
9     check_post_deleted_or_removed,
10     get_local_user_view_from_jwt,
11     is_mod_or_admin,
12     local_site_to_slur_regex,
13   },
14   websocket::{
15     send::{send_comment_ws_message, send_local_notifs},
16     UserOperationCrud,
17   },
18 };
19 use lemmy_db_schema::{
20   source::{
21     actor_language::CommunityLanguage,
22     comment::{Comment, CommentUpdateForm},
23     local_site::LocalSite,
24   },
25   traits::Crud,
26   utils::naive_now,
27 };
28 use lemmy_db_views::structs::CommentView;
29 use lemmy_utils::{
30   error::LemmyError,
31   utils::{remove_slurs, scrape_text_for_mentions},
32   ConnectionId,
33 };
34
35 #[async_trait::async_trait(?Send)]
36 impl PerformCrud for EditComment {
37   type Response = CommentResponse;
38
39   #[tracing::instrument(skip(context, websocket_id))]
40   async fn perform(
41     &self,
42     context: &Data<LemmyContext>,
43     websocket_id: Option<ConnectionId>,
44   ) -> Result<CommentResponse, LemmyError> {
45     let data: &EditComment = self;
46     let local_user_view =
47       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
48     let local_site = LocalSite::read(context.pool()).await?;
49
50     let comment_id = data.comment_id;
51     let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
52
53     // TODO is this necessary? It should really only need to check on create
54     check_community_ban(
55       local_user_view.person.id,
56       orig_comment.community.id,
57       context.pool(),
58     )
59     .await?;
60     check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
61     check_post_deleted_or_removed(&orig_comment.post)?;
62
63     // Verify that only the creator can edit
64     if local_user_view.person.id != orig_comment.creator.id {
65       return Err(LemmyError::from_message("no_comment_edit_allowed"));
66     }
67
68     if data.distinguished.is_some() {
69       // Verify that only a mod or admin can distinguish a comment
70       is_mod_or_admin(
71         context.pool(),
72         local_user_view.person.id,
73         orig_comment.community.id,
74       )
75       .await?;
76     }
77
78     let language_id = self.language_id;
79     CommunityLanguage::is_allowed_community_language(
80       context.pool(),
81       language_id,
82       orig_comment.community.id,
83     )
84     .await?;
85
86     // Update the Content
87     let content_slurs_removed = data
88       .content
89       .as_ref()
90       .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
91     let comment_id = data.comment_id;
92     let form = CommentUpdateForm::builder()
93       .content(content_slurs_removed)
94       .distinguished(data.distinguished)
95       .language_id(data.language_id)
96       .updated(Some(Some(naive_now())))
97       .build();
98     let updated_comment = Comment::update(context.pool(), comment_id, &form)
99       .await
100       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
101
102     // Do the mentions / recipients
103     let updated_comment_content = updated_comment.content.clone();
104     let mentions = scrape_text_for_mentions(&updated_comment_content);
105     let recipient_ids = send_local_notifs(
106       mentions,
107       &updated_comment,
108       &local_user_view.person,
109       &orig_comment.post,
110       false,
111       context,
112     )
113     .await?;
114
115     send_comment_ws_message(
116       data.comment_id,
117       UserOperationCrud::EditComment,
118       websocket_id,
119       data.form_id.clone(),
120       None,
121       recipient_ids,
122       context,
123     )
124     .await
125   }
126 }