Make Algo.should_index_post a fallible async function, for maximum extensibility

We may want to perform some more complicated operations here in the future
This commit is contained in:
Aleksei Voronov 2023-09-21 13:31:27 +02:00
parent c02bded6f8
commit 901c4b6e97
3 changed files with 20 additions and 14 deletions

View File

@ -12,7 +12,12 @@ pub use self::nederlandskie::Nederlandskie;
#[async_trait] #[async_trait]
pub trait Algo { pub trait Algo {
fn should_index_post(&self, author_did: &str, languages: &HashSet<String>, text: &str) -> bool; async fn should_index_post(
&self,
author_did: &str,
languages: &HashSet<String>,
text: &str,
) -> Result<bool>;
async fn fetch_posts( async fn fetch_posts(
&self, &self,

View File

@ -24,13 +24,13 @@ impl Nederlandskie {
/// An algorithm that serves posts written in Russian by people living in Netherlands /// An algorithm that serves posts written in Russian by people living in Netherlands
#[async_trait] #[async_trait]
impl Algo for Nederlandskie { impl Algo for Nederlandskie {
fn should_index_post( async fn should_index_post(
&self, &self,
_author_did: &str, _author_did: &str,
_languages: &HashSet<String>, _languages: &HashSet<String>,
text: &str, text: &str,
) -> bool { ) -> Result<bool> {
self.language_detector.detect_language_of(text) == Some(Russian) Ok(self.language_detector.detect_language_of(text) == Some(Russian))
} }
async fn fetch_posts( async fn fetch_posts(

View File

@ -65,17 +65,18 @@ impl OperationProcessor for PostIndexer {
languages, languages,
text, text,
} => { } => {
if self for algo in self.algos.iter_all() {
.algos if algo.should_index_post(author_did, languages, text).await? {
.iter_all() info!("Received insertable post from {author_did}: {text}");
.any(|a| a.should_index_post(author_did, languages, text))
{
info!("Received insertable post from {author_did}: {text}");
self.database self.database
.insert_profile_if_it_doesnt_exist(&author_did) .insert_profile_if_it_doesnt_exist(&author_did)
.await?; .await?;
self.database.insert_post(&author_did, &cid, &uri).await?;
self.database.insert_post(&author_did, &cid, &uri).await?;
break;
}
} }
} }
Operation::DeletePost { uri } => { Operation::DeletePost { uri } => {