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