]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
User / community blocking. Fixes #426 (#1604)
[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 = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
36
37     let comment_id = data.comment_id;
38     let orig_comment = blocking(context.pool(), move |conn| {
39       CommentView::read(conn, comment_id, None)
40     })
41     .await??;
42
43     // TODO is this necessary? It should really only need to check on create
44     check_community_ban(
45       local_user_view.person.id,
46       orig_comment.community.id,
47       context.pool(),
48     )
49     .await?;
50
51     // Verify that only the creator can edit
52     if local_user_view.person.id != orig_comment.creator.id {
53       return Err(ApiError::err("no_comment_edit_allowed").into());
54     }
55
56     // Do the update
57     let content_slurs_removed = remove_slurs(&data.content.to_owned());
58     let comment_id = data.comment_id;
59     let updated_comment = blocking(context.pool(), move |conn| {
60       Comment::update_content(conn, comment_id, &content_slurs_removed)
61     })
62     .await?
63     .map_err(|_| ApiError::err("couldnt_update_comment"))?;
64
65     // Send the apub update
66     CreateOrUpdateComment::send(
67       &updated_comment,
68       &local_user_view.person,
69       CreateOrUpdateType::Update,
70       context,
71     )
72     .await?;
73
74     // Do the mentions / recipients
75     let updated_comment_content = updated_comment.content.to_owned();
76     let mentions = scrape_text_for_mentions(&updated_comment_content);
77     let recipient_ids = send_local_notifs(
78       mentions,
79       updated_comment,
80       local_user_view.person.clone(),
81       orig_comment.post,
82       context.pool(),
83       false,
84     )
85     .await?;
86
87     send_comment_ws_message(
88       data.comment_id,
89       UserOperationCrud::EditComment,
90       websocket_id,
91       data.form_id.to_owned(),
92       None,
93       recipient_ids,
94       context,
95     )
96     .await
97   }
98 }