use itertools::Itertools as _; use super::{ Id, Message, event::{Deleted, Event, Sent}, }; use crate::event::{Instant, Sequence}; #[derive(Clone, Debug, Eq, PartialEq)] pub struct History { pub message: Message, pub deleted: Option, } // State interface impl History { pub fn id(&self) -> &Id { &self.message.id } // 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 // return the edited message, not the original, even if that's not how it was // "as sent.") pub fn as_sent(&self) -> Message { self.message.clone() } pub fn as_of(&self, resume_point: Sequence) -> Option { self.events() .filter(Sequence::up_to(resume_point)) .collect() } // Snapshot of this message as of all events recorded in this history. pub fn as_snapshot(&self) -> Option { self.events().collect() } } // Events interface impl History { fn sent(&self) -> Event { Sent { message: self.message.clone(), } .into() } fn deleted(&self) -> Option { self.deleted.map(|instant| { Deleted { instant, id: self.message.id.clone(), } .into() }) } pub fn events(&self) -> impl Iterator + use<> { [self.sent()] .into_iter() .merge_by(self.deleted(), Sequence::merge) } }