1use ruma_common::{
2 serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId,
3 TransactionId, UserId,
4};
5use ruma_macros::{event_enum, EventEnumFromEvent};
6use serde::{de, Deserialize};
7use serde_json::value::RawValue as RawJsonValue;
8
9use super::room::encrypted;
10
11pub const RECOMMENDED_STRIPPED_STATE_EVENT_TYPES: &[StateEventType] = &[
16 StateEventType::RoomCreate,
17 StateEventType::RoomName,
18 StateEventType::RoomAvatar,
19 StateEventType::RoomTopic,
20 StateEventType::RoomJoinRules,
21 StateEventType::RoomCanonicalAlias,
22 StateEventType::RoomEncryption,
23];
24
25event_enum! {
26 enum GlobalAccountData {
28 "m.direct" => super::direct,
29 "m.identity_server" => super::identity_server,
30 "m.ignored_user_list" => super::ignored_user_list,
31 "m.push_rules" => super::push_rules,
32 "m.secret_storage.default_key" => super::secret_storage::default_key,
33 "m.secret_storage.key.*" => super::secret_storage::key,
34 #[cfg(feature = "unstable-msc4278")]
35 "m.media_preview_config" => super::media_preview_config,
36 #[cfg(feature = "unstable-msc4278")]
37 #[ruma_enum(ident = UnstableMediaPreviewConfig)]
38 "io.element.msc4278.media_preview_config" => super::media_preview_config,
39 #[cfg(feature = "unstable-msc2545")]
40 #[ruma_enum(ident = AccountImagePack, alias = "m.image_pack")]
41 "im.ponies.user_emotes" => super::image_pack,
42 #[cfg(feature = "unstable-msc2545")]
43 #[ruma_enum(ident = ImagePackRooms, alias = "m.image_pack.rooms")]
44 "im.ponies.emote_rooms" => super::image_pack,
45 }
46
47 enum RoomAccountData {
49 "m.fully_read" => super::fully_read,
50 "m.tag" => super::tag,
51 "m.marked_unread" => super::marked_unread,
52 #[cfg(feature = "unstable-msc2867")]
53 #[ruma_enum(ident = UnstableMarkedUnread)]
54 "com.famedly.marked_unread" => super::marked_unread,
55 }
56
57 enum EphemeralRoom {
59 "m.receipt" => super::receipt,
60 "m.typing" => super::typing,
61 }
62
63 enum MessageLike {
65 #[cfg(feature = "unstable-msc3927")]
66 #[ruma_enum(alias = "m.audio")]
67 "org.matrix.msc1767.audio" => super::audio,
68 "m.call.answer" => super::call::answer,
69 "m.call.invite" => super::call::invite,
70 "m.call.hangup" => super::call::hangup,
71 "m.call.candidates" => super::call::candidates,
72 "m.call.negotiate" => super::call::negotiate,
73 "m.call.reject" => super::call::reject,
74 #[ruma_enum(alias = "org.matrix.call.sdp_stream_metadata_changed")]
75 "m.call.sdp_stream_metadata_changed" => super::call::sdp_stream_metadata_changed,
76 "m.call.select_answer" => super::call::select_answer,
77 #[cfg(feature = "unstable-msc3954")]
78 #[ruma_enum(alias = "m.emote")]
79 "org.matrix.msc1767.emote" => super::emote,
80 #[cfg(feature = "unstable-msc3956")]
81 #[ruma_enum(alias = "m.encrypted")]
82 "org.matrix.msc1767.encrypted" => super::encrypted,
83 #[cfg(feature = "unstable-msc3551")]
84 #[ruma_enum(alias = "m.file")]
85 "org.matrix.msc1767.file" => super::file,
86 #[cfg(feature = "unstable-msc3552")]
87 #[ruma_enum(alias = "m.image")]
88 "org.matrix.msc1767.image" => super::image,
89 "m.key.verification.ready" => super::key::verification::ready,
90 "m.key.verification.start" => super::key::verification::start,
91 "m.key.verification.cancel" => super::key::verification::cancel,
92 "m.key.verification.accept" => super::key::verification::accept,
93 "m.key.verification.key" => super::key::verification::key,
94 "m.key.verification.mac" => super::key::verification::mac,
95 "m.key.verification.done" => super::key::verification::done,
96 #[cfg(feature = "unstable-msc3488")]
97 "m.location" => super::location,
98 #[cfg(feature = "unstable-msc1767")]
99 #[ruma_enum(alias = "m.message")]
100 "org.matrix.msc1767.message" => super::message,
101 #[cfg(feature = "unstable-msc3381")]
102 "m.poll.start" => super::poll::start,
103 #[cfg(feature = "unstable-msc3381")]
104 #[ruma_enum(ident = UnstablePollStart)]
105 "org.matrix.msc3381.poll.start" => super::poll::unstable_start,
106 #[cfg(feature = "unstable-msc3381")]
107 "m.poll.response" => super::poll::response,
108 #[cfg(feature = "unstable-msc3381")]
109 #[ruma_enum(ident = UnstablePollResponse)]
110 "org.matrix.msc3381.poll.response" => super::poll::unstable_response,
111 #[cfg(feature = "unstable-msc3381")]
112 "m.poll.end" => super::poll::end,
113 #[cfg(feature = "unstable-msc3381")]
114 #[ruma_enum(ident = UnstablePollEnd)]
115 "org.matrix.msc3381.poll.end" => super::poll::unstable_end,
116 #[cfg(feature = "unstable-msc3489")]
117 #[ruma_enum(alias = "m.beacon")]
118 "org.matrix.msc3672.beacon" => super::beacon,
119 "m.reaction" => super::reaction,
120 "m.room.encrypted" => super::room::encrypted,
121 "m.room.message" => super::room::message,
122 "m.room.redaction" => super::room::redaction,
123 "m.sticker" => super::sticker,
124 #[cfg(feature = "unstable-msc3553")]
125 #[ruma_enum(alias = "m.video")]
126 "org.matrix.msc1767.video" => super::video,
127 #[cfg(feature = "unstable-msc3245")]
128 #[ruma_enum(alias = "m.voice")]
129 "org.matrix.msc3245.voice.v2" => super::voice,
130 #[cfg(feature = "unstable-msc4075")]
131 #[ruma_enum(alias = "m.call.notify")]
132 "org.matrix.msc4075.call.notify" => super::call::notify,
133 }
134
135 enum State {
137 "m.policy.rule.room" => super::policy::rule::room,
138 "m.policy.rule.server" => super::policy::rule::server,
139 "m.policy.rule.user" => super::policy::rule::user,
140 "m.room.aliases" => super::room::aliases,
141 "m.room.avatar" => super::room::avatar,
142 "m.room.canonical_alias" => super::room::canonical_alias,
143 "m.room.create" => super::room::create,
144 "m.room.encryption" => super::room::encryption,
145 "m.room.guest_access" => super::room::guest_access,
146 "m.room.history_visibility" => super::room::history_visibility,
147 "m.room.join_rules" => super::room::join_rules,
148 "m.room.member" => super::room::member,
149 "m.room.name" => super::room::name,
150 "m.room.pinned_events" => super::room::pinned_events,
151 "m.room.power_levels" => super::room::power_levels,
152 "m.room.server_acl" => super::room::server_acl,
153 "m.room.third_party_invite" => super::room::third_party_invite,
154 "m.room.tombstone" => super::room::tombstone,
155 "m.room.topic" => super::room::topic,
156 "m.space.child" => super::space::child,
157 "m.space.parent" => super::space::parent,
158 #[cfg(feature = "unstable-msc2545")]
159 #[ruma_enum(ident = RoomImagePack, alias = "m.image_pack")]
160 "im.ponies.room_emotes" => super::image_pack,
161 #[cfg(feature = "unstable-msc3489")]
162 #[ruma_enum(alias = "m.beacon_info")]
163 "org.matrix.msc3672.beacon_info" => super::beacon_info,
164 #[cfg(feature = "unstable-msc3401")]
165 #[ruma_enum(alias = "m.call.member")]
166 "org.matrix.msc3401.call.member" => super::call::member,
167 #[cfg(feature = "unstable-msc4171")]
168 #[ruma_enum(alias = "m.member_hints")]
169 "io.element.functional_members" => super::member_hints,
170 }
171
172 enum ToDevice {
174 "m.dummy" => super::dummy,
175 "m.room_key" => super::room_key,
176 "m.room_key_request" => super::room_key_request,
177 "m.forwarded_room_key" => super::forwarded_room_key,
178 "m.key.verification.request" => super::key::verification::request,
179 "m.key.verification.ready" => super::key::verification::ready,
180 "m.key.verification.start" => super::key::verification::start,
181 "m.key.verification.cancel" => super::key::verification::cancel,
182 "m.key.verification.accept" => super::key::verification::accept,
183 "m.key.verification.key" => super::key::verification::key,
184 "m.key.verification.mac" => super::key::verification::mac,
185 "m.key.verification.done" => super::key::verification::done,
186 "m.room.encrypted" => super::room::encrypted,
187 "m.secret.request"=> super::secret::request,
188 "m.secret.send" => super::secret::send,
189 }
190}
191
192macro_rules! timeline_event_accessors {
193 (
194 $(
195 #[doc = $docs:literal]
196 pub fn $field:ident(&self) -> $ty:ty;
197 )*
198 ) => {
199 $(
200 #[doc = $docs]
201 pub fn $field(&self) -> $ty {
202 match self {
203 Self::MessageLike(ev) => ev.$field(),
204 Self::State(ev) => ev.$field(),
205 }
206 }
207 )*
208 };
209}
210
211#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
213#[derive(Clone, Debug, EventEnumFromEvent)]
214pub enum AnyTimelineEvent {
215 MessageLike(AnyMessageLikeEvent),
217
218 State(AnyStateEvent),
220}
221
222impl AnyTimelineEvent {
223 timeline_event_accessors! {
224 pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
226
227 pub fn room_id(&self) -> &RoomId;
229
230 pub fn event_id(&self) -> &EventId;
232
233 pub fn sender(&self) -> &UserId;
235
236 pub fn transaction_id(&self) -> Option<&TransactionId>;
238 }
239
240 pub fn event_type(&self) -> TimelineEventType {
242 match self {
243 Self::MessageLike(e) => e.event_type().into(),
244 Self::State(e) => e.event_type().into(),
245 }
246 }
247}
248
249#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
253#[derive(Clone, Debug, EventEnumFromEvent)]
254pub enum AnySyncTimelineEvent {
255 MessageLike(AnySyncMessageLikeEvent),
257
258 State(AnySyncStateEvent),
260}
261
262impl AnySyncTimelineEvent {
263 timeline_event_accessors! {
264 pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
266
267 pub fn event_id(&self) -> &EventId;
269
270 pub fn sender(&self) -> &UserId;
272
273 pub fn transaction_id(&self) -> Option<&TransactionId>;
275 }
276
277 pub fn event_type(&self) -> TimelineEventType {
279 match self {
280 Self::MessageLike(e) => e.event_type().into(),
281 Self::State(e) => e.event_type().into(),
282 }
283 }
284
285 pub fn into_full_event(self, room_id: OwnedRoomId) -> AnyTimelineEvent {
287 match self {
288 Self::MessageLike(ev) => AnyTimelineEvent::MessageLike(ev.into_full_event(room_id)),
289 Self::State(ev) => AnyTimelineEvent::State(ev.into_full_event(room_id)),
290 }
291 }
292}
293
294impl From<AnyTimelineEvent> for AnySyncTimelineEvent {
295 fn from(ev: AnyTimelineEvent) -> Self {
296 match ev {
297 AnyTimelineEvent::MessageLike(ev) => Self::MessageLike(ev.into()),
298 AnyTimelineEvent::State(ev) => Self::State(ev.into()),
299 }
300 }
301}
302
303#[derive(Deserialize)]
304#[allow(clippy::exhaustive_structs)]
305struct EventDeHelper {
306 state_key: Option<de::IgnoredAny>,
307}
308
309impl<'de> Deserialize<'de> for AnyTimelineEvent {
310 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
311 where
312 D: de::Deserializer<'de>,
313 {
314 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
315 let EventDeHelper { state_key } = from_raw_json_value(&json)?;
316
317 if state_key.is_some() {
318 Ok(AnyTimelineEvent::State(from_raw_json_value(&json)?))
319 } else {
320 Ok(AnyTimelineEvent::MessageLike(from_raw_json_value(&json)?))
321 }
322 }
323}
324
325impl<'de> Deserialize<'de> for AnySyncTimelineEvent {
326 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
327 where
328 D: de::Deserializer<'de>,
329 {
330 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
331 let EventDeHelper { state_key } = from_raw_json_value(&json)?;
332
333 if state_key.is_some() {
334 Ok(AnySyncTimelineEvent::State(from_raw_json_value(&json)?))
335 } else {
336 Ok(AnySyncTimelineEvent::MessageLike(from_raw_json_value(&json)?))
337 }
338 }
339}
340
341impl AnyMessageLikeEventContent {
342 pub fn relation(&self) -> Option<encrypted::Relation> {
347 #[cfg(feature = "unstable-msc3489")]
348 use super::beacon::BeaconEventContent;
349 use super::key::verification::{
350 accept::KeyVerificationAcceptEventContent, cancel::KeyVerificationCancelEventContent,
351 done::KeyVerificationDoneEventContent, key::KeyVerificationKeyEventContent,
352 mac::KeyVerificationMacEventContent, ready::KeyVerificationReadyEventContent,
353 start::KeyVerificationStartEventContent,
354 };
355 #[cfg(feature = "unstable-msc3381")]
356 use super::poll::{
357 end::PollEndEventContent, response::PollResponseEventContent,
358 unstable_end::UnstablePollEndEventContent,
359 unstable_response::UnstablePollResponseEventContent,
360 };
361
362 match self {
363 #[rustfmt::skip]
364 Self::KeyVerificationReady(KeyVerificationReadyEventContent { relates_to, .. })
365 | Self::KeyVerificationStart(KeyVerificationStartEventContent { relates_to, .. })
366 | Self::KeyVerificationCancel(KeyVerificationCancelEventContent { relates_to, .. })
367 | Self::KeyVerificationAccept(KeyVerificationAcceptEventContent { relates_to, .. })
368 | Self::KeyVerificationKey(KeyVerificationKeyEventContent { relates_to, .. })
369 | Self::KeyVerificationMac(KeyVerificationMacEventContent { relates_to, .. })
370 | Self::KeyVerificationDone(KeyVerificationDoneEventContent { relates_to, .. }) => {
371 Some(encrypted::Relation::Reference(relates_to.clone()))
372 },
373 Self::Reaction(ev) => Some(encrypted::Relation::Annotation(ev.relates_to.clone())),
374 Self::RoomEncrypted(ev) => ev.relates_to.clone(),
375 Self::RoomMessage(ev) => ev.relates_to.clone().map(Into::into),
376 #[cfg(feature = "unstable-msc1767")]
377 Self::Message(ev) => ev.relates_to.clone().map(Into::into),
378 #[cfg(feature = "unstable-msc3954")]
379 Self::Emote(ev) => ev.relates_to.clone().map(Into::into),
380 #[cfg(feature = "unstable-msc3956")]
381 Self::Encrypted(ev) => ev.relates_to.clone(),
382 #[cfg(feature = "unstable-msc3245")]
383 Self::Voice(ev) => ev.relates_to.clone().map(Into::into),
384 #[cfg(feature = "unstable-msc3927")]
385 Self::Audio(ev) => ev.relates_to.clone().map(Into::into),
386 #[cfg(feature = "unstable-msc3488")]
387 Self::Location(ev) => ev.relates_to.clone().map(Into::into),
388 #[cfg(feature = "unstable-msc3551")]
389 Self::File(ev) => ev.relates_to.clone().map(Into::into),
390 #[cfg(feature = "unstable-msc3552")]
391 Self::Image(ev) => ev.relates_to.clone().map(Into::into),
392 #[cfg(feature = "unstable-msc3553")]
393 Self::Video(ev) => ev.relates_to.clone().map(Into::into),
394 #[cfg(feature = "unstable-msc3381")]
395 Self::PollResponse(PollResponseEventContent { relates_to, .. })
396 | Self::UnstablePollResponse(UnstablePollResponseEventContent { relates_to, .. })
397 | Self::PollEnd(PollEndEventContent { relates_to, .. })
398 | Self::UnstablePollEnd(UnstablePollEndEventContent { relates_to, .. }) => {
399 Some(encrypted::Relation::Reference(relates_to.clone()))
400 }
401 #[cfg(feature = "unstable-msc3489")]
402 Self::Beacon(BeaconEventContent { relates_to, .. }) => {
403 Some(encrypted::Relation::Reference(relates_to.clone()))
404 }
405 #[cfg(feature = "unstable-msc3381")]
406 Self::PollStart(_) | Self::UnstablePollStart(_) => None,
407 #[cfg(feature = "unstable-msc4075")]
408 Self::CallNotify(_) => None,
409 Self::CallSdpStreamMetadataChanged(_)
410 | Self::CallNegotiate(_)
411 | Self::CallReject(_)
412 | Self::CallSelectAnswer(_)
413 | Self::CallAnswer(_)
414 | Self::CallInvite(_)
415 | Self::CallHangup(_)
416 | Self::CallCandidates(_)
417 | Self::RoomRedaction(_)
418 | Self::Sticker(_)
419 | Self::_Custom { .. } => None,
420 }
421 }
422}