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