]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
06536330f30146898f6212cc67f59631e8e52ff1
[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   comment::{CommentResponse, EditComment},
5   context::LemmyContext,
6   utils::{check_community_ban, get_local_user_view_from_jwt, local_site_to_slur_regex},
7   websocket::{
8     send::{send_comment_ws_message, send_local_notifs},
9     UserOperationCrud,
10   },
11 };
12 use lemmy_db_schema::{
13   source::{
14     actor_language::CommunityLanguage,
15     comment::{Comment, CommentUpdateForm},
16     local_site::LocalSite,
17   },
18   traits::Crud,
19   utils::naive_now,
20 };
21 use lemmy_db_views::structs::CommentView;
22 use lemmy_utils::{
23   error::LemmyError,
24   utils::{mention::scrape_text_for_mentions, slurs::remove_slurs},
25   ConnectionId,
26 };
27
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for EditComment {
30   type Response = CommentResponse;
31
32   #[tracing::instrument(skip(context, websocket_id))]
33   async fn perform(
34     &self,
35     context: &Data<LemmyContext>,
36     websocket_id: Option<ConnectionId>,
37   ) -> Result<CommentResponse, LemmyError> {
38     let data: &EditComment = self;
39     let local_user_view =
40       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
41     let local_site = LocalSite::read(context.pool()).await?;
42
43     let comment_id = data.comment_id;
44     let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
45
46     check_community_ban(
47       local_user_view.person.id,
48       orig_comment.community.id,
49       context.pool(),
50     )
51     .await?;
52
53     // Verify that only the creator can edit
54     if local_user_view.person.id != orig_comment.creator.id {
55       return Err(LemmyError::from_message("no_comment_edit_allowed"));
56     }
57
58     let language_id = self.language_id;
59     CommunityLanguage::is_allowed_community_language(
60       context.pool(),
61       language_id,
62       orig_comment.community.id,
63     )
64     .await?;
65
66     // Update the Content
67     let content_slurs_removed = data
68       .content
69       .as_ref()
70       .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
71     let comment_id = data.comment_id;
72     let form = CommentUpdateForm::builder()
73       .content(content_slurs_removed)
74       .language_id(data.language_id)
75       .updated(Some(Some(naive_now())))
76       .build();
77     let updated_comment = Comment::update(context.pool(), comment_id, &form)
78       .await
79       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
80
81     // Do the mentions / recipients
82     let updated_comment_content = updated_comment.content.clone();
83     let mentions = scrape_text_for_mentions(&updated_comment_content);
84     let recipient_ids = send_local_notifs(
85       mentions,
86       &updated_comment,
87       &local_user_view.person,
88       &orig_comment.post,
89       false,
90       context,
91     )
92     .await?;
93
94     send_comment_ws_message(
95       data.comment_id,
96       UserOperationCrud::EditComment,
97       websocket_id,
98       data.form_id.clone(),
99       None,
100       recipient_ids,
101       context,
102     )
103     .await
104   }
105 }