]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
1d5f1c5a2745b6492039fc6cbdaece1785797350
[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::UserOperationCrud,
8 };
9 use lemmy_db_schema::{
10   source::{
11     actor_language::CommunityLanguage,
12     comment::{Comment, CommentUpdateForm},
13     local_site::LocalSite,
14   },
15   traits::Crud,
16   utils::naive_now,
17 };
18 use lemmy_db_views::structs::CommentView;
19 use lemmy_utils::{
20   error::LemmyError,
21   utils::{mention::scrape_text_for_mentions, slurs::remove_slurs},
22   ConnectionId,
23 };
24
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for EditComment {
27   type Response = CommentResponse;
28
29   #[tracing::instrument(skip(context, websocket_id))]
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     let local_site = LocalSite::read(context.pool()).await?;
39
40     let comment_id = data.comment_id;
41     let orig_comment = CommentView::read(context.pool(), comment_id, None).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(LemmyError::from_message("no_comment_edit_allowed"));
53     }
54
55     let language_id = self.language_id;
56     CommunityLanguage::is_allowed_community_language(
57       context.pool(),
58       language_id,
59       orig_comment.community.id,
60     )
61     .await?;
62
63     // Update the Content
64     let content_slurs_removed = data
65       .content
66       .as_ref()
67       .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
68     let comment_id = data.comment_id;
69     let form = CommentUpdateForm::builder()
70       .content(content_slurs_removed)
71       .language_id(data.language_id)
72       .updated(Some(Some(naive_now())))
73       .build();
74     let updated_comment = Comment::update(context.pool(), comment_id, &form)
75       .await
76       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
77
78     // Do the mentions / recipients
79     let updated_comment_content = updated_comment.content.clone();
80     let mentions = scrape_text_for_mentions(&updated_comment_content);
81     let recipient_ids = context
82       .send_local_notifs(
83         mentions,
84         &updated_comment,
85         &local_user_view.person,
86         &orig_comment.post,
87         false,
88       )
89       .await?;
90
91     context
92       .send_comment_ws_message(
93         &UserOperationCrud::EditComment,
94         data.comment_id,
95         websocket_id,
96         data.form_id.clone(),
97         None,
98         recipient_ids,
99       )
100       .await
101   }
102 }