1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
7 check_post_deleted_or_removed,
9 get_local_user_view_from_jwt,
12 use lemmy_apub::activities::{
13 comment::create_or_update::CreateOrUpdateComment,
16 use lemmy_db_schema::source::comment::Comment;
17 use lemmy_db_views::comment_view::CommentView;
19 utils::{remove_slurs, scrape_text_for_mentions},
24 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
26 #[async_trait::async_trait(?Send)]
27 impl PerformCrud for EditComment {
28 type Response = CommentResponse;
32 context: &Data<LemmyContext>,
33 websocket_id: Option<ConnectionId>,
34 ) -> Result<CommentResponse, LemmyError> {
35 let data: &EditComment = self;
37 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
39 let comment_id = data.comment_id;
40 let orig_comment = blocking(context.pool(), move |conn| {
41 CommentView::read(conn, comment_id, None)
45 // TODO is this necessary? It should really only need to check on create
47 local_user_view.person.id,
48 orig_comment.community.id,
52 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
53 check_post_deleted_or_removed(&orig_comment.post)?;
55 // Verify that only the creator can edit
56 if local_user_view.person.id != orig_comment.creator.id {
57 return Err(ApiError::err_plain("no_comment_edit_allowed").into());
61 let content_slurs_removed =
62 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
63 let comment_id = data.comment_id;
64 let updated_comment = blocking(context.pool(), move |conn| {
65 Comment::update_content(conn, comment_id, &content_slurs_removed)
68 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
70 // Send the apub update
71 CreateOrUpdateComment::send(
73 &local_user_view.person,
74 CreateOrUpdateType::Update,
79 // Do the mentions / recipients
80 let updated_comment_content = updated_comment.content.to_owned();
81 let mentions = scrape_text_for_mentions(&updated_comment_content);
82 let recipient_ids = send_local_notifs(
85 local_user_view.person.clone(),
93 send_comment_ws_message(
95 UserOperationCrud::EditComment,
97 data.form_id.to_owned(),