1 use actix_web::web::Data;
2 use lemmy_api_common::{
3 comment::{CommentResponse, EditComment},
7 check_community_deleted_or_removed,
8 check_post_deleted_or_removed,
9 get_local_user_view_from_jwt,
13 use lemmy_apub::protocol::activities::{
14 create_or_update::comment::CreateOrUpdateComment,
17 use lemmy_db_schema::{
19 actor_language::CommunityLanguage,
20 comment::{Comment, CommentForm},
24 use lemmy_db_views::structs::CommentView;
27 utils::{remove_slurs, scrape_text_for_mentions},
30 use lemmy_websocket::{
31 send::{send_comment_ws_message, send_local_notifs},
36 use crate::PerformCrud;
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?;
52 let comment_id = data.comment_id;
53 let orig_comment = blocking(context.pool(), move |conn| {
54 CommentView::read(conn, comment_id, None)
58 // TODO is this necessary? It should really only need to check on create
60 local_user_view.person.id,
61 orig_comment.community.id,
65 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
66 check_post_deleted_or_removed(&orig_comment.post)?;
68 // Verify that only the creator can edit
69 if local_user_view.person.id != orig_comment.creator.id {
70 return Err(LemmyError::from_message("no_comment_edit_allowed"));
73 if data.distinguished.is_some() {
74 // Verify that only a mod or admin can distinguish a comment
77 local_user_view.person.id,
78 orig_comment.community.id,
83 let language_id = self.language_id;
84 blocking(context.pool(), move |conn| {
85 CommunityLanguage::is_allowed_community_language(conn, language_id, orig_comment.community.id)
90 let content_slurs_removed = data
93 .map(|c| remove_slurs(c, &context.settings().slur_regex()));
94 let comment_id = data.comment_id;
95 let form = CommentForm {
96 creator_id: orig_comment.comment.creator_id,
97 post_id: orig_comment.comment.post_id,
98 content: content_slurs_removed.unwrap_or(orig_comment.comment.content),
99 distinguished: data.distinguished,
100 language_id: data.language_id,
103 let updated_comment = blocking(context.pool(), move |conn| {
104 Comment::update(conn, comment_id, &form)
107 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
109 // Do the mentions / recipients
110 let updated_comment_content = updated_comment.content.to_owned();
111 let mentions = scrape_text_for_mentions(&updated_comment_content);
112 let recipient_ids = send_local_notifs(
115 &local_user_view.person,
122 // Send the apub update
123 CreateOrUpdateComment::send(
124 updated_comment.into(),
125 &local_user_view.person.into(),
126 CreateOrUpdateType::Update,
132 send_comment_ws_message(
134 UserOperationCrud::EditComment,
136 data.form_id.to_owned(),