Add test case for community.hidden in post_view (ref #5074) (#5106)

This commit is contained in:
Nutomic 2024-10-08 01:39:25 +02:00 committed by GitHub
parent b009d45729
commit f5066b1f0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 85 additions and 65 deletions

View file

@ -216,32 +216,30 @@ fn queries<'a>() -> Queries<
query = query.filter(post::community_id.eq(community_id));
}
if let Some(listing_type) = options.listing_type {
let is_subscribed = exists(
community_follower::table.filter(
post::community_id
.eq(community_follower::community_id)
.and(community_follower::person_id.eq(person_id_join)),
),
);
let is_subscribed = exists(
community_follower::table.filter(
post::community_id
.eq(community_follower::community_id)
.and(community_follower::person_id.eq(person_id_join)),
),
);
match listing_type {
ListingType::Subscribed => query = query.filter(is_subscribed), /* TODO could be this: and(community_follower::person_id.eq(person_id_join)), */
ListingType::Local => {
query = query
.filter(community::local.eq(true))
.filter(community::hidden.eq(false).or(is_subscribed))
}
ListingType::All => query = query.filter(community::hidden.eq(false).or(is_subscribed)),
ListingType::ModeratorView => {
query = query.filter(exists(
community_moderator::table.filter(
post::community_id
.eq(community_moderator::community_id)
.and(community_moderator::person_id.eq(person_id_join)),
),
));
}
match options.listing_type.unwrap_or_default() {
ListingType::Subscribed => query = query.filter(is_subscribed), /* TODO could be this: and(community_follower::person_id.eq(person_id_join)), */
ListingType::Local => {
query = query
.filter(community::local.eq(true))
.filter(community::hidden.eq(false).or(is_subscribed))
}
ListingType::All => query = query.filter(community::hidden.eq(false).or(is_subscribed)),
ListingType::ModeratorView => {
query = query.filter(exists(
community_moderator::table.filter(
post::community_id
.eq(community_moderator::community_id)
.and(community_moderator::person_id.eq(person_id_join)),
),
));
}
}

View file

@ -346,47 +346,30 @@ fn queries<'a>() -> Queries<
query = query.filter(post_aggregates::creator_id.eq(creator_id));
}
if let Some(listing_type) = options.listing_type {
if let Some(person_id) = options.local_user.person_id() {
let is_subscribed = exists(
community_follower::table.filter(
post_aggregates::community_id
.eq(community_follower::community_id)
.and(community_follower::person_id.eq(person_id)),
let is_subscribed = exists(
community_follower::table.filter(
post_aggregates::community_id
.eq(community_follower::community_id)
.and(community_follower::person_id.eq(person_id_join)),
),
);
match options.listing_type.unwrap_or_default() {
ListingType::Subscribed => query = query.filter(is_subscribed),
ListingType::Local => {
query = query
.filter(community::local.eq(true))
.filter(community::hidden.eq(false).or(is_subscribed));
}
ListingType::All => query = query.filter(community::hidden.eq(false).or(is_subscribed)),
ListingType::ModeratorView => {
query = query.filter(exists(
community_moderator::table.filter(
post::community_id
.eq(community_moderator::community_id)
.and(community_moderator::person_id.eq(person_id_join)),
),
);
match listing_type {
ListingType::Subscribed => query = query.filter(is_subscribed),
ListingType::Local => {
query = query
.filter(community::local.eq(true))
.filter(community::hidden.eq(false).or(is_subscribed));
}
ListingType::All => query = query.filter(community::hidden.eq(false).or(is_subscribed)),
ListingType::ModeratorView => {
query = query.filter(exists(
community_moderator::table.filter(
post::community_id
.eq(community_moderator::community_id)
.and(community_moderator::person_id.eq(person_id)),
),
));
}
}
));
}
// If your person_id is missing, only show local
else {
match listing_type {
ListingType::Local => {
query = query
.filter(community::local.eq(true))
.filter(community::hidden.eq(false));
}
_ => query = query.filter(community::hidden.eq(false)),
}
}
} else {
query = query.filter(community::hidden.eq(false));
}
if let Some(search_term) = &options.search_term {
@ -754,6 +737,8 @@ mod tests {
comment::{Comment, CommentInsertForm},
community::{
Community,
CommunityFollower,
CommunityFollowerForm,
CommunityInsertForm,
CommunityModerator,
CommunityModeratorForm,
@ -782,7 +767,7 @@ mod tests {
},
site::Site,
},
traits::{Bannable, Blockable, Crud, Joinable, Likeable, Saveable},
traits::{Bannable, Blockable, Crud, Followable, Joinable, Likeable, Saveable},
utils::{build_db_pool, build_db_pool_for_tests, DbPool, RANK_DEFAULT},
CommunityVisibility,
PostSortType,
@ -1432,6 +1417,43 @@ mod tests {
cleanup(data, pool).await
}
#[tokio::test]
#[serial]
async fn post_listings_hidden_community() -> LemmyResult<()> {
let pool = &build_db_pool().await?;
let pool = &mut pool.into();
let data = init_data(pool).await?;
Community::update(
pool,
data.inserted_community.id,
&CommunityUpdateForm {
hidden: Some(true),
..Default::default()
},
)
.await?;
let posts = PostQuery::default().list(&data.site, pool).await?;
assert!(posts.is_empty());
let posts = data.default_post_query().list(&data.site, pool).await?;
assert!(posts.is_empty());
// Follow the community
let form = CommunityFollowerForm {
community_id: data.inserted_community.id,
person_id: data.local_user_view.person.id,
pending: false,
};
CommunityFollower::follow(pool, &form).await?;
let posts = data.default_post_query().list(&data.site, pool).await?;
assert!(!posts.is_empty());
cleanup(data, pool).await
}
#[tokio::test]
#[serial]
async fn post_listing_instance_block() -> LemmyResult<()> {