2021-03-25 19:19:40 +00:00
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-05-03 17:44:13 +00:00
|
|
|
comment::{CommentResponse, EditComment},
|
|
|
|
utils::{
|
|
|
|
blocking,
|
|
|
|
check_community_ban,
|
|
|
|
check_community_deleted_or_removed,
|
|
|
|
check_post_deleted_or_removed,
|
|
|
|
get_local_user_view_from_jwt,
|
2022-08-17 11:38:52 +00:00
|
|
|
is_mod_or_admin,
|
2022-05-03 17:44:13 +00:00
|
|
|
},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2021-10-29 10:32:42 +00:00
|
|
|
use lemmy_apub::protocol::activities::{
|
|
|
|
create_or_update::comment::CreateOrUpdateComment,
|
2021-07-31 15:09:43 +00:00
|
|
|
CreateOrUpdateType,
|
|
|
|
};
|
2022-08-23 21:40:56 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-06 18:27:58 +00:00
|
|
|
source::{
|
|
|
|
actor_language::CommunityLanguage,
|
|
|
|
comment::{Comment, CommentForm},
|
|
|
|
},
|
2022-08-23 21:40:56 +00:00
|
|
|
traits::Crud,
|
|
|
|
};
|
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::{
|
2022-06-02 14:33:41 +00:00
|
|
|
error::LemmyError,
|
2021-03-25 19:19:40 +00:00
|
|
|
utils::{remove_slurs, scrape_text_for_mentions},
|
|
|
|
ConnectionId,
|
|
|
|
};
|
2021-10-18 21:36:44 +00:00
|
|
|
use lemmy_websocket::{
|
|
|
|
send::{send_comment_ws_message, send_local_notifs},
|
|
|
|
LemmyContext,
|
|
|
|
UserOperationCrud,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-10-29 10:32:42 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for EditComment {
|
|
|
|
type Response = CommentResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<CommentResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &EditComment = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let comment_id = data.comment_id;
|
|
|
|
let orig_comment = blocking(context.pool(), move |conn| {
|
2021-07-05 16:07:26 +00:00
|
|
|
CommentView::read(conn, comment_id, None)
|
2021-03-25 19:19:40 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-08-19 20:54:15 +00:00
|
|
|
// TODO is this necessary? It should really only need to check on create
|
2021-03-25 19:19:40 +00:00
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2021-10-14 16:33:19 +00:00
|
|
|
check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
|
|
|
|
check_post_deleted_or_removed(&orig_comment.post)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Verify that only the creator can edit
|
|
|
|
if local_user_view.person.id != orig_comment.creator.id {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("no_comment_edit_allowed"));
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:40:56 +00:00
|
|
|
if data.distinguished.is_some() {
|
2022-08-17 11:38:52 +00:00
|
|
|
// Verify that only a mod or admin can distinguish a comment
|
|
|
|
is_mod_or_admin(
|
|
|
|
context.pool(),
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2022-10-06 18:27:58 +00:00
|
|
|
let language_id = self.language_id;
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
CommunityLanguage::is_allowed_community_language(conn, language_id, orig_comment.community.id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
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-08-23 21:57:44 +00:00
|
|
|
.map(|c| remove_slurs(c, &context.settings().slur_regex()));
|
2022-08-23 21:40:56 +00:00
|
|
|
let comment_id = data.comment_id;
|
|
|
|
let form = CommentForm {
|
|
|
|
creator_id: orig_comment.comment.creator_id,
|
|
|
|
post_id: orig_comment.comment.post_id,
|
|
|
|
content: content_slurs_removed.unwrap_or(orig_comment.comment.content),
|
|
|
|
distinguished: data.distinguished,
|
|
|
|
language_id: data.language_id,
|
|
|
|
..Default::default()
|
2022-08-17 11:38:52 +00:00
|
|
|
};
|
2022-08-23 21:40:56 +00:00
|
|
|
let updated_comment = blocking(context.pool(), move |conn| {
|
|
|
|
Comment::update(conn, comment_id, &form)
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Do the mentions / recipients
|
|
|
|
let updated_comment_content = updated_comment.content.to_owned();
|
|
|
|
let mentions = scrape_text_for_mentions(&updated_comment_content);
|
|
|
|
let recipient_ids = send_local_notifs(
|
|
|
|
mentions,
|
2021-10-18 21:36:44 +00:00
|
|
|
&updated_comment,
|
|
|
|
&local_user_view.person,
|
|
|
|
&orig_comment.post,
|
2021-03-25 19:19:40 +00:00
|
|
|
false,
|
2021-10-18 21:36:44 +00:00
|
|
|
context,
|
2021-03-25 19:19:40 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2021-11-06 12:37:55 +00:00
|
|
|
// Send the apub update
|
|
|
|
CreateOrUpdateComment::send(
|
|
|
|
updated_comment.into(),
|
|
|
|
&local_user_view.person.into(),
|
|
|
|
CreateOrUpdateType::Update,
|
|
|
|
context,
|
2021-11-15 22:54:25 +00:00
|
|
|
&mut 0,
|
2021-11-06 12:37:55 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
send_comment_ws_message(
|
|
|
|
data.comment_id,
|
|
|
|
UserOperationCrud::EditComment,
|
2021-03-25 19:19:40 +00:00
|
|
|
websocket_id,
|
2021-08-17 18:04:58 +00:00
|
|
|
data.form_id.to_owned(),
|
|
|
|
None,
|
|
|
|
recipient_ids,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|