]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/create.rs
Major refactor, adding newtypes for apub crate
[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   activities::{
15     comment::create_or_update::CreateOrUpdateComment,
16     voting::vote::{Vote, VoteType},
17     CreateOrUpdateType,
18   },
19   fetcher::post_or_comment::PostOrComment,
20   generate_apub_endpoint,
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_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
69
70     // Check if post is locked, no new comments
71     if post.locked {
72       return Err(ApiError::err_plain("locked").into());
73     }
74
75     // If there's a parent_id, check to make sure that comment is in that post
76     if let Some(parent_id) = data.parent_id {
77       // Make sure the parent comment exists
78       let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
79         .await?
80         .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
81
82       check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
83
84       // Strange issue where sometimes the post ID is incorrect
85       if parent.post_id != post_id {
86         return Err(ApiError::err_plain("couldnt_create_comment").into());
87       }
88     }
89
90     let comment_form = CommentForm {
91       content: content_slurs_removed,
92       parent_id: data.parent_id.to_owned(),
93       post_id: data.post_id,
94       creator_id: local_user_view.person.id,
95       ..CommentForm::default()
96     };
97
98     // Create the comment
99     let comment_form2 = comment_form.clone();
100     let inserted_comment = blocking(context.pool(), move |conn| {
101       Comment::create(conn, &comment_form2)
102     })
103     .await?
104     .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
105
106     // Necessary to update the ap_id
107     let inserted_comment_id = inserted_comment.id;
108     let protocol_and_hostname = context.settings().get_protocol_and_hostname();
109
110     let updated_comment: Comment =
111       blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
112         let apub_id = generate_apub_endpoint(
113           EndpointType::Comment,
114           &inserted_comment_id.to_string(),
115           &protocol_and_hostname,
116         )?;
117         Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
118       })
119       .await?
120       .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
121
122     CreateOrUpdateComment::send(
123       &updated_comment.clone().into(),
124       &local_user_view.person.clone().into(),
125       CreateOrUpdateType::Create,
126       context,
127     )
128     .await?;
129
130     // Scan the comment for user mentions, add those rows
131     let post_id = post.id;
132     let mentions = scrape_text_for_mentions(&comment_form.content);
133     let recipient_ids = send_local_notifs(
134       mentions,
135       &updated_comment,
136       &local_user_view.person,
137       &post,
138       true,
139       context,
140     )
141     .await?;
142
143     // You like your own comment by default
144     let like_form = CommentLikeForm {
145       comment_id: inserted_comment.id,
146       post_id,
147       person_id: local_user_view.person.id,
148       score: 1,
149     };
150
151     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
152     blocking(context.pool(), like)
153       .await?
154       .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
155
156     let object = PostOrComment::Comment(updated_comment.into());
157     Vote::send(
158       &object,
159       &local_user_view.person.clone().into(),
160       community_id,
161       VoteType::Like,
162       context,
163     )
164     .await?;
165
166     let person_id = local_user_view.person.id;
167     let comment_id = inserted_comment.id;
168     let comment_view = blocking(context.pool(), move |conn| {
169       CommentView::read(conn, comment_id, Some(person_id))
170     })
171     .await??;
172
173     // If its a comment to yourself, mark it as read
174     if local_user_view.person.id == comment_view.get_recipient_id() {
175       let comment_id = inserted_comment.id;
176       blocking(context.pool(), move |conn| {
177         Comment::update_read(conn, comment_id, true)
178       })
179       .await?
180       .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
181     }
182     // If its a reply, mark the parent as read
183     if let Some(parent_id) = data.parent_id {
184       let parent_comment = blocking(context.pool(), move |conn| {
185         CommentView::read(conn, parent_id, Some(person_id))
186       })
187       .await??;
188       if local_user_view.person.id == parent_comment.get_recipient_id() {
189         blocking(context.pool(), move |conn| {
190           Comment::update_read(conn, parent_id, true)
191         })
192         .await?
193         .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
194       }
195       // If the parent has PersonMentions mark them as read too
196       let person_id = local_user_view.person.id;
197       let person_mention = blocking(context.pool(), move |conn| {
198         PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
199       })
200       .await?;
201       if let Ok(mention) = person_mention {
202         blocking(context.pool(), move |conn| {
203           PersonMention::update_read(conn, mention.id, true)
204         })
205         .await?
206         .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
207       }
208     }
209
210     send_comment_ws_message(
211       inserted_comment.id,
212       UserOperationCrud::CreateComment,
213       websocket_id,
214       data.form_id.to_owned(),
215       Some(local_user_view.person.id),
216       recipient_ids,
217       context,
218     )
219     .await
220   }
221 }