1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
8 check_post_deleted_or_removed,
10 get_local_user_view_from_jwt,
16 comment::create_or_update::CreateOrUpdateComment,
17 voting::vote::{Vote, VoteType},
20 fetcher::post_or_comment::PostOrComment,
21 generate_apub_endpoint,
24 use lemmy_db_schema::{
26 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
27 person_mention::PersonMention,
29 traits::{Crud, Likeable},
31 use lemmy_db_views::comment_view::CommentView;
33 utils::{remove_slurs, scrape_text_for_mentions},
38 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
40 #[async_trait::async_trait(?Send)]
41 impl PerformCrud for CreateComment {
42 type Response = CommentResponse;
46 context: &Data<LemmyContext>,
47 websocket_id: Option<ConnectionId>,
48 ) -> Result<CommentResponse, LemmyError> {
49 let data: &CreateComment = self;
51 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
53 let content_slurs_removed =
54 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
56 // Check for a community ban
57 let post_id = data.post_id;
58 let post = get_post(post_id, context.pool()).await?;
59 let community_id = post.community_id;
61 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
62 check_community_deleted_or_removed(community_id, context.pool()).await?;
63 check_post_deleted_or_removed(&post)?;
65 check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
67 // Check if post is locked, no new comments
69 return Err(ApiError::err_plain("locked").into());
72 // If there's a parent_id, check to make sure that comment is in that post
73 if let Some(parent_id) = data.parent_id {
74 // Make sure the parent comment exists
75 let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
77 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
79 check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
81 // Strange issue where sometimes the post ID is incorrect
82 if parent.post_id != post_id {
83 return Err(ApiError::err_plain("couldnt_create_comment").into());
87 let comment_form = CommentForm {
88 content: content_slurs_removed,
89 parent_id: data.parent_id.to_owned(),
90 post_id: data.post_id,
91 creator_id: local_user_view.person.id,
92 ..CommentForm::default()
96 let comment_form2 = comment_form.clone();
97 let inserted_comment = blocking(context.pool(), move |conn| {
98 Comment::create(conn, &comment_form2)
101 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
103 // Necessary to update the ap_id
104 let inserted_comment_id = inserted_comment.id;
105 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
107 let updated_comment: Comment =
108 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
109 let apub_id = generate_apub_endpoint(
110 EndpointType::Comment,
111 &inserted_comment_id.to_string(),
112 &protocol_and_hostname,
114 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
117 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
119 CreateOrUpdateComment::send(
121 &local_user_view.person,
122 CreateOrUpdateType::Create,
127 // Scan the comment for user mentions, add those rows
128 let post_id = post.id;
129 let mentions = scrape_text_for_mentions(&comment_form.content);
130 let recipient_ids = send_local_notifs(
132 updated_comment.clone(),
133 local_user_view.person.clone(),
141 // You like your own comment by default
142 let like_form = CommentLikeForm {
143 comment_id: inserted_comment.id,
145 person_id: local_user_view.person.id,
149 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
150 blocking(context.pool(), like)
152 .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
154 let object = PostOrComment::Comment(updated_comment);
157 &local_user_view.person,
164 let person_id = local_user_view.person.id;
165 let comment_id = inserted_comment.id;
166 let comment_view = blocking(context.pool(), move |conn| {
167 CommentView::read(conn, comment_id, Some(person_id))
171 // If its a comment to yourself, mark it as read
172 if local_user_view.person.id == comment_view.get_recipient_id() {
173 let comment_id = inserted_comment.id;
174 blocking(context.pool(), move |conn| {
175 Comment::update_read(conn, comment_id, true)
178 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
180 // If its a reply, mark the parent as read
181 if let Some(parent_id) = data.parent_id {
182 let parent_comment = blocking(context.pool(), move |conn| {
183 CommentView::read(conn, parent_id, Some(person_id))
186 if local_user_view.person.id == parent_comment.get_recipient_id() {
187 blocking(context.pool(), move |conn| {
188 Comment::update_read(conn, parent_id, true)
191 .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
193 // If the parent has PersonMentions mark them as read too
194 let person_id = local_user_view.person.id;
195 let person_mention = blocking(context.pool(), move |conn| {
196 PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
199 if let Ok(mention) = person_mention {
200 blocking(context.pool(), move |conn| {
201 PersonMention::update_read(conn, mention.id, true)
204 .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
208 send_comment_ws_message(
210 UserOperationCrud::CreateComment,
212 data.form_id.to_owned(),
213 Some(local_user_view.person.id),