ruma_events/
enums.rs

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
11/// Event types that servers should send as [stripped state] to help clients identify a room when
12/// they can't access the full room state.
13///
14/// [stripped state]: https://spec.matrix.org/latest/client-server-api/#stripped-state
15pub 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    /// Any global account data event.
27    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    /// Any room account data event.
48    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        #[cfg(feature = "unstable-msc4278")]
56        "m.media_preview_config" => super::media_preview_config,
57        #[cfg(feature = "unstable-msc4278")]
58        #[ruma_enum(ident = UnstableMediaPreviewConfig)]
59        "io.element.msc4278.media_preview_config" => super::media_preview_config,
60    }
61
62    /// Any ephemeral room event.
63    enum EphemeralRoom {
64        "m.receipt" => super::receipt,
65        "m.typing" => super::typing,
66    }
67
68    /// Any message-like event.
69    enum MessageLike {
70        #[cfg(feature = "unstable-msc3927")]
71        #[ruma_enum(alias = "m.audio")]
72        "org.matrix.msc1767.audio" => super::audio,
73        "m.call.answer" => super::call::answer,
74        "m.call.invite" => super::call::invite,
75        "m.call.hangup" => super::call::hangup,
76        "m.call.candidates" => super::call::candidates,
77        "m.call.negotiate" => super::call::negotiate,
78        "m.call.reject" => super::call::reject,
79        #[ruma_enum(alias = "org.matrix.call.sdp_stream_metadata_changed")]
80        "m.call.sdp_stream_metadata_changed" => super::call::sdp_stream_metadata_changed,
81        "m.call.select_answer" => super::call::select_answer,
82        #[cfg(feature = "unstable-msc3954")]
83        #[ruma_enum(alias = "m.emote")]
84        "org.matrix.msc1767.emote" => super::emote,
85        #[cfg(feature = "unstable-msc3956")]
86        #[ruma_enum(alias = "m.encrypted")]
87        "org.matrix.msc1767.encrypted" => super::encrypted,
88        #[cfg(feature = "unstable-msc3551")]
89        #[ruma_enum(alias = "m.file")]
90        "org.matrix.msc1767.file" => super::file,
91        #[cfg(feature = "unstable-msc3552")]
92        #[ruma_enum(alias = "m.image")]
93        "org.matrix.msc1767.image" => super::image,
94        "m.key.verification.ready" => super::key::verification::ready,
95        "m.key.verification.start" => super::key::verification::start,
96        "m.key.verification.cancel" => super::key::verification::cancel,
97        "m.key.verification.accept" => super::key::verification::accept,
98        "m.key.verification.key" => super::key::verification::key,
99        "m.key.verification.mac" => super::key::verification::mac,
100        "m.key.verification.done" => super::key::verification::done,
101        #[cfg(feature = "unstable-msc3488")]
102        "m.location" => super::location,
103        #[cfg(feature = "unstable-msc1767")]
104        #[ruma_enum(alias = "m.message")]
105        "org.matrix.msc1767.message" => super::message,
106        #[cfg(feature = "unstable-msc3381")]
107        "m.poll.start" => super::poll::start,
108        #[cfg(feature = "unstable-msc3381")]
109        #[ruma_enum(ident = UnstablePollStart)]
110        "org.matrix.msc3381.poll.start" => super::poll::unstable_start,
111        #[cfg(feature = "unstable-msc3381")]
112        "m.poll.response" => super::poll::response,
113        #[cfg(feature = "unstable-msc3381")]
114        #[ruma_enum(ident = UnstablePollResponse)]
115        "org.matrix.msc3381.poll.response" => super::poll::unstable_response,
116        #[cfg(feature = "unstable-msc3381")]
117        "m.poll.end" => super::poll::end,
118        #[cfg(feature = "unstable-msc3381")]
119        #[ruma_enum(ident = UnstablePollEnd)]
120        "org.matrix.msc3381.poll.end" => super::poll::unstable_end,
121        #[cfg(feature = "unstable-msc3489")]
122        #[ruma_enum(alias = "m.beacon")]
123        "org.matrix.msc3672.beacon" => super::beacon,
124        "m.reaction" => super::reaction,
125        "m.room.encrypted" => super::room::encrypted,
126        "m.room.message" => super::room::message,
127        "m.room.redaction" => super::room::redaction,
128        "m.sticker" => super::sticker,
129        #[cfg(feature = "unstable-msc3553")]
130        #[ruma_enum(alias = "m.video")]
131        "org.matrix.msc1767.video" => super::video,
132        #[cfg(feature = "unstable-msc3245")]
133        #[ruma_enum(alias = "m.voice")]
134        "org.matrix.msc3245.voice.v2" => super::voice,
135        #[cfg(feature = "unstable-msc4075")]
136        #[ruma_enum(alias = "m.call.notify")]
137        "org.matrix.msc4075.call.notify" => super::call::notify,
138    }
139
140    /// Any state event.
141    enum State {
142        "m.policy.rule.room" => super::policy::rule::room,
143        "m.policy.rule.server" => super::policy::rule::server,
144        "m.policy.rule.user" => super::policy::rule::user,
145        "m.room.aliases" => super::room::aliases,
146        "m.room.avatar" => super::room::avatar,
147        "m.room.canonical_alias" => super::room::canonical_alias,
148        "m.room.create" => super::room::create,
149        "m.room.encryption" => super::room::encryption,
150        "m.room.guest_access" => super::room::guest_access,
151        "m.room.history_visibility" => super::room::history_visibility,
152        "m.room.join_rules" => super::room::join_rules,
153        "m.room.member" => super::room::member,
154        "m.room.name" => super::room::name,
155        "m.room.pinned_events" => super::room::pinned_events,
156        "m.room.power_levels" => super::room::power_levels,
157        "m.room.server_acl" => super::room::server_acl,
158        "m.room.third_party_invite" => super::room::third_party_invite,
159        "m.room.tombstone" => super::room::tombstone,
160        "m.room.topic" => super::room::topic,
161        "m.space.child" => super::space::child,
162        "m.space.parent" => super::space::parent,
163        #[cfg(feature = "unstable-msc2545")]
164        #[ruma_enum(ident = RoomImagePack, alias = "m.image_pack")]
165        "im.ponies.room_emotes" => super::image_pack,
166        #[cfg(feature = "unstable-msc3489")]
167        #[ruma_enum(alias = "m.beacon_info")]
168        "org.matrix.msc3672.beacon_info" => super::beacon_info,
169        #[cfg(feature = "unstable-msc3401")]
170        #[ruma_enum(alias = "m.call.member")]
171        "org.matrix.msc3401.call.member" => super::call::member,
172        #[cfg(feature = "unstable-msc4171")]
173        #[ruma_enum(alias = "m.member_hints")]
174        "io.element.functional_members" => super::member_hints,
175    }
176
177    /// Any to-device event.
178    enum ToDevice {
179        "m.dummy" => super::dummy,
180        "m.room_key" => super::room_key,
181        "m.room_key_request" => super::room_key_request,
182        "m.room_key.withheld" => super::room_key::withheld,
183        "m.forwarded_room_key" => super::forwarded_room_key,
184        "m.key.verification.request" => super::key::verification::request,
185        "m.key.verification.ready" => super::key::verification::ready,
186        "m.key.verification.start" => super::key::verification::start,
187        "m.key.verification.cancel" => super::key::verification::cancel,
188        "m.key.verification.accept" => super::key::verification::accept,
189        "m.key.verification.key" => super::key::verification::key,
190        "m.key.verification.mac" => super::key::verification::mac,
191        "m.key.verification.done" => super::key::verification::done,
192        "m.room.encrypted" => super::room::encrypted,
193        "m.secret.request"=> super::secret::request,
194        "m.secret.send" => super::secret::send,
195    }
196}
197
198macro_rules! timeline_event_accessors {
199    (
200        $(
201            #[doc = $docs:literal]
202            pub fn $field:ident(&self) -> $ty:ty;
203        )*
204    ) => {
205        $(
206            #[doc = $docs]
207            pub fn $field(&self) -> $ty {
208                match self {
209                    Self::MessageLike(ev) => ev.$field(),
210                    Self::State(ev) => ev.$field(),
211                }
212            }
213        )*
214    };
215}
216
217/// Any room event.
218#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
219#[derive(Clone, Debug, EventEnumFromEvent)]
220pub enum AnyTimelineEvent {
221    /// Any message-like event.
222    MessageLike(AnyMessageLikeEvent),
223
224    /// Any state event.
225    State(AnyStateEvent),
226}
227
228impl AnyTimelineEvent {
229    timeline_event_accessors! {
230        /// Returns this event's `origin_server_ts` field.
231        pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
232
233        /// Returns this event's `room_id` field.
234        pub fn room_id(&self) -> &RoomId;
235
236        /// Returns this event's `event_id` field.
237        pub fn event_id(&self) -> &EventId;
238
239        /// Returns this event's `sender` field.
240        pub fn sender(&self) -> &UserId;
241
242        /// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
243        pub fn transaction_id(&self) -> Option<&TransactionId>;
244    }
245
246    /// Returns this event's `type`.
247    pub fn event_type(&self) -> TimelineEventType {
248        match self {
249            Self::MessageLike(e) => e.event_type().into(),
250            Self::State(e) => e.event_type().into(),
251        }
252    }
253}
254
255/// Any sync room event.
256///
257/// Sync room events are room event without a `room_id`, as returned in `/sync` responses.
258#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
259#[derive(Clone, Debug, EventEnumFromEvent)]
260pub enum AnySyncTimelineEvent {
261    /// Any sync message-like event.
262    MessageLike(AnySyncMessageLikeEvent),
263
264    /// Any sync state event.
265    State(AnySyncStateEvent),
266}
267
268impl AnySyncTimelineEvent {
269    timeline_event_accessors! {
270        /// Returns this event's `origin_server_ts` field.
271        pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
272
273        /// Returns this event's `event_id` field.
274        pub fn event_id(&self) -> &EventId;
275
276        /// Returns this event's `sender` field.
277        pub fn sender(&self) -> &UserId;
278
279        /// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
280        pub fn transaction_id(&self) -> Option<&TransactionId>;
281    }
282
283    /// Returns this event's `type`.
284    pub fn event_type(&self) -> TimelineEventType {
285        match self {
286            Self::MessageLike(e) => e.event_type().into(),
287            Self::State(e) => e.event_type().into(),
288        }
289    }
290
291    /// Converts `self` to an `AnyTimelineEvent` by adding the given a room ID.
292    pub fn into_full_event(self, room_id: OwnedRoomId) -> AnyTimelineEvent {
293        match self {
294            Self::MessageLike(ev) => AnyTimelineEvent::MessageLike(ev.into_full_event(room_id)),
295            Self::State(ev) => AnyTimelineEvent::State(ev.into_full_event(room_id)),
296        }
297    }
298}
299
300impl From<AnyTimelineEvent> for AnySyncTimelineEvent {
301    fn from(ev: AnyTimelineEvent) -> Self {
302        match ev {
303            AnyTimelineEvent::MessageLike(ev) => Self::MessageLike(ev.into()),
304            AnyTimelineEvent::State(ev) => Self::State(ev.into()),
305        }
306    }
307}
308
309#[derive(Deserialize)]
310#[allow(clippy::exhaustive_structs)]
311struct EventDeHelper {
312    state_key: Option<de::IgnoredAny>,
313}
314
315impl<'de> Deserialize<'de> for AnyTimelineEvent {
316    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
317    where
318        D: de::Deserializer<'de>,
319    {
320        let json = Box::<RawJsonValue>::deserialize(deserializer)?;
321        let EventDeHelper { state_key } = from_raw_json_value(&json)?;
322
323        if state_key.is_some() {
324            Ok(AnyTimelineEvent::State(from_raw_json_value(&json)?))
325        } else {
326            Ok(AnyTimelineEvent::MessageLike(from_raw_json_value(&json)?))
327        }
328    }
329}
330
331impl<'de> Deserialize<'de> for AnySyncTimelineEvent {
332    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
333    where
334        D: de::Deserializer<'de>,
335    {
336        let json = Box::<RawJsonValue>::deserialize(deserializer)?;
337        let EventDeHelper { state_key } = from_raw_json_value(&json)?;
338
339        if state_key.is_some() {
340            Ok(AnySyncTimelineEvent::State(from_raw_json_value(&json)?))
341        } else {
342            Ok(AnySyncTimelineEvent::MessageLike(from_raw_json_value(&json)?))
343        }
344    }
345}
346
347impl AnyMessageLikeEventContent {
348    /// Get a copy of the event's `m.relates_to` field, if any.
349    ///
350    /// This is a helper function intended for encryption. There should not be a reason to access
351    /// `m.relates_to` without first destructuring an `AnyMessageLikeEventContent` otherwise.
352    pub fn relation(&self) -> Option<encrypted::Relation> {
353        #[cfg(feature = "unstable-msc3489")]
354        use super::beacon::BeaconEventContent;
355        use super::key::verification::{
356            accept::KeyVerificationAcceptEventContent, cancel::KeyVerificationCancelEventContent,
357            done::KeyVerificationDoneEventContent, key::KeyVerificationKeyEventContent,
358            mac::KeyVerificationMacEventContent, ready::KeyVerificationReadyEventContent,
359            start::KeyVerificationStartEventContent,
360        };
361        #[cfg(feature = "unstable-msc3381")]
362        use super::poll::{
363            end::PollEndEventContent, response::PollResponseEventContent,
364            unstable_end::UnstablePollEndEventContent,
365            unstable_response::UnstablePollResponseEventContent,
366        };
367
368        match self {
369            #[rustfmt::skip]
370            Self::KeyVerificationReady(KeyVerificationReadyEventContent { relates_to, .. })
371            | Self::KeyVerificationStart(KeyVerificationStartEventContent { relates_to, .. })
372            | Self::KeyVerificationCancel(KeyVerificationCancelEventContent { relates_to, .. })
373            | Self::KeyVerificationAccept(KeyVerificationAcceptEventContent { relates_to, .. })
374            | Self::KeyVerificationKey(KeyVerificationKeyEventContent { relates_to, .. })
375            | Self::KeyVerificationMac(KeyVerificationMacEventContent { relates_to, .. })
376            | Self::KeyVerificationDone(KeyVerificationDoneEventContent { relates_to, .. }) => {
377                Some(encrypted::Relation::Reference(relates_to.clone()))
378            },
379            Self::Reaction(ev) => Some(encrypted::Relation::Annotation(ev.relates_to.clone())),
380            Self::RoomEncrypted(ev) => ev.relates_to.clone(),
381            Self::RoomMessage(ev) => ev.relates_to.clone().map(Into::into),
382            #[cfg(feature = "unstable-msc1767")]
383            Self::Message(ev) => ev.relates_to.clone().map(Into::into),
384            #[cfg(feature = "unstable-msc3954")]
385            Self::Emote(ev) => ev.relates_to.clone().map(Into::into),
386            #[cfg(feature = "unstable-msc3956")]
387            Self::Encrypted(ev) => ev.relates_to.clone(),
388            #[cfg(feature = "unstable-msc3245")]
389            Self::Voice(ev) => ev.relates_to.clone().map(Into::into),
390            #[cfg(feature = "unstable-msc3927")]
391            Self::Audio(ev) => ev.relates_to.clone().map(Into::into),
392            #[cfg(feature = "unstable-msc3488")]
393            Self::Location(ev) => ev.relates_to.clone().map(Into::into),
394            #[cfg(feature = "unstable-msc3551")]
395            Self::File(ev) => ev.relates_to.clone().map(Into::into),
396            #[cfg(feature = "unstable-msc3552")]
397            Self::Image(ev) => ev.relates_to.clone().map(Into::into),
398            #[cfg(feature = "unstable-msc3553")]
399            Self::Video(ev) => ev.relates_to.clone().map(Into::into),
400            #[cfg(feature = "unstable-msc3381")]
401            Self::PollResponse(PollResponseEventContent { relates_to, .. })
402            | Self::UnstablePollResponse(UnstablePollResponseEventContent { relates_to, .. })
403            | Self::PollEnd(PollEndEventContent { relates_to, .. })
404            | Self::UnstablePollEnd(UnstablePollEndEventContent { relates_to, .. }) => {
405                Some(encrypted::Relation::Reference(relates_to.clone()))
406            }
407            #[cfg(feature = "unstable-msc3489")]
408            Self::Beacon(BeaconEventContent { relates_to, .. }) => {
409                Some(encrypted::Relation::Reference(relates_to.clone()))
410            }
411            #[cfg(feature = "unstable-msc3381")]
412            Self::PollStart(_) | Self::UnstablePollStart(_) => None,
413            #[cfg(feature = "unstable-msc4075")]
414            Self::CallNotify(_) => None,
415            Self::CallSdpStreamMetadataChanged(_)
416            | Self::CallNegotiate(_)
417            | Self::CallReject(_)
418            | Self::CallSelectAnswer(_)
419            | Self::CallAnswer(_)
420            | Self::CallInvite(_)
421            | Self::CallHangup(_)
422            | Self::CallCandidates(_)
423            | Self::RoomRedaction(_)
424            | Self::Sticker(_)
425            | Self::_Custom { .. } => None,
426        }
427    }
428}