Skip to main content

tuwunel_core/matrix/
event.rs

1mod 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
30/// Abstraction of a PDU so users can have their own PDU types.
31pub 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	/// Serialize into a Ruma JSON format, consuming.
38	#[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	/// Serialize into a Ruma JSON format
48	#[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	//
161	// Canonical properties
162	//
163
164	/// All the authenticating events for this event.
165	fn auth_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_;
166
167	/// All the authenticating events for this event.
168	fn auth_events_into(
169		self,
170	) -> impl IntoIterator<IntoIter = impl Iterator<Item = OwnedEventId>> + Send;
171
172	/// The event's content.
173	fn content(&self) -> &RawJsonValue;
174
175	/// The `EventId` of this event.
176	fn event_id(&self) -> &EventId;
177
178	/// The time of creation on the originating server.
179	fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
180
181	/// The events before this event.
182	fn prev_events(&self) -> impl DoubleEndedIterator<Item = &EventId> + Clone + Send + '_;
183
184	/// If this event is a redaction event this is the event it redacts.
185	fn redacts(&self) -> Option<&EventId>;
186
187	/// see: <https://spec.matrix.org/v1.14/rooms/v11/#rejected-events>
188	fn rejected(&self) -> bool;
189
190	/// The `RoomId` of this event.
191	fn room_id(&self) -> &RoomId;
192
193	/// The `UserId` of this event.
194	fn sender(&self) -> &UserId;
195
196	/// The state key for this event.
197	fn state_key(&self) -> Option<&str>;
198
199	/// The event type.
200	fn kind(&self) -> &TimelineEventType;
201
202	/// Metadata container; peer-trusted only.
203	fn unsigned(&self) -> Option<&RawJsonValue>;
204
205	//#[deprecated]
206	#[inline]
207	fn event_type(&self) -> &TimelineEventType { self.kind() }
208}