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:
parent
c02bded6f8
commit
901c4b6e97
|
@ -12,7 +12,12 @@ pub use self::nederlandskie::Nederlandskie;
|
|||
|
||||
#[async_trait]
|
||||
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(
|
||||
&self,
|
||||
|
|
|
@ -24,13 +24,13 @@ impl Nederlandskie {
|
|||
/// An algorithm that serves posts written in Russian by people living in Netherlands
|
||||
#[async_trait]
|
||||
impl Algo for Nederlandskie {
|
||||
fn should_index_post(
|
||||
async fn should_index_post(
|
||||
&self,
|
||||
_author_did: &str,
|
||||
_languages: &HashSet<String>,
|
||||
text: &str,
|
||||
) -> bool {
|
||||
self.language_detector.detect_language_of(text) == Some(Russian)
|
||||
) -> Result<bool> {
|
||||
Ok(self.language_detector.detect_language_of(text) == Some(Russian))
|
||||
}
|
||||
|
||||
async fn fetch_posts(
|
||||
|
|
|
@ -65,17 +65,18 @@ impl OperationProcessor for PostIndexer {
|
|||
languages,
|
||||
text,
|
||||
} => {
|
||||
if self
|
||||
.algos
|
||||
.iter_all()
|
||||
.any(|a| a.should_index_post(author_did, languages, text))
|
||||
{
|
||||
info!("Received insertable post from {author_did}: {text}");
|
||||
for algo in self.algos.iter_all() {
|
||||
if algo.should_index_post(author_did, languages, text).await? {
|
||||
info!("Received insertable post from {author_did}: {text}");
|
||||
|
||||
self.database
|
||||
.insert_profile_if_it_doesnt_exist(&author_did)
|
||||
.await?;
|
||||
self.database.insert_post(&author_did, &cid, &uri).await?;
|
||||
self.database
|
||||
.insert_profile_if_it_doesnt_exist(&author_did)
|
||||
.await?;
|
||||
|
||||
self.database.insert_post(&author_did, &cid, &uri).await?;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Operation::DeletePost { uri } => {
|
||||
|
|
Loading…
Reference in New Issue