]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/create.rs
Fixing some comment websocket issues. (#1768)
[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   generate_apub_endpoint,
19   EndpointType,
20   PostOrComment,
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 = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
44
45     let content_slurs_removed = remove_slurs(&data.content.to_owned());
46
47     // Check for a community ban
48     let post_id = data.post_id;
49     let post = get_post(post_id, context.pool()).await?;
50     let community_id = post.community_id;
51
52     check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
53
54     check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
55
56     // Check if post is locked, no new comments
57     if post.locked {
58       return Err(ApiError::err("locked").into());
59     }
60
61     // If there's a parent_id, check to make sure that comment is in that post
62     if let Some(parent_id) = data.parent_id {
63       // Make sure the parent comment exists
64       let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
65         .await?
66         .map_err(|_| ApiError::err("couldnt_create_comment"))?;
67
68       check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
69
70       // Strange issue where sometimes the post ID is incorrect
71       if parent.post_id != post_id {
72         return Err(ApiError::err("couldnt_create_comment").into());
73       }
74     }
75
76     let comment_form = CommentForm {
77       content: content_slurs_removed,
78       parent_id: data.parent_id.to_owned(),
79       post_id: data.post_id,
80       creator_id: local_user_view.person.id,
81       ..CommentForm::default()
82     };
83
84     // Create the comment
85     let comment_form2 = comment_form.clone();
86     let inserted_comment = blocking(context.pool(), move |conn| {
87       Comment::create(conn, &comment_form2)
88     })
89     .await?
90     .map_err(|_| ApiError::err("couldnt_create_comment"))?;
91
92     // Necessary to update the ap_id
93     let inserted_comment_id = inserted_comment.id;
94     let updated_comment: Comment =
95       blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
96         let apub_id =
97           generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?;
98         Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
99       })
100       .await?
101       .map_err(|_| ApiError::err("couldnt_create_comment"))?;
102
103     CreateOrUpdateComment::send(
104       &updated_comment,
105       &local_user_view.person,
106       CreateOrUpdateType::Create,
107       context,
108     )
109     .await?;
110
111     // Scan the comment for user mentions, add those rows
112     let post_id = post.id;
113     let mentions = scrape_text_for_mentions(&comment_form.content);
114     let recipient_ids = send_local_notifs(
115       mentions,
116       updated_comment.clone(),
117       local_user_view.person.clone(),
118       post,
119       context.pool(),
120       true,
121     )
122     .await?;
123
124     // You like your own comment by default
125     let like_form = CommentLikeForm {
126       comment_id: inserted_comment.id,
127       post_id,
128       person_id: local_user_view.person.id,
129       score: 1,
130     };
131
132     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
133     if blocking(context.pool(), like).await?.is_err() {
134       return Err(ApiError::err("couldnt_like_comment").into());
135     }
136
137     let object = PostOrComment::Comment(Box::new(updated_comment));
138     Vote::send(
139       &object,
140       &local_user_view.person,
141       community_id,
142       VoteType::Like,
143       context,
144     )
145     .await?;
146
147     let person_id = local_user_view.person.id;
148     let comment_id = inserted_comment.id;
149     let comment_view = blocking(context.pool(), move |conn| {
150       CommentView::read(conn, comment_id, Some(person_id))
151     })
152     .await??;
153
154     // If its a comment to yourself, mark it as read
155     if local_user_view.person.id == comment_view.get_recipient_id() {
156       let comment_id = inserted_comment.id;
157       blocking(context.pool(), move |conn| {
158         Comment::update_read(conn, comment_id, true)
159       })
160       .await?
161       .map_err(|_| ApiError::err("couldnt_update_comment"))?;
162     }
163
164     send_comment_ws_message(
165       inserted_comment.id,
166       UserOperationCrud::CreateComment,
167       websocket_id,
168       data.form_id.to_owned(),
169       Some(local_user_view.person.id),
170       recipient_ids,
171       context,
172     )
173     .await
174   }
175 }