use async_trait::async_trait;
use toolkit_macros::domain_model;
use tracing::debug;
use uuid::Uuid;
use crate::domain::error::Result;
#[domain_model]
#[derive(Debug, Clone)]
pub enum WebhookEvent {
SessionCreated {
session_id: Uuid,
tenant_id: String,
user_id: String,
session_type_id: Option<Uuid>,
},
SessionArchived {
session_id: Uuid,
tenant_id: String,
user_id: String,
},
SessionRestored {
session_id: Uuid,
tenant_id: String,
user_id: String,
},
SessionSoftDeleted {
session_id: Uuid,
tenant_id: String,
user_id: String,
},
SessionHardDeleted {
session_id: Uuid,
tenant_id: String,
user_id: String,
},
MessageDeleted {
session_id: Uuid,
message_id: Uuid,
tenant_id: String,
user_id: String,
deleted_count: u64,
deleted_at: time::OffsetDateTime,
},
}
impl WebhookEvent {
#[must_use]
pub fn kind(&self) -> &'static str {
match self {
Self::SessionCreated { .. } => "session.created",
Self::SessionArchived { .. } => "session.archived",
Self::SessionRestored { .. } => "session.restored",
Self::SessionSoftDeleted { .. } => "session.soft_deleted",
Self::SessionHardDeleted { .. } => "session.hard_deleted",
Self::MessageDeleted { .. } => "message.deleted",
}
}
}
#[async_trait]
pub trait WebhookEmitter: Send + Sync {
async fn emit(&self, event: WebhookEvent) -> Result<()>;
}
#[domain_model]
#[derive(Debug, Default, Clone)]
pub struct NoopWebhookEmitter;
#[async_trait]
impl WebhookEmitter for NoopWebhookEmitter {
async fn emit(&self, event: WebhookEvent) -> Result<()> {
debug!(
event = event.kind(),
"webhook emitter (noop) \u{2014} event swallowed"
);
Ok(())
}
}
#[cfg(test)]
#[path = "webhook_tests.rs"]
mod webhook_tests;