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