2023-09-07 18:02:36 +01:00
|
|
|
use anyhow::Result;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
use crate::services::bluesky::{Bluesky, Operation, OperationProcessor};
|
|
|
|
use crate::services::database::Database;
|
|
|
|
|
2023-09-16 16:16:01 +01:00
|
|
|
pub struct PostIndexer<'a> {
|
2023-09-07 18:02:36 +01:00
|
|
|
database: &'a Database,
|
2023-09-07 18:06:39 +01:00
|
|
|
bluesky: &'a Bluesky,
|
2023-09-07 18:02:36 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 16:16:01 +01:00
|
|
|
impl<'a> PostIndexer<'a> {
|
2023-09-07 18:06:39 +01:00
|
|
|
pub fn new(database: &'a Database, bluesky: &'a Bluesky) -> Self {
|
2023-09-07 18:02:36 +01:00
|
|
|
Self { database, bluesky }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 16:16:01 +01:00
|
|
|
impl<'a> PostIndexer<'a> {
|
2023-09-07 18:02:36 +01:00
|
|
|
pub async fn start(&self) -> Result<()> {
|
|
|
|
Ok(self.bluesky.subscribe_to_operations(self).await?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2023-09-16 16:16:01 +01:00
|
|
|
impl<'a> OperationProcessor for PostIndexer<'a> {
|
2023-09-07 18:02:36 +01:00
|
|
|
async fn process_operation(&self, operation: &Operation) -> Result<()> {
|
|
|
|
match operation {
|
|
|
|
Operation::CreatePost {
|
|
|
|
author_did,
|
|
|
|
cid,
|
|
|
|
uri,
|
|
|
|
languages,
|
|
|
|
text,
|
|
|
|
} => {
|
|
|
|
// TODO: Configure this via env vars
|
|
|
|
if !languages.contains("ru") {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlueSky gets confused a lot about Russian vs Ukrainian, so skip posts
|
|
|
|
// that may be in Ukrainian regardless of whether Russian is in the list
|
|
|
|
// TODO: Configure this via env vars
|
|
|
|
if languages.contains("uk") {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("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?;
|
|
|
|
}
|
|
|
|
Operation::DeletePost { uri } => {
|
|
|
|
println!("received a post do delete: {uri}");
|
|
|
|
|
|
|
|
// TODO: Delete posts from db
|
|
|
|
// self.database.delete_post(&self.db_connection_pool, &uri).await?;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|