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,
11 local_site_to_slur_regex,
14 use lemmy_apub::protocol::activities::{
15 create_or_update::comment::CreateOrUpdateComment,
18 use lemmy_db_schema::{
20 actor_language::CommunityLanguage,
21 comment::{Comment, CommentUpdateForm},
22 local_site::LocalSite,
26 use lemmy_db_views::structs::CommentView;
29 utils::{remove_slurs, scrape_text_for_mentions},
32 use lemmy_websocket::{
33 send::{send_comment_ws_message, send_local_notifs},
38 use crate::PerformCrud;
40 #[async_trait::async_trait(?Send)]
41 impl PerformCrud for EditComment {
42 type Response = CommentResponse;
44 #[tracing::instrument(skip(context, websocket_id))]
47 context: &Data<LemmyContext>,
48 websocket_id: Option<ConnectionId>,
49 ) -> Result<CommentResponse, LemmyError> {
50 let data: &EditComment = self;
52 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
53 let local_site = blocking(context.pool(), LocalSite::read).await??;
55 let comment_id = data.comment_id;
56 let orig_comment = blocking(context.pool(), move |conn| {
57 CommentView::read(conn, comment_id, None)
61 // TODO is this necessary? It should really only need to check on create
63 local_user_view.person.id,
64 orig_comment.community.id,
68 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
69 check_post_deleted_or_removed(&orig_comment.post)?;
71 // Verify that only the creator can edit
72 if local_user_view.person.id != orig_comment.creator.id {
73 return Err(LemmyError::from_message("no_comment_edit_allowed"));
76 if data.distinguished.is_some() {
77 // Verify that only a mod or admin can distinguish a comment
80 local_user_view.person.id,
81 orig_comment.community.id,
86 let language_id = self.language_id;
87 blocking(context.pool(), move |conn| {
88 CommunityLanguage::is_allowed_community_language(conn, language_id, orig_comment.community.id)
93 let content_slurs_removed = data
96 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
97 let comment_id = data.comment_id;
98 let form = CommentUpdateForm::builder()
99 .content(content_slurs_removed)
100 .distinguished(data.distinguished)
101 .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(),