]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/create.rs
User / community blocking. Fixes #426 (#1604)
[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_person_block,
7   comment::*,
8   get_local_user_view_from_jwt,
9   get_post,
10   send_local_notifs,
11 };
12 use lemmy_apub::{
13   activities::{
14     comment::create_or_update::CreateOrUpdateComment,
15     voting::vote::{Vote, VoteType},
16     CreateOrUpdateType,
17   },
18   generate_apub_endpoint,
19   EndpointType,
20   PostOrComment,
21 };
22 use lemmy_db_queries::{source::comment::Comment_, Crud, Likeable};
23 use lemmy_db_schema::source::comment::*;
24 use lemmy_utils::{
25   utils::{remove_slurs, scrape_text_for_mentions},
26   ApiError,
27   ConnectionId,
28   LemmyError,
29 };
30 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
31
32 #[async_trait::async_trait(?Send)]
33 impl PerformCrud for CreateComment {
34   type Response = CommentResponse;
35
36   async fn perform(
37     &self,
38     context: &Data<LemmyContext>,
39     websocket_id: Option<ConnectionId>,
40   ) -> Result<CommentResponse, LemmyError> {
41     let data: &CreateComment = self;
42     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
43
44     let content_slurs_removed = remove_slurs(&data.content.to_owned());
45
46     // Check for a community ban
47     let post_id = data.post_id;
48     let post = get_post(post_id, context.pool()).await?;
49     let community_id = post.community_id;
50
51     check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
52
53     check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
54
55     // Check if post is locked, no new comments
56     if post.locked {
57       return Err(ApiError::err("locked").into());
58     }
59
60     // If there's a parent_id, check to make sure that comment is in that post
61     if let Some(parent_id) = data.parent_id {
62       // Make sure the parent comment exists
63       let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
64         .await?
65         .map_err(|_| ApiError::err("couldnt_create_comment"))?;
66
67       check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
68
69       // Strange issue where sometimes the post ID is incorrect
70       if parent.post_id != post_id {
71         return Err(ApiError::err("couldnt_create_comment").into());
72       }
73     }
74
75     let comment_form = CommentForm {
76       content: content_slurs_removed,
77       parent_id: data.parent_id.to_owned(),
78       post_id: data.post_id,
79       creator_id: local_user_view.person.id,
80       ..CommentForm::default()
81     };
82
83     // Create the comment
84     let comment_form2 = comment_form.clone();
85     let inserted_comment = blocking(context.pool(), move |conn| {
86       Comment::create(conn, &comment_form2)
87     })
88     .await?
89     .map_err(|_| ApiError::err("couldnt_create_comment"))?;
90
91     // Necessary to update the ap_id
92     let inserted_comment_id = inserted_comment.id;
93     let updated_comment: Comment =
94       blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
95         let apub_id =
96           generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?;
97         Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
98       })
99       .await?
100       .map_err(|_| ApiError::err("couldnt_create_comment"))?;
101
102     CreateOrUpdateComment::send(
103       &updated_comment,
104       &local_user_view.person,
105       CreateOrUpdateType::Create,
106       context,
107     )
108     .await?;
109
110     // Scan the comment for user mentions, add those rows
111     let post_id = post.id;
112     let mentions = scrape_text_for_mentions(&comment_form.content);
113     let recipient_ids = send_local_notifs(
114       mentions,
115       updated_comment.clone(),
116       local_user_view.person.clone(),
117       post,
118       context.pool(),
119       true,
120     )
121     .await?;
122
123     // You like your own comment by default
124     let like_form = CommentLikeForm {
125       comment_id: inserted_comment.id,
126       post_id,
127       person_id: local_user_view.person.id,
128       score: 1,
129     };
130
131     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
132     if blocking(context.pool(), like).await?.is_err() {
133       return Err(ApiError::err("couldnt_like_comment").into());
134     }
135
136     let object = PostOrComment::Comment(Box::new(updated_comment));
137     Vote::send(
138       &object,
139       &local_user_view.person,
140       community_id,
141       VoteType::Like,
142       context,
143     )
144     .await?;
145
146     // If its a comment to yourself, mark it as read
147     if local_user_view.person.id == inserted_comment.creator_id {
148       let comment_id = inserted_comment.id;
149       blocking(context.pool(), move |conn| {
150         Comment::update_read(conn, comment_id, true)
151       })
152       .await?
153       .map_err(|_| ApiError::err("couldnt_update_comment"))?;
154     }
155
156     send_comment_ws_message(
157       inserted_comment.id,
158       UserOperationCrud::CreateComment,
159       websocket_id,
160       data.form_id.to_owned(),
161       Some(local_user_view.person.id),
162       recipient_ids,
163       context,
164     )
165     .await
166   }
167 }