2023-09-16 20:07:00 +01:00
|
|
|
mod nederlandskie;
|
|
|
|
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
|
|
|
|
use crate::services::database::{Database, Post};
|
|
|
|
|
2023-09-21 09:36:47 +01:00
|
|
|
pub use self::nederlandskie::Nederlandskie;
|
2023-09-16 20:07:00 +01:00
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait Algo {
|
2023-09-21 12:31:27 +01:00
|
|
|
async fn should_index_post(
|
|
|
|
&self,
|
|
|
|
author_did: &str,
|
|
|
|
languages: &HashSet<String>,
|
|
|
|
text: &str,
|
|
|
|
) -> Result<bool>;
|
2023-09-16 20:07:00 +01:00
|
|
|
|
|
|
|
async fn fetch_posts(
|
|
|
|
&self,
|
|
|
|
database: &Database,
|
|
|
|
limit: i32,
|
|
|
|
earlier_than: Option<(DateTime<Utc>, &str)>,
|
|
|
|
) -> Result<Vec<Post>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type AnyAlgo = Box<dyn Algo + Sync + Send>;
|
2023-09-21 09:36:47 +01:00
|
|
|
type AlgosMap = HashMap<String, AnyAlgo>;
|
2023-09-16 20:07:00 +01:00
|
|
|
|
2023-09-21 09:36:47 +01:00
|
|
|
pub struct Algos {
|
|
|
|
algos: AlgosMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Algos {
|
|
|
|
pub fn iter_names(&self) -> impl Iterator<Item = &str> {
|
|
|
|
self.algos.keys().map(String::as_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter_all(&self) -> impl Iterator<Item = &AnyAlgo> {
|
|
|
|
self.algos.values()
|
|
|
|
}
|
2023-09-16 20:07:00 +01:00
|
|
|
|
2023-09-21 09:36:47 +01:00
|
|
|
pub fn get_by_name(&self, name: &str) -> Option<&AnyAlgo> {
|
|
|
|
self.algos.get(name)
|
|
|
|
}
|
2023-09-16 20:07:00 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 09:36:47 +01:00
|
|
|
pub struct AlgosBuilder {
|
|
|
|
algos: AlgosMap,
|
2023-09-16 20:07:00 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 09:36:47 +01:00
|
|
|
impl AlgosBuilder {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
algos: AlgosMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add<T: Algo + Send + Sync + 'static>(mut self, name: &str, algo: T) -> Self {
|
|
|
|
self.algos.insert(name.to_owned(), Box::new(algo));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> Algos {
|
|
|
|
Algos { algos: self.algos }
|
|
|
|
}
|
2023-09-16 20:07:00 +01:00
|
|
|
}
|