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_queries::{
25 source::{comment::Comment_, person_mention::PersonMention_},
29 use lemmy_db_schema::source::{comment::*, person_mention::PersonMention};
30 use lemmy_db_views::comment_view::CommentView;
32 utils::{remove_slurs, scrape_text_for_mentions},
37 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
39 #[async_trait::async_trait(?Send)]
40 impl PerformCrud for CreateComment {
41 type Response = CommentResponse;
45 context: &Data<LemmyContext>,
46 websocket_id: Option<ConnectionId>,
47 ) -> Result<CommentResponse, LemmyError> {
48 let data: &CreateComment = self;
50 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
52 let content_slurs_removed =
53 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
55 // Check for a community ban
56 let post_id = data.post_id;
57 let post = get_post(post_id, context.pool()).await?;
58 let community_id = post.community_id;
60 check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
61 check_community_deleted_or_removed(community_id, context.pool()).await?;
62 check_post_deleted_or_removed(&post)?;
64 check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;
66 // Check if post is locked, no new comments
68 return Err(ApiError::err_plain("locked").into());
71 // If there's a parent_id, check to make sure that comment is in that post
72 if let Some(parent_id) = data.parent_id {
73 // Make sure the parent comment exists
74 let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
76 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
78 check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
80 // Strange issue where sometimes the post ID is incorrect
81 if parent.post_id != post_id {
82 return Err(ApiError::err_plain("couldnt_create_comment").into());
86 let comment_form = CommentForm {
87 content: content_slurs_removed,
88 parent_id: data.parent_id.to_owned(),
89 post_id: data.post_id,
90 creator_id: local_user_view.person.id,
91 ..CommentForm::default()
95 let comment_form2 = comment_form.clone();
96 let inserted_comment = blocking(context.pool(), move |conn| {
97 Comment::create(conn, &comment_form2)
100 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
102 // Necessary to update the ap_id
103 let inserted_comment_id = inserted_comment.id;
104 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
106 let updated_comment: Comment =
107 blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
108 let apub_id = generate_apub_endpoint(
109 EndpointType::Comment,
110 &inserted_comment_id.to_string(),
111 &protocol_and_hostname,
113 Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
116 .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
118 CreateOrUpdateComment::send(
120 &local_user_view.person,
121 CreateOrUpdateType::Create,
126 // Scan the comment for user mentions, add those rows
127 let post_id = post.id;
128 let mentions = scrape_text_for_mentions(&comment_form.content);
129 let recipient_ids = send_local_notifs(
131 updated_comment.clone(),
132 local_user_view.person.clone(),
140 // You like your own comment by default
141 let like_form = CommentLikeForm {
142 comment_id: inserted_comment.id,
144 person_id: local_user_view.person.id,
148 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
149 blocking(context.pool(), like)
151 .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
153 let object = PostOrComment::Comment(updated_comment);
156 &local_user_view.person,
163 let person_id = local_user_view.person.id;
164 let comment_id = inserted_comment.id;
165 let comment_view = blocking(context.pool(), move |conn| {
166 CommentView::read(conn, comment_id, Some(person_id))
170 // If its a comment to yourself, mark it as read
171 if local_user_view.person.id == comment_view.get_recipient_id() {
172 let comment_id = inserted_comment.id;
173 blocking(context.pool(), move |conn| {
174 Comment::update_read(conn, comment_id, true)
177 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
179 // If its a reply, mark the parent as read
180 if let Some(parent_id) = data.parent_id {
181 let parent_comment = blocking(context.pool(), move |conn| {
182 CommentView::read(conn, parent_id, Some(person_id))
185 if local_user_view.person.id == parent_comment.get_recipient_id() {
186 blocking(context.pool(), move |conn| {
187 Comment::update_read(conn, parent_id, true)
190 .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
192 // If the parent has PersonMentions mark them as read too
193 let person_id = local_user_view.person.id;
194 let person_mention = blocking(context.pool(), move |conn| {
195 PersonMention::read_by_comment_and_person(conn, parent_id, person_id)
198 if let Ok(mention) = person_mention {
199 blocking(context.pool(), move |conn| {
200 PersonMention::update_read(conn, mention.id, true)
203 .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
207 send_comment_ws_message(
209 UserOperationCrud::CreateComment,
211 data.form_id.to_owned(),
212 Some(local_user_view.person.id),