]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/create.rs
Allow comment replies from blocked users. Fixes #1793 (#1969)
[lemmy.git] / crates / api_crud / src / comment / create.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   check_community_ban,
6   check_community_deleted_or_removed,
7   check_post_deleted_or_removed,
8   comment::*,
9   get_local_user_view_from_jwt,
10   get_post,
11 };
12 use lemmy_apub::{
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},
19     CreateOrUpdateType,
20   },
21   EndpointType,
22 };
23 use lemmy_db_schema::{
24   source::{
25     comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
26     person_mention::PersonMention,
27   },
28   traits::{Crud, Likeable},
29 };
30 use lemmy_db_views::comment_view::CommentView;
31 use lemmy_utils::{
32   utils::{remove_slurs, scrape_text_for_mentions},
33   ApiError,
34   ConnectionId,
35   LemmyError,
36 };
37 use lemmy_websocket::{
38   send::{send_comment_ws_message, send_local_notifs},
39   LemmyContext,
40   UserOperationCrud,
41 };
42
43 #[async_trait::async_trait(?Send)]
44 impl PerformCrud for CreateComment {
45   type Response = CommentResponse;
46
47   async fn perform(
48     &self,
49     context: &Data<LemmyContext>,
50     websocket_id: Option<ConnectionId>,
51   ) -> Result<CommentResponse, LemmyError> {
52     let data: &CreateComment = self;
53     let local_user_view =
54       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
55
56     let content_slurs_removed =
57       remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
58
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;
63
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)?;
67
68     // Check if post is locked, no new comments
69     if post.locked {
70       return Err(ApiError::err_plain("locked").into());
71     }
72
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))
77         .await?
78         .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
79
80       // Strange issue where sometimes the post ID is incorrect
81       if parent.post_id != post_id {
82         return Err(ApiError::err_plain("couldnt_create_comment").into());
83       }
84     }
85
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()
92     };
93
94     // Create the comment
95     let comment_form2 = comment_form.clone();
96     let inserted_comment = blocking(context.pool(), move |conn| {
97       Comment::create(conn, &comment_form2)
98     })
99     .await?
100     .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
101
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();
105
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,
112         )?;
113         Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
114       })
115       .await?
116       .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
117
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(
122       mentions,
123       &updated_comment,
124       &local_user_view.person,
125       &post,
126       true,
127       context,
128     )
129     .await?;
130
131     // You like your own comment by default
132     let like_form = CommentLikeForm {
133       comment_id: inserted_comment.id,
134       post_id,
135       person_id: local_user_view.person.id,
136       score: 1,
137     };
138
139     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
140     blocking(context.pool(), like)
141       .await?
142       .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
143
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,
149       context,
150       &mut 0,
151     )
152     .await?;
153     let object = PostOrComment::Comment(Box::new(apub_comment));
154     Vote::send(
155       &object,
156       &local_user_view.person.clone().into(),
157       community_id,
158       VoteType::Like,
159       context,
160     )
161     .await?;
162
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))
167     })
168     .await??;
169
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)
175       })
176       .await?
177       .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
178     }
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))
183       })
184       .await??;
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)
188         })
189         .await?
190         .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
191       }
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)
196       })
197       .await?;
198       if let Ok(mention) = person_mention {
199         blocking(context.pool(), move |conn| {
200           PersonMention::update_read(conn, mention.id, true)
201         })
202         .await?
203         .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
204       }
205     }
206
207     send_comment_ws_message(
208       inserted_comment.id,
209       UserOperationCrud::CreateComment,
210       websocket_id,
211       data.form_id.to_owned(),
212       Some(local_user_view.person.id),
213       recipient_ids,
214       context,
215     )
216     .await
217   }
218 }