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 get_local_user_view_from_jwt,
12 local_site_to_slur_regex,
16 generate_local_apub_endpoint,
17 objects::comment::ApubComment,
18 protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
21 use lemmy_db_schema::{
23 actor_language::CommunityLanguage,
24 comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
25 comment_reply::{CommentReply, CommentReplyUpdateForm},
26 local_site::LocalSite,
27 person_mention::{PersonMention, PersonMentionUpdateForm},
29 traits::{Crud, Likeable},
33 utils::{remove_slurs, scrape_text_for_mentions},
36 use lemmy_websocket::{
37 send::{send_comment_ws_message, send_local_notifs},
42 #[async_trait::async_trait(?Send)]
43 impl PerformCrud for CreateComment {
44 type Response = CommentResponse;
46 #[tracing::instrument(skip(context, websocket_id))]
49 context: &Data<LemmyContext>,
50 websocket_id: Option<ConnectionId>,
51 ) -> Result<CommentResponse, LemmyError> {
52 let data: &CreateComment = self;
54 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
55 let local_site = blocking(context.pool(), LocalSite::read).await??;
57 let content_slurs_removed = remove_slurs(
58 &data.content.to_owned(),
59 &local_site_to_slur_regex(&local_site),
62 // Check for a community ban
63 let post_id = data.post_id;
64 let post = get_post(post_id, context.pool()).await?;
65 let community_id = post.community_id;
67 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
68 check_community_deleted_or_removed(community_id, context.pool()).await?;
69 check_post_deleted_or_removed(&post)?;
71 // Check if post is locked, no new comments
73 return Err(LemmyError::from_message("locked"));
76 // Fetch the parent, if it exists
77 let parent_opt = if let Some(parent_id) = data.parent_id {
78 blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
85 // If there's a parent_id, check to make sure that comment is in that post
86 // Strange issue where sometimes the post ID of the parent comment is incorrect
87 if let Some(parent) = parent_opt.as_ref() {
88 if parent.post_id != post_id {
89 return Err(LemmyError::from_message("couldnt_create_comment"));
93 // if no language is set, copy language from parent post/comment
94 let parent_language = parent_opt
96 .map(|p| p.language_id)
97 .unwrap_or(post.language_id);
98 let language_id = data.language_id.unwrap_or(parent_language);
100 blocking(context.pool(), move |conn| {
101 CommunityLanguage::is_allowed_community_language(conn, Some(language_id), community_id)
105 let comment_form = CommentInsertForm::builder()
106 .content(content_slurs_removed.to_owned())
107 .post_id(data.post_id)
108 .creator_id(local_user_view.person.id)
109 .language_id(Some(language_id))
112 // Create the comment
113 let comment_form2 = comment_form.clone();
114 let parent_path = parent_opt.to_owned().map(|t| t.path);
115 let inserted_comment = blocking(context.pool(), move |conn| {
116 Comment::create(conn, &comment_form2, parent_path.as_ref())
119 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
121 // Necessary to update the ap_id
122 let inserted_comment_id = inserted_comment.id;
123 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
125 let updated_comment: Comment =
126 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
127 let apub_id = generate_local_apub_endpoint(
128 EndpointType::Comment,
129 &inserted_comment_id.to_string(),
130 &protocol_and_hostname,
135 &CommentUpdateForm::builder().ap_id(Some(apub_id)).build(),
139 .map_err(|e| e.with_message("couldnt_create_comment"))?;
141 // Scan the comment for user mentions, add those rows
142 let post_id = post.id;
143 let mentions = scrape_text_for_mentions(&content_slurs_removed);
144 let recipient_ids = send_local_notifs(
147 &local_user_view.person,
154 // You like your own comment by default
155 let like_form = CommentLikeForm {
156 comment_id: inserted_comment.id,
158 person_id: local_user_view.person.id,
162 let like = move |conn: &mut _| CommentLike::like(conn, &like_form);
163 blocking(context.pool(), like)
165 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
167 let apub_comment: ApubComment = updated_comment.into();
168 CreateOrUpdateComment::send(
169 apub_comment.clone(),
170 &local_user_view.person.clone().into(),
171 CreateOrUpdateType::Create,
177 // If its a reply, mark the parent as read
178 if let Some(parent) = parent_opt {
179 let parent_id = parent.id;
180 let comment_reply = blocking(context.pool(), move |conn| {
181 CommentReply::read_by_comment(conn, parent_id)
184 if let Ok(reply) = comment_reply {
185 blocking(context.pool(), move |conn| {
186 CommentReply::update(conn, reply.id, &CommentReplyUpdateForm { read: Some(true) })
189 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_replies"))?;
192 // If the parent has PersonMentions mark them as read too
193 let person_id = local_user_view.person.id;
194 let person_mention = blocking(context.pool(), move |conn| {
195 PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
198 if let Ok(mention) = person_mention {
199 blocking(context.pool(), move |conn| {
200 PersonMention::update(
203 &PersonMentionUpdateForm { read: Some(true) },
207 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_person_mentions"))?;
211 send_comment_ws_message(
213 UserOperationCrud::CreateComment,
215 data.form_id.to_owned(),
216 Some(local_user_view.person.id),