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;
35 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
37 let comment_id = data.comment_id;
38 let orig_comment = blocking(context.pool(), move |conn| {
39 CommentView::read(conn, comment_id, None)
43 // TODO is this necessary? It should really only need to check on create
45 local_user_view.person.id,
46 orig_comment.community.id,
51 // Verify that only the creator can edit
52 if local_user_view.person.id != orig_comment.creator.id {
53 return Err(ApiError::err("no_comment_edit_allowed").into());
57 let content_slurs_removed = remove_slurs(&data.content.to_owned());
58 let comment_id = data.comment_id;
59 let updated_comment = blocking(context.pool(), move |conn| {
60 Comment::update_content(conn, comment_id, &content_slurs_removed)
63 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
65 // Send the apub update
66 CreateOrUpdateComment::send(
68 &local_user_view.person,
69 CreateOrUpdateType::Update,
74 // Do the mentions / recipients
75 let updated_comment_content = updated_comment.content.to_owned();
76 let mentions = scrape_text_for_mentions(&updated_comment_content);
77 let recipient_ids = send_local_notifs(
80 local_user_view.person.clone(),
87 send_comment_ws_message(
89 UserOperationCrud::EditComment,
91 data.form_id.to_owned(),