1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
7 get_local_user_view_from_jwt,
10 use lemmy_apub::activities::{
11 comment::create_or_update::CreateOrUpdateComment,
14 use lemmy_db_queries::source::comment::Comment_;
15 use lemmy_db_schema::source::comment::*;
16 use lemmy_db_views::comment_view::CommentView;
18 utils::{remove_slurs, scrape_text_for_mentions},
23 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for EditComment {
27 type Response = CommentResponse;
31 context: &Data<LemmyContext>,
32 websocket_id: Option<ConnectionId>,
33 ) -> Result<CommentResponse, LemmyError> {
34 let data: &EditComment = self;
36 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38 let comment_id = data.comment_id;
39 let orig_comment = blocking(context.pool(), move |conn| {
40 CommentView::read(conn, comment_id, None)
44 // TODO is this necessary? It should really only need to check on create
46 local_user_view.person.id,
47 orig_comment.community.id,
52 // Verify that only the creator can edit
53 if local_user_view.person.id != orig_comment.creator.id {
54 return Err(ApiError::err_plain("no_comment_edit_allowed").into());
58 let content_slurs_removed =
59 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
60 let comment_id = data.comment_id;
61 let updated_comment = blocking(context.pool(), move |conn| {
62 Comment::update_content(conn, comment_id, &content_slurs_removed)
65 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
67 // Send the apub update
68 CreateOrUpdateComment::send(
70 &local_user_view.person,
71 CreateOrUpdateType::Update,
76 // Do the mentions / recipients
77 let updated_comment_content = updated_comment.content.to_owned();
78 let mentions = scrape_text_for_mentions(&updated_comment_content);
79 let recipient_ids = send_local_notifs(
82 local_user_view.person.clone(),
90 send_comment_ws_message(
92 UserOperationCrud::EditComment,
94 data.form_id.to_owned(),