1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, EditComment},
8 check_community_deleted_or_removed,
9 check_post_deleted_or_removed,
10 get_local_user_view_from_jwt,
12 local_site_to_slur_regex,
15 use lemmy_apub::protocol::activities::{
16 create_or_update::comment::CreateOrUpdateComment,
19 use lemmy_db_schema::{
21 actor_language::CommunityLanguage,
22 comment::{Comment, CommentUpdateForm},
23 local_site::LocalSite,
27 use lemmy_db_views::structs::CommentView;
30 utils::{remove_slurs, scrape_text_for_mentions},
33 use lemmy_websocket::{
34 send::{send_comment_ws_message, send_local_notifs},
39 #[async_trait::async_trait(?Send)]
40 impl PerformCrud for EditComment {
41 type Response = CommentResponse;
43 #[tracing::instrument(skip(context, websocket_id))]
46 context: &Data<LemmyContext>,
47 websocket_id: Option<ConnectionId>,
48 ) -> Result<CommentResponse, LemmyError> {
49 let data: &EditComment = self;
51 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
52 let local_site = blocking(context.pool(), LocalSite::read).await??;
54 let comment_id = data.comment_id;
55 let orig_comment = blocking(context.pool(), move |conn| {
56 CommentView::read(conn, comment_id, None)
60 // TODO is this necessary? It should really only need to check on create
62 local_user_view.person.id,
63 orig_comment.community.id,
67 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
68 check_post_deleted_or_removed(&orig_comment.post)?;
70 // Verify that only the creator can edit
71 if local_user_view.person.id != orig_comment.creator.id {
72 return Err(LemmyError::from_message("no_comment_edit_allowed"));
75 if data.distinguished.is_some() {
76 // Verify that only a mod or admin can distinguish a comment
79 local_user_view.person.id,
80 orig_comment.community.id,
85 let language_id = self.language_id;
86 blocking(context.pool(), move |conn| {
87 CommunityLanguage::is_allowed_community_language(conn, language_id, orig_comment.community.id)
92 let content_slurs_removed = data
95 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
96 let comment_id = data.comment_id;
97 let form = CommentUpdateForm::builder()
98 .content(content_slurs_removed)
99 .distinguished(data.distinguished)
100 .language_id(data.language_id)
102 let updated_comment = blocking(context.pool(), move |conn| {
103 Comment::update(conn, comment_id, &form)
106 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
108 // Do the mentions / recipients
109 let updated_comment_content = updated_comment.content.to_owned();
110 let mentions = scrape_text_for_mentions(&updated_comment_content);
111 let recipient_ids = send_local_notifs(
114 &local_user_view.person,
121 // Send the apub update
122 CreateOrUpdateComment::send(
123 updated_comment.into(),
124 &local_user_view.person.into(),
125 CreateOrUpdateType::Update,
131 send_comment_ws_message(
133 UserOperationCrud::EditComment,
135 data.form_id.to_owned(),