From 0763d0564438eef2e133e75764a77d3f3894e5ca Mon Sep 17 00:00:00 2001 From: keithadler <271127604+keithadler@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:07:28 -0700 Subject: [PATCH] Scope Brazil2026ElectionFilter to viewers in Brazil Brazil2026ElectionFilter is added to the For You pre-scoring chain unconditionally and never reads the viewer's country, so it currently applies to every viewer in every country rather than to the viewers the Electoral Court order covers. The scoping mechanisms already exist and were simply unused: - ScoredPostsQuery carries country_code - Filter::enable(&self, query) gates a filter per request, and is honoured by the pipeline at candidate_pipeline.rs:352 Eight other filters in the same chain already use enable() for this purpose (VideoFilter, TopicIdsFilter, NewUserMinEngagementFilter, InventoryHoldoutFilter and others). Unknown or empty country codes leave the filter disabled, matching the existing convention for legal withholding in visibility-filtering, where viewer_in_withheld_country returns false when the viewer country is not known. Adds tests for the enabled, disabled, case-insensitive and unknown-country cases. Existing tests call filter() directly and are unaffected. Co-Authored-By: Claude Opus 5 --- .../filters/brazil_2026_election_filter.rs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/home-mixer/filters/brazil_2026_election_filter.rs b/home-mixer/filters/brazil_2026_election_filter.rs index 2f832ca7..55bfa868 100644 --- a/home-mixer/filters/brazil_2026_election_filter.rs +++ b/home-mixer/filters/brazil_2026_election_filter.rs @@ -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> = LazyLock::new(|| { FxHashSet::from_iter([ @@ -1378,6 +1381,13 @@ impl Brazil2026ElectionFilter { } impl Filter 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, @@ -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())); + } }