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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use ruma::{
events::{
room::member::{MembershipState, RoomMemberEventContent},
RedactContent, RedactedEventContent, StateEventContent, StrippedStateEvent, SyncStateEvent,
},
EventId, OwnedEventId, RoomVersionId,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum MinimalStateEvent<C: StateEventContent + RedactContent>
where
C::Redacted: StateEventContent + RedactedEventContent + DeserializeOwned,
{
Original(OriginalMinimalStateEvent<C>),
Redacted(RedactedMinimalStateEvent<C::Redacted>),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OriginalMinimalStateEvent<C>
where
C: StateEventContent,
{
pub content: C,
pub event_id: Option<OwnedEventId>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RedactedMinimalStateEvent<C>
where
C: StateEventContent + RedactedEventContent,
{
pub content: C,
pub event_id: Option<OwnedEventId>,
}
impl<C> MinimalStateEvent<C>
where
C: StateEventContent + RedactContent,
C::Redacted: StateEventContent + RedactedEventContent + DeserializeOwned,
{
pub fn event_id(&self) -> Option<&EventId> {
match self {
MinimalStateEvent::Original(ev) => ev.event_id.as_deref(),
MinimalStateEvent::Redacted(ev) => ev.event_id.as_deref(),
}
}
pub fn as_original(&self) -> Option<&OriginalMinimalStateEvent<C>> {
match self {
MinimalStateEvent::Original(ev) => Some(ev),
MinimalStateEvent::Redacted(_) => None,
}
}
pub fn into_original(self) -> Option<OriginalMinimalStateEvent<C>> {
match self {
MinimalStateEvent::Original(ev) => Some(ev),
MinimalStateEvent::Redacted(_) => None,
}
}
pub fn redact(&mut self, room_version: &RoomVersionId)
where
C: Clone,
{
if let MinimalStateEvent::Original(ev) = self {
*self = MinimalStateEvent::Redacted(RedactedMinimalStateEvent {
content: ev.content.clone().redact(room_version),
event_id: ev.event_id.clone(),
});
}
}
}
pub type MinimalRoomMemberEvent = MinimalStateEvent<RoomMemberEventContent>;
impl MinimalRoomMemberEvent {
pub fn membership(&self) -> &MembershipState {
match self {
MinimalStateEvent::Original(ev) => &ev.content.membership,
MinimalStateEvent::Redacted(ev) => &ev.content.membership,
}
}
}
impl<C> From<&SyncStateEvent<C>> for MinimalStateEvent<C>
where
C: Clone + StateEventContent + RedactContent,
C::Redacted: Clone + StateEventContent + RedactedEventContent + DeserializeOwned,
{
fn from(ev: &SyncStateEvent<C>) -> Self {
match ev {
SyncStateEvent::Original(ev) => Self::Original(OriginalMinimalStateEvent {
content: ev.content.clone(),
event_id: Some(ev.event_id.clone()),
}),
SyncStateEvent::Redacted(ev) => Self::Redacted(RedactedMinimalStateEvent {
content: ev.content.clone(),
event_id: Some(ev.event_id.clone()),
}),
}
}
}
impl<C> From<&StrippedStateEvent<C>> for MinimalStateEvent<C>
where
C: Clone + StateEventContent + RedactContent,
C::Redacted: StateEventContent + RedactedEventContent + DeserializeOwned,
{
fn from(ev: &StrippedStateEvent<C>) -> Self {
Self::Original(OriginalMinimalStateEvent { content: ev.content.clone(), event_id: None })
}
}