]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Merge logic for comment create and update
[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::comment::create_or_update::{
11   CreateOrUpdateComment,
12   CreateOrUpdateType,
13 };
14 use lemmy_db_queries::{source::comment::Comment_, DeleteableOrRemoveable};
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::{messages::SendComment, 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     check_community_ban(
44       local_user_view.person.id,
45       orig_comment.community.id,
46       context.pool(),
47     )
48     .await?;
49
50     // Verify that only the creator can edit
51     if local_user_view.person.id != orig_comment.creator.id {
52       return Err(ApiError::err("no_comment_edit_allowed").into());
53     }
54
55     // Do the update
56     let content_slurs_removed = remove_slurs(&data.content.to_owned());
57     let comment_id = data.comment_id;
58     let updated_comment = blocking(context.pool(), move |conn| {
59       Comment::update_content(conn, comment_id, &content_slurs_removed)
60     })
61     .await?
62     .map_err(|_| ApiError::err("couldnt_update_comment"))?;
63
64     CreateOrUpdateComment::send(
65       &updated_comment,
66       &local_user_view.person,
67       CreateOrUpdateType::Update,
68       context,
69     )
70     .await?;
71
72     // Do the mentions / recipients
73     let updated_comment_content = updated_comment.content.to_owned();
74     let mentions = scrape_text_for_mentions(&updated_comment_content);
75     let recipient_ids = send_local_notifs(
76       mentions,
77       updated_comment,
78       local_user_view.person.clone(),
79       orig_comment.post,
80       context.pool(),
81       false,
82     )
83     .await?;
84
85     let comment_id = data.comment_id;
86     let person_id = local_user_view.person.id;
87     let mut comment_view = blocking(context.pool(), move |conn| {
88       CommentView::read(conn, comment_id, Some(person_id))
89     })
90     .await??;
91
92     // Blank out deleted or removed info
93     if comment_view.comment.deleted || comment_view.comment.removed {
94       comment_view.comment = comment_view.comment.blank_out_deleted_or_removed_info();
95     }
96
97     let res = CommentResponse {
98       comment_view,
99       recipient_ids,
100       form_id: data.form_id.to_owned(),
101     };
102
103     context.chat_server().do_send(SendComment {
104       op: UserOperationCrud::EditComment,
105       comment: res.clone(),
106       websocket_id,
107     });
108
109     Ok(res)
110   }
111 }