]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Major refactor, adding newtypes for apub crate
[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   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::activities::{
12   comment::create_or_update::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 #[async_trait::async_trait(?Send)]
30 impl PerformCrud for EditComment {
31   type Response = CommentResponse;
32
33   async fn perform(
34     &self,
35     context: &Data<LemmyContext>,
36     websocket_id: Option<ConnectionId>,
37   ) -> Result<CommentResponse, LemmyError> {
38     let data: &EditComment = self;
39     let local_user_view =
40       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
41
42     let comment_id = data.comment_id;
43     let orig_comment = blocking(context.pool(), move |conn| {
44       CommentView::read(conn, comment_id, None)
45     })
46     .await??;
47
48     // TODO is this necessary? It should really only need to check on create
49     check_community_ban(
50       local_user_view.person.id,
51       orig_comment.community.id,
52       context.pool(),
53     )
54     .await?;
55     check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
56     check_post_deleted_or_removed(&orig_comment.post)?;
57
58     // Verify that only the creator can edit
59     if local_user_view.person.id != orig_comment.creator.id {
60       return Err(ApiError::err_plain("no_comment_edit_allowed").into());
61     }
62
63     // Do the update
64     let content_slurs_removed =
65       remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
66     let comment_id = data.comment_id;
67     let updated_comment = blocking(context.pool(), move |conn| {
68       Comment::update_content(conn, comment_id, &content_slurs_removed)
69     })
70     .await?
71     .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
72
73     // Send the apub update
74     CreateOrUpdateComment::send(
75       &updated_comment.clone().into(),
76       &local_user_view.person.clone().into(),
77       CreateOrUpdateType::Update,
78       context,
79     )
80     .await?;
81
82     // Do the mentions / recipients
83     let updated_comment_content = updated_comment.content.to_owned();
84     let mentions = scrape_text_for_mentions(&updated_comment_content);
85     let recipient_ids = send_local_notifs(
86       mentions,
87       &updated_comment,
88       &local_user_view.person,
89       &orig_comment.post,
90       false,
91       context,
92     )
93     .await?;
94
95     send_comment_ws_message(
96       data.comment_id,
97       UserOperationCrud::EditComment,
98       websocket_id,
99       data.form_id.to_owned(),
100       None,
101       recipient_ids,
102       context,
103     )
104     .await
105   }
106 }