]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/create.rs
Fixing clippy lints. (#1885)
[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 };
13 use lemmy_apub::{
14   fetcher::post_or_comment::PostOrComment,
15   generate_local_apub_endpoint,
16   objects::comment::ApubComment,
17   protocol::activities::{
18     create_or_update::comment::CreateOrUpdateComment,
19     voting::vote::{Vote, VoteType},
20     CreateOrUpdateType,
21   },
22   EndpointType,
23 };
24 use lemmy_db_schema::{
25   source::{
26     comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
27     person_mention::PersonMention,
28   },
29   traits::{Crud, Likeable},
30 };
31 use lemmy_db_views::comment_view::CommentView;
32 use lemmy_utils::{
33   utils::{remove_slurs, scrape_text_for_mentions},
34   ApiError,
35   ConnectionId,
36   LemmyError,
37 };
38 use lemmy_websocket::{
39   send::{send_comment_ws_message, send_local_notifs},
40   LemmyContext,
41   UserOperationCrud,
42 };
43
44 #[async_trait::async_trait(?Send)]
45 impl PerformCrud for CreateComment {
46   type Response = CommentResponse;
47
48   async fn perform(
49     &self,
50     context: &Data<LemmyContext>,
51     websocket_id: Option<ConnectionId>,
52   ) -> Result<CommentResponse, LemmyError> {
53     let data: &CreateComment = self;
54     let local_user_view =
55       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
56
57     let content_slurs_removed =
58       remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
59
60     // Check for a community ban
61     let post_id = data.post_id;
62     let post = get_post(post_id, context.pool()).await?;
63     let community_id = post.community_id;
64
65     check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
66     check_community_deleted_or_removed(community_id, context.pool()).await?;
67     check_post_deleted_or_removed(&post)?;
68
69     check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
70
71     // Check if post is locked, no new comments
72     if post.locked {
73       return Err(ApiError::err_plain("locked").into());
74     }
75
76     // If there's a parent_id, check to make sure that comment is in that post
77     if let Some(parent_id) = data.parent_id {
78       // Make sure the parent comment exists
79       let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
80         .await?
81         .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
82
83       check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
84
85       // Strange issue where sometimes the post ID is incorrect
86       if parent.post_id != post_id {
87         return Err(ApiError::err_plain("couldnt_create_comment").into());
88       }
89     }
90
91     let comment_form = CommentForm {
92       content: content_slurs_removed,
93       parent_id: data.parent_id.to_owned(),
94       post_id: data.post_id,
95       creator_id: local_user_view.person.id,
96       ..CommentForm::default()
97     };
98
99     // Create the comment
100     let comment_form2 = comment_form.clone();
101     let inserted_comment = blocking(context.pool(), move |conn| {
102       Comment::create(conn, &comment_form2)
103     })
104     .await?
105     .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
106
107     // Necessary to update the ap_id
108     let inserted_comment_id = inserted_comment.id;
109     let protocol_and_hostname = context.settings().get_protocol_and_hostname();
110
111     let updated_comment: Comment =
112       blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
113         let apub_id = generate_local_apub_endpoint(
114           EndpointType::Comment,
115           &inserted_comment_id.to_string(),
116           &protocol_and_hostname,
117         )?;
118         Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
119       })
120       .await?
121       .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
122
123     // Scan the comment for user mentions, add those rows
124     let post_id = post.id;
125     let mentions = scrape_text_for_mentions(&comment_form.content);
126     let recipient_ids = send_local_notifs(
127       mentions,
128       &updated_comment,
129       &local_user_view.person,
130       &post,
131       true,
132       context,
133     )
134     .await?;
135
136     // You like your own comment by default
137     let like_form = CommentLikeForm {
138       comment_id: inserted_comment.id,
139       post_id,
140       person_id: local_user_view.person.id,
141       score: 1,
142     };
143
144     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
145     blocking(context.pool(), like)
146       .await?
147       .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
148
149     let apub_comment: ApubComment = updated_comment.into();
150     CreateOrUpdateComment::send(
151       apub_comment.clone(),
152       &local_user_view.person.clone().into(),
153       CreateOrUpdateType::Create,
154       context,
155     )
156     .await?;
157     let object = PostOrComment::Comment(Box::new(apub_comment));
158     Vote::send(
159       &object,
160       &local_user_view.person.clone().into(),
161       community_id,
162       VoteType::Like,
163       context,
164     )
165     .await?;
166
167     let person_id = local_user_view.person.id;
168     let comment_id = inserted_comment.id;
169     let comment_view = blocking(context.pool(), move |conn| {
170       CommentView::read(conn, comment_id, Some(person_id))
171     })
172     .await??;
173
174     // If its a comment to yourself, mark it as read
175     if local_user_view.person.id == comment_view.get_recipient_id() {
176       let comment_id = inserted_comment.id;
177       blocking(context.pool(), move |conn| {
178         Comment::update_read(conn, comment_id, true)
179       })
180       .await?
181       .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
182     }
183     // If its a reply, mark the parent as read
184     if let Some(parent_id) = data.parent_id {
185       let parent_comment = blocking(context.pool(), move |conn| {
186         CommentView::read(conn, parent_id, Some(person_id))
187       })
188       .await??;
189       if local_user_view.person.id == parent_comment.get_recipient_id() {
190         blocking(context.pool(), move |conn| {
191           Comment::update_read(conn, parent_id, true)
192         })
193         .await?
194         .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
195       }
196       // If the parent has PersonMentions mark them as read too
197       let person_id = local_user_view.person.id;
198       let person_mention = blocking(context.pool(), move |conn| {
199         PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
200       })
201       .await?;
202       if let Ok(mention) = person_mention {
203         blocking(context.pool(), move |conn| {
204           PersonMention::update_read(conn, mention.id, true)
205         })
206         .await?
207         .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
208       }
209     }
210
211     send_comment_ws_message(
212       inserted_comment.id,
213       UserOperationCrud::CreateComment,
214       websocket_id,
215       data.form_id.to_owned(),
216       Some(local_user_view.person.id),
217       recipient_ids,
218       context,
219     )
220     .await
221   }
222 }