]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Adding distinguish comment. Fixes #2002 (#2391)
[lemmy.git] / crates / api_crud / src / comment / update.rs
1 use actix_web::web::Data;
2 use lemmy_api_common::{
3   comment::{CommentResponse, EditComment},
4   utils::{
5     blocking,
6     check_community_ban,
7     check_community_deleted_or_removed,
8     check_post_deleted_or_removed,
9     get_local_user_view_from_jwt,
10     is_mod_or_admin,
11   },
12 };
13 use lemmy_apub::protocol::activities::{
14   create_or_update::comment::CreateOrUpdateComment,
15   CreateOrUpdateType,
16 };
17 use lemmy_db_schema::source::comment::Comment;
18 use lemmy_db_views::structs::CommentView;
19 use lemmy_utils::{
20   error::LemmyError,
21   utils::{remove_slurs, scrape_text_for_mentions},
22   ConnectionId,
23 };
24 use lemmy_websocket::{
25   send::{send_comment_ws_message, send_local_notifs},
26   LemmyContext,
27   UserOperationCrud,
28 };
29
30 use crate::PerformCrud;
31
32 #[async_trait::async_trait(?Send)]
33 impl PerformCrud for EditComment {
34   type Response = CommentResponse;
35
36   #[tracing::instrument(skip(context, websocket_id))]
37   async fn perform(
38     &self,
39     context: &Data<LemmyContext>,
40     websocket_id: Option<ConnectionId>,
41   ) -> Result<CommentResponse, LemmyError> {
42     let data: &EditComment = self;
43     let local_user_view =
44       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
45
46     let comment_id = data.comment_id;
47     let orig_comment = blocking(context.pool(), move |conn| {
48       CommentView::read(conn, comment_id, None)
49     })
50     .await??;
51     let mut updated_comment = orig_comment.comment.clone();
52
53     // TODO is this necessary? It should really only need to check on create
54     check_community_ban(
55       local_user_view.person.id,
56       orig_comment.community.id,
57       context.pool(),
58     )
59     .await?;
60     check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
61     check_post_deleted_or_removed(&orig_comment.post)?;
62
63     // Verify that only the creator can edit
64     if local_user_view.person.id != orig_comment.creator.id {
65       return Err(LemmyError::from_message("no_comment_edit_allowed"));
66     }
67
68     if let Some(distinguished) = data.distinguished {
69       // Verify that only a mod or admin can distinguish a comment
70       is_mod_or_admin(
71         context.pool(),
72         local_user_view.person.id,
73         orig_comment.community.id,
74       )
75       .await?;
76
77       updated_comment = blocking(context.pool(), move |conn| {
78         Comment::update_distinguished(conn, comment_id, distinguished)
79       })
80       .await?
81       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
82     }
83
84     // Update the Content
85     if let Some(content) = &data.content {
86       let content_slurs_removed = remove_slurs(content, &context.settings().slur_regex());
87       let comment_id = data.comment_id;
88       updated_comment = blocking(context.pool(), move |conn| {
89         Comment::update_content(conn, comment_id, &content_slurs_removed)
90       })
91       .await?
92       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
93     };
94
95     // Do the mentions / recipients
96     let updated_comment_content = updated_comment.content.to_owned();
97     let mentions = scrape_text_for_mentions(&updated_comment_content);
98     let recipient_ids = send_local_notifs(
99       mentions,
100       &updated_comment,
101       &local_user_view.person,
102       &orig_comment.post,
103       false,
104       context,
105     )
106     .await?;
107
108     // Send the apub update
109     CreateOrUpdateComment::send(
110       updated_comment.into(),
111       &local_user_view.person.into(),
112       CreateOrUpdateType::Update,
113       context,
114       &mut 0,
115     )
116     .await?;
117
118     send_comment_ws_message(
119       data.comment_id,
120       UserOperationCrud::EditComment,
121       websocket_id,
122       data.form_id.to_owned(),
123       None,
124       recipient_ids,
125       context,
126     )
127     .await
128   }
129 }