diff --git a/src/algos/nederlandskie.rs b/src/algos/nederlandskie.rs index ffba237..d060b61 100644 --- a/src/algos/nederlandskie.rs +++ b/src/algos/nederlandskie.rs @@ -14,11 +14,25 @@ use crate::services::database::{self, Database}; /// An algorithm that serves posts written in Russian by people living in Netherlands pub struct Nederlandskie { language_detector: Arc, + database: Arc, } impl Nederlandskie { - pub fn new(language_detector: Arc) -> Self { - Self { language_detector } + pub fn new(language_detector: Arc, database: Arc) -> Self { + Self { + language_detector, + database, + } + } +} + +impl Nederlandskie { + fn is_post_in_russian(&self, post: &bluesky::PostRecord) -> bool { + self.language_detector.detect_language_of(&post.text) == Some(Russian) + } + + async fn is_profile_residing_in_netherlands(&self, did: &str) -> Result { + Ok(self.database.is_profile_in_this_country(did, "nl").await? == Some(true)) } } @@ -26,10 +40,11 @@ impl Nederlandskie { impl Algo for Nederlandskie { async fn should_index_post( &self, - _author_did: &str, + author_did: &str, post: &bluesky::PostRecord, ) -> Result { - Ok(self.language_detector.detect_language_of(&post.text) == Some(Russian)) + Ok(self.is_post_in_russian(&post) + || self.is_profile_residing_in_netherlands(author_did).await?) } async fn fetch_posts( diff --git a/src/main.rs b/src/main.rs index 31f1a4a..66e8163 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,10 @@ async fn main() -> Result<()> { let algos = Arc::new( AlgosBuilder::new() - .add("nederlandskie", Nederlandskie::new(language_detector)) + .add( + "nederlandskie", + Nederlandskie::new(language_detector, database.clone()), + ) .build(), ); diff --git a/src/services/database.rs b/src/services/database.rs index e68bf70..4b99e4d 100644 --- a/src/services/database.rs +++ b/src/services/database.rs @@ -192,6 +192,27 @@ impl Database { Ok(true) } + pub async fn is_profile_in_this_country( + &self, + did: &str, + country: &str, + ) -> Result> { + let mut params = Parameters::new(); + + Ok(query( + &select("likely_country_of_living") + .from("Profile") + .where_(format!("did = {}", params.next())) + .where_("has_been_processed = TRUE") + .to_string(), + ) + .bind(did) + .map(|r: PgRow| r.get("likely_country_of_living")) + .map(|c: String| c == country) + .fetch_optional(&self.connection_pool) + .await?) + } + pub async fn fetch_subscription_cursor(&self, did: &str) -> Result> { let mut params = Parameters::new();