]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
f03ad5f50b5d6d3a3ca19a376f6bd9294665ffd5
[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   },
12 };
13 use lemmy_apub::protocol::activities::{
14   create_or_update::comment::CreateOrUpdateComment,
15   CreateOrUpdateType,
16 };
17 use lemmy_db_schema::{
18   source::{
19     actor_language::CommunityLanguage,
20     comment::{Comment, CommentForm},
21   },
22   traits::Crud,
23 };
24 use lemmy_db_views::structs::CommentView;
25 use lemmy_utils::{
26   error::LemmyError,
27   utils::{remove_slurs, scrape_text_for_mentions},
28   ConnectionId,
29 };
30 use lemmy_websocket::{
31   send::{send_comment_ws_message, send_local_notifs},
32   LemmyContext,
33   UserOperationCrud,
34 };
35
36 use crate::PerformCrud;
37
38 #[async_trait::async_trait(?Send)]
39 impl PerformCrud for EditComment {
40   type Response = CommentResponse;
41
42   #[tracing::instrument(skip(context, websocket_id))]
43   async fn perform(
44     &self,
45     context: &Data<LemmyContext>,
46     websocket_id: Option<ConnectionId>,
47   ) -> Result<CommentResponse, LemmyError> {
48     let data: &EditComment = self;
49     let local_user_view =
50       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
51
52     let comment_id = data.comment_id;
53     let orig_comment = blocking(context.pool(), move |conn| {
54       CommentView::read(conn, comment_id, None)
55     })
56     .await??;
57
58     // TODO is this necessary? It should really only need to check on create
59     check_community_ban(
60       local_user_view.person.id,
61       orig_comment.community.id,
62       context.pool(),
63     )
64     .await?;
65     check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
66     check_post_deleted_or_removed(&orig_comment.post)?;
67
68     // Verify that only the creator can edit
69     if local_user_view.person.id != orig_comment.creator.id {
70       return Err(LemmyError::from_message("no_comment_edit_allowed"));
71     }
72
73     if data.distinguished.is_some() {
74       // Verify that only a mod or admin can distinguish a comment
75       is_mod_or_admin(
76         context.pool(),
77         local_user_view.person.id,
78         orig_comment.community.id,
79       )
80       .await?;
81     }
82
83     let language_id = self.language_id;
84     blocking(context.pool(), move |conn| {
85       CommunityLanguage::is_allowed_community_language(conn, language_id, orig_comment.community.id)
86     })
87     .await??;
88
89     // Update the Content
90     let content_slurs_removed = data
91       .content
92       .as_ref()
93       .map(|c| remove_slurs(c, &context.settings().slur_regex()));
94     let comment_id = data.comment_id;
95     let form = CommentForm {
96       creator_id: orig_comment.comment.creator_id,
97       post_id: orig_comment.comment.post_id,
98       content: content_slurs_removed.unwrap_or(orig_comment.comment.content),
99       distinguished: data.distinguished,
100       language_id: data.language_id,
101       ..Default::default()
102     };
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 }