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