]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/create.rs
Dont allow posts to deleted / removed communities. Fixes #1827 (#1828)
[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_person_block,
8   check_post_deleted_or_removed,
9   comment::*,
10   get_local_user_view_from_jwt,
11   get_post,
12   send_local_notifs,
13 };
14 use lemmy_apub::{
15   activities::{
16     comment::create_or_update::CreateOrUpdateComment,
17     voting::vote::{Vote, VoteType},
18     CreateOrUpdateType,
19   },
20   fetcher::post_or_comment::PostOrComment,
21   generate_apub_endpoint,
22   EndpointType,
23 };
24 use lemmy_db_queries::{
25   source::{comment::Comment_, person_mention::PersonMention_},
26   Crud,
27   Likeable,
28 };
29 use lemmy_db_schema::source::{comment::*, person_mention::PersonMention};
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::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
38
39 #[async_trait::async_trait(?Send)]
40 impl PerformCrud for CreateComment {
41   type Response = CommentResponse;
42
43   async fn perform(
44     &self,
45     context: &Data<LemmyContext>,
46     websocket_id: Option<ConnectionId>,
47   ) -> Result<CommentResponse, LemmyError> {
48     let data: &CreateComment = self;
49     let local_user_view =
50       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
51
52     let content_slurs_removed =
53       remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
54
55     // Check for a community ban
56     let post_id = data.post_id;
57     let post = get_post(post_id, context.pool()).await?;
58     let community_id = post.community_id;
59
60     check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
61     check_community_deleted_or_removed(community_id, context.pool()).await?;
62     check_post_deleted_or_removed(&post)?;
63
64     check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
65
66     // Check if post is locked, no new comments
67     if post.locked {
68       return Err(ApiError::err_plain("locked").into());
69     }
70
71     // If there's a parent_id, check to make sure that comment is in that post
72     if let Some(parent_id) = data.parent_id {
73       // Make sure the parent comment exists
74       let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
75         .await?
76         .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
77
78       check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
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_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     CreateOrUpdateComment::send(
119       &updated_comment,
120       &local_user_view.person,
121       CreateOrUpdateType::Create,
122       context,
123     )
124     .await?;
125
126     // Scan the comment for user mentions, add those rows
127     let post_id = post.id;
128     let mentions = scrape_text_for_mentions(&comment_form.content);
129     let recipient_ids = send_local_notifs(
130       mentions,
131       updated_comment.clone(),
132       local_user_view.person.clone(),
133       post,
134       context.pool(),
135       true,
136       &context.settings(),
137     )
138     .await?;
139
140     // You like your own comment by default
141     let like_form = CommentLikeForm {
142       comment_id: inserted_comment.id,
143       post_id,
144       person_id: local_user_view.person.id,
145       score: 1,
146     };
147
148     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
149     blocking(context.pool(), like)
150       .await?
151       .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
152
153     let object = PostOrComment::Comment(updated_comment);
154     Vote::send(
155       &object,
156       &local_user_view.person,
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 }