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,
15 generate_local_apub_endpoint,
16 objects::comment::ApubComment,
17 protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
20 use lemmy_db_schema::{
22 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
23 comment_reply::CommentReply,
24 person_mention::PersonMention,
26 traits::{Crud, Likeable},
30 utils::{remove_slurs, scrape_text_for_mentions},
33 use lemmy_websocket::{
34 send::{send_comment_ws_message, send_local_notifs},
39 #[async_trait::async_trait(?Send)]
40 impl PerformCrud for CreateComment {
41 type Response = CommentResponse;
43 #[tracing::instrument(skip(context, websocket_id))]
46 context: &Data<LemmyContext>,
47 websocket_id: Option<ConnectionId>,
48 ) -> Result<CommentResponse, LemmyError> {
49 let data: &CreateComment = self;
51 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
53 let content_slurs_removed =
54 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
56 // Check for a community ban
57 let post_id = data.post_id;
58 let post = get_post(post_id, context.pool()).await?;
59 let community_id = post.community_id;
61 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
62 check_community_deleted_or_removed(community_id, context.pool()).await?;
63 check_post_deleted_or_removed(&post)?;
65 // Check if post is locked, no new comments
67 return Err(LemmyError::from_message("locked"));
70 // Fetch the parent, if it exists
71 let parent_opt = if let Some(parent_id) = data.parent_id {
72 blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
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(LemmyError::from_message("couldnt_create_comment"));
87 // if no language is set, copy language from parent post/comment
88 let parent_language = parent_opt
90 .map(|p| p.language_id)
91 .unwrap_or(post.language_id);
92 let language_id = Some(data.language_id.unwrap_or(parent_language));
94 let comment_form = CommentForm {
95 content: content_slurs_removed,
96 post_id: data.post_id,
97 creator_id: local_user_view.person.id,
99 ..CommentForm::default()
102 // Create the comment
103 let comment_form2 = comment_form.clone();
104 let parent_path = parent_opt.to_owned().map(|t| t.path);
105 let inserted_comment = blocking(context.pool(), move |conn| {
106 Comment::create(conn, &comment_form2, parent_path.as_ref())
109 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
111 // Necessary to update the ap_id
112 let inserted_comment_id = inserted_comment.id;
113 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
115 let updated_comment: Comment =
116 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
117 let apub_id = generate_local_apub_endpoint(
118 EndpointType::Comment,
119 &inserted_comment_id.to_string(),
120 &protocol_and_hostname,
122 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
125 .map_err(|e| e.with_message("couldnt_create_comment"))?;
127 // Scan the comment for user mentions, add those rows
128 let post_id = post.id;
129 let mentions = scrape_text_for_mentions(&comment_form.content);
130 let recipient_ids = send_local_notifs(
133 &local_user_view.person,
140 // You like your own comment by default
141 let like_form = CommentLikeForm {
142 comment_id: inserted_comment.id,
144 person_id: local_user_view.person.id,
148 let like = move |conn: &mut _| CommentLike::like(conn, &like_form);
149 blocking(context.pool(), like)
151 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
153 let apub_comment: ApubComment = updated_comment.into();
154 CreateOrUpdateComment::send(
155 apub_comment.clone(),
156 &local_user_view.person.clone().into(),
157 CreateOrUpdateType::Create,
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 = blocking(context.pool(), move |conn| {
167 CommentReply::read_by_comment(conn, parent_id)
170 if let Ok(reply) = comment_reply {
171 blocking(context.pool(), move |conn| {
172 CommentReply::update_read(conn, reply.id, true)
175 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_replies"))?;
178 // If the parent has PersonMentions mark them as read too
179 let person_id = local_user_view.person.id;
180 let person_mention = blocking(context.pool(), move |conn| {
181 PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
184 if let Ok(mention) = person_mention {
185 blocking(context.pool(), move |conn| {
186 PersonMention::update_read(conn, mention.id, true)
189 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_person_mentions"))?;
193 send_comment_ws_message(
195 UserOperationCrud::CreateComment,
197 data.form_id.to_owned(),
198 Some(local_user_view.person.id),