]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Extract Activitypub logic into separate library (#2288)
[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   },
11 };
12 use lemmy_apub::protocol::activities::{
13   create_or_update::comment::CreateOrUpdateComment,
14   CreateOrUpdateType,
15 };
16 use lemmy_db_schema::source::comment::Comment;
17 use lemmy_db_views::structs::CommentView;
18 use lemmy_utils::{
19   error::LemmyError,
20   utils::{remove_slurs, scrape_text_for_mentions},
21   ConnectionId,
22 };
23 use lemmy_websocket::{
24   send::{send_comment_ws_message, send_local_notifs},
25   LemmyContext,
26   UserOperationCrud,
27 };
28
29 use crate::PerformCrud;
30
31 #[async_trait::async_trait(?Send)]
32 impl PerformCrud for EditComment {
33   type Response = CommentResponse;
34
35   #[tracing::instrument(skip(context, websocket_id))]
36   async fn perform(
37     &self,
38     context: &Data<LemmyContext>,
39     websocket_id: Option<ConnectionId>,
40   ) -> Result<CommentResponse, LemmyError> {
41     let data: &EditComment = self;
42     let local_user_view =
43       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
44
45     let comment_id = data.comment_id;
46     let orig_comment = blocking(context.pool(), move |conn| {
47       CommentView::read(conn, comment_id, None)
48     })
49     .await??;
50
51     // TODO is this necessary? It should really only need to check on create
52     check_community_ban(
53       local_user_view.person.id,
54       orig_comment.community.id,
55       context.pool(),
56     )
57     .await?;
58     check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
59     check_post_deleted_or_removed(&orig_comment.post)?;
60
61     // Verify that only the creator can edit
62     if local_user_view.person.id != orig_comment.creator.id {
63       return Err(LemmyError::from_message("no_comment_edit_allowed"));
64     }
65
66     // Do the update
67     let content_slurs_removed =
68       remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
69     let comment_id = data.comment_id;
70     let updated_comment = blocking(context.pool(), move |conn| {
71       Comment::update_content(conn, comment_id, &content_slurs_removed)
72     })
73     .await?
74     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
75
76     // Do the mentions / recipients
77     let updated_comment_content = updated_comment.content.to_owned();
78     let mentions = scrape_text_for_mentions(&updated_comment_content);
79     let recipient_ids = send_local_notifs(
80       mentions,
81       &updated_comment,
82       &local_user_view.person,
83       &orig_comment.post,
84       false,
85       context,
86     )
87     .await?;
88
89     // Send the apub update
90     CreateOrUpdateComment::send(
91       updated_comment.into(),
92       &local_user_view.person.into(),
93       CreateOrUpdateType::Update,
94       context,
95       &mut 0,
96     )
97     .await?;
98
99     send_comment_ws_message(
100       data.comment_id,
101       UserOperationCrud::EditComment,
102       websocket_id,
103       data.form_id.to_owned(),
104       None,
105       recipient_ids,
106       context,
107     )
108     .await
109   }
110 }