tuwunel_core/matrix/
event.rs1mod content;
2mod filter;
3mod format;
4mod id;
5mod redact;
6mod relation;
7pub mod state_key;
8mod type_ext;
9mod unsigned;
10
11use std::fmt::Debug;
12
13use ruma::{
14 CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, UserId,
15 events::TimelineEventType, room_version_rules::RoomVersionRules,
16};
17use serde::Deserialize;
18use serde_json::{Value as JsonValue, value::RawValue as RawJsonValue};
19
20pub use self::{
21 filter::Matches,
22 id::*,
23 relation::RelationTypeEqual,
24 state_key::{StateKey, TypeStateKey},
25 type_ext::TypeExt,
26};
27use super::pdu::Pdu;
28use crate::{Result, utils};
29
30pub trait Event: Clone + Debug + Send + Sync {
32 #[inline]
33 fn is_type_and_state_key(&self, kind: &TimelineEventType, state_key: &str) -> bool {
34 self.kind() == kind && self.state_key() == Some(state_key)
35 }
36
37 #[inline]
39 fn into_format<T>(self) -> T
40 where
41 T: From<format::Owned<Self>>,
42 Self: Sized,
43 {
44 format::Owned(self).into()
45 }
46
47 #[inline]
49 fn to_format<'a, T>(&'a self) -> T
50 where
51 T: From<format::Ref<'a, Self>>,
52 Self: Sized + 'a,
53 {
54 format::Ref(self).into()
55 }
56
57 #[inline]
58 fn contains_unsigned_property<T>(&self, property: &str, is_type: T) -> bool
59 where
60 T: FnOnce(&JsonValue) -> bool,
61 Self: Sized,
62 {
63 unsigned::contains_unsigned_property::<T, _>(self, property, is_type)
64 }
65
66 #[inline]
67 fn get_unsigned_property<T>(&self, property: &str) -> Result<T>
68 where
69 T: for<'de> Deserialize<'de>,
70 Self: Sized,
71 {
72 unsigned::get_unsigned_property::<T, _>(self, property)
73 }
74
75 #[inline]
76 fn get_unsigned_as_value(&self) -> JsonValue
77 where
78 Self: Sized,
79 {
80 unsigned::get_unsigned_as_value(self)
81 }
82
83 #[inline]
84 fn get_unsigned<T>(&self) -> Result<T>
85 where
86 T: for<'de> Deserialize<'de>,
87 Self: Sized,
88 {
89 unsigned::get_unsigned::<T, _>(self)
90 }
91
92 #[inline]
93 fn get_content_as_value(&self) -> JsonValue
94 where
95 Self: Sized,
96 {
97 content::as_value(self)
98 }
99
100 #[inline]
101 fn get_content<T>(&self) -> Result<T>
102 where
103 for<'de> T: Deserialize<'de>,
104 Self: Sized,
105 {
106 content::get::<T, _>(self)
107 }
108
109 #[inline]
110 fn redacts_id(&self, room_rules: &RoomVersionRules) -> Option<OwnedEventId>
111 where
112 Self: Sized,
113 {
114 redact::redacts_id(self, room_rules)
115 }
116
117 #[inline]
118 fn is_redacted(&self) -> bool
119 where
120 Self: Sized,
121 {
122 redact::is_redacted(self)
123 }
124
125 #[inline]
126 fn into_canonical_object(self) -> CanonicalJsonObject
127 where
128 Self: Sized,
129 {
130 utils::to_canonical_object(self.into_pdu()).expect("failed to create Value::Object")
131 }
132
133 #[inline]
134 fn to_canonical_object(&self) -> CanonicalJsonObject {
135 utils::to_canonical_object(self.as_pdu()).expect("failed to create Value::Object")
136 }
137
138 #[inline]
139 fn into_value(self) -> JsonValue
140 where
141 Self: Sized,
142 {
143 serde_json::to_value(self.into_pdu()).expect("failed to create JSON Value")
144 }
145
146 #[inline]
147 fn to_value(&self) -> JsonValue {
148 serde_json::to_value(self.as_pdu()).expect("failed to create JSON Value")
149 }
150
151 #[inline]
152 fn as_mut_pdu(&mut self) -> &mut Pdu { unimplemented!("not a mutable Pdu") }
153
154 fn as_pdu(&self) -> &Pdu;
155
156 fn into_pdu(self) -> Pdu;
157
158 fn is_owned(&self) -> bool;
159
160 fn auth_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_;
166
167 fn auth_events_into(
169 self,
170 ) -> impl IntoIterator<IntoIter = impl Iterator<Item = OwnedEventId>> + Send;
171
172 fn content(&self) -> &RawJsonValue;
174
175 fn event_id(&self) -> &EventId;
177
178 fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
180
181 fn prev_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_;
183
184 fn redacts(&self) -> Option<&EventId>;
186
187 fn rejected(&self) -> bool;
189
190 fn room_id(&self) -> &RoomId;
192
193 fn sender(&self) -> &UserId;
195
196 fn state_key(&self) -> Option<&str>;
198
199 fn kind(&self) -> &TimelineEventType;
201
202 fn unsigned(&self) -> Option<&RawJsonValue>;
204
205 #[inline]
207 fn event_type(&self) -> &TimelineEventType { self.kind() }
208}