Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions home-mixer/filters/brazil_2026_election_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use xai_candidate_pipeline::filter::{Filter, FilterResult};

// OmarAzizSenador deleted his account at the time this code was written.

/// Viewer country code the Electoral Court order applies to.
const BRAZIL_COUNTRY_CODE: &str = "br";

/// User ids reported to the Electoral Court for the Brazil 2026 election.
static BRAZIL_2026_ELECTION_USER_IDS: LazyLock<FxHashSet<u64>> = LazyLock::new(|| {
FxHashSet::from_iter([
Expand Down Expand Up @@ -1378,6 +1381,13 @@ impl Brazil2026ElectionFilter {
}

impl Filter<ScoredPostsQuery, PostCandidate> for Brazil2026ElectionFilter {
/// The Electoral Court order applies to viewers in Brazil, so scope the
/// filter to them. Without this the filter runs for every viewer in every
/// country, which is broader than the order requires.
fn enable(&self, query: &ScoredPostsQuery) -> bool {
query.country_code.eq_ignore_ascii_case(BRAZIL_COUNTRY_CODE)
}

fn filter(
&self,
query: &ScoredPostsQuery,
Expand Down Expand Up @@ -1570,4 +1580,36 @@ mod tests {
));
}
}

fn query_from_country(country_code: &str) -> ScoredPostsQuery {
ScoredPostsQuery {
country_code: country_code.to_string(),
..Default::default()
}
}

#[test]
fn enabled_for_viewers_in_brazil() {
assert!(Brazil2026ElectionFilter.enable(&query_from_country("br")));
}

#[test]
fn enabled_regardless_of_country_code_case() {
assert!(Brazil2026ElectionFilter.enable(&query_from_country("BR")));
}

#[test]
fn disabled_for_viewers_outside_brazil() {
for country_code in ["us", "gb", "jp", "pt"] {
assert!(
!Brazil2026ElectionFilter.enable(&query_from_country(country_code)),
"expected filter to be disabled for {country_code}"
);
}
}

#[test]
fn disabled_when_country_code_is_unknown() {
assert!(!Brazil2026ElectionFilter.enable(&ScoredPostsQuery::default()));
}
}