2023-09-16 20:07:00 +01:00
|
|
|
mod nederlandskie;
|
|
|
|
|
2023-10-15 10:55:25 +01:00
|
|
|
use std::collections::HashMap;
|
2023-09-16 20:07:00 +01:00
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
|
2023-10-15 10:55:25 +01:00
|
|
|
use crate::services::bluesky;
|
|
|
|
use crate::services::database::{self, Database};
|
2023-09-16 20:07:00 +01:00
|
|
|
|
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-10-15 10:55:25 +01:00
|
|
|
async fn should_index_post(&self, author_did: &str, post: &bluesky::PostRecord)
|
|
|
|
-> 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)>,
|
2023-10-15 10:55:25 +01:00
|
|
|
) -> Result<Vec<database::Post>>;
|
2023-09-16 20:07:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-22 16:15:48 +01:00
|
|
|
#[derive(Default)]
|
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 {
|
2023-09-22 16:15:48 +01:00
|
|
|
Default::default()
|
2023-09-21 09:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|