]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Moving settings and secrets to context.
[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   comment::*,
7   get_local_user_view_from_jwt,
8   send_local_notifs,
9 };
10 use lemmy_apub::activities::{
11   comment::create_or_update::CreateOrUpdateComment,
12   CreateOrUpdateType,
13 };
14 use lemmy_db_queries::source::comment::Comment_;
15 use lemmy_db_schema::source::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::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
24
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for EditComment {
27   type Response = CommentResponse;
28
29   async fn perform(
30     &self,
31     context: &Data<LemmyContext>,
32     websocket_id: Option<ConnectionId>,
33   ) -> Result<CommentResponse, LemmyError> {
34     let data: &EditComment = self;
35     let local_user_view =
36       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
37
38     let comment_id = data.comment_id;
39     let orig_comment = blocking(context.pool(), move |conn| {
40       CommentView::read(conn, comment_id, None)
41     })
42     .await??;
43
44     // TODO is this necessary? It should really only need to check on create
45     check_community_ban(
46       local_user_view.person.id,
47       orig_comment.community.id,
48       context.pool(),
49     )
50     .await?;
51
52     // Verify that only the creator can edit
53     if local_user_view.person.id != orig_comment.creator.id {
54       return Err(ApiError::err("no_comment_edit_allowed").into());
55     }
56
57     // Do the update
58     let content_slurs_removed =
59       remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
60     let comment_id = data.comment_id;
61     let updated_comment = blocking(context.pool(), move |conn| {
62       Comment::update_content(conn, comment_id, &content_slurs_removed)
63     })
64     .await?
65     .map_err(|_| ApiError::err("couldnt_update_comment"))?;
66
67     // Send the apub update
68     CreateOrUpdateComment::send(
69       &updated_comment,
70       &local_user_view.person,
71       CreateOrUpdateType::Update,
72       context,
73     )
74     .await?;
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.clone(),
83       orig_comment.post,
84       context.pool(),
85       false,
86       &context.settings(),
87     )
88     .await?;
89
90     send_comment_ws_message(
91       data.comment_id,
92       UserOperationCrud::EditComment,
93       websocket_id,
94       data.form_id.to_owned(),
95       None,
96       recipient_ids,
97       context,
98     )
99     .await
100   }
101 }