]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/create.rs
Merge pull request #1677 from LemmyNet/remove-fat-deps
[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_or_update::CreateOrUpdateComment, CreateOrUpdateType},
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     CreateOrUpdateComment::send(
92       &updated_comment,
93       &local_user_view.person,
94       CreateOrUpdateType::Create,
95       context,
96     )
97     .await?;
98
99     // Scan the comment for user mentions, add those rows
100     let post_id = post.id;
101     let mentions = scrape_text_for_mentions(&comment_form.content);
102     let recipient_ids = send_local_notifs(
103       mentions,
104       updated_comment.clone(),
105       local_user_view.person.clone(),
106       post,
107       context.pool(),
108       true,
109     )
110     .await?;
111
112     // You like your own comment by default
113     let like_form = CommentLikeForm {
114       comment_id: inserted_comment.id,
115       post_id,
116       person_id: local_user_view.person.id,
117       score: 1,
118     };
119
120     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
121     if blocking(context.pool(), like).await?.is_err() {
122       return Err(ApiError::err("couldnt_like_comment").into());
123     }
124
125     updated_comment
126       .send_like(&local_user_view.person, context)
127       .await?;
128
129     let person_id = local_user_view.person.id;
130     let mut comment_view = blocking(context.pool(), move |conn| {
131       CommentView::read(conn, inserted_comment.id, Some(person_id))
132     })
133     .await??;
134
135     // If its a comment to yourself, mark it as read
136     let comment_id = comment_view.comment.id;
137     if local_user_view.person.id == comment_view.get_recipient_id() {
138       blocking(context.pool(), move |conn| {
139         Comment::update_read(conn, comment_id, true)
140       })
141       .await?
142       .map_err(|_| ApiError::err("couldnt_update_comment"))?;
143       comment_view.comment.read = true;
144     }
145
146     let mut res = CommentResponse {
147       comment_view,
148       recipient_ids,
149       form_id: data.form_id.to_owned(),
150     };
151
152     context.chat_server().do_send(SendComment {
153       op: UserOperationCrud::CreateComment,
154       comment: res.clone(),
155       websocket_id,
156     });
157
158     res.recipient_ids = Vec::new(); // Necessary to avoid doubles
159
160     Ok(res)
161   }
162 }