1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
8 get_local_user_view_from_jwt,
14 comment::create_or_update::CreateOrUpdateComment,
15 voting::vote::{Vote, VoteType},
18 fetcher::post_or_comment::PostOrComment,
19 generate_apub_endpoint,
22 use lemmy_db_queries::{source::comment::Comment_, Crud, Likeable};
23 use lemmy_db_schema::source::comment::*;
24 use lemmy_db_views::comment_view::CommentView;
26 utils::{remove_slurs, scrape_text_for_mentions},
31 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
33 #[async_trait::async_trait(?Send)]
34 impl PerformCrud for CreateComment {
35 type Response = CommentResponse;
39 context: &Data<LemmyContext>,
40 websocket_id: Option<ConnectionId>,
41 ) -> Result<CommentResponse, LemmyError> {
42 let data: &CreateComment = self;
43 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
45 let content_slurs_removed = remove_slurs(&data.content.to_owned());
47 // Check for a community ban
48 let post_id = data.post_id;
49 let post = get_post(post_id, context.pool()).await?;
50 let community_id = post.community_id;
52 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
54 check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
56 // Check if post is locked, no new comments
58 return Err(ApiError::err("locked").into());
61 // If there's a parent_id, check to make sure that comment is in that post
62 if let Some(parent_id) = data.parent_id {
63 // Make sure the parent comment exists
64 let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
66 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
68 check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
70 // Strange issue where sometimes the post ID is incorrect
71 if parent.post_id != post_id {
72 return Err(ApiError::err("couldnt_create_comment").into());
76 let comment_form = CommentForm {
77 content: content_slurs_removed,
78 parent_id: data.parent_id.to_owned(),
79 post_id: data.post_id,
80 creator_id: local_user_view.person.id,
81 ..CommentForm::default()
85 let comment_form2 = comment_form.clone();
86 let inserted_comment = blocking(context.pool(), move |conn| {
87 Comment::create(conn, &comment_form2)
90 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
92 // Necessary to update the ap_id
93 let inserted_comment_id = inserted_comment.id;
94 let updated_comment: Comment =
95 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
97 generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?;
98 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
101 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
103 CreateOrUpdateComment::send(
105 &local_user_view.person,
106 CreateOrUpdateType::Create,
111 // Scan the comment for user mentions, add those rows
112 let post_id = post.id;
113 let mentions = scrape_text_for_mentions(&comment_form.content);
114 let recipient_ids = send_local_notifs(
116 updated_comment.clone(),
117 local_user_view.person.clone(),
124 // You like your own comment by default
125 let like_form = CommentLikeForm {
126 comment_id: inserted_comment.id,
128 person_id: local_user_view.person.id,
132 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
133 if blocking(context.pool(), like).await?.is_err() {
134 return Err(ApiError::err("couldnt_like_comment").into());
137 let object = PostOrComment::Comment(Box::new(updated_comment));
140 &local_user_view.person,
147 let person_id = local_user_view.person.id;
148 let comment_id = inserted_comment.id;
149 let comment_view = blocking(context.pool(), move |conn| {
150 CommentView::read(conn, comment_id, Some(person_id))
154 // If its a comment to yourself, mark it as read
155 if local_user_view.person.id == comment_view.get_recipient_id() {
156 let comment_id = inserted_comment.id;
157 blocking(context.pool(), move |conn| {
158 Comment::update_read(conn, comment_id, true)
161 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
164 send_comment_ws_message(
166 UserOperationCrud::CreateComment,
168 data.form_id.to_owned(),
169 Some(local_user_view.person.id),