Skip to content

Commit

Permalink
Add test case for community.hidden in post_view (ref #5074)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Oct 7, 2024
1 parent 432d46c commit 4ab8277
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 65 deletions.
48 changes: 23 additions & 25 deletions crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
));
}
}

Expand Down
102 changes: 62 additions & 40 deletions crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
),
);
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)),
),
));
}
}
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));
}
// 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)),
}
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)),
),
));
}
} else {
query = query.filter(community::hidden.eq(false));
}

if let Some(search_term) = &options.search_term {
Expand Down Expand Up @@ -754,6 +737,8 @@ mod tests {
comment::{Comment, CommentInsertForm},
community::{
Community,
CommunityFollower,
CommunityFollowerForm,
CommunityInsertForm,
CommunityModerator,
CommunityModeratorForm,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<()> {
Expand Down

0 comments on commit 4ab8277

Please sign in to comment.