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;
44 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
46 let content_slurs_removed =
47 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
49 // Check for a community ban
50 let post_id = data.post_id;
51 let post = get_post(post_id, context.pool()).await?;
52 let community_id = post.community_id;
54 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
56 check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
58 // Check if post is locked, no new comments
60 return Err(ApiError::err("locked").into());
63 // If there's a parent_id, check to make sure that comment is in that post
64 if let Some(parent_id) = data.parent_id {
65 // Make sure the parent comment exists
66 let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
68 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
70 check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
72 // Strange issue where sometimes the post ID is incorrect
73 if parent.post_id != post_id {
74 return Err(ApiError::err("couldnt_create_comment").into());
78 let comment_form = CommentForm {
79 content: content_slurs_removed,
80 parent_id: data.parent_id.to_owned(),
81 post_id: data.post_id,
82 creator_id: local_user_view.person.id,
83 ..CommentForm::default()
87 let comment_form2 = comment_form.clone();
88 let inserted_comment = blocking(context.pool(), move |conn| {
89 Comment::create(conn, &comment_form2)
92 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
94 // Necessary to update the ap_id
95 let inserted_comment_id = inserted_comment.id;
96 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
98 let updated_comment: Comment =
99 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
100 let apub_id = generate_apub_endpoint(
101 EndpointType::Comment,
102 &inserted_comment_id.to_string(),
103 &protocol_and_hostname,
105 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
108 .map_err(|_| ApiError::err("couldnt_create_comment"))?;
110 CreateOrUpdateComment::send(
112 &local_user_view.person,
113 CreateOrUpdateType::Create,
118 // Scan the comment for user mentions, add those rows
119 let post_id = post.id;
120 let mentions = scrape_text_for_mentions(&comment_form.content);
121 let recipient_ids = send_local_notifs(
123 updated_comment.clone(),
124 local_user_view.person.clone(),
132 // You like your own comment by default
133 let like_form = CommentLikeForm {
134 comment_id: inserted_comment.id,
136 person_id: local_user_view.person.id,
140 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
141 if blocking(context.pool(), like).await?.is_err() {
142 return Err(ApiError::err("couldnt_like_comment").into());
145 let object = PostOrComment::Comment(Box::new(updated_comment));
148 &local_user_view.person,
155 let person_id = local_user_view.person.id;
156 let comment_id = inserted_comment.id;
157 let comment_view = blocking(context.pool(), move |conn| {
158 CommentView::read(conn, comment_id, Some(person_id))
162 // If its a comment to yourself, mark it as read
163 if local_user_view.person.id == comment_view.get_recipient_id() {
164 let comment_id = inserted_comment.id;
165 blocking(context.pool(), move |conn| {
166 Comment::update_read(conn, comment_id, true)
169 .map_err(|_| ApiError::err("couldnt_update_comment"))?;
172 send_comment_ws_message(
174 UserOperationCrud::CreateComment,
176 data.form_id.to_owned(),
177 Some(local_user_view.person.id),