2022-11-02 19:18:22 +00:00
|
|
|
use crate::PerformCrud;
|
2021-03-25 19:19:40 +00:00
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2023-06-06 16:27:22 +00:00
|
|
|
build_response::{build_comment_response, send_local_notifs},
|
2022-05-03 17:44:13 +00:00
|
|
|
comment::{CommentResponse, EditComment},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::{check_community_ban, local_site_to_slur_regex, local_user_view_from_jwt},
|
2021-07-31 15:09:43 +00:00
|
|
|
};
|
2022-08-23 21:40:56 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-06 18:27:58 +00:00
|
|
|
source::{
|
|
|
|
actor_language::CommunityLanguage,
|
2022-10-27 09:24:07 +00:00
|
|
|
comment::{Comment, CommentUpdateForm},
|
|
|
|
local_site::LocalSite,
|
2022-10-06 18:27:58 +00:00
|
|
|
},
|
2022-08-23 21:40:56 +00:00
|
|
|
traits::Crud,
|
2023-02-14 21:41:22 +00:00
|
|
|
utils::naive_now,
|
2022-08-23 21:40:56 +00:00
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::structs::CommentView;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_utils::{
|
2023-07-10 14:50:07 +00:00
|
|
|
error::{LemmyError, LemmyErrorExt, LemmyErrorType},
|
2023-04-15 14:45:11 +00:00
|
|
|
utils::{
|
|
|
|
mention::scrape_text_for_mentions,
|
|
|
|
slurs::remove_slurs,
|
|
|
|
validation::is_valid_body_field,
|
|
|
|
},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for EditComment {
|
|
|
|
type Response = CommentResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &EditComment = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let comment_id = data.comment_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2021-03-25 19:19:40 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Verify that only the creator can edit
|
|
|
|
if local_user_view.person.id != orig_comment.creator.id {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::NoCommentEditAllowed)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 18:27:58 +00:00
|
|
|
let language_id = self.language_id;
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityLanguage::is_allowed_community_language(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
language_id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
|
2022-08-17 11:38:52 +00:00
|
|
|
// Update the Content
|
2022-08-23 21:40:56 +00:00
|
|
|
let content_slurs_removed = data
|
|
|
|
.content
|
|
|
|
.as_ref()
|
2022-10-27 09:24:07 +00:00
|
|
|
.map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
|
2023-04-15 14:45:11 +00:00
|
|
|
|
2023-06-26 08:47:01 +00:00
|
|
|
is_valid_body_field(&content_slurs_removed, false)?;
|
2023-04-15 14:45:11 +00:00
|
|
|
|
2022-08-23 21:40:56 +00:00
|
|
|
let comment_id = data.comment_id;
|
2022-10-27 09:24:07 +00:00
|
|
|
let form = CommentUpdateForm::builder()
|
|
|
|
.content(content_slurs_removed)
|
|
|
|
.language_id(data.language_id)
|
2023-02-14 21:41:22 +00:00
|
|
|
.updated(Some(Some(naive_now())))
|
2022-10-27 09:24:07 +00:00
|
|
|
.build();
|
2023-07-11 13:09:59 +00:00
|
|
|
let updated_comment = Comment::update(&mut context.pool(), comment_id, &form)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Do the mentions / recipients
|
2022-11-19 04:33:54 +00:00
|
|
|
let updated_comment_content = updated_comment.content.clone();
|
2021-03-25 19:19:40 +00:00
|
|
|
let mentions = scrape_text_for_mentions(&updated_comment_content);
|
2023-06-06 16:27:22 +00:00
|
|
|
let recipient_ids = send_local_notifs(
|
|
|
|
mentions,
|
|
|
|
&updated_comment,
|
|
|
|
&local_user_view.person,
|
|
|
|
&orig_comment.post,
|
|
|
|
false,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
build_comment_response(
|
|
|
|
context,
|
|
|
|
updated_comment.id,
|
|
|
|
Some(local_user_view),
|
|
|
|
self.form_id.clone(),
|
|
|
|
recipient_ids,
|
|
|
|
)
|
|
|
|
.await
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|