]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/create.rs
Don't drop error context when adding a message to errors (#1958)
[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   ConnectionId,
34   LemmyError,
35 };
36 use lemmy_websocket::{
37   send::{send_comment_ws_message, send_local_notifs},
38   LemmyContext,
39   UserOperationCrud,
40 };
41
42 #[async_trait::async_trait(?Send)]
43 impl PerformCrud for CreateComment {
44   type Response = CommentResponse;
45
46   #[tracing::instrument(skip(context, websocket_id))]
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(LemmyError::from_message("locked"));
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(LemmyError::from)
79         .map_err(|e| e.with_message("couldnt_create_comment"))?;
80
81       // Strange issue where sometimes the post ID is incorrect
82       if parent.post_id != post_id {
83         return Err(LemmyError::from_message("couldnt_create_comment"));
84       }
85     }
86
87     let comment_form = CommentForm {
88       content: content_slurs_removed,
89       parent_id: data.parent_id.to_owned(),
90       post_id: data.post_id,
91       creator_id: local_user_view.person.id,
92       ..CommentForm::default()
93     };
94
95     // Create the comment
96     let comment_form2 = comment_form.clone();
97     let inserted_comment = blocking(context.pool(), move |conn| {
98       Comment::create(conn, &comment_form2)
99     })
100     .await?
101     .map_err(LemmyError::from)
102     .map_err(|e| e.with_message("couldnt_create_comment"))?;
103
104     // Necessary to update the ap_id
105     let inserted_comment_id = inserted_comment.id;
106     let protocol_and_hostname = context.settings().get_protocol_and_hostname();
107
108     let updated_comment: Comment =
109       blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
110         let apub_id = generate_local_apub_endpoint(
111           EndpointType::Comment,
112           &inserted_comment_id.to_string(),
113           &protocol_and_hostname,
114         )?;
115         Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
116       })
117       .await?
118       .map_err(LemmyError::from)
119       .map_err(|e| e.with_message("couldnt_create_comment"))?;
120
121     // Scan the comment for user mentions, add those rows
122     let post_id = post.id;
123     let mentions = scrape_text_for_mentions(&comment_form.content);
124     let recipient_ids = send_local_notifs(
125       mentions,
126       &updated_comment,
127       &local_user_view.person,
128       &post,
129       true,
130       context,
131     )
132     .await?;
133
134     // You like your own comment by default
135     let like_form = CommentLikeForm {
136       comment_id: inserted_comment.id,
137       post_id,
138       person_id: local_user_view.person.id,
139       score: 1,
140     };
141
142     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
143     blocking(context.pool(), like)
144       .await?
145       .map_err(LemmyError::from)
146       .map_err(|e| e.with_message("couldnt_like_comment"))?;
147
148     let apub_comment: ApubComment = updated_comment.into();
149     CreateOrUpdateComment::send(
150       apub_comment.clone(),
151       &local_user_view.person.clone().into(),
152       CreateOrUpdateType::Create,
153       context,
154       &mut 0,
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(LemmyError::from)
182       .map_err(|e| e.with_message("couldnt_update_comment"))?;
183     }
184     // If its a reply, mark the parent as read
185     if let Some(parent_id) = data.parent_id {
186       let parent_comment = blocking(context.pool(), move |conn| {
187         CommentView::read(conn, parent_id, Some(person_id))
188       })
189       .await??;
190       if local_user_view.person.id == parent_comment.get_recipient_id() {
191         blocking(context.pool(), move |conn| {
192           Comment::update_read(conn, parent_id, true)
193         })
194         .await?
195         .map_err(LemmyError::from)
196         .map_err(|e| e.with_message("couldnt_update_parent_comment"))?;
197       }
198       // If the parent has PersonMentions mark them as read too
199       let person_id = local_user_view.person.id;
200       let person_mention = blocking(context.pool(), move |conn| {
201         PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
202       })
203       .await?;
204       if let Ok(mention) = person_mention {
205         blocking(context.pool(), move |conn| {
206           PersonMention::update_read(conn, mention.id, true)
207         })
208         .await?
209         .map_err(LemmyError::from)
210         .map_err(|e| e.with_message("couldnt_update_person_mentions"))?;
211       }
212     }
213
214     send_comment_ws_message(
215       inserted_comment.id,
216       UserOperationCrud::CreateComment,
217       websocket_id,
218       data.form_id.to_owned(),
219       Some(local_user_view.person.id),
220       recipient_ids,
221       context,
222     )
223     .await
224   }
225 }