Skip to content

Commit

Permalink
Merge pull request #283 from LemmyNet/main
Browse files Browse the repository at this point in the history
[pull] master from LemmyNet:main
  • Loading branch information
pull[bot] authored Feb 21, 2025
2 parents 748ef53 + 493734d commit c3e47a8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
11 changes: 5 additions & 6 deletions crates/db_views/src/comment/comment_view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::structs::{CommentSlimView, CommentView};
use crate::{
structs::{CommentSlimView, CommentView},
utils::filter_blocked,
};
use diesel::{
dsl::exists,
result::Error,
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions crates/db_views/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ pub mod reports;
#[cfg(feature = "full")]
pub mod site;
pub mod structs;
#[cfg(feature = "full")]
pub mod utils;
45 changes: 22 additions & 23 deletions crates/db_views/src/post/post_view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::structs::{PaginationCursor, PostView};
use crate::{
structs::{PaginationCursor, PostView},
utils::filter_blocked,
};
use diesel::{
debug_query,
dsl::{exists, not},
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -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(())
Expand Down
13 changes: 13 additions & 0 deletions crates/db_views/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit c3e47a8

Please sign in to comment.