Add the shared domain event bus.
This commit is contained in:
29
crates/bds-core/src/model/event.rs
Normal file
29
crates/bds-core/src/model/event.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{DomainEntity, NotificationAction};
|
||||
|
||||
/// The single event shape shared by desktop, CLI synchronization, and future
|
||||
/// remote clients.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum DomainEvent {
|
||||
EntityChanged {
|
||||
project_id: String,
|
||||
entity: DomainEntity,
|
||||
entity_id: String,
|
||||
action: NotificationAction,
|
||||
},
|
||||
SettingsChanged {
|
||||
project_id: Option<String>,
|
||||
key: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl DomainEvent {
|
||||
pub fn project_id(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::EntityChanged { project_id, .. } => Some(project_id),
|
||||
Self::SettingsChanged { project_id, .. } => project_id.as_deref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,13 +28,18 @@ pub struct GeneratedFileHash {
|
||||
)]
|
||||
#[diesel(sql_type = diesel::sql_types::Text)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum NotificationEntity {
|
||||
pub enum DomainEntity {
|
||||
Post,
|
||||
Media,
|
||||
Tag,
|
||||
Script,
|
||||
Template,
|
||||
Project,
|
||||
Setting,
|
||||
}
|
||||
|
||||
pub type NotificationEntity = DomainEntity;
|
||||
|
||||
#[derive(
|
||||
Debug, Clone, PartialEq, Eq, Serialize, Deserialize, diesel::AsExpression, diesel::FromSqlRow,
|
||||
)]
|
||||
@@ -46,27 +51,33 @@ pub enum NotificationAction {
|
||||
Deleted,
|
||||
}
|
||||
|
||||
impl std::str::FromStr for NotificationEntity {
|
||||
impl std::str::FromStr for DomainEntity {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
match value {
|
||||
"post" => Ok(Self::Post),
|
||||
"media" => Ok(Self::Media),
|
||||
"tag" => Ok(Self::Tag),
|
||||
"script" => Ok(Self::Script),
|
||||
"template" => Ok(Self::Template),
|
||||
"project" => Ok(Self::Project),
|
||||
"setting" => Ok(Self::Setting),
|
||||
_ => Err(format!("invalid NotificationEntity: {value}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NotificationEntity {
|
||||
impl DomainEntity {
|
||||
pub const fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Post => "post",
|
||||
Self::Media => "media",
|
||||
Self::Tag => "tag",
|
||||
Self::Script => "script",
|
||||
Self::Template => "template",
|
||||
Self::Project => "project",
|
||||
Self::Setting => "setting",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,8 +113,7 @@ impl NotificationAction {
|
||||
check_for_backend(diesel::sqlite::Sqlite)
|
||||
)]
|
||||
pub struct DbNotification {
|
||||
#[diesel(deserialize_as = i32)]
|
||||
pub id: i64,
|
||||
pub id: i32,
|
||||
pub entity_type: NotificationEntity,
|
||||
pub entity_id: String,
|
||||
pub action: NotificationAction,
|
||||
@@ -112,6 +122,7 @@ pub struct DbNotification {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub seen_at: Option<i64>,
|
||||
pub created_at: i64,
|
||||
pub project_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
mod event;
|
||||
mod generation;
|
||||
mod import;
|
||||
mod media;
|
||||
@@ -8,8 +9,9 @@ mod script;
|
||||
mod tag;
|
||||
mod template;
|
||||
|
||||
pub use event::DomainEvent;
|
||||
pub use generation::{
|
||||
DbNotification, GeneratedFileHash, NotificationAction, NotificationEntity,
|
||||
DbNotification, DomainEntity, GeneratedFileHash, NotificationAction, NotificationEntity,
|
||||
PublishingPreferences, SshMode,
|
||||
};
|
||||
pub use import::{
|
||||
|
||||
Reference in New Issue
Block a user