use docbox_database::models::tenant::Tenant;
use docbox_database::models::{
document_box::{DocumentBox, WithScope},
file::File,
folder::Folder,
link::Link,
};
use serde::Serialize;
pub mod mpsc;
pub mod noop;
pub mod sqs;
use noop::NoopEventPublisher;
use sqs::{SqsEventPublisherFactory, TenantSqsEventQueue};
#[derive(Clone)]
pub struct EventPublisherFactory {
sqs: SqsEventPublisherFactory,
}
impl EventPublisherFactory {
pub fn new(sqs: SqsEventPublisherFactory) -> Self {
Self { sqs }
}
pub fn create_event_publisher(&self, tenant: &Tenant) -> TenantEventPublisher {
match tenant.event_queue_url.as_ref() {
Some(value) => {
let target = TenantSqsEventQueue {
tenant_id: tenant.id,
event_queue_url: value.clone(),
};
TenantEventPublisher::Sqs(self.sqs.create_event_publisher(target))
}
None => TenantEventPublisher::Noop(NoopEventPublisher),
}
}
}
#[derive(Clone)]
pub enum TenantEventPublisher {
Sqs(sqs::SqsEventPublisher),
Noop(noop::NoopEventPublisher),
Mpsc(mpsc::MpscEventPublisher),
}
impl TenantEventPublisher {
pub fn publish_event(&self, event: TenantEventMessage) {
match self {
TenantEventPublisher::Sqs(inner) => inner.publish_event(event),
TenantEventPublisher::Noop(inner) => inner.publish_event(event),
TenantEventPublisher::Mpsc(inner) => inner.publish_event(event),
}
}
}
#[derive(Debug, Serialize)]
#[serde(tag = "event", content = "data", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum TenantEventMessage {
DocumentBoxCreated(DocumentBox),
FileCreated(WithScope<File>),
FolderCreated(WithScope<Folder>),
LinkCreated(WithScope<Link>),
DocumentBoxDeleted(DocumentBox),
FileDeleted(WithScope<File>),
FolderDeleted(WithScope<Folder>),
LinkDeleted(WithScope<Link>),
}
pub trait EventPublisher: Send + Sync + 'static {
fn publish_event(&self, event: TenantEventMessage);
}