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_, DeleteableOrRemoveable};
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::{messages::SendComment, 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)
44 local_user_view.person.id,
45 orig_comment.community.id,
50 // Verify that only the creator can edit
51 if local_user_view.person.id != orig_comment.creator.id {
52 return Err(ApiError::err("no_comment_edit_allowed").into());
56 let content_slurs_removed = remove_slurs(&data.content.to_owned());
57 let comment_id = data.comment_id;
58 let updated_comment = blocking(context.pool(), move |conn| {
59 Comment::update_content(conn, comment_id, &content_slurs_removed)
62 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
64 // Send the apub update
65 CreateOrUpdateComment::send(
67 &local_user_view.person,
68 CreateOrUpdateType::Update,
73 // Do the mentions / recipients
74 let updated_comment_content = updated_comment.content.to_owned();
75 let mentions = scrape_text_for_mentions(&updated_comment_content);
76 let recipient_ids = send_local_notifs(
79 local_user_view.person.clone(),
86 let comment_id = data.comment_id;
87 let person_id = local_user_view.person.id;
88 let mut comment_view = blocking(context.pool(), move |conn| {
89 CommentView::read(conn, comment_id, Some(person_id))
93 // Blank out deleted or removed info
94 if comment_view.comment.deleted || comment_view.comment.removed {
95 comment_view.comment = comment_view.comment.blank_out_deleted_or_removed_info();
98 let res = CommentResponse {
101 form_id: data.form_id.to_owned(),
104 context.chat_server().do_send(SendComment {
105 op: UserOperationCrud::EditComment,
106 comment: res.clone(),