1use ruma_common::{
2 EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, TransactionId, UserId,
3 serde::{JsonCastable, from_raw_json_value},
4};
5#[cfg(feature = "unstable-msc3381")]
6use ruma_events::{
7 poll::{start::PollStartEventContent, unstable_start::UnstablePollStartEventContent},
8 room::encrypted::Replacement,
9};
10use ruma_macros::{EventEnumFromEvent, event_enum};
11use serde::{Deserialize, Serialize, de};
12use serde_json::value::RawValue as RawJsonValue;
13
14use super::room::{encrypted, member::MembershipState};
15
16pub const RECOMMENDED_STRIPPED_STATE_EVENT_TYPES: &[StateEventType] = &[
21 StateEventType::RoomCreate,
22 StateEventType::RoomName,
23 StateEventType::RoomAvatar,
24 StateEventType::RoomTopic,
25 StateEventType::RoomJoinRules,
26 StateEventType::RoomCanonicalAlias,
27 StateEventType::RoomEncryption,
28];
29
30pub const RECOMMENDED_TRANSFERABLE_STATE_EVENT_TYPES: &[StateEventType] = &[
35 StateEventType::RoomServerAcl,
36 StateEventType::RoomEncryption,
37 StateEventType::RoomName,
38 StateEventType::RoomAvatar,
39 StateEventType::RoomTopic,
40 StateEventType::RoomGuestAccess,
41 StateEventType::RoomHistoryVisibility,
42 StateEventType::RoomJoinRules,
43 StateEventType::RoomPowerLevels,
44 #[cfg(feature = "unstable-msc4495")]
45 StateEventType::RoomPresenceSharing,
46];
47
48#[cfg(feature = "unstable-msc4560")]
52pub const ALLOWED_STRIPPED_STATE_EVENTS: &[StateEventType] = &[
53 StateEventType::RoomCreate,
54 StateEventType::RoomName,
55 StateEventType::RoomAvatar,
56 StateEventType::RoomTopic,
57 StateEventType::RoomJoinRules,
58 StateEventType::RoomCanonicalAlias,
59 StateEventType::RoomGuestAccess,
60 StateEventType::RoomHistoryVisibility,
61 StateEventType::RoomEncryption,
62 StateEventType::SpaceChild,
63 StateEventType::SpaceParent,
64];
65
66event_enum! {
67 enum GlobalAccountData {
69 "m.direct" => super::direct,
70 #[cfg(feature = "unstable-msc4359")]
71 #[ruma_enum(ident = DoNotDisturb, alias = "m.do_not_disturb")]
72 "dm.filament.do_not_disturb" => super::do_not_disturb,
73 "m.identity_server" => super::identity_server,
74 "m.invite_permission_config" => super::invite_permission_config,
75 #[cfg(feature = "unstable-msc4380")]
76 #[ruma_enum(ident = UnstableInvitePermissionConfig)]
77 "org.matrix.msc4380.invite_permission_config" => super::invite_permission_config,
78 "m.ignored_user_list" => super::ignored_user_list,
79 "m.image_pack.rooms" => super::image_pack::rooms,
80 "m.push_rules" => super::push_rules,
81 "m.secret_storage.default_key" => super::secret_storage::default_key,
82 "m.secret_storage.key.*" => super::secret_storage::key,
83 #[cfg(feature = "unstable-msc4278")]
84 "m.media_preview_config" => super::media_preview_config,
85 #[cfg(feature = "unstable-msc4278")]
86 #[ruma_enum(ident = UnstableMediaPreviewConfig)]
87 "io.element.msc4278.media_preview_config" => super::media_preview_config,
88 "m.recent_emoji" => super::recent_emoji,
89 "m.key_backup" => super::key_backup,
90 #[cfg(feature = "unstable-msc4495")]
91 #[ruma_enum(ident = PresenceSharing)]
92 "org.continuwuity.presence_v2.msc4495.presence.sharing" => super::presence::sharing,
93 #[cfg(feature = "unstable-msc4495")]
94 #[ruma_enum(ident = PresencePrompted)]
95 "org.continuwuity.presence_v2.msc4495.presence.prompted" => super::presence::prompted,
96 #[cfg(feature = "unstable-msc4532")]
97 #[ruma_enum(ident = PresencePersistent)]
98 "org.continuwuity.presence_v2.msc4532.presence.persistent" => super::presence::persistent,
99 }
100
101 enum RoomAccountData {
103 "m.fully_read" => super::fully_read,
104 "m.tag" => super::tag,
105 "m.marked_unread" => super::marked_unread,
106 #[cfg(feature = "unstable-msc4278")]
107 "m.media_preview_config" => super::media_preview_config,
108 #[cfg(feature = "unstable-msc4278")]
109 #[ruma_enum(ident = UnstableMediaPreviewConfig)]
110 "io.element.msc4278.media_preview_config" => super::media_preview_config,
111 #[cfg(feature = "unstable-msc3230")]
112 #[ruma_enum(alias = "m.space_order")]
113 "org.matrix.msc3230.space_order" => super::space_order,
114 }
115
116 enum EphemeralRoom {
118 "m.receipt" => super::receipt,
119 "m.typing" => super::typing,
120 }
121
122 enum MessageLike {
124 #[cfg(feature = "unstable-msc3927")]
125 #[ruma_enum(alias = "m.audio")]
126 "org.matrix.msc1767.audio" => super::audio,
127 "m.call.answer" => super::call::answer,
128 "m.call.invite" => super::call::invite,
129 "m.call.hangup" => super::call::hangup,
130 "m.call.candidates" => super::call::candidates,
131 "m.call.negotiate" => super::call::negotiate,
132 "m.call.reject" => super::call::reject,
133 #[ruma_enum(alias = "org.matrix.call.sdp_stream_metadata_changed")]
134 "m.call.sdp_stream_metadata_changed" => super::call::sdp_stream_metadata_changed,
135 "m.call.select_answer" => super::call::select_answer,
136 #[cfg(feature = "unstable-msc3954")]
137 #[ruma_enum(alias = "m.emote")]
138 "org.matrix.msc1767.emote" => super::emote,
139 #[cfg(feature = "unstable-msc3956")]
140 #[ruma_enum(alias = "m.encrypted")]
141 "org.matrix.msc1767.encrypted" => super::encrypted,
142 #[cfg(feature = "unstable-msc3551")]
143 #[ruma_enum(alias = "m.file")]
144 "org.matrix.msc1767.file" => super::file,
145 #[cfg(feature = "unstable-msc3552")]
146 #[ruma_enum(alias = "m.image")]
147 "org.matrix.msc1767.image" => super::image,
148 "m.key.verification.ready" => super::key::verification::ready,
149 "m.key.verification.start" => super::key::verification::start,
150 "m.key.verification.cancel" => super::key::verification::cancel,
151 "m.key.verification.accept" => super::key::verification::accept,
152 "m.key.verification.key" => super::key::verification::key,
153 "m.key.verification.mac" => super::key::verification::mac,
154 "m.key.verification.done" => super::key::verification::done,
155 #[cfg(feature = "unstable-msc3488")]
156 "m.location" => super::location,
157 #[cfg(feature = "unstable-msc1767")]
158 #[ruma_enum(alias = "m.message")]
159 "org.matrix.msc1767.message" => super::message,
160 #[cfg(feature = "unstable-msc3381")]
161 "m.poll.start" => super::poll::start,
162 #[cfg(feature = "unstable-msc3381")]
163 #[ruma_enum(ident = UnstablePollStart)]
164 "org.matrix.msc3381.poll.start" => super::poll::unstable_start,
165 #[cfg(feature = "unstable-msc3381")]
166 "m.poll.response" => super::poll::response,
167 #[cfg(feature = "unstable-msc3381")]
168 #[ruma_enum(ident = UnstablePollResponse)]
169 "org.matrix.msc3381.poll.response" => super::poll::unstable_response,
170 #[cfg(feature = "unstable-msc3381")]
171 "m.poll.end" => super::poll::end,
172 #[cfg(feature = "unstable-msc3381")]
173 #[ruma_enum(ident = UnstablePollEnd)]
174 "org.matrix.msc3381.poll.end" => super::poll::unstable_end,
175 #[cfg(feature = "unstable-msc3489")]
176 #[ruma_enum(alias = "m.beacon")]
177 "org.matrix.msc3672.beacon" => super::beacon,
178 "m.reaction" => super::reaction,
179 "m.room.encrypted" => super::room::encrypted,
180 "m.room.message" => super::room::message,
181 "m.room.redaction" => super::room::redaction,
182 "m.sticker" => super::sticker,
183 #[cfg(feature = "unstable-msc3553")]
184 #[ruma_enum(alias = "m.video")]
185 "org.matrix.msc1767.video" => super::video,
186 #[cfg(feature = "unstable-msc3245")]
187 #[ruma_enum(alias = "m.voice")]
188 "org.matrix.msc3245.voice.v2" => super::voice,
189 #[cfg(feature = "unstable-msc4075")]
190 #[ruma_enum(alias = "m.call.notify")]
191 #[allow(deprecated)]
192 "org.matrix.msc4075.call.notify" => super::call::notify,
193 #[cfg(feature = "unstable-msc4075")]
194 #[ruma_enum(alias = "m.rtc.notification")]
195 "org.matrix.msc4075.rtc.notification" => super::rtc::notification,
196 #[cfg(feature = "unstable-msc4310")]
197 #[ruma_enum(alias = "m.rtc.decline")]
198 "org.matrix.msc4310.rtc.decline" => super::rtc::decline,
199 }
200
201 enum State {
203 "m.policy.rule.room" => super::policy::rule::room,
204 "m.policy.rule.server" => super::policy::rule::server,
205 "m.policy.rule.user" => super::policy::rule::user,
206 "m.room.avatar" => super::room::avatar,
207 "m.room.canonical_alias" => super::room::canonical_alias,
208 "m.room.create" => super::room::create,
209 "m.room.encryption" => super::room::encryption,
210 #[cfg(feature = "unstable-msc4362")]
211 "m.room.encrypted" => super::room::encrypted::unstable_state,
212 "m.room.guest_access" => super::room::guest_access,
213 "m.room.history_visibility" => super::room::history_visibility,
214 "m.room.image_pack" => super::room::image_pack,
215 "m.room.join_rules" => super::room::join_rules,
216 #[cfg(feature = "unstable-msc4334")]
217 #[ruma_enum(alias = "m.room.language")]
218 "org.matrix.msc4334.room.language" => super::room::language,
219 "m.room.member" => super::room::member,
220 "m.room.name" => super::room::name,
221 "m.room.pinned_events" => super::room::pinned_events,
222 "m.room.policy" => super::room::policy,
223 "m.room.power_levels" => super::room::power_levels,
224 #[cfg(feature = "unstable-msc1763")]
225 #[ruma_enum(alias = "m.room.retention")]
226 "org.matrix.msc1763.retention" => super::room::retention,
227 "m.room.server_acl" => super::room::server_acl,
228 "m.room.third_party_invite" => super::room::third_party_invite,
229 "m.room.tombstone" => super::room::tombstone,
230 "m.room.topic" => super::room::topic,
231 #[cfg(feature = "unstable-msc4495")]
232 #[ruma_enum(alias = "m.room.presence_sharing")]
233 "org.continuwuity.presence_v2.msc4495.room.presence_sharing" => super::room::presence_sharing,
234 "m.space.child" => super::space::child,
235 "m.space.parent" => super::space::parent,
236 #[cfg(feature = "unstable-msc3489")]
237 #[ruma_enum(alias = "m.beacon_info")]
238 "org.matrix.msc3672.beacon_info" => super::beacon_info,
239 #[cfg(feature = "unstable-msc3401")]
240 #[ruma_enum(alias = "m.call.member")]
241 "org.matrix.msc3401.call.member" => super::call::member,
242 #[cfg(feature = "unstable-msc4171")]
243 #[ruma_enum(alias = "m.member_hints")]
244 "io.element.functional_members" => super::member_hints,
245 }
246
247 enum ToDevice {
249 "m.dummy" => super::dummy,
250 "m.room_key" => super::room_key,
251 "m.room_key_bundle" => super::room_key_bundle,
252 "m.room_key_request" => super::room_key_request,
253 "m.room_key.withheld" => super::room_key::withheld,
254 "m.forwarded_room_key" => super::forwarded_room_key,
255 "m.key.verification.request" => super::key::verification::request,
256 "m.key.verification.ready" => super::key::verification::ready,
257 "m.key.verification.start" => super::key::verification::start,
258 "m.key.verification.cancel" => super::key::verification::cancel,
259 "m.key.verification.accept" => super::key::verification::accept,
260 "m.key.verification.key" => super::key::verification::key,
261 "m.key.verification.mac" => super::key::verification::mac,
262 "m.key.verification.done" => super::key::verification::done,
263 "m.room.encrypted" => super::room::encrypted,
264 "m.secret.request"=> super::secret::request,
265 "m.secret.send" => super::secret::send,
266 #[cfg(feature = "unstable-msc4385")]
267 #[ruma_enum(alias = "m.secret.push")]
268 "io.element.msc4385.secret.push" => super::secret::push,
269 #[cfg(feature = "unstable-msc4471")]
270 #[ruma_enum(alias = "m.stream.subscribe")]
271 "org.matrix.msc4471.stream.subscribe" => super::stream::subscribe,
272 #[cfg(feature = "unstable-msc4471")]
273 #[ruma_enum(alias = "m.stream.cancel")]
274 "org.matrix.msc4471.stream.cancel" => super::stream::cancel,
275 #[cfg(feature = "unstable-msc4471")]
276 #[ruma_enum(alias = "m.stream.update")]
277 "org.matrix.msc4471.stream.update" => super::stream::update,
278 }
279}
280
281macro_rules! timeline_event_accessors {
282 (
283 $(
284 #[doc = $docs:literal]
285 pub fn $field:ident(&self) -> $ty:ty;
286 )*
287 ) => {
288 $(
289 #[doc = $docs]
290 pub fn $field(&self) -> $ty {
291 match self {
292 Self::MessageLike(ev) => ev.$field(),
293 Self::State(ev) => ev.$field(),
294 }
295 }
296 )*
297 };
298}
299
300#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
302#[derive(Clone, Debug, EventEnumFromEvent)]
303pub enum AnyTimelineEvent {
304 MessageLike(AnyMessageLikeEvent),
306
307 State(AnyStateEvent),
309}
310
311impl AnyTimelineEvent {
312 timeline_event_accessors! {
313 pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
315
316 pub fn room_id(&self) -> &RoomId;
318
319 pub fn event_id(&self) -> &EventId;
321
322 pub fn sender(&self) -> &UserId;
324
325 pub fn transaction_id(&self) -> Option<&TransactionId>;
327
328 pub fn unsigned_membership(&self) -> Option<&MembershipState>;
330
331 pub fn is_redacted(&self) -> bool;
333 }
334
335 pub fn event_type(&self) -> TimelineEventType {
337 match self {
338 Self::MessageLike(e) => e.event_type().into(),
339 Self::State(e) => e.event_type().into(),
340 }
341 }
342}
343
344#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
348#[derive(Clone, Debug, EventEnumFromEvent)]
349pub enum AnySyncTimelineEvent {
350 MessageLike(AnySyncMessageLikeEvent),
352
353 State(AnySyncStateEvent),
355}
356
357impl AnySyncTimelineEvent {
358 timeline_event_accessors! {
359 pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
361
362 pub fn event_id(&self) -> &EventId;
364
365 pub fn sender(&self) -> &UserId;
367
368 pub fn transaction_id(&self) -> Option<&TransactionId>;
370
371 pub fn unsigned_membership(&self) -> Option<&MembershipState>;
373
374 pub fn is_redacted(&self) -> bool;
376 }
377
378 pub fn event_type(&self) -> TimelineEventType {
380 match self {
381 Self::MessageLike(e) => e.event_type().into(),
382 Self::State(e) => e.event_type().into(),
383 }
384 }
385
386 pub fn into_full_event(self, room_id: OwnedRoomId) -> AnyTimelineEvent {
388 match self {
389 Self::MessageLike(ev) => AnyTimelineEvent::MessageLike(ev.into_full_event(room_id)),
390 Self::State(ev) => AnyTimelineEvent::State(ev.into_full_event(room_id)),
391 }
392 }
393}
394
395impl From<AnyTimelineEvent> for AnySyncTimelineEvent {
396 fn from(ev: AnyTimelineEvent) -> Self {
397 match ev {
398 AnyTimelineEvent::MessageLike(ev) => Self::MessageLike(ev.into()),
399 AnyTimelineEvent::State(ev) => Self::State(ev.into()),
400 }
401 }
402}
403
404#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
406#[derive(Clone, Debug, EventEnumFromEvent, Serialize)]
407#[serde(untagged)]
408pub enum AnyTimelineEventContent {
409 MessageLike(AnyMessageLikeEventContent),
411
412 State(AnyStateEventContent),
414}
415
416impl AnyTimelineEventContent {
417 pub fn event_type(&self) -> TimelineEventType {
419 match self {
420 Self::MessageLike(content) => {
421 <AnyMessageLikeEventContent as crate::MessageLikeEventContent>::event_type(content)
422 .into()
423 }
424 Self::State(content) => {
425 <AnyStateEventContent as crate::StateEventContent>::event_type(content).into()
426 }
427 }
428 }
429}
430
431impl JsonCastable<AnyTimelineEventContent> for AnyMessageLikeEventContent {}
432impl JsonCastable<AnyTimelineEventContent> for AnyStateEventContent {}
433
434#[derive(Deserialize)]
435#[allow(clippy::exhaustive_structs)]
436struct EventDeHelper {
437 state_key: Option<de::IgnoredAny>,
438}
439
440impl<'de> Deserialize<'de> for AnyTimelineEvent {
441 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
442 where
443 D: de::Deserializer<'de>,
444 {
445 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
446 let EventDeHelper { state_key } = from_raw_json_value(&json)?;
447
448 if state_key.is_some() {
449 Ok(AnyTimelineEvent::State(from_raw_json_value(&json)?))
450 } else {
451 Ok(AnyTimelineEvent::MessageLike(from_raw_json_value(&json)?))
452 }
453 }
454}
455
456impl<'de> Deserialize<'de> for AnySyncTimelineEvent {
457 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
458 where
459 D: de::Deserializer<'de>,
460 {
461 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
462 let EventDeHelper { state_key } = from_raw_json_value(&json)?;
463
464 if state_key.is_some() {
465 Ok(AnySyncTimelineEvent::State(from_raw_json_value(&json)?))
466 } else {
467 Ok(AnySyncTimelineEvent::MessageLike(from_raw_json_value(&json)?))
468 }
469 }
470}
471
472impl AnyMessageLikeEventContent {
473 pub fn relation(&self) -> Option<encrypted::Relation> {
478 #[cfg(feature = "unstable-msc3489")]
479 use super::beacon::BeaconEventContent;
480 use super::key::verification::{
481 accept::KeyVerificationAcceptEventContent, cancel::KeyVerificationCancelEventContent,
482 done::KeyVerificationDoneEventContent, key::KeyVerificationKeyEventContent,
483 mac::KeyVerificationMacEventContent, ready::KeyVerificationReadyEventContent,
484 start::KeyVerificationStartEventContent,
485 };
486 #[cfg(feature = "unstable-msc3381")]
487 use super::poll::{
488 end::PollEndEventContent, response::PollResponseEventContent,
489 unstable_end::UnstablePollEndEventContent,
490 unstable_response::UnstablePollResponseEventContent,
491 };
492
493 match self {
494 #[rustfmt::skip]
495 Self::KeyVerificationReady(KeyVerificationReadyEventContent { relates_to, .. })
496 | Self::KeyVerificationStart(KeyVerificationStartEventContent { relates_to, .. })
497 | Self::KeyVerificationCancel(KeyVerificationCancelEventContent { relates_to, .. })
498 | Self::KeyVerificationAccept(KeyVerificationAcceptEventContent { relates_to, .. })
499 | Self::KeyVerificationKey(KeyVerificationKeyEventContent { relates_to, .. })
500 | Self::KeyVerificationMac(KeyVerificationMacEventContent { relates_to, .. })
501 | Self::KeyVerificationDone(KeyVerificationDoneEventContent { relates_to, .. }) => {
502 Some(encrypted::Relation::Reference(relates_to.clone()))
503 },
504 Self::Reaction(ev) => Some(encrypted::Relation::Annotation(ev.relates_to.clone())),
505 Self::RoomEncrypted(ev) => ev.relates_to.clone(),
506 Self::RoomMessage(ev) => ev.relates_to.clone().map(Into::into),
507 #[cfg(feature = "unstable-msc1767")]
508 Self::Message(ev) => ev.relates_to.clone().map(Into::into),
509 #[cfg(feature = "unstable-msc3954")]
510 Self::Emote(ev) => ev.relates_to.clone().map(Into::into),
511 #[cfg(feature = "unstable-msc3956")]
512 Self::Encrypted(ev) => ev.relates_to.clone(),
513 #[cfg(feature = "unstable-msc3245")]
514 Self::Voice(ev) => ev.relates_to.clone().map(Into::into),
515 #[cfg(feature = "unstable-msc3927")]
516 Self::Audio(ev) => ev.relates_to.clone().map(Into::into),
517 #[cfg(feature = "unstable-msc3488")]
518 Self::Location(ev) => ev.relates_to.clone().map(Into::into),
519 #[cfg(feature = "unstable-msc3551")]
520 Self::File(ev) => ev.relates_to.clone().map(Into::into),
521 #[cfg(feature = "unstable-msc3552")]
522 Self::Image(ev) => ev.relates_to.clone().map(Into::into),
523 #[cfg(feature = "unstable-msc3553")]
524 Self::Video(ev) => ev.relates_to.clone().map(Into::into),
525 #[cfg(feature = "unstable-msc3381")]
526 Self::PollResponse(PollResponseEventContent { relates_to, .. })
527 | Self::UnstablePollResponse(UnstablePollResponseEventContent { relates_to, .. })
528 | Self::PollEnd(PollEndEventContent { relates_to, .. })
529 | Self::UnstablePollEnd(UnstablePollEndEventContent { relates_to, .. }) => {
530 Some(encrypted::Relation::Reference(relates_to.clone()))
531 }
532 #[cfg(feature = "unstable-msc3489")]
533 Self::Beacon(BeaconEventContent { relates_to, .. }) => {
534 Some(encrypted::Relation::Reference(relates_to.clone()))
535 }
536 #[cfg(feature = "unstable-msc3381")]
537 Self::UnstablePollStart(UnstablePollStartEventContent::New(content)) => {
538 content.relates_to.clone().map(Into::into)
539 }
540 #[cfg(feature = "unstable-msc3381")]
541 Self::UnstablePollStart(UnstablePollStartEventContent::Replacement(content)) => {
542 Some(encrypted::Relation::Replacement(Replacement::new(
543 content.relates_to.event_id.clone(),
544 )))
545 }
546 #[cfg(feature = "unstable-msc3381")]
547 Self::PollStart(PollStartEventContent { relates_to, .. }) => {
548 relates_to.clone().map(Into::into)
549 }
550 #[cfg(feature = "unstable-msc4075")]
551 Self::CallNotify(_) => None,
552 #[cfg(feature = "unstable-msc4075")]
553 Self::RtcNotification(ev) => ev.relates_to.clone().map(encrypted::Relation::Reference),
554 #[cfg(feature = "unstable-msc4310")]
555 Self::RtcDecline(ev) => Some(encrypted::Relation::Reference(ev.relates_to.clone())),
556 Self::CallSdpStreamMetadataChanged(_)
557 | Self::CallNegotiate(_)
558 | Self::CallReject(_)
559 | Self::CallSelectAnswer(_)
560 | Self::CallAnswer(_)
561 | Self::CallInvite(_)
562 | Self::CallHangup(_)
563 | Self::CallCandidates(_)
564 | Self::RoomRedaction(_)
565 | Self::Sticker(_)
566 | Self::_Custom { .. } => None,
567 }
568 }
569}