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