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 #[cfg(feature = "unstable-msc4268")]
177 #[ruma_enum(alias = "m.room_key_bundle")]
178 "io.element.msc4268.room_key_bundle" => super::room_key_bundle,
179 "m.room_key_request" => super::room_key_request,
180 "m.room_key.withheld" => super::room_key::withheld,
181 "m.forwarded_room_key" => super::forwarded_room_key,
182 "m.key.verification.request" => super::key::verification::request,
183 "m.key.verification.ready" => super::key::verification::ready,
184 "m.key.verification.start" => super::key::verification::start,
185 "m.key.verification.cancel" => super::key::verification::cancel,
186 "m.key.verification.accept" => super::key::verification::accept,
187 "m.key.verification.key" => super::key::verification::key,
188 "m.key.verification.mac" => super::key::verification::mac,
189 "m.key.verification.done" => super::key::verification::done,
190 "m.room.encrypted" => super::room::encrypted,
191 "m.secret.request"=> super::secret::request,
192 "m.secret.send" => super::secret::send,
193 }
194}
195
196macro_rules! timeline_event_accessors {
197 (
198 $(
199 #[doc = $docs:literal]
200 pub fn $field:ident(&self) -> $ty:ty;
201 )*
202 ) => {
203 $(
204 #[doc = $docs]
205 pub fn $field(&self) -> $ty {
206 match self {
207 Self::MessageLike(ev) => ev.$field(),
208 Self::State(ev) => ev.$field(),
209 }
210 }
211 )*
212 };
213}
214
215#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
217#[derive(Clone, Debug, EventEnumFromEvent)]
218pub enum AnyTimelineEvent {
219 MessageLike(AnyMessageLikeEvent),
221
222 State(AnyStateEvent),
224}
225
226impl AnyTimelineEvent {
227 timeline_event_accessors! {
228 pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
230
231 pub fn room_id(&self) -> &RoomId;
233
234 pub fn event_id(&self) -> &EventId;
236
237 pub fn sender(&self) -> &UserId;
239
240 pub fn transaction_id(&self) -> Option<&TransactionId>;
242 }
243
244 pub fn event_type(&self) -> TimelineEventType {
246 match self {
247 Self::MessageLike(e) => e.event_type().into(),
248 Self::State(e) => e.event_type().into(),
249 }
250 }
251}
252
253#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
257#[derive(Clone, Debug, EventEnumFromEvent)]
258pub enum AnySyncTimelineEvent {
259 MessageLike(AnySyncMessageLikeEvent),
261
262 State(AnySyncStateEvent),
264}
265
266impl AnySyncTimelineEvent {
267 timeline_event_accessors! {
268 pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
270
271 pub fn event_id(&self) -> &EventId;
273
274 pub fn sender(&self) -> &UserId;
276
277 pub fn transaction_id(&self) -> Option<&TransactionId>;
279 }
280
281 pub fn event_type(&self) -> TimelineEventType {
283 match self {
284 Self::MessageLike(e) => e.event_type().into(),
285 Self::State(e) => e.event_type().into(),
286 }
287 }
288
289 pub fn into_full_event(self, room_id: OwnedRoomId) -> AnyTimelineEvent {
291 match self {
292 Self::MessageLike(ev) => AnyTimelineEvent::MessageLike(ev.into_full_event(room_id)),
293 Self::State(ev) => AnyTimelineEvent::State(ev.into_full_event(room_id)),
294 }
295 }
296}
297
298impl From<AnyTimelineEvent> for AnySyncTimelineEvent {
299 fn from(ev: AnyTimelineEvent) -> Self {
300 match ev {
301 AnyTimelineEvent::MessageLike(ev) => Self::MessageLike(ev.into()),
302 AnyTimelineEvent::State(ev) => Self::State(ev.into()),
303 }
304 }
305}
306
307#[derive(Deserialize)]
308#[allow(clippy::exhaustive_structs)]
309struct EventDeHelper {
310 state_key: Option<de::IgnoredAny>,
311}
312
313impl<'de> Deserialize<'de> for AnyTimelineEvent {
314 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
315 where
316 D: de::Deserializer<'de>,
317 {
318 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
319 let EventDeHelper { state_key } = from_raw_json_value(&json)?;
320
321 if state_key.is_some() {
322 Ok(AnyTimelineEvent::State(from_raw_json_value(&json)?))
323 } else {
324 Ok(AnyTimelineEvent::MessageLike(from_raw_json_value(&json)?))
325 }
326 }
327}
328
329impl<'de> Deserialize<'de> for AnySyncTimelineEvent {
330 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
331 where
332 D: de::Deserializer<'de>,
333 {
334 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
335 let EventDeHelper { state_key } = from_raw_json_value(&json)?;
336
337 if state_key.is_some() {
338 Ok(AnySyncTimelineEvent::State(from_raw_json_value(&json)?))
339 } else {
340 Ok(AnySyncTimelineEvent::MessageLike(from_raw_json_value(&json)?))
341 }
342 }
343}
344
345impl AnyMessageLikeEventContent {
346 pub fn relation(&self) -> Option<encrypted::Relation> {
351 #[cfg(feature = "unstable-msc3489")]
352 use super::beacon::BeaconEventContent;
353 use super::key::verification::{
354 accept::KeyVerificationAcceptEventContent, cancel::KeyVerificationCancelEventContent,
355 done::KeyVerificationDoneEventContent, key::KeyVerificationKeyEventContent,
356 mac::KeyVerificationMacEventContent, ready::KeyVerificationReadyEventContent,
357 start::KeyVerificationStartEventContent,
358 };
359 #[cfg(feature = "unstable-msc3381")]
360 use super::poll::{
361 end::PollEndEventContent, response::PollResponseEventContent,
362 unstable_end::UnstablePollEndEventContent,
363 unstable_response::UnstablePollResponseEventContent,
364 };
365
366 match self {
367 #[rustfmt::skip]
368 Self::KeyVerificationReady(KeyVerificationReadyEventContent { relates_to, .. })
369 | Self::KeyVerificationStart(KeyVerificationStartEventContent { relates_to, .. })
370 | Self::KeyVerificationCancel(KeyVerificationCancelEventContent { relates_to, .. })
371 | Self::KeyVerificationAccept(KeyVerificationAcceptEventContent { relates_to, .. })
372 | Self::KeyVerificationKey(KeyVerificationKeyEventContent { relates_to, .. })
373 | Self::KeyVerificationMac(KeyVerificationMacEventContent { relates_to, .. })
374 | Self::KeyVerificationDone(KeyVerificationDoneEventContent { relates_to, .. }) => {
375 Some(encrypted::Relation::Reference(relates_to.clone()))
376 },
377 Self::Reaction(ev) => Some(encrypted::Relation::Annotation(ev.relates_to.clone())),
378 Self::RoomEncrypted(ev) => ev.relates_to.clone(),
379 Self::RoomMessage(ev) => ev.relates_to.clone().map(Into::into),
380 #[cfg(feature = "unstable-msc1767")]
381 Self::Message(ev) => ev.relates_to.clone().map(Into::into),
382 #[cfg(feature = "unstable-msc3954")]
383 Self::Emote(ev) => ev.relates_to.clone().map(Into::into),
384 #[cfg(feature = "unstable-msc3956")]
385 Self::Encrypted(ev) => ev.relates_to.clone(),
386 #[cfg(feature = "unstable-msc3245")]
387 Self::Voice(ev) => ev.relates_to.clone().map(Into::into),
388 #[cfg(feature = "unstable-msc3927")]
389 Self::Audio(ev) => ev.relates_to.clone().map(Into::into),
390 #[cfg(feature = "unstable-msc3488")]
391 Self::Location(ev) => ev.relates_to.clone().map(Into::into),
392 #[cfg(feature = "unstable-msc3551")]
393 Self::File(ev) => ev.relates_to.clone().map(Into::into),
394 #[cfg(feature = "unstable-msc3552")]
395 Self::Image(ev) => ev.relates_to.clone().map(Into::into),
396 #[cfg(feature = "unstable-msc3553")]
397 Self::Video(ev) => ev.relates_to.clone().map(Into::into),
398 #[cfg(feature = "unstable-msc3381")]
399 Self::PollResponse(PollResponseEventContent { relates_to, .. })
400 | Self::UnstablePollResponse(UnstablePollResponseEventContent { relates_to, .. })
401 | Self::PollEnd(PollEndEventContent { relates_to, .. })
402 | Self::UnstablePollEnd(UnstablePollEndEventContent { relates_to, .. }) => {
403 Some(encrypted::Relation::Reference(relates_to.clone()))
404 }
405 #[cfg(feature = "unstable-msc3489")]
406 Self::Beacon(BeaconEventContent { relates_to, .. }) => {
407 Some(encrypted::Relation::Reference(relates_to.clone()))
408 }
409 #[cfg(feature = "unstable-msc3381")]
410 Self::PollStart(_) | Self::UnstablePollStart(_) => None,
411 #[cfg(feature = "unstable-msc4075")]
412 Self::CallNotify(_) => None,
413 Self::CallSdpStreamMetadataChanged(_)
414 | Self::CallNegotiate(_)
415 | Self::CallReject(_)
416 | Self::CallSelectAnswer(_)
417 | Self::CallAnswer(_)
418 | Self::CallInvite(_)
419 | Self::CallHangup(_)
420 | Self::CallCandidates(_)
421 | Self::RoomRedaction(_)
422 | Self::Sticker(_)
423 | Self::_Custom { .. } => None,
424 }
425 }
426}