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