1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 build_response::{build_comment_response, send_local_notifs},
5 comment::{CommentResponse, CreateComment},
9 check_community_deleted_or_removed,
10 check_post_deleted_or_removed,
11 generate_local_apub_endpoint,
13 local_site_to_slur_regex,
14 local_user_view_from_jwt,
19 use lemmy_db_schema::{
20 impls::actor_language::default_post_language,
22 actor_language::CommunityLanguage,
23 comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
24 comment_reply::{CommentReply, CommentReplyUpdateForm},
25 local_site::LocalSite,
26 person_mention::{PersonMention, PersonMentionUpdateForm},
28 traits::{Crud, Likeable},
31 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
33 mention::scrape_text_for_mentions,
35 validation::is_valid_body_field,
39 const MAX_COMMENT_DEPTH_LIMIT: usize = 100;
41 #[async_trait::async_trait(?Send)]
42 impl PerformCrud for CreateComment {
43 type Response = CommentResponse;
45 #[tracing::instrument(skip(context))]
46 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
47 let data: &CreateComment = self;
48 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
49 let local_site = LocalSite::read(&mut context.pool()).await?;
51 let content = remove_slurs(
52 &data.content.clone(),
53 &local_site_to_slur_regex(&local_site),
55 is_valid_body_field(&Some(content.clone()), false)?;
56 let content = sanitize_html(&content);
58 // Check for a community ban
59 let post_id = data.post_id;
60 let post = get_post(post_id, &mut context.pool()).await?;
61 let community_id = post.community_id;
63 check_community_ban(local_user_view.person.id, community_id, &mut context.pool()).await?;
64 check_community_deleted_or_removed(community_id, &mut context.pool()).await?;
65 check_post_deleted_or_removed(&post)?;
67 // Check if post is locked, no new comments
69 return Err(LemmyErrorType::Locked)?;
72 // Fetch the parent, if it exists
73 let parent_opt = if let Some(parent_id) = data.parent_id {
74 Comment::read(&mut context.pool(), parent_id).await.ok()
79 // If there's a parent_id, check to make sure that comment is in that post
80 // Strange issue where sometimes the post ID of the parent comment is incorrect
81 if let Some(parent) = parent_opt.as_ref() {
82 if parent.post_id != post_id {
83 return Err(LemmyErrorType::CouldntCreateComment)?;
85 check_comment_depth(parent)?;
88 CommunityLanguage::is_allowed_community_language(
95 // attempt to set default language if none was provided
96 let language_id = match data.language_id {
97 Some(lid) => Some(lid),
99 default_post_language(
102 local_user_view.local_user.id,
108 let comment_form = CommentInsertForm::builder()
109 .content(content.clone())
110 .post_id(data.post_id)
111 .creator_id(local_user_view.person.id)
112 .language_id(language_id)
115 // Create the comment
116 let parent_path = parent_opt.clone().map(|t| t.path);
117 let inserted_comment =
118 Comment::create(&mut context.pool(), &comment_form, parent_path.as_ref())
120 .with_lemmy_type(LemmyErrorType::CouldntCreateComment)?;
122 // Necessary to update the ap_id
123 let inserted_comment_id = inserted_comment.id;
124 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
126 let apub_id = generate_local_apub_endpoint(
127 EndpointType::Comment,
128 &inserted_comment_id.to_string(),
129 &protocol_and_hostname,
131 let updated_comment = Comment::update(
134 &CommentUpdateForm::builder().ap_id(Some(apub_id)).build(),
137 .with_lemmy_type(LemmyErrorType::CouldntCreateComment)?;
139 // Scan the comment for user mentions, add those rows
140 let mentions = scrape_text_for_mentions(&content);
141 let recipient_ids = send_local_notifs(
144 &local_user_view.person,
151 // You like your own comment by default
152 let like_form = CommentLikeForm {
153 comment_id: inserted_comment.id,
155 person_id: local_user_view.person.id,
159 CommentLike::like(&mut context.pool(), &like_form)
161 .with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;
163 // If its a reply, mark the parent as read
164 if let Some(parent) = parent_opt {
165 let parent_id = parent.id;
166 let comment_reply = CommentReply::read_by_comment(&mut context.pool(), parent_id).await;
167 if let Ok(reply) = comment_reply {
168 CommentReply::update(
171 &CommentReplyUpdateForm { read: Some(true) },
174 .with_lemmy_type(LemmyErrorType::CouldntUpdateReplies)?;
177 // If the parent has PersonMentions mark them as read too
178 let person_id = local_user_view.person.id;
180 PersonMention::read_by_comment_and_person(&mut context.pool(), parent_id, person_id).await;
181 if let Ok(mention) = person_mention {
182 PersonMention::update(
185 &PersonMentionUpdateForm { read: Some(true) },
188 .with_lemmy_type(LemmyErrorType::CouldntUpdatePersonMentions)?;
192 build_comment_response(
195 Some(local_user_view),
196 self.form_id.clone(),
203 pub fn check_comment_depth(comment: &Comment) -> Result<(), LemmyError> {
204 let path = &comment.path.0;
205 let length = path.split('.').count();
206 if length > MAX_COMMENT_DEPTH_LIMIT {
207 Err(LemmyErrorType::MaxCommentDepthReached)?