2021-11-06 12:37:55 +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, CreateComment},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{
|
|
|
|
check_community_ban,
|
|
|
|
check_community_deleted_or_removed,
|
|
|
|
check_post_deleted_or_removed,
|
2022-11-28 14:29:33 +00:00
|
|
|
generate_local_apub_endpoint,
|
2022-05-03 17:44:13 +00:00
|
|
|
get_post,
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site_to_slur_regex,
|
2023-05-25 14:50:07 +00:00
|
|
|
local_user_view_from_jwt,
|
2022-11-28 14:29:33 +00:00
|
|
|
EndpointType,
|
2022-05-03 17:44:13 +00:00
|
|
|
},
|
2021-07-31 14:57:37 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
2022-10-06 18:27:58 +00:00
|
|
|
actor_language::CommunityLanguage,
|
2022-10-27 09:24:07 +00:00
|
|
|
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
|
|
|
|
comment_reply::{CommentReply, CommentReplyUpdateForm},
|
|
|
|
local_site::LocalSite,
|
|
|
|
person_mention::{PersonMention, PersonMentionUpdateForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
},
|
|
|
|
traits::{Crud, Likeable},
|
2021-10-08 14:28:32 +00:00
|
|
|
};
|
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
|
|
|
};
|
2023-07-10 14:50:07 +00:00
|
|
|
|
2023-06-08 14:31:26 +00:00
|
|
|
const MAX_COMMENT_DEPTH_LIMIT: usize = 100;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for CreateComment {
|
|
|
|
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: &CreateComment = 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
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let content_slurs_removed = remove_slurs(
|
2022-11-19 04:33:54 +00:00
|
|
|
&data.content.clone(),
|
2022-10-27 09:24:07 +00:00
|
|
|
&local_site_to_slur_regex(&local_site),
|
|
|
|
);
|
2023-06-26 08:47:01 +00:00
|
|
|
is_valid_body_field(&Some(content_slurs_removed.clone()), false)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Check for a community ban
|
|
|
|
let post_id = data.post_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let post = get_post(post_id, &mut context.pool()).await?;
|
2021-08-02 20:33:40 +00:00
|
|
|
let community_id = post.community_id;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
check_community_ban(local_user_view.person.id, community_id, &mut context.pool()).await?;
|
|
|
|
check_community_deleted_or_removed(community_id, &mut context.pool()).await?;
|
2021-10-14 16:33:19 +00:00
|
|
|
check_post_deleted_or_removed(&post)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Check if post is locked, no new comments
|
|
|
|
if post.locked {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::Locked)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 03:55:59 +00:00
|
|
|
// Fetch the parent, if it exists
|
|
|
|
let parent_opt = if let Some(parent_id) = data.parent_id {
|
2023-07-11 13:09:59 +00:00
|
|
|
Comment::read(&mut context.pool(), parent_id).await.ok()
|
2022-07-30 03:55:59 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-08-19 20:54:15 +00:00
|
|
|
|
2022-07-30 03:55:59 +00:00
|
|
|
// If there's a parent_id, check to make sure that comment is in that post
|
|
|
|
// Strange issue where sometimes the post ID of the parent comment is incorrect
|
|
|
|
if let Some(parent) = parent_opt.as_ref() {
|
2021-03-25 19:19:40 +00:00
|
|
|
if parent.post_id != post_id {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::CouldntCreateComment)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
2023-06-08 14:31:26 +00:00
|
|
|
check_comment_depth(parent)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:40:56 +00:00
|
|
|
// if no language is set, copy language from parent post/comment
|
|
|
|
let parent_language = parent_opt
|
|
|
|
.as_ref()
|
|
|
|
.map(|p| p.language_id)
|
|
|
|
.unwrap_or(post.language_id);
|
2022-10-06 18:27:58 +00:00
|
|
|
let language_id = data.language_id.unwrap_or(parent_language);
|
|
|
|
|
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
|
|
|
Some(language_id),
|
|
|
|
community_id,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-08-23 21:40:56 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let comment_form = CommentInsertForm::builder()
|
2022-11-19 04:33:54 +00:00
|
|
|
.content(content_slurs_removed.clone())
|
2022-10-27 09:24:07 +00:00
|
|
|
.post_id(data.post_id)
|
|
|
|
.creator_id(local_user_view.person.id)
|
|
|
|
.language_id(Some(language_id))
|
|
|
|
.build();
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Create the comment
|
2022-11-19 04:33:54 +00:00
|
|
|
let parent_path = parent_opt.clone().map(|t| t.path);
|
2023-07-11 13:09:59 +00:00
|
|
|
let inserted_comment =
|
|
|
|
Comment::create(&mut context.pool(), &comment_form, parent_path.as_ref())
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntCreateComment)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Necessary to update the ap_id
|
|
|
|
let inserted_comment_id = inserted_comment.id;
|
2021-09-22 15:57:09 +00:00
|
|
|
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let apub_id = generate_local_apub_endpoint(
|
|
|
|
EndpointType::Comment,
|
|
|
|
&inserted_comment_id.to_string(),
|
|
|
|
&protocol_and_hostname,
|
|
|
|
)?;
|
|
|
|
let updated_comment = Comment::update(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
inserted_comment_id,
|
|
|
|
&CommentUpdateForm::builder().ap_id(Some(apub_id)).build(),
|
|
|
|
)
|
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntCreateComment)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Scan the comment for user mentions, add those rows
|
2022-10-27 09:24:07 +00:00
|
|
|
let mentions = scrape_text_for_mentions(&content_slurs_removed);
|
2023-06-06 16:27:22 +00:00
|
|
|
let recipient_ids = send_local_notifs(
|
|
|
|
mentions,
|
|
|
|
&updated_comment,
|
|
|
|
&local_user_view.person,
|
|
|
|
&post,
|
|
|
|
true,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// You like your own comment by default
|
|
|
|
let like_form = CommentLikeForm {
|
|
|
|
comment_id: inserted_comment.id,
|
2023-06-06 16:27:22 +00:00
|
|
|
post_id: post.id,
|
2021-03-25 19:19:40 +00:00
|
|
|
person_id: local_user_view.person.id,
|
|
|
|
score: 1,
|
|
|
|
};
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
CommentLike::like(&mut context.pool(), &like_form)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-10-08 14:28:32 +00:00
|
|
|
// If its a reply, mark the parent as read
|
2022-07-30 03:55:59 +00:00
|
|
|
if let Some(parent) = parent_opt {
|
|
|
|
let parent_id = parent.id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let comment_reply = CommentReply::read_by_comment(&mut context.pool(), parent_id).await;
|
2022-07-30 03:55:59 +00:00
|
|
|
if let Ok(reply) = comment_reply {
|
2022-11-09 10:05:00 +00:00
|
|
|
CommentReply::update(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
reply.id,
|
|
|
|
&CommentReplyUpdateForm { read: Some(true) },
|
|
|
|
)
|
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateReplies)?;
|
2021-10-08 14:28:32 +00:00
|
|
|
}
|
2022-07-30 03:55:59 +00:00
|
|
|
|
2021-10-08 14:28:32 +00:00
|
|
|
// If the parent has PersonMentions mark them as read too
|
|
|
|
let person_id = local_user_view.person.id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let person_mention =
|
2023-07-11 13:09:59 +00:00
|
|
|
PersonMention::read_by_comment_and_person(&mut context.pool(), parent_id, person_id).await;
|
2021-10-08 14:28:32 +00:00
|
|
|
if let Ok(mention) = person_mention {
|
2022-11-09 10:05:00 +00:00
|
|
|
PersonMention::update(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
mention.id,
|
|
|
|
&PersonMentionUpdateForm { read: Some(true) },
|
|
|
|
)
|
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdatePersonMentions)?;
|
2021-10-08 14:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
build_comment_response(
|
|
|
|
context,
|
|
|
|
inserted_comment.id,
|
|
|
|
Some(local_user_view),
|
|
|
|
self.form_id.clone(),
|
|
|
|
recipient_ids,
|
|
|
|
)
|
|
|
|
.await
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-08 14:31:26 +00:00
|
|
|
|
|
|
|
pub fn check_comment_depth(comment: &Comment) -> Result<(), LemmyError> {
|
|
|
|
let path = &comment.path.0;
|
2023-06-28 09:19:26 +00:00
|
|
|
let length = path.split('.').count();
|
2023-06-08 14:31:26 +00:00
|
|
|
if length > MAX_COMMENT_DEPTH_LIMIT {
|
2023-07-10 14:50:07 +00:00
|
|
|
Err(LemmyErrorType::MaxCommentDepthReached)?
|
2023-06-08 14:31:26 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|