1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
7 get_local_user_view_from_jwt,
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;
16 utils::{remove_slurs, scrape_text_for_mentions},
21 use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperationCrud};
23 #[async_trait::async_trait(?Send)]
24 impl PerformCrud for CreateComment {
25 type Response = CommentResponse;
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?;
35 let content_slurs_removed = remove_slurs(&data.content.to_owned());
37 // Check for a community ban
38 let post_id = data.post_id;
39 let post = get_post(post_id, context.pool()).await?;
41 check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
43 // Check if post is locked, no new comments
45 return Err(ApiError::err("locked").into());
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
52 match blocking(context.pool(), move |conn| Comment::read(&conn, parent_id)).await? {
53 Ok(comment) => comment,
54 Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
56 if parent.post_id != post_id {
57 return Err(ApiError::err("couldnt_create_comment").into());
61 let comment_form = CommentForm {
62 content: content_slurs_removed,
63 parent_id: data.parent_id.to_owned(),
64 post_id: data.post_id,
65 creator_id: local_user_view.person.id,
66 ..CommentForm::default()
70 let comment_form2 = comment_form.clone();
71 let inserted_comment = match blocking(context.pool(), move |conn| {
72 Comment::create(&conn, &comment_form2)
76 Ok(comment) => comment,
77 Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
80 // Necessary to update the ap_id
81 let inserted_comment_id = inserted_comment.id;
82 let updated_comment: Comment =
83 match blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
85 generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?;
86 Ok(Comment::update_ap_id(&conn, inserted_comment_id, apub_id)?)
90 Ok(comment) => comment,
91 Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
95 .send_create(&local_user_view.person, context)
98 // Scan the comment for user mentions, add those rows
99 let post_id = post.id;
100 let mentions = scrape_text_for_mentions(&comment_form.content);
101 let recipient_ids = send_local_notifs(
103 updated_comment.clone(),
104 local_user_view.person.clone(),
111 // You like your own comment by default
112 let like_form = CommentLikeForm {
113 comment_id: inserted_comment.id,
115 person_id: local_user_view.person.id,
119 let like = move |conn: &'_ _| CommentLike::like(&conn, &like_form);
120 if blocking(context.pool(), like).await?.is_err() {
121 return Err(ApiError::err("couldnt_like_comment").into());
125 .send_like(&local_user_view.person, context)
128 let person_id = local_user_view.person.id;
129 let mut comment_view = blocking(context.pool(), move |conn| {
130 CommentView::read(&conn, inserted_comment.id, Some(person_id))
134 // If its a comment to yourself, mark it as read
135 let comment_id = comment_view.comment.id;
136 if local_user_view.person.id == comment_view.get_recipient_id() {
137 match blocking(context.pool(), move |conn| {
138 Comment::update_read(conn, comment_id, true)
142 Ok(comment) => comment,
143 Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
145 comment_view.comment.read = true;
148 let mut res = CommentResponse {
151 form_id: data.form_id.to_owned(),
154 context.chat_server().do_send(SendComment {
155 op: UserOperationCrud::CreateComment,
156 comment: res.clone(),
160 res.recipient_ids = Vec::new(); // Necessary to avoid doubles