1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
use itertools::Itertools as _;
use super::{
Body, Id, Message,
event::{Deleted, Event, Sent},
};
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
// 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<S>(&self, sequence: S) -> Option<Message>
where
S: Into<Sequence>,
{
self.events()
.filter(Sequence::up_to(sequence.into()))
.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(),
}
.into()
}
fn deleted(&self) -> Option<Event> {
self.message.deleted.map(|instant| {
Deleted {
instant,
id: self.message.id.clone(),
}
.into()
})
}
}
|