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},
7 send_activity::{ActivityChannel, SendActivityData},
10 local_site_to_slur_regex,
11 local_user_view_from_jwt,
15 use lemmy_db_schema::{
17 actor_language::CommunityLanguage,
18 comment::{Comment, CommentUpdateForm},
19 local_site::LocalSite,
24 use lemmy_db_views::structs::CommentView;
26 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
28 mention::scrape_text_for_mentions,
30 validation::is_valid_body_field,
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?;
42 let comment_id = data.comment_id;
43 let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
46 local_user_view.person.id,
47 orig_comment.community.id,
52 // Verify that only the creator can edit
53 if local_user_view.person.id != orig_comment.creator.id {
54 return Err(LemmyErrorType::NoCommentEditAllowed)?;
57 let language_id = data.language_id;
58 CommunityLanguage::is_allowed_community_language(
61 orig_comment.community.id,
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);
73 let comment_id = data.comment_id;
74 let form = CommentUpdateForm {
76 language_id: data.language_id,
77 updated: Some(Some(naive_now())),
80 let updated_comment = Comment::update(&mut context.pool(), comment_id, &form)
82 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
84 // Do the mentions / recipients
85 let updated_comment_content = updated_comment.content.clone();
86 let mentions = scrape_text_for_mentions(&updated_comment_content);
87 let recipient_ids = send_local_notifs(
90 &local_user_view.person,
97 ActivityChannel::submit_activity(
98 SendActivityData::UpdateComment(updated_comment.clone()),
104 build_comment_response(
107 Some(local_user_view),