use crate::PerformCrud;
use actix_web::web::Data;
use lemmy_api_common::{
- blocking,
- check_community_ban,
- check_community_deleted_or_removed,
- check_post_deleted_or_removed,
- comment::*,
- get_local_user_view_from_jwt,
- get_post,
+ comment::{CommentResponse, CreateComment},
+ utils::{
+ blocking,
+ check_community_ban,
+ check_community_deleted_or_removed,
+ check_post_deleted_or_removed,
+ get_local_user_view_from_jwt,
+ get_post,
+ local_site_to_slur_regex,
+ },
};
use lemmy_apub::{
generate_local_apub_endpoint,
};
use lemmy_db_schema::{
source::{
- comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
- person_mention::PersonMention,
+ actor_language::CommunityLanguage,
+ comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
+ comment_reply::{CommentReply, CommentReplyUpdateForm},
+ local_site::LocalSite,
+ person_mention::{PersonMention, PersonMentionUpdateForm},
},
traits::{Crud, Likeable},
};
-use lemmy_db_views::comment_view::CommentView;
use lemmy_utils::{
+ error::LemmyError,
utils::{remove_slurs, scrape_text_for_mentions},
ConnectionId,
- LemmyError,
};
use lemmy_websocket::{
send::{send_comment_ws_message, send_local_notifs},
let data: &CreateComment = self;
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
+ let local_site = blocking(context.pool(), LocalSite::read).await??;
- let content_slurs_removed =
- remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
+ let content_slurs_removed = remove_slurs(
+ &data.content.to_owned(),
+ &local_site_to_slur_regex(&local_site),
+ );
// Check for a community ban
let post_id = data.post_id;
return Err(LemmyError::from_message("locked"));
}
- // If there's a parent_id, check to make sure that comment is in that post
- if let Some(parent_id) = data.parent_id {
- // Make sure the parent comment exists
- let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
+ // Fetch the parent, if it exists
+ let parent_opt = if let Some(parent_id) = data.parent_id {
+ blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
.await?
- .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
+ .ok()
+ } else {
+ None
+ };
- // Strange issue where sometimes the post ID is incorrect
+ // If there's a parent_id, check to make sure that comment is in that post
+ // Strange issue where sometimes the post ID of the parent comment is incorrect
+ if let Some(parent) = parent_opt.as_ref() {
if parent.post_id != post_id {
return Err(LemmyError::from_message("couldnt_create_comment"));
}
}
- let comment_form = CommentForm {
- content: content_slurs_removed,
- parent_id: data.parent_id.to_owned(),
- post_id: data.post_id,
- creator_id: local_user_view.person.id,
- ..CommentForm::default()
- };
+ // if no language is set, copy language from parent post/comment
+ let parent_language = parent_opt
+ .as_ref()
+ .map(|p| p.language_id)
+ .unwrap_or(post.language_id);
+ let language_id = data.language_id.unwrap_or(parent_language);
+
+ blocking(context.pool(), move |conn| {
+ CommunityLanguage::is_allowed_community_language(conn, Some(language_id), community_id)
+ })
+ .await??;
+
+ let comment_form = CommentInsertForm::builder()
+ .content(content_slurs_removed.to_owned())
+ .post_id(data.post_id)
+ .creator_id(local_user_view.person.id)
+ .language_id(Some(language_id))
+ .build();
// Create the comment
let comment_form2 = comment_form.clone();
+ let parent_path = parent_opt.to_owned().map(|t| t.path);
let inserted_comment = blocking(context.pool(), move |conn| {
- Comment::create(conn, &comment_form2)
+ Comment::create(conn, &comment_form2, parent_path.as_ref())
})
.await?
.map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
&inserted_comment_id.to_string(),
&protocol_and_hostname,
)?;
- Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
+ Ok(Comment::update(
+ conn,
+ inserted_comment_id,
+ &CommentUpdateForm::builder().ap_id(Some(apub_id)).build(),
+ )?)
})
.await?
.map_err(|e| e.with_message("couldnt_create_comment"))?;
// Scan the comment for user mentions, add those rows
let post_id = post.id;
- let mentions = scrape_text_for_mentions(&comment_form.content);
+ let mentions = scrape_text_for_mentions(&content_slurs_removed);
let recipient_ids = send_local_notifs(
mentions,
&updated_comment,
score: 1,
};
- let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
+ let like = move |conn: &mut _| CommentLike::like(conn, &like_form);
blocking(context.pool(), like)
.await?
.map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
)
.await?;
- let person_id = local_user_view.person.id;
- let comment_id = inserted_comment.id;
- let comment_view = blocking(context.pool(), move |conn| {
- CommentView::read(conn, comment_id, Some(person_id))
- })
- .await??;
-
- // If its a comment to yourself, mark it as read
- if local_user_view.person.id == comment_view.get_recipient_id() {
- let comment_id = inserted_comment.id;
- blocking(context.pool(), move |conn| {
- Comment::update_read(conn, comment_id, true)
- })
- .await?
- .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
- }
// If its a reply, mark the parent as read
- if let Some(parent_id) = data.parent_id {
- let parent_comment = blocking(context.pool(), move |conn| {
- CommentView::read(conn, parent_id, Some(person_id))
+ if let Some(parent) = parent_opt {
+ let parent_id = parent.id;
+ let comment_reply = blocking(context.pool(), move |conn| {
+ CommentReply::read_by_comment(conn, parent_id)
})
- .await??;
- if local_user_view.person.id == parent_comment.get_recipient_id() {
+ .await?;
+ if let Ok(reply) = comment_reply {
blocking(context.pool(), move |conn| {
- Comment::update_read(conn, parent_id, true)
+ CommentReply::update(conn, reply.id, &CommentReplyUpdateForm { read: Some(true) })
})
.await?
- .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_parent_comment"))?;
+ .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_replies"))?;
}
+
// If the parent has PersonMentions mark them as read too
let person_id = local_user_view.person.id;
let person_mention = blocking(context.pool(), move |conn| {
.await?;
if let Ok(mention) = person_mention {
blocking(context.pool(), move |conn| {
- PersonMention::update_read(conn, mention.id, true)
+ PersonMention::update(
+ conn,
+ mention.id,
+ &PersonMentionUpdateForm { read: Some(true) },
+ )
})
.await?
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_person_mentions"))?;