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