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,
15 comment::create_or_update::CreateOrUpdateComment,
16 voting::vote::{Vote, VoteType},
19 fetcher::post_or_comment::PostOrComment,
20 generate_apub_endpoint,
23 use lemmy_db_schema::{
25 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
26 person_mention::PersonMention,
28 traits::{Crud, Likeable},
30 use lemmy_db_views::comment_view::CommentView;
32 utils::{remove_slurs, scrape_text_for_mentions},
37 use lemmy_websocket::{
38 send::{send_comment_ws_message, send_local_notifs},
43 #[async_trait::async_trait(?Send)]
44 impl PerformCrud for CreateComment {
45 type Response = CommentResponse;
49 context: &Data<LemmyContext>,
50 websocket_id: Option<ConnectionId>,
51 ) -> Result<CommentResponse, LemmyError> {
52 let data: &CreateComment = self;
54 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
56 let content_slurs_removed =
57 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
59 // Check for a community ban
60 let post_id = data.post_id;
61 let post = get_post(post_id, context.pool()).await?;
62 let community_id = post.community_id;
64 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
65 check_community_deleted_or_removed(community_id, context.pool()).await?;
66 check_post_deleted_or_removed(&post)?;
68 check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
70 // Check if post is locked, no new comments
72 return Err(ApiError::err_plain("locked").into());
75 // If there's a parent_id, check to make sure that comment is in that post
76 if let Some(parent_id) = data.parent_id {
77 // Make sure the parent comment exists
78 let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
80 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
82 check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
84 // Strange issue where sometimes the post ID is incorrect
85 if parent.post_id != post_id {
86 return Err(ApiError::err_plain("couldnt_create_comment").into());
90 let comment_form = CommentForm {
91 content: content_slurs_removed,
92 parent_id: data.parent_id.to_owned(),
93 post_id: data.post_id,
94 creator_id: local_user_view.person.id,
95 ..CommentForm::default()
99 let comment_form2 = comment_form.clone();
100 let inserted_comment = blocking(context.pool(), move |conn| {
101 Comment::create(conn, &comment_form2)
104 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
106 // Necessary to update the ap_id
107 let inserted_comment_id = inserted_comment.id;
108 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
110 let updated_comment: Comment =
111 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
112 let apub_id = generate_apub_endpoint(
113 EndpointType::Comment,
114 &inserted_comment_id.to_string(),
115 &protocol_and_hostname,
117 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
120 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
122 CreateOrUpdateComment::send(
123 &updated_comment.clone().into(),
124 &local_user_view.person.clone().into(),
125 CreateOrUpdateType::Create,
130 // Scan the comment for user mentions, add those rows
131 let post_id = post.id;
132 let mentions = scrape_text_for_mentions(&comment_form.content);
133 let recipient_ids = send_local_notifs(
136 &local_user_view.person,
143 // You like your own comment by default
144 let like_form = CommentLikeForm {
145 comment_id: inserted_comment.id,
147 person_id: local_user_view.person.id,
151 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
152 blocking(context.pool(), like)
154 .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
156 let object = PostOrComment::Comment(updated_comment.into());
159 &local_user_view.person.clone().into(),
166 let person_id = local_user_view.person.id;
167 let comment_id = inserted_comment.id;
168 let comment_view = blocking(context.pool(), move |conn| {
169 CommentView::read(conn, comment_id, Some(person_id))
173 // If its a comment to yourself, mark it as read
174 if local_user_view.person.id == comment_view.get_recipient_id() {
175 let comment_id = inserted_comment.id;
176 blocking(context.pool(), move |conn| {
177 Comment::update_read(conn, comment_id, true)
180 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
182 // If its a reply, mark the parent as read
183 if let Some(parent_id) = data.parent_id {
184 let parent_comment = blocking(context.pool(), move |conn| {
185 CommentView::read(conn, parent_id, Some(person_id))
188 if local_user_view.person.id == parent_comment.get_recipient_id() {
189 blocking(context.pool(), move |conn| {
190 Comment::update_read(conn, parent_id, true)
193 .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
195 // If the parent has PersonMentions mark them as read too
196 let person_id = local_user_view.person.id;
197 let person_mention = blocking(context.pool(), move |conn| {
198 PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
201 if let Ok(mention) = person_mention {
202 blocking(context.pool(), move |conn| {
203 PersonMention::update_read(conn, mention.id, true)
206 .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
210 send_comment_ws_message(
212 UserOperationCrud::CreateComment,
214 data.form_id.to_owned(),
215 Some(local_user_view.person.id),