diff options
| author | ojacobson <ojacobson@noreply.codeberg.org> | 2025-08-27 06:10:29 +0200 |
|---|---|---|
| committer | ojacobson <ojacobson@noreply.codeberg.org> | 2025-08-27 06:10:29 +0200 |
| commit | 8712c3a19c279d664ce75e8e90d6dde1bda56cb4 (patch) | |
| tree | 93c95548126eea048cd8962345b720b883d391c1 /src/message/history.rs | |
| parent | 7b131e35fdea1a68aaf9230d157bafb200557ef8 (diff) | |
| parent | f839449d5505b5352bd0da931b980a7a0305234f (diff) | |
Implement storage of synchronized entities in terms of events, not state.
Conversations, users, messages, and all other "synchronized" entities now have an in-memory implementation of their lifecycle, rather than a database-backed one. These operations take a history, apply one lifecycle change to that history, and emit a new history. Storage is then implemented by applying the events in this new history to the database.
The storage methods in repo types, which process these events by emitting SQL statements, make necessary assumptions that the events being passed to them are coherent with the data already in storage. For example, the code to handle a conversation's delete event is allowed to assume that the database already contains a row for that conversation, inserted in response to a prior conversation creation event.
Data retrieval is not modified in this commit, and probably never will be without a more thorough storage rewrite. The whole intention of the data modelling approach I've been using is that a single row per entity represents its entire history, in turn so that the data in the database should be legible to people approaching it using normal SQL tools.
Developed as an aesthetic response to increasing unease with the lack of an ORM versus the boring-ness of our actual queries.
Merges event-based-storage into main.
Diffstat (limited to 'src/message/history.rs')
| -rw-r--r-- | src/message/history.rs | 70 |
1 files changed, 57 insertions, 13 deletions
diff --git a/src/message/history.rs b/src/message/history.rs index 2abdf2c..92cecc9 100644 --- a/src/message/history.rs +++ b/src/message/history.rs @@ -1,18 +1,67 @@ use itertools::Itertools as _; use super::{ - Message, + Body, Id, Message, event::{Deleted, Event, Sent}, }; -use crate::event::Sequence; +use crate::{ + conversation::Conversation, + event::{Instant, Sequence}, + user::{self, User}, +}; #[derive(Clone, Debug, Eq, PartialEq)] pub struct History { pub message: Message, } +// Lifecycle interface +impl History { + pub fn begin(conversation: &Conversation, sender: &User, body: &Body, sent: Instant) -> Self { + Self { + message: Message { + id: Id::generate(), + conversation: conversation.id.clone(), + sender: sender.id.clone(), + body: body.clone(), + sent, + deleted: None, + }, + } + } + + pub fn delete(self, deleted: Instant) -> Result<Self, DeleteError> { + if self.message.deleted.is_none() { + Ok(Self { + message: Message { + deleted: Some(deleted), + ..self.message + }, + }) + } else { + Err(DeleteError::Deleted(self.into())) + } + } +} + +#[derive(Debug, thiserror::Error)] +pub enum DeleteError { + #[error("message {} already deleted", .0.message.id)] + // Payload is boxed here to avoid copying an entire `History` around in any errors this error + // gets chained into. See <https://rust-lang.github.io/rust-clippy/master/index.html#result_large_err>. + Deleted(Box<History>), +} + // State interface impl History { + pub fn id(&self) -> &Id { + &self.message.id + } + + pub fn sender(&self) -> &user::Id { + &self.message.sender + } + // Snapshot of this message as it was when sent. (Note to the future: it's okay // if this returns a redacted or modified version of the message. If we // implement message editing by redacting the original body, then this should @@ -30,15 +79,16 @@ impl History { .filter(Sequence::up_to(sequence.into())) .collect() } - - // Snapshot of this message as of all events recorded in this history. - pub fn as_snapshot(&self) -> Option<Message> { - self.events().collect() - } } // Events interface impl History { + pub fn events(&self) -> impl Iterator<Item = Event> + Clone + use<> { + [self.sent()] + .into_iter() + .merge_by(self.deleted(), Sequence::merge) + } + fn sent(&self) -> Event { Sent { message: self.message.clone(), @@ -55,10 +105,4 @@ impl History { .into() }) } - - pub fn events(&self) -> impl Iterator<Item = Event> + use<> { - [self.sent()] - .into_iter() - .merge_by(self.deleted(), Sequence::merge) - } } |
