]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/update.rs
Remove form_id params (#3812)
[lemmy.git] / crates / api_crud / src / comment / update.rs
1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4   build_response::{build_comment_response, send_local_notifs},
5   comment::{CommentResponse, EditComment},
6   context::LemmyContext,
7   send_activity::{ActivityChannel, SendActivityData},
8   utils::{
9     check_community_ban,
10     local_site_to_slur_regex,
11     local_user_view_from_jwt,
12     sanitize_html_opt,
13   },
14 };
15 use lemmy_db_schema::{
16   source::{
17     actor_language::CommunityLanguage,
18     comment::{Comment, CommentUpdateForm},
19     local_site::LocalSite,
20   },
21   traits::Crud,
22   utils::naive_now,
23 };
24 use lemmy_db_views::structs::CommentView;
25 use lemmy_utils::{
26   error::{LemmyError, LemmyErrorExt, LemmyErrorType},
27   utils::{
28     mention::scrape_text_for_mentions,
29     slurs::remove_slurs,
30     validation::is_valid_body_field,
31   },
32 };
33
34 #[tracing::instrument(skip(context))]
35 pub async fn update_comment(
36   data: Json<EditComment>,
37   context: Data<LemmyContext>,
38 ) -> Result<Json<CommentResponse>, LemmyError> {
39   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
40   let local_site = LocalSite::read(&mut context.pool()).await?;
41
42   let comment_id = data.comment_id;
43   let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
44
45   check_community_ban(
46     local_user_view.person.id,
47     orig_comment.community.id,
48     &mut context.pool(),
49   )
50   .await?;
51
52   // Verify that only the creator can edit
53   if local_user_view.person.id != orig_comment.creator.id {
54     return Err(LemmyErrorType::NoCommentEditAllowed)?;
55   }
56
57   let language_id = data.language_id;
58   CommunityLanguage::is_allowed_community_language(
59     &mut context.pool(),
60     language_id,
61     orig_comment.community.id,
62   )
63   .await?;
64
65   // Update the Content
66   let content = data
67     .content
68     .as_ref()
69     .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
70   is_valid_body_field(&content, false)?;
71   let content = sanitize_html_opt(&content);
72
73   let comment_id = data.comment_id;
74   let form = CommentUpdateForm::builder()
75     .content(content)
76     .language_id(data.language_id)
77     .updated(Some(Some(naive_now())))
78     .build();
79   let updated_comment = Comment::update(&mut context.pool(), comment_id, &form)
80     .await
81     .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
82
83   // Do the mentions / recipients
84   let updated_comment_content = updated_comment.content.clone();
85   let mentions = scrape_text_for_mentions(&updated_comment_content);
86   let recipient_ids = send_local_notifs(
87     mentions,
88     &updated_comment,
89     &local_user_view.person,
90     &orig_comment.post,
91     false,
92     &context,
93   )
94   .await?;
95
96   ActivityChannel::submit_activity(
97     SendActivityData::UpdateComment(updated_comment.clone()),
98     &context,
99   )
100   .await?;
101
102   Ok(Json(
103     build_comment_response(
104       &context,
105       updated_comment.id,
106       Some(local_user_view),
107       recipient_ids,
108     )
109     .await?,
110   ))
111 }