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