]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Use audience field to federate items in groups (fixes #2464) (#2584)
[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     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::note::CreateOrUpdateNote,
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 #[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     let local_site = LocalSite::read(context.pool()).await?;
52
53     let comment_id = data.comment_id;
54     let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
55
56     // TODO is this necessary? It should really only need to check on create
57     check_community_ban(
58       local_user_view.person.id,
59       orig_comment.community.id,
60       context.pool(),
61     )
62     .await?;
63     check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
64     check_post_deleted_or_removed(&orig_comment.post)?;
65
66     // Verify that only the creator can edit
67     if local_user_view.person.id != orig_comment.creator.id {
68       return Err(LemmyError::from_message("no_comment_edit_allowed"));
69     }
70
71     if data.distinguished.is_some() {
72       // Verify that only a mod or admin can distinguish a comment
73       is_mod_or_admin(
74         context.pool(),
75         local_user_view.person.id,
76         orig_comment.community.id,
77       )
78       .await?;
79     }
80
81     let language_id = self.language_id;
82     CommunityLanguage::is_allowed_community_language(
83       context.pool(),
84       language_id,
85       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, &local_site_to_slur_regex(&local_site)));
94     let comment_id = data.comment_id;
95     let form = CommentUpdateForm::builder()
96       .content(content_slurs_removed)
97       .distinguished(data.distinguished)
98       .language_id(data.language_id)
99       .build();
100     let updated_comment = Comment::update(context.pool(), comment_id, &form)
101       .await
102       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
103
104     // Do the mentions / recipients
105     let updated_comment_content = updated_comment.content.clone();
106     let mentions = scrape_text_for_mentions(&updated_comment_content);
107     let recipient_ids = send_local_notifs(
108       mentions,
109       &updated_comment,
110       &local_user_view.person,
111       &orig_comment.post,
112       false,
113       context,
114     )
115     .await?;
116
117     // Send the apub update
118     CreateOrUpdateNote::send(
119       updated_comment.into(),
120       &local_user_view.person.into(),
121       CreateOrUpdateType::Update,
122       context,
123       &mut 0,
124     )
125     .await?;
126
127     send_comment_ws_message(
128       data.comment_id,
129       UserOperationCrud::EditComment,
130       websocket_id,
131       data.form_id.clone(),
132       None,
133       recipient_ids,
134       context,
135     )
136     .await
137   }
138 }