Show posts/comments from followed communities despite instance block (#5437)

* Show posts/comments from followed communities despite instance block (fixes #4303)

* move to utils
This commit is contained in:
Nutomic 2025-02-20 22:18:38 +00:00 committed by GitHub
parent 544a4cc039
commit 493734d1b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 29 deletions

View file

@ -1,4 +1,7 @@
use crate::structs::{CommentSlimView, CommentView};
use crate::{
structs::{CommentSlimView, CommentView},
utils::filter_blocked,
};
use diesel::{
dsl::exists,
result::Error,
@ -233,11 +236,7 @@ impl CommentQuery<'_> {
),
));
// Don't show blocked communities or persons
query = query
.filter(instance_actions::blocked.is_null())
.filter(community_actions::blocked.is_null())
.filter(person_actions::blocked.is_null());
query = query.filter(filter_blocked());
};
if !o.local_user.show_nsfw(site) {

View file

@ -22,3 +22,5 @@ pub mod reports;
#[cfg(feature = "full")]
pub mod site;
pub mod structs;
#[cfg(feature = "full")]
pub mod utils;

View file

@ -1,4 +1,7 @@
use crate::structs::{PaginationCursor, PostView};
use crate::{
structs::{PaginationCursor, PostView},
utils::filter_blocked,
};
use diesel::{
debug_query,
dsl::{exists, not},
@ -571,10 +574,7 @@ impl<'a> PostQuery<'a> {
));
}
// Don't show blocked instances, communities or persons
query = query.filter(community_actions::blocked.is_null());
query = query.filter(instance_actions::blocked.is_null());
query = query.filter(person_actions::blocked.is_null());
query = query.filter(filter_blocked());
}
let (limit, offset) = limit_and_offset(o.page, o.limit)?;
@ -1606,6 +1606,12 @@ mod tests {
#[serial]
async fn post_listing_instance_block(data: &mut Data) -> LemmyResult<()> {
const POST_FROM_BLOCKED_INSTANCE: &str = "post on blocked instance";
const POST_LISTING_WITH_BLOCKED: [&str; 4] = [
POST_FROM_BLOCKED_INSTANCE,
POST_WITH_TAGS,
POST_BY_BOT,
POST,
];
let pool = &data.pool();
let pool = &mut pool.into();
@ -1632,15 +1638,7 @@ mod tests {
// no instance block, should return all posts
let post_listings_all = data.default_post_query().list(&data.site, pool).await?;
assert_eq!(
vec![
POST_FROM_BLOCKED_INSTANCE,
POST_WITH_TAGS,
POST_BY_BOT,
POST
],
names(&post_listings_all)
);
assert_eq!(POST_LISTING_WITH_BLOCKED, *names(&post_listings_all));
// block the instance
let block_form = InstanceBlockForm {
@ -1659,18 +1657,19 @@ mod tests {
.iter()
.all(|p| p.post.id != post_from_blocked_instance.id));
// Follow community from the blocked instance to see posts anyway
let mut follow_form =
CommunityFollowerForm::new(inserted_community.id, data.tegan_local_user_view.person.id);
follow_form.state = Some(CommunityFollowerState::Accepted);
CommunityFollower::follow(pool, &follow_form).await?;
let post_listings_bypass = data.default_post_query().list(&data.site, pool).await?;
assert_eq!(POST_LISTING_WITH_BLOCKED, *names(&post_listings_bypass));
CommunityFollower::unfollow(pool, &follow_form).await?;
// after unblocking it should return all posts again
InstanceBlock::unblock(pool, &block_form).await?;
let post_listings_blocked = data.default_post_query().list(&data.site, pool).await?;
assert_eq!(
vec![
POST_FROM_BLOCKED_INSTANCE,
POST_WITH_TAGS,
POST_BY_BOT,
POST
],
names(&post_listings_blocked)
);
assert_eq!(POST_LISTING_WITH_BLOCKED, *names(&post_listings_blocked));
Instance::delete(pool, blocked_instance.id).await?;
Ok(())

View file

@ -0,0 +1,13 @@
use diesel::{BoolExpressionMethods, ExpressionMethods};
use lemmy_db_schema::schema::{community_actions, instance_actions, person_actions};
/// Hide all content from blocked communities and persons. Content from blocked instances is also
/// hidden, unless the user followed the community explicitly.
#[diesel::dsl::auto_type]
pub(crate) fn filter_blocked() -> _ {
instance_actions::blocked
.is_null()
.or(community_actions::followed.is_not_null())
.and(community_actions::blocked.is_null())
.and(person_actions::blocked.is_null())
}