]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Change to_apub and from_apub to take by value and avoid cloning
[lemmy.git] / crates / api_crud / src / comment / update.rs
1 use actix_web::web::Data;
2
3 use lemmy_api_common::{
4   blocking,
5   check_community_ban,
6   check_community_deleted_or_removed,
7   check_post_deleted_or_removed,
8   comment::*,
9   get_local_user_view_from_jwt,
10 };
11 use lemmy_apub::protocol::activities::{
12   create_or_update::comment::CreateOrUpdateComment,
13   CreateOrUpdateType,
14 };
15 use lemmy_db_schema::source::comment::Comment;
16 use lemmy_db_views::comment_view::CommentView;
17 use lemmy_utils::{
18   utils::{remove_slurs, scrape_text_for_mentions},
19   ApiError,
20   ConnectionId,
21   LemmyError,
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   async fn perform(
36     &self,
37     context: &Data<LemmyContext>,
38     websocket_id: Option<ConnectionId>,
39   ) -> Result<CommentResponse, LemmyError> {
40     let data: &EditComment = self;
41     let local_user_view =
42       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
43
44     let comment_id = data.comment_id;
45     let orig_comment = blocking(context.pool(), move |conn| {
46       CommentView::read(conn, comment_id, None)
47     })
48     .await??;
49
50     // TODO is this necessary? It should really only need to check on create
51     check_community_ban(
52       local_user_view.person.id,
53       orig_comment.community.id,
54       context.pool(),
55     )
56     .await?;
57     check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
58     check_post_deleted_or_removed(&orig_comment.post)?;
59
60     // Verify that only the creator can edit
61     if local_user_view.person.id != orig_comment.creator.id {
62       return Err(ApiError::err_plain("no_comment_edit_allowed").into());
63     }
64
65     // Do the update
66     let content_slurs_removed =
67       remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
68     let comment_id = data.comment_id;
69     let updated_comment = blocking(context.pool(), move |conn| {
70       Comment::update_content(conn, comment_id, &content_slurs_removed)
71     })
72     .await?
73     .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
74
75     // Do the mentions / recipients
76     let updated_comment_content = updated_comment.content.to_owned();
77     let mentions = scrape_text_for_mentions(&updated_comment_content);
78     let recipient_ids = send_local_notifs(
79       mentions,
80       &updated_comment,
81       &local_user_view.person,
82       &orig_comment.post,
83       false,
84       context,
85     )
86     .await?;
87
88     // Send the apub update
89     CreateOrUpdateComment::send(
90       updated_comment.into(),
91       &local_user_view.person.into(),
92       CreateOrUpdateType::Update,
93       context,
94     )
95     .await?;
96
97     send_comment_ws_message(
98       data.comment_id,
99       UserOperationCrud::EditComment,
100       websocket_id,
101       data.form_id.to_owned(),
102       None,
103       recipient_ids,
104       context,
105     )
106     .await
107   }
108 }