Dont show replies / mentions from blocked users. Fixes #4227 (#4727)

* Dont show replies / mentions from blocked users. Fixes #4227

* Adding unit tests for reply and mention views.

- Also cleaned up some unwraps in the tests.

* Add allow deprecated to pass clippy for deprecated wav crate.

---------

Co-authored-by: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com>
This commit is contained in:
Dessalines 2024-05-22 08:50:26 -04:00 committed by GitHub
parent 943c31cc72
commit 7d7cd8ded4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 1107 additions and 976 deletions

1460
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -44,6 +44,7 @@ pub mod site;
pub mod sitemap;
/// Converts the captcha to a base64 encoded wav audio file
#[allow(deprecated)]
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> LemmyResult<String> {
let letters = captcha.as_wav();

View file

@ -87,117 +87,3 @@ impl CommentReply {
.optional()
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::indexing_slicing)]
mod tests {
use crate::{
source::{
comment::{Comment, CommentInsertForm},
comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
community::{Community, CommunityInsertForm},
instance::Instance,
person::{Person, PersonInsertForm},
post::{Post, PostInsertForm},
},
traits::Crud,
utils::build_db_pool_for_tests,
};
use pretty_assertions::assert_eq;
use serial_test::serial;
#[tokio::test]
#[serial]
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
.await
.unwrap();
let new_person = PersonInsertForm::builder()
.name("terrylake".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(pool, &new_person).await.unwrap();
let recipient_form = PersonInsertForm::builder()
.name("terrylakes recipient".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community lake".to_string())
.title("nada".to_owned())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(pool, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
.creator_id(inserted_person.id)
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(pool, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
.creator_id(inserted_person.id)
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
let comment_reply_form = CommentReplyInsertForm {
recipient_id: inserted_recipient.id,
comment_id: inserted_comment.id,
read: None,
};
let inserted_reply = CommentReply::create(pool, &comment_reply_form)
.await
.unwrap();
let expected_reply = CommentReply {
id: inserted_reply.id,
recipient_id: inserted_reply.recipient_id,
comment_id: inserted_reply.comment_id,
read: false,
published: inserted_reply.published,
};
let read_reply = CommentReply::read(pool, inserted_reply.id)
.await
.unwrap()
.unwrap();
let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
let updated_reply = CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form)
.await
.unwrap();
Comment::delete(pool, inserted_comment.id).await.unwrap();
Post::delete(pool, inserted_post.id).await.unwrap();
Community::delete(pool, inserted_community.id)
.await
.unwrap();
Person::delete(pool, inserted_person.id).await.unwrap();
Person::delete(pool, inserted_recipient.id).await.unwrap();
Instance::delete(pool, inserted_instance.id).await.unwrap();
assert_eq!(expected_reply, read_reply);
assert_eq!(expected_reply, inserted_reply);
assert_eq!(expected_reply, updated_reply);
}
}

View file

@ -74,117 +74,3 @@ impl PersonMention {
.optional()
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::indexing_slicing)]
mod tests {
use crate::{
source::{
comment::{Comment, CommentInsertForm},
community::{Community, CommunityInsertForm},
instance::Instance,
person::{Person, PersonInsertForm},
person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
post::{Post, PostInsertForm},
},
traits::Crud,
utils::build_db_pool_for_tests,
};
use pretty_assertions::assert_eq;
use serial_test::serial;
#[tokio::test]
#[serial]
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
.await
.unwrap();
let new_person = PersonInsertForm::builder()
.name("terrylake".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(pool, &new_person).await.unwrap();
let recipient_form = PersonInsertForm::builder()
.name("terrylakes recipient".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_recipient = Person::create(pool, &recipient_form).await.unwrap();
let new_community = CommunityInsertForm::builder()
.name("test community lake".to_string())
.title("nada".to_owned())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(pool, &new_community).await.unwrap();
let new_post = PostInsertForm::builder()
.name("A test post".into())
.creator_id(inserted_person.id)
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(pool, &new_post).await.unwrap();
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
.creator_id(inserted_person.id)
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
let person_mention_form = PersonMentionInsertForm {
recipient_id: inserted_recipient.id,
comment_id: inserted_comment.id,
read: None,
};
let inserted_mention = PersonMention::create(pool, &person_mention_form)
.await
.unwrap();
let expected_mention = PersonMention {
id: inserted_mention.id,
recipient_id: inserted_mention.recipient_id,
comment_id: inserted_mention.comment_id,
read: false,
published: inserted_mention.published,
};
let read_mention = PersonMention::read(pool, inserted_mention.id)
.await
.unwrap()
.unwrap();
let person_mention_update_form = PersonMentionUpdateForm { read: Some(false) };
let updated_mention =
PersonMention::update(pool, inserted_mention.id, &person_mention_update_form)
.await
.unwrap();
Comment::delete(pool, inserted_comment.id).await.unwrap();
Post::delete(pool, inserted_post.id).await.unwrap();
Community::delete(pool, inserted_community.id)
.await
.unwrap();
Person::delete(pool, inserted_person.id).await.unwrap();
Person::delete(pool, inserted_recipient.id).await.unwrap();
Instance::delete(pool, inserted_instance.id).await.unwrap();
assert_eq!(expected_mention, read_mention);
assert_eq!(expected_mention, inserted_mention);
assert_eq!(expected_mention, updated_mention);
}
}

View file

@ -437,7 +437,6 @@ impl<'a> CommentQuery<'a> {
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::indexing_slicing)]
mod tests {
@ -498,32 +497,28 @@ mod tests {
inserted_community: Community,
}
async fn init_data(pool: &mut DbPool<'_>) -> Data {
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
.await
.unwrap();
async fn init_data(pool: &mut DbPool<'_>) -> LemmyResult<Data> {
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let timmy_person_form = PersonInsertForm::builder()
.name("timmy".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_timmy_person = Person::create(pool, &timmy_person_form).await.unwrap();
let inserted_timmy_person = Person::create(pool, &timmy_person_form).await?;
let timmy_local_user_form = LocalUserInsertForm::builder()
.person_id(inserted_timmy_person.id)
.admin(Some(true))
.password_encrypted(String::new())
.build();
let inserted_timmy_local_user = LocalUser::create(pool, &timmy_local_user_form, vec![])
.await
.unwrap();
let inserted_timmy_local_user = LocalUser::create(pool, &timmy_local_user_form, vec![]).await?;
let sara_person_form = PersonInsertForm::builder()
.name("sara".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_sara_person = Person::create(pool, &sara_person_form).await.unwrap();
let inserted_sara_person = Person::create(pool, &sara_person_form).await?;
let new_community = CommunityInsertForm::builder()
.name("test community 5".to_string())
@ -532,7 +527,7 @@ mod tests {
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(pool, &new_community).await.unwrap();
let inserted_community = Community::create(pool, &new_community).await?;
let new_post = PostInsertForm::builder()
.name("A test post 2".into())
@ -540,8 +535,8 @@ mod tests {
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(pool, &new_post).await.unwrap();
let english_id = Language::read_id_from_code(pool, Some("en")).await.unwrap();
let inserted_post = Post::create(pool, &new_post).await?;
let english_id = Language::read_id_from_code(pool, Some("en")).await?;
// Create a comment tree with this hierarchy
// 0
@ -558,7 +553,7 @@ mod tests {
.language_id(english_id)
.build();
let inserted_comment_0 = Comment::create(pool, &comment_form_0, None).await.unwrap();
let inserted_comment_0 = Comment::create(pool, &comment_form_0, None).await?;
let comment_form_1 = CommentInsertForm::builder()
.content("Comment 1, A test blocked comment".into())
@ -567,11 +562,10 @@ mod tests {
.language_id(english_id)
.build();
let inserted_comment_1 = Comment::create(pool, &comment_form_1, Some(&inserted_comment_0.path))
.await
.unwrap();
let inserted_comment_1 =
Comment::create(pool, &comment_form_1, Some(&inserted_comment_0.path)).await?;
let finnish_id = Language::read_id_from_code(pool, Some("fi")).await.unwrap();
let finnish_id = Language::read_id_from_code(pool, Some("fi")).await?;
let comment_form_2 = CommentInsertForm::builder()
.content("Comment 2".into())
.creator_id(inserted_timmy_person.id)
@ -579,9 +573,8 @@ mod tests {
.language_id(finnish_id)
.build();
let inserted_comment_2 = Comment::create(pool, &comment_form_2, Some(&inserted_comment_0.path))
.await
.unwrap();
let inserted_comment_2 =
Comment::create(pool, &comment_form_2, Some(&inserted_comment_0.path)).await?;
let comment_form_3 = CommentInsertForm::builder()
.content("Comment 3".into())
@ -591,14 +584,11 @@ mod tests {
.build();
let _inserted_comment_3 =
Comment::create(pool, &comment_form_3, Some(&inserted_comment_1.path))
.await
.unwrap();
Comment::create(pool, &comment_form_3, Some(&inserted_comment_1.path)).await?;
let polish_id = Language::read_id_from_code(pool, Some("pl"))
.await
.unwrap()
.unwrap();
.await?
.ok_or(LemmyErrorType::LanguageNotAllowed)?;
let comment_form_4 = CommentInsertForm::builder()
.content("Comment 4".into())
.creator_id(inserted_timmy_person.id)
@ -606,9 +596,8 @@ mod tests {
.language_id(Some(polish_id))
.build();
let inserted_comment_4 = Comment::create(pool, &comment_form_4, Some(&inserted_comment_1.path))
.await
.unwrap();
let inserted_comment_4 =
Comment::create(pool, &comment_form_4, Some(&inserted_comment_1.path)).await?;
let comment_form_5 = CommentInsertForm::builder()
.content("Comment 5".into())
@ -617,18 +606,14 @@ mod tests {
.build();
let _inserted_comment_5 =
Comment::create(pool, &comment_form_5, Some(&inserted_comment_4.path))
.await
.unwrap();
Comment::create(pool, &comment_form_5, Some(&inserted_comment_4.path)).await?;
let timmy_blocks_sara_form = PersonBlockForm {
person_id: inserted_timmy_person.id,
target_id: inserted_sara_person.id,
};
let inserted_block = PersonBlock::block(pool, &timmy_blocks_sara_form)
.await
.unwrap();
let inserted_block = PersonBlock::block(pool, &timmy_blocks_sara_form).await?;
let expected_block = PersonBlock {
person_id: inserted_timmy_person.id,
@ -644,7 +629,7 @@ mod tests {
score: 1,
};
let _inserted_comment_like = CommentLike::like(pool, &comment_like_form).await.unwrap();
let _inserted_comment_like = CommentLike::like(pool, &comment_like_form).await?;
let timmy_local_user_view = LocalUserView {
local_user: inserted_timmy_local_user.clone(),
@ -652,7 +637,7 @@ mod tests {
person: inserted_timmy_person.clone(),
counts: Default::default(),
};
Data {
Ok(Data {
inserted_instance,
inserted_comment_0,
inserted_comment_1,
@ -661,7 +646,7 @@ mod tests {
timmy_local_user_view,
inserted_sara_person,
inserted_community,
}
})
}
#[tokio::test]
@ -669,7 +654,7 @@ mod tests {
async fn test_crud() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
let expected_comment_view_no_person = expected_comment_view(&data, pool).await?;
@ -714,7 +699,7 @@ mod tests {
Some(data.timmy_local_user_view.person.id),
)
.await?
.unwrap();
.ok_or(LemmyErrorType::CouldntFindComment)?;
// Make sure block set the creator blocked
assert!(read_comment_from_blocked_person.creator_blocked);
@ -727,7 +712,7 @@ mod tests {
async fn test_liked_only() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
// Unblock sara first
let timmy_unblocks_sara_form = PersonBlockForm {
@ -743,7 +728,7 @@ mod tests {
person_id: data.timmy_local_user_view.person.id,
score: 1,
};
CommentLike::like(pool, &comment_like_form).await.unwrap();
CommentLike::like(pool, &comment_like_form).await?;
let read_liked_comment_views = CommentQuery {
local_user: (Some(&data.timmy_local_user_view)),
@ -779,7 +764,7 @@ mod tests {
async fn test_comment_tree() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
let top_path = data.inserted_comment_0.path.clone();
let read_comment_views_top_path = CommentQuery {
@ -852,7 +837,7 @@ mod tests {
async fn test_languages() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
// by default, user has all languages enabled and should see all comments
// (except from blocked user)
@ -916,7 +901,7 @@ mod tests {
async fn test_distinguished_first() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
let form = CommentUpdateForm {
distinguished: Some(true),
@ -941,7 +926,7 @@ mod tests {
async fn test_creator_is_moderator() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
// Make one of the inserted persons a moderator
let person_id = data.inserted_sara_person.id;
@ -972,7 +957,7 @@ mod tests {
async fn test_creator_is_admin() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
let comments = CommentQuery {
sort: (Some(CommentSortType::Old)),
@ -997,7 +982,7 @@ mod tests {
async fn test_saved_order() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
// Save two comments
let save_comment_0_form = CommentSavedForm {
@ -1173,7 +1158,7 @@ mod tests {
async fn local_only_instance() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
Community::update(
pool,
@ -1219,7 +1204,7 @@ mod tests {
async fn comment_listing_local_user_banned_from_community() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
// Test that comment view shows if local user is blocked from community
let banned_from_comm_person = PersonInsertForm::test_form(data.inserted_instance.id, "jill");
@ -1262,7 +1247,7 @@ mod tests {
async fn comment_listing_local_user_not_banned_from_community() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let data = init_data(pool).await;
let data = init_data(pool).await?;
let comment_view = CommentView::read(
pool,

View file

@ -1,6 +1,6 @@
use crate::structs::CommentReplyView;
use diesel::{
dsl::exists,
dsl::{exists, not},
pg::Pg,
result::Error,
sql_types,
@ -217,6 +217,11 @@ fn queries<'a>() -> Queries<
CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
};
// Don't show replies from blocked persons
if let Some(my_person_id) = options.my_person_id {
query = query.filter(not(is_creator_blocked(my_person_id)));
}
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
query
@ -268,7 +273,7 @@ impl CommentReplyView {
}
}
#[derive(Default)]
#[derive(Default, Clone)]
pub struct CommentReplyQuery {
pub my_person_id: Option<PersonId>,
pub recipient_id: Option<PersonId>,
@ -284,3 +289,141 @@ impl CommentReplyQuery {
queries().list(pool, self).await
}
}
#[cfg(test)]
#[allow(clippy::indexing_slicing)]
mod tests {
use crate::{comment_reply_view::CommentReplyQuery, structs::CommentReplyView};
use lemmy_db_schema::{
source::{
comment::{Comment, CommentInsertForm},
comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
community::{Community, CommunityInsertForm},
instance::Instance,
person::{Person, PersonInsertForm},
person_block::{PersonBlock, PersonBlockForm},
post::{Post, PostInsertForm},
},
traits::{Blockable, Crud},
utils::build_db_pool_for_tests,
};
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
use pretty_assertions::assert_eq;
use serial_test::serial;
#[tokio::test]
#[serial]
async fn test_crud() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let terry_form = PersonInsertForm::builder()
.name("terrylake".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_terry = Person::create(pool, &terry_form).await?;
let recipient_form = PersonInsertForm::builder()
.name("terrylakes recipient".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_recipient = Person::create(pool, &recipient_form).await?;
let recipient_id = inserted_recipient.id;
let new_community = CommunityInsertForm::builder()
.name("test community lake".to_string())
.title("nada".to_owned())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(pool, &new_community).await?;
let new_post = PostInsertForm::builder()
.name("A test post".into())
.creator_id(inserted_terry.id)
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(pool, &new_post).await?;
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
.creator_id(inserted_terry.id)
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(pool, &comment_form, None).await?;
let comment_reply_form = CommentReplyInsertForm {
recipient_id: inserted_recipient.id,
comment_id: inserted_comment.id,
read: None,
};
let inserted_reply = CommentReply::create(pool, &comment_reply_form).await?;
let expected_reply = CommentReply {
id: inserted_reply.id,
recipient_id: inserted_reply.recipient_id,
comment_id: inserted_reply.comment_id,
read: false,
published: inserted_reply.published,
};
let read_reply = CommentReply::read(pool, inserted_reply.id)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
let updated_reply =
CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form).await?;
// Test to make sure counts and blocks work correctly
let unread_replies = CommentReplyView::get_unread_replies(pool, recipient_id).await?;
let query = CommentReplyQuery {
recipient_id: Some(recipient_id),
my_person_id: Some(recipient_id),
sort: None,
unread_only: false,
show_bot_accounts: true,
page: None,
limit: None,
};
let replies = query.clone().list(pool).await?;
assert_eq!(1, unread_replies);
assert_eq!(1, replies.len());
// Block the person, and make sure these counts are now empty
let block_form = PersonBlockForm {
person_id: recipient_id,
target_id: inserted_terry.id,
};
PersonBlock::block(pool, &block_form).await?;
let unread_replies_after_block =
CommentReplyView::get_unread_replies(pool, recipient_id).await?;
let replies_after_block = query.list(pool).await?;
assert_eq!(0, unread_replies_after_block);
assert_eq!(0, replies_after_block.len());
Comment::delete(pool, inserted_comment.id).await?;
Post::delete(pool, inserted_post.id).await?;
Community::delete(pool, inserted_community.id).await?;
Person::delete(pool, inserted_terry.id).await?;
Person::delete(pool, inserted_recipient.id).await?;
Instance::delete(pool, inserted_instance.id).await?;
assert_eq!(expected_reply, read_reply);
assert_eq!(expected_reply, inserted_reply);
assert_eq!(expected_reply, updated_reply);
Ok(())
}
}

View file

@ -1,6 +1,6 @@
use crate::structs::PersonMentionView;
use diesel::{
dsl::exists,
dsl::{exists, not},
pg::Pg,
result::Error,
sql_types,
@ -216,6 +216,11 @@ fn queries<'a>() -> Queries<
CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
};
// Don't show mentions from blocked persons
if let Some(my_person_id) = options.my_person_id {
query = query.filter(not(is_creator_blocked(my_person_id)));
}
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
query
@ -249,6 +254,15 @@ impl PersonMentionView {
person_mention::table
.inner_join(comment::table)
.left_join(
person_block::table.on(
comment::creator_id
.eq(person_block::target_id)
.and(person_block::person_id.eq(my_person_id)),
),
)
// Dont count replies from blocked users
.filter(person_block::person_id.is_null())
.filter(person_mention::recipient_id.eq(my_person_id))
.filter(person_mention::read.eq(false))
.filter(comment::deleted.eq(false))
@ -259,7 +273,7 @@ impl PersonMentionView {
}
}
#[derive(Default)]
#[derive(Default, Clone)]
pub struct PersonMentionQuery {
pub my_person_id: Option<PersonId>,
pub recipient_id: Option<PersonId>,
@ -275,3 +289,143 @@ impl PersonMentionQuery {
queries().list(pool, self).await
}
}
#[cfg(test)]
#[allow(clippy::indexing_slicing)]
mod tests {
use crate::{person_mention_view::PersonMentionQuery, structs::PersonMentionView};
use lemmy_db_schema::{
source::{
comment::{Comment, CommentInsertForm},
community::{Community, CommunityInsertForm},
instance::Instance,
person::{Person, PersonInsertForm},
person_block::{PersonBlock, PersonBlockForm},
person_mention::{PersonMention, PersonMentionInsertForm, PersonMentionUpdateForm},
post::{Post, PostInsertForm},
},
traits::{Blockable, Crud},
utils::build_db_pool_for_tests,
};
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
use pretty_assertions::assert_eq;
use serial_test::serial;
#[tokio::test]
#[serial]
async fn test_crud() -> LemmyResult<()> {
let pool = &build_db_pool_for_tests().await;
let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
let new_person = PersonInsertForm::builder()
.name("terrylake".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_person = Person::create(pool, &new_person).await?;
let recipient_form = PersonInsertForm::builder()
.name("terrylakes recipient".into())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_recipient = Person::create(pool, &recipient_form).await?;
let recipient_id = inserted_recipient.id;
let new_community = CommunityInsertForm::builder()
.name("test community lake".to_string())
.title("nada".to_owned())
.public_key("pubkey".to_string())
.instance_id(inserted_instance.id)
.build();
let inserted_community = Community::create(pool, &new_community).await?;
let new_post = PostInsertForm::builder()
.name("A test post".into())
.creator_id(inserted_person.id)
.community_id(inserted_community.id)
.build();
let inserted_post = Post::create(pool, &new_post).await?;
let comment_form = CommentInsertForm::builder()
.content("A test comment".into())
.creator_id(inserted_person.id)
.post_id(inserted_post.id)
.build();
let inserted_comment = Comment::create(pool, &comment_form, None).await?;
let person_mention_form = PersonMentionInsertForm {
recipient_id: inserted_recipient.id,
comment_id: inserted_comment.id,
read: None,
};
let inserted_mention = PersonMention::create(pool, &person_mention_form).await?;
let expected_mention = PersonMention {
id: inserted_mention.id,
recipient_id: inserted_mention.recipient_id,
comment_id: inserted_mention.comment_id,
read: false,
published: inserted_mention.published,
};
let read_mention = PersonMention::read(pool, inserted_mention.id)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let person_mention_update_form = PersonMentionUpdateForm { read: Some(false) };
let updated_mention =
PersonMention::update(pool, inserted_mention.id, &person_mention_update_form).await?;
// Test to make sure counts and blocks work correctly
let unread_mentions = PersonMentionView::get_unread_mentions(pool, recipient_id).await?;
let query = PersonMentionQuery {
recipient_id: Some(recipient_id),
my_person_id: Some(recipient_id),
sort: None,
unread_only: false,
show_bot_accounts: true,
page: None,
limit: None,
};
let mentions = query.clone().list(pool).await?;
assert_eq!(1, unread_mentions);
assert_eq!(1, mentions.len());
// Block the person, and make sure these counts are now empty
let block_form = PersonBlockForm {
person_id: recipient_id,
target_id: inserted_person.id,
};
PersonBlock::block(pool, &block_form).await?;
let unread_mentions_after_block =
PersonMentionView::get_unread_mentions(pool, recipient_id).await?;
let mentions_after_block = query.list(pool).await?;
assert_eq!(0, unread_mentions_after_block);
assert_eq!(0, mentions_after_block.len());
Comment::delete(pool, inserted_comment.id).await?;
Post::delete(pool, inserted_post.id).await?;
Community::delete(pool, inserted_community.id).await?;
Person::delete(pool, inserted_person.id).await?;
Person::delete(pool, inserted_recipient.id).await?;
Instance::delete(pool, inserted_instance.id).await?;
assert_eq!(expected_mention, read_mention);
assert_eq!(expected_mention, inserted_mention);
assert_eq!(expected_mention, updated_mention);
Ok(())
}
}