]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Moving settings to Database. (#2492)
[lemmy.git] / crates / api_crud / src / comment / update.rs
1 use actix_web::web::Data;
2 use lemmy_api_common::{
3   comment::{CommentResponse, EditComment},
4   utils::{
5     blocking,
6     check_community_ban,
7     check_community_deleted_or_removed,
8     check_post_deleted_or_removed,
9     get_local_user_view_from_jwt,
10     is_mod_or_admin,
11     local_site_to_slur_regex,
12   },
13 };
14 use lemmy_apub::protocol::activities::{
15   create_or_update::comment::CreateOrUpdateComment,
16   CreateOrUpdateType,
17 };
18 use lemmy_db_schema::{
19   source::{
20     actor_language::CommunityLanguage,
21     comment::{Comment, CommentUpdateForm},
22     local_site::LocalSite,
23   },
24   traits::Crud,
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 use lemmy_websocket::{
33   send::{send_comment_ws_message, send_local_notifs},
34   LemmyContext,
35   UserOperationCrud,
36 };
37
38 use crate::PerformCrud;
39
40 #[async_trait::async_trait(?Send)]
41 impl PerformCrud for EditComment {
42   type Response = CommentResponse;
43
44   #[tracing::instrument(skip(context, websocket_id))]
45   async fn perform(
46     &self,
47     context: &Data<LemmyContext>,
48     websocket_id: Option<ConnectionId>,
49   ) -> Result<CommentResponse, LemmyError> {
50     let data: &EditComment = self;
51     let local_user_view =
52       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
53     let local_site = blocking(context.pool(), LocalSite::read).await??;
54
55     let comment_id = data.comment_id;
56     let orig_comment = blocking(context.pool(), move |conn| {
57       CommentView::read(conn, comment_id, None)
58     })
59     .await??;
60
61     // TODO is this necessary? It should really only need to check on create
62     check_community_ban(
63       local_user_view.person.id,
64       orig_comment.community.id,
65       context.pool(),
66     )
67     .await?;
68     check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
69     check_post_deleted_or_removed(&orig_comment.post)?;
70
71     // Verify that only the creator can edit
72     if local_user_view.person.id != orig_comment.creator.id {
73       return Err(LemmyError::from_message("no_comment_edit_allowed"));
74     }
75
76     if data.distinguished.is_some() {
77       // Verify that only a mod or admin can distinguish a comment
78       is_mod_or_admin(
79         context.pool(),
80         local_user_view.person.id,
81         orig_comment.community.id,
82       )
83       .await?;
84     }
85
86     let language_id = self.language_id;
87     blocking(context.pool(), move |conn| {
88       CommunityLanguage::is_allowed_community_language(conn, language_id, orig_comment.community.id)
89     })
90     .await??;
91
92     // Update the Content
93     let content_slurs_removed = data
94       .content
95       .as_ref()
96       .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
97     let comment_id = data.comment_id;
98     let form = CommentUpdateForm::builder()
99       .content(content_slurs_removed)
100       .distinguished(data.distinguished)
101       .language_id(data.language_id)
102       .build();
103     let updated_comment = blocking(context.pool(), move |conn| {
104       Comment::update(conn, comment_id, &form)
105     })
106     .await?
107     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
108
109     // Do the mentions / recipients
110     let updated_comment_content = updated_comment.content.to_owned();
111     let mentions = scrape_text_for_mentions(&updated_comment_content);
112     let recipient_ids = send_local_notifs(
113       mentions,
114       &updated_comment,
115       &local_user_view.person,
116       &orig_comment.post,
117       false,
118       context,
119     )
120     .await?;
121
122     // Send the apub update
123     CreateOrUpdateComment::send(
124       updated_comment.into(),
125       &local_user_view.person.into(),
126       CreateOrUpdateType::Update,
127       context,
128       &mut 0,
129     )
130     .await?;
131
132     send_comment_ws_message(
133       data.comment_id,
134       UserOperationCrud::EditComment,
135       websocket_id,
136       data.form_id.to_owned(),
137       None,
138       recipient_ids,
139       context,
140     )
141     .await
142   }
143 }