1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
7 check_post_deleted_or_removed,
9 get_local_user_view_from_jwt,
13 fetcher::post_or_comment::PostOrComment,
14 generate_local_apub_endpoint,
15 objects::comment::ApubComment,
16 protocol::activities::{
17 create_or_update::comment::CreateOrUpdateComment,
18 voting::vote::{Vote, VoteType},
23 use lemmy_db_schema::{
25 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
26 person_mention::PersonMention,
28 traits::{Crud, Likeable},
30 use lemmy_db_views::comment_view::CommentView;
32 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?;
56 let content_slurs_removed =
57 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
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 // If there's a parent_id, check to make sure that comment is in that post
74 if let Some(parent_id) = data.parent_id {
75 // Make sure the parent comment exists
76 let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
78 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
80 // Strange issue where sometimes the post ID is incorrect
81 if parent.post_id != post_id {
82 return Err(LemmyError::from_message("couldnt_create_comment"));
86 let comment_form = CommentForm {
87 content: content_slurs_removed,
88 parent_id: data.parent_id.to_owned(),
89 post_id: data.post_id,
90 creator_id: local_user_view.person.id,
91 ..CommentForm::default()
95 let comment_form2 = comment_form.clone();
96 let inserted_comment = blocking(context.pool(), move |conn| {
97 Comment::create(conn, &comment_form2)
100 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
102 // Necessary to update the ap_id
103 let inserted_comment_id = inserted_comment.id;
104 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
106 let updated_comment: Comment =
107 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
108 let apub_id = generate_local_apub_endpoint(
109 EndpointType::Comment,
110 &inserted_comment_id.to_string(),
111 &protocol_and_hostname,
113 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
116 .map_err(|e| e.with_message("couldnt_create_comment"))?;
118 // Scan the comment for user mentions, add those rows
119 let post_id = post.id;
120 let mentions = scrape_text_for_mentions(&comment_form.content);
121 let recipient_ids = send_local_notifs(
124 &local_user_view.person,
131 // You like your own comment by default
132 let like_form = CommentLikeForm {
133 comment_id: inserted_comment.id,
135 person_id: local_user_view.person.id,
139 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
140 blocking(context.pool(), like)
142 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
144 let apub_comment: ApubComment = updated_comment.into();
145 CreateOrUpdateComment::send(
146 apub_comment.clone(),
147 &local_user_view.person.clone().into(),
148 CreateOrUpdateType::Create,
153 let object = PostOrComment::Comment(Box::new(apub_comment));
156 &local_user_view.person.clone().into(),
163 let person_id = local_user_view.person.id;
164 let comment_id = inserted_comment.id;
165 let comment_view = blocking(context.pool(), move |conn| {
166 CommentView::read(conn, comment_id, Some(person_id))
170 // If its a comment to yourself, mark it as read
171 if local_user_view.person.id == comment_view.get_recipient_id() {
172 let comment_id = inserted_comment.id;
173 blocking(context.pool(), move |conn| {
174 Comment::update_read(conn, comment_id, true)
177 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
179 // If its a reply, mark the parent as read
180 if let Some(parent_id) = data.parent_id {
181 let parent_comment = blocking(context.pool(), move |conn| {
182 CommentView::read(conn, parent_id, Some(person_id))
185 if local_user_view.person.id == parent_comment.get_recipient_id() {
186 blocking(context.pool(), move |conn| {
187 Comment::update_read(conn, parent_id, true)
190 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_parent_comment"))?;
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_read(conn, mention.id, true)
203 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_person_mentions"))?;
207 send_comment_ws_message(
209 UserOperationCrud::CreateComment,
211 data.form_id.to_owned(),
212 Some(local_user_view.person.id),