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