1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, CreateComment},
8 check_community_deleted_or_removed,
9 check_post_deleted_or_removed,
10 generate_local_apub_endpoint,
11 get_local_user_view_from_jwt,
13 local_site_to_slur_regex,
16 websocket::UserOperationCrud,
18 use lemmy_db_schema::{
20 actor_language::CommunityLanguage,
21 comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
22 comment_reply::{CommentReply, CommentReplyUpdateForm},
23 local_site::LocalSite,
24 person_mention::{PersonMention, PersonMentionUpdateForm},
26 traits::{Crud, Likeable},
31 mention::scrape_text_for_mentions,
33 validation::is_valid_body_field,
38 #[async_trait::async_trait(?Send)]
39 impl PerformCrud for CreateComment {
40 type Response = CommentResponse;
42 #[tracing::instrument(skip(context, websocket_id))]
45 context: &Data<LemmyContext>,
46 websocket_id: Option<ConnectionId>,
47 ) -> Result<CommentResponse, LemmyError> {
48 let data: &CreateComment = self;
50 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
51 let local_site = LocalSite::read(context.pool()).await?;
53 let content_slurs_removed = remove_slurs(
54 &data.content.clone(),
55 &local_site_to_slur_regex(&local_site),
57 is_valid_body_field(&Some(content_slurs_removed.clone()))?;
59 // Check for a community ban
60 let post_id = data.post_id;
61 let post = get_post(post_id, context.pool()).await?;
62 let community_id = post.community_id;
64 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
65 check_community_deleted_or_removed(community_id, context.pool()).await?;
66 check_post_deleted_or_removed(&post)?;
68 // Check if post is locked, no new comments
70 return Err(LemmyError::from_message("locked"));
73 // Fetch the parent, if it exists
74 let parent_opt = if let Some(parent_id) = data.parent_id {
75 Comment::read(context.pool(), parent_id).await.ok()
80 // If there's a parent_id, check to make sure that comment is in that post
81 // Strange issue where sometimes the post ID of the parent comment is incorrect
82 if let Some(parent) = parent_opt.as_ref() {
83 if parent.post_id != post_id {
84 return Err(LemmyError::from_message("couldnt_create_comment"));
88 // if no language is set, copy language from parent post/comment
89 let parent_language = parent_opt
91 .map(|p| p.language_id)
92 .unwrap_or(post.language_id);
93 let language_id = data.language_id.unwrap_or(parent_language);
95 CommunityLanguage::is_allowed_community_language(
102 let comment_form = CommentInsertForm::builder()
103 .content(content_slurs_removed.clone())
104 .post_id(data.post_id)
105 .creator_id(local_user_view.person.id)
106 .language_id(Some(language_id))
109 // Create the comment
110 let comment_form2 = comment_form.clone();
111 let parent_path = parent_opt.clone().map(|t| t.path);
112 let inserted_comment = Comment::create(context.pool(), &comment_form2, parent_path.as_ref())
114 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
116 // Necessary to update the ap_id
117 let inserted_comment_id = inserted_comment.id;
118 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
120 let apub_id = generate_local_apub_endpoint(
121 EndpointType::Comment,
122 &inserted_comment_id.to_string(),
123 &protocol_and_hostname,
125 let updated_comment = Comment::update(
128 &CommentUpdateForm::builder().ap_id(Some(apub_id)).build(),
131 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
133 // Scan the comment for user mentions, add those rows
134 let post_id = post.id;
135 let mentions = scrape_text_for_mentions(&content_slurs_removed);
136 let recipient_ids = context
140 &local_user_view.person,
146 // You like your own comment by default
147 let like_form = CommentLikeForm {
148 comment_id: inserted_comment.id,
150 person_id: local_user_view.person.id,
154 CommentLike::like(context.pool(), &like_form)
156 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
158 // If its a reply, mark the parent as read
159 if let Some(parent) = parent_opt {
160 let parent_id = parent.id;
161 let comment_reply = CommentReply::read_by_comment(context.pool(), parent_id).await;
162 if let Ok(reply) = comment_reply {
163 CommentReply::update(
166 &CommentReplyUpdateForm { read: Some(true) },
169 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_replies"))?;
172 // If the parent has PersonMentions mark them as read too
173 let person_id = local_user_view.person.id;
175 PersonMention::read_by_comment_and_person(context.pool(), parent_id, person_id).await;
176 if let Ok(mention) = person_mention {
177 PersonMention::update(
180 &PersonMentionUpdateForm { read: Some(true) },
183 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_person_mentions"))?;
188 .send_comment_ws_message(
189 &UserOperationCrud::CreateComment,
192 data.form_id.clone(),
193 Some(local_user_view.person.id),