]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Don't drop error context when adding a message to errors (#1958)
[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   ConnectionId,
20   LemmyError,
21 };
22 use lemmy_websocket::{
23   send::{send_comment_ws_message, send_local_notifs},
24   LemmyContext,
25   UserOperationCrud,
26 };
27
28 use crate::PerformCrud;
29
30 #[async_trait::async_trait(?Send)]
31 impl PerformCrud for EditComment {
32   type Response = CommentResponse;
33
34   #[tracing::instrument(skip(context, websocket_id))]
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(LemmyError::from_message("no_comment_edit_allowed"));
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(LemmyError::from)
74     .map_err(|e| e.with_message("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 }