ruma_state_res/events/
traits.rs

1use std::{
2    borrow::Borrow,
3    fmt::{Debug, Display},
4    hash::Hash,
5    sync::Arc,
6};
7
8use ruma_common::{EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId};
9use ruma_events::TimelineEventType;
10use serde_json::value::RawValue as RawJsonValue;
11
12/// Abstraction of a PDU so users can have their own PDU types.
13pub trait Event {
14    type Id: Clone + Debug + Display + Eq + Ord + Hash + Borrow<EventId>;
15
16    /// The `EventId` of this event.
17    fn event_id(&self) -> &Self::Id;
18
19    /// The `RoomId` of this event.
20    fn room_id(&self) -> &RoomId;
21
22    /// The `UserId` of this event.
23    fn sender(&self) -> &UserId;
24
25    /// The time of creation on the originating server.
26    fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
27
28    /// The event type.
29    fn event_type(&self) -> &TimelineEventType;
30
31    /// The event's content.
32    fn content(&self) -> &RawJsonValue;
33
34    /// The state key for this event.
35    fn state_key(&self) -> Option<&str>;
36
37    /// The events before this event.
38    // Requires GATs to avoid boxing (and TAIT for making it convenient).
39    fn prev_events(&self) -> Box<dyn DoubleEndedIterator<Item = &Self::Id> + '_>;
40
41    /// All the authenticating events for this event.
42    // Requires GATs to avoid boxing (and TAIT for making it convenient).
43    fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &Self::Id> + '_>;
44
45    /// If this event is a redaction event this is the event it redacts.
46    fn redacts(&self) -> Option<&Self::Id>;
47}
48
49impl<T: Event> Event for &T {
50    type Id = T::Id;
51
52    fn event_id(&self) -> &Self::Id {
53        (*self).event_id()
54    }
55
56    fn room_id(&self) -> &RoomId {
57        (*self).room_id()
58    }
59
60    fn sender(&self) -> &UserId {
61        (*self).sender()
62    }
63
64    fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
65        (*self).origin_server_ts()
66    }
67
68    fn event_type(&self) -> &TimelineEventType {
69        (*self).event_type()
70    }
71
72    fn content(&self) -> &RawJsonValue {
73        (*self).content()
74    }
75
76    fn state_key(&self) -> Option<&str> {
77        (*self).state_key()
78    }
79
80    fn prev_events(&self) -> Box<dyn DoubleEndedIterator<Item = &Self::Id> + '_> {
81        (*self).prev_events()
82    }
83
84    fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &Self::Id> + '_> {
85        (*self).auth_events()
86    }
87
88    fn redacts(&self) -> Option<&Self::Id> {
89        (*self).redacts()
90    }
91}
92
93impl<T: Event> Event for Arc<T> {
94    type Id = T::Id;
95
96    fn event_id(&self) -> &Self::Id {
97        (**self).event_id()
98    }
99
100    fn room_id(&self) -> &RoomId {
101        (**self).room_id()
102    }
103
104    fn sender(&self) -> &UserId {
105        (**self).sender()
106    }
107
108    fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
109        (**self).origin_server_ts()
110    }
111
112    fn event_type(&self) -> &TimelineEventType {
113        (**self).event_type()
114    }
115
116    fn content(&self) -> &RawJsonValue {
117        (**self).content()
118    }
119
120    fn state_key(&self) -> Option<&str> {
121        (**self).state_key()
122    }
123
124    fn prev_events(&self) -> Box<dyn DoubleEndedIterator<Item = &Self::Id> + '_> {
125        (**self).prev_events()
126    }
127
128    fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &Self::Id> + '_> {
129        (**self).auth_events()
130    }
131
132    fn redacts(&self) -> Option<&Self::Id> {
133        (**self).redacts()
134    }
135}