1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, EditComment},
7 check_community_deleted_or_removed,
8 check_post_deleted_or_removed,
9 get_local_user_view_from_jwt,
11 local_site_to_slur_regex,
14 send::{send_comment_ws_message, send_local_notifs},
19 use lemmy_apub::protocol::activities::{
20 create_or_update::note::CreateOrUpdateNote,
23 use lemmy_db_schema::{
25 actor_language::CommunityLanguage,
26 comment::{Comment, CommentUpdateForm},
27 local_site::LocalSite,
31 use lemmy_db_views::structs::CommentView;
34 utils::{remove_slurs, scrape_text_for_mentions},
38 #[async_trait::async_trait(?Send)]
39 impl PerformCrud for EditComment {
40 type Response = CommentResponse;
42 #[tracing::instrument(skip(context, websocket_id))]
45 context: &Data<LemmyContext>,
46 websocket_id: Option<ConnectionId>,
47 ) -> Result<CommentResponse, LemmyError> {
48 let data: &EditComment = self;
50 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
51 let local_site = LocalSite::read(context.pool()).await?;
53 let comment_id = data.comment_id;
54 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
56 // TODO is this necessary? It should really only need to check on create
58 local_user_view.person.id,
59 orig_comment.community.id,
63 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
64 check_post_deleted_or_removed(&orig_comment.post)?;
66 // Verify that only the creator can edit
67 if local_user_view.person.id != orig_comment.creator.id {
68 return Err(LemmyError::from_message("no_comment_edit_allowed"));
71 if data.distinguished.is_some() {
72 // Verify that only a mod or admin can distinguish a comment
75 local_user_view.person.id,
76 orig_comment.community.id,
81 let language_id = self.language_id;
82 CommunityLanguage::is_allowed_community_language(
85 orig_comment.community.id,
90 let content_slurs_removed = data
93 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
94 let comment_id = data.comment_id;
95 let form = CommentUpdateForm::builder()
96 .content(content_slurs_removed)
97 .distinguished(data.distinguished)
98 .language_id(data.language_id)
100 let updated_comment = Comment::update(context.pool(), comment_id, &form)
102 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
104 // Do the mentions / recipients
105 let updated_comment_content = updated_comment.content.clone();
106 let mentions = scrape_text_for_mentions(&updated_comment_content);
107 let recipient_ids = send_local_notifs(
110 &local_user_view.person,
117 // Send the apub update
118 CreateOrUpdateNote::send(
119 updated_comment.into(),
120 &local_user_view.person.into(),
121 CreateOrUpdateType::Update,
127 send_comment_ws_message(
129 UserOperationCrud::EditComment,
131 data.form_id.clone(),