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