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