2022-04-08 16:52:59 +00:00
|
|
|
use color_eyre::Result;
|
2022-04-11 11:52:29 +00:00
|
|
|
use tracing::info;
|
2022-04-08 16:52:59 +00:00
|
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
|
2022-04-16 08:06:48 +00:00
|
|
|
use phoebe::{
|
|
|
|
get_linked_channels, link_messages,
|
|
|
|
prelude::{ChatEvent, SqlitePool},
|
|
|
|
service::Service,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn dyn_service(service: &str) -> &'static str {
|
|
|
|
match service {
|
|
|
|
"discord" => "discord",
|
|
|
|
"matrix" => "matrix",
|
|
|
|
_ => panic!("Unsupported service: {}", service),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_events(
|
|
|
|
db: SqlitePool,
|
|
|
|
mut service: Box<dyn Service + Send + Sync>,
|
|
|
|
mut rx: tokio::sync::broadcast::Receiver<ChatEvent>,
|
|
|
|
) {
|
|
|
|
info!("Handling events for {}…", service.tag());
|
|
|
|
|
|
|
|
let mut conn = db
|
|
|
|
.acquire()
|
|
|
|
.await
|
|
|
|
.expect("Failed to acquire core DB connection");
|
|
|
|
|
|
|
|
while let Ok(event) = rx.recv().await {
|
|
|
|
match event {
|
|
|
|
phoebe::prelude::ChatEvent::NewMessage(message) => {
|
|
|
|
let linked_channels =
|
|
|
|
get_linked_channels(&mut conn, &message.origin.channel, dyn_service).await;
|
|
|
|
|
|
|
|
let mut resulting_messages = vec![];
|
|
|
|
for destination_channel in linked_channels {
|
|
|
|
resulting_messages.extend(
|
|
|
|
service
|
|
|
|
.send_chat_message(&message, destination_channel)
|
|
|
|
.await,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = link_messages(&mut conn, &message.origin, &resulting_messages).await
|
|
|
|
{
|
|
|
|
tracing::error!("Failed to link messages: {e}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-08 16:52:59 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
color_eyre::install()?;
|
|
|
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_target(true)
|
|
|
|
.with_env_filter(EnvFilter::from_default_env())
|
|
|
|
.init();
|
|
|
|
|
2022-04-11 11:52:29 +00:00
|
|
|
let (tx, _) = tokio::sync::broadcast::channel(512);
|
2022-04-08 16:52:59 +00:00
|
|
|
|
|
|
|
let db = phoebe::open_core_db().await?;
|
2022-04-11 11:52:29 +00:00
|
|
|
let services: Vec<Box<dyn Service + Send + Sync>> = vec![Box::new(
|
2022-04-08 16:52:59 +00:00
|
|
|
phoebe_discord::setup(db.clone(), tx.clone()).await?,
|
|
|
|
)];
|
|
|
|
|
2022-04-16 08:06:48 +00:00
|
|
|
let handles = services
|
|
|
|
.into_iter()
|
|
|
|
.map(|srv| tokio::spawn(handle_events(db.clone(), srv, tx.subscribe())));
|
2022-04-11 11:52:29 +00:00
|
|
|
|
|
|
|
let _ = futures::future::join_all(handles).await;
|
|
|
|
|
2022-04-08 16:52:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|