From 53944ef14af4d37c08464cb1bb9f3a8f09277194 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Thu, 21 Aug 2025 23:47:15 -0400 Subject: Collapse redundant "deleted_at" timestaps and "deleted" event instants. These were separated as there wasn't an obvious way to serialize two fields with the same _type_ with different _prefixes_. Turns out this is a common problem, and someone's written a crate for it that remaps the names for you. --- src/conversation/history.rs | 5 ++--- src/conversation/repo.rs | 15 +++++---------- src/conversation/snapshot.rs | 14 +++++++++++--- src/message/history.rs | 5 ++--- src/message/repo.rs | 18 ++++++------------ src/message/snapshot.rs | 13 ++++++++++--- 6 files changed, 36 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/conversation/history.rs b/src/conversation/history.rs index 601614c..746a1b0 100644 --- a/src/conversation/history.rs +++ b/src/conversation/history.rs @@ -4,12 +4,11 @@ use super::{ Conversation, Id, event::{Created, Deleted, Event}, }; -use crate::event::{Instant, Sequence}; +use crate::event::Sequence; #[derive(Clone, Debug, Eq, PartialEq)] pub struct History { pub conversation: Conversation, - pub deleted: Option, } // State interface @@ -58,7 +57,7 @@ impl History { } fn deleted(&self) -> Option { - self.deleted.map(|instant| { + self.conversation.deleted.map(|instant| { Deleted { instant, id: self.conversation.id.clone(), diff --git a/src/conversation/repo.rs b/src/conversation/repo.rs index e40a5bd..7e38b62 100644 --- a/src/conversation/repo.rs +++ b/src/conversation/repo.rs @@ -59,9 +59,8 @@ impl Conversations<'_> { created, id, name: name.clone(), - deleted_at: None, + deleted: None, }, - deleted: None, }; Ok(conversation) @@ -93,9 +92,8 @@ impl Conversations<'_> { created: Instant::new(row.created_at, row.created_sequence), id: row.id, name: Name::optional(row.display_name, row.canonical_name)?.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) }) .fetch_one(&mut *self.0) @@ -131,9 +129,8 @@ impl Conversations<'_> { created: Instant::new(row.created_at, row.created_sequence), id: row.id, name: Name::optional(row.display_name, row.canonical_name)?.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) }) .fetch(&mut *self.0) @@ -170,9 +167,8 @@ impl Conversations<'_> { created: Instant::new(row.created_at, row.created_sequence), id: row.id, name: Name::optional(row.display_name, row.canonical_name)?.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) }) .fetch(&mut *self.0) @@ -297,9 +293,8 @@ impl Conversations<'_> { created: Instant::new(row.created_at, row.created_sequence), id: row.id, name: Name::optional(row.display_name, row.canonical_name)?.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) }) .fetch(&mut *self.0) diff --git a/src/conversation/snapshot.rs b/src/conversation/snapshot.rs index da9eaae..440f3c0 100644 --- a/src/conversation/snapshot.rs +++ b/src/conversation/snapshot.rs @@ -1,8 +1,10 @@ +use serde_with::with_prefix; + use super::{ Id, event::{Created, Event}, }; -use crate::{clock::DateTime, event::Instant, name::Name}; +use crate::{event::Instant, name::Name}; #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] pub struct Conversation { @@ -10,10 +12,16 @@ pub struct Conversation { pub created: Instant, pub id: Id, pub name: Name, - #[serde(skip_serializing_if = "Option::is_none")] - pub deleted_at: Option, + #[serde( + flatten, + with = "prefix_deleted", + skip_serializing_if = "Option::is_none" + )] + pub deleted: Option, } +with_prefix!(prefix_deleted "deleted_"); + impl Conversation { fn apply(state: Option, event: Event) -> Option { match (state, event) { diff --git a/src/message/history.rs b/src/message/history.rs index 7585e1c..d4d4500 100644 --- a/src/message/history.rs +++ b/src/message/history.rs @@ -4,12 +4,11 @@ use super::{ Id, Message, event::{Deleted, Event, Sent}, }; -use crate::event::{Instant, Sequence}; +use crate::event::Sequence; #[derive(Clone, Debug, Eq, PartialEq)] pub struct History { pub message: Message, - pub deleted: Option, } // State interface @@ -43,7 +42,7 @@ impl History { } fn deleted(&self) -> Option { - self.deleted.map(|instant| { + self.message.deleted.map(|instant| { Deleted { instant, id: self.message.id.clone(), diff --git a/src/message/repo.rs b/src/message/repo.rs index b4c086d..2e9700a 100644 --- a/src/message/repo.rs +++ b/src/message/repo.rs @@ -58,9 +58,8 @@ impl Messages<'_> { sender: row.sender, id: row.id, body: row.body.unwrap_or_default(), - deleted_at: None, + deleted: None, }, - deleted: None, }) .fetch_one(&mut *self.0) .await?; @@ -99,9 +98,8 @@ impl Messages<'_> { sender: row.sender, id: row.id, body: row.body.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) .fetch_all(&mut *self.0) .await?; @@ -136,9 +134,8 @@ impl Messages<'_> { sender: row.sender, id: row.id, body: row.body.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) .fetch_all(&mut *self.0) .await?; @@ -172,9 +169,8 @@ impl Messages<'_> { sender: row.sender, id: row.id, body: row.body.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) .fetch_one(&mut *self.0) .await?; @@ -277,9 +273,8 @@ impl Messages<'_> { conversation: row.conversation, sender: row.sender, body: row.body.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) .fetch_all(&mut *self.0) .await?; @@ -313,9 +308,8 @@ impl Messages<'_> { sender: row.sender, id: row.id, body: row.body.unwrap_or_default(), - deleted_at: row.deleted_at, + deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }, - deleted: Instant::optional(row.deleted_at, row.deleted_sequence), }) .fetch_all(&mut *self.0) .await?; diff --git a/src/message/snapshot.rs b/src/message/snapshot.rs index 12d4daa..19a98a3 100644 --- a/src/message/snapshot.rs +++ b/src/message/snapshot.rs @@ -2,7 +2,8 @@ use super::{ Body, Id, event::{Event, Sent}, }; -use crate::{clock::DateTime, conversation, event::Instant, user}; +use crate::{conversation, event::Instant, user}; +use serde_with::with_prefix; #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] pub struct Message { @@ -12,10 +13,16 @@ pub struct Message { pub sender: user::Id, pub id: Id, pub body: Body, - #[serde(skip_serializing_if = "Option::is_none")] - pub deleted_at: Option, + #[serde( + flatten, + with = "prefix_deleted", + skip_serializing_if = "Option::is_none" + )] + pub deleted: Option, } +with_prefix!(prefix_deleted "deleted_"); + impl Message { fn apply(state: Option, event: Event) -> Option { match (state, event) { -- cgit v1.2.3