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;
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];
45
46event_enum! {
47 enum GlobalAccountData {
49 "m.direct" => super::direct,
50 #[cfg(feature = "unstable-msc4359")]
51 #[ruma_enum(ident = DoNotDisturb, alias = "m.do_not_disturb")]
52 "dm.filament.do_not_disturb" => super::do_not_disturb,
53 "m.identity_server" => super::identity_server,
54 "m.invite_permission_config" => super::invite_permission_config,
55 #[cfg(feature = "unstable-msc4380")]
56 #[ruma_enum(ident = UnstableInvitePermissionConfig)]
57 "org.matrix.msc4380.invite_permission_config" => super::invite_permission_config,
58 "m.ignored_user_list" => super::ignored_user_list,
59 "m.push_rules" => super::push_rules,
60 "m.secret_storage.default_key" => super::secret_storage::default_key,
61 "m.secret_storage.key.*" => super::secret_storage::key,
62 #[cfg(feature = "unstable-msc4278")]
63 "m.media_preview_config" => super::media_preview_config,
64 #[cfg(feature = "unstable-msc4278")]
65 #[ruma_enum(ident = UnstableMediaPreviewConfig)]
66 "io.element.msc4278.media_preview_config" => super::media_preview_config,
67 #[cfg(feature = "unstable-msc2545")]
68 #[ruma_enum(ident = AccountImagePack, alias = "m.image_pack")]
69 "im.ponies.user_emotes" => super::image_pack,
70 #[cfg(feature = "unstable-msc2545")]
71 #[ruma_enum(ident = ImagePackRooms, alias = "m.image_pack.rooms")]
72 "im.ponies.emote_rooms" => super::image_pack,
73 "m.recent_emoji" => super::recent_emoji,
74 "m.key_backup" => super::key_backup,
75 }
76
77 enum RoomAccountData {
79 "m.fully_read" => super::fully_read,
80 "m.tag" => super::tag,
81 "m.marked_unread" => super::marked_unread,
82 #[cfg(feature = "unstable-msc2867")]
83 #[ruma_enum(ident = UnstableMarkedUnread)]
84 "com.famedly.marked_unread" => super::marked_unread,
85 #[cfg(feature = "unstable-msc4278")]
86 "m.media_preview_config" => super::media_preview_config,
87 #[cfg(feature = "unstable-msc4278")]
88 #[ruma_enum(ident = UnstableMediaPreviewConfig)]
89 "io.element.msc4278.media_preview_config" => super::media_preview_config,
90 #[cfg(feature = "unstable-msc3230")]
91 #[ruma_enum(alias = "m.space_order")]
92 "org.matrix.msc3230.space_order" => super::space_order,
93 }
94
95 enum EphemeralRoom {
97 "m.receipt" => super::receipt,
98 "m.typing" => super::typing,
99 }
100
101 enum MessageLike {
103 #[cfg(feature = "unstable-msc3927")]
104 #[ruma_enum(alias = "m.audio")]
105 "org.matrix.msc1767.audio" => super::audio,
106 "m.call.answer" => super::call::answer,
107 "m.call.invite" => super::call::invite,
108 "m.call.hangup" => super::call::hangup,
109 "m.call.candidates" => super::call::candidates,
110 "m.call.negotiate" => super::call::negotiate,
111 "m.call.reject" => super::call::reject,
112 #[ruma_enum(alias = "org.matrix.call.sdp_stream_metadata_changed")]
113 "m.call.sdp_stream_metadata_changed" => super::call::sdp_stream_metadata_changed,
114 "m.call.select_answer" => super::call::select_answer,
115 #[cfg(feature = "unstable-msc3954")]
116 #[ruma_enum(alias = "m.emote")]
117 "org.matrix.msc1767.emote" => super::emote,
118 #[cfg(feature = "unstable-msc3956")]
119 #[ruma_enum(alias = "m.encrypted")]
120 "org.matrix.msc1767.encrypted" => super::encrypted,
121 #[cfg(feature = "unstable-msc3551")]
122 #[ruma_enum(alias = "m.file")]
123 "org.matrix.msc1767.file" => super::file,
124 #[cfg(feature = "unstable-msc3552")]
125 #[ruma_enum(alias = "m.image")]
126 "org.matrix.msc1767.image" => super::image,
127 "m.key.verification.ready" => super::key::verification::ready,
128 "m.key.verification.start" => super::key::verification::start,
129 "m.key.verification.cancel" => super::key::verification::cancel,
130 "m.key.verification.accept" => super::key::verification::accept,
131 "m.key.verification.key" => super::key::verification::key,
132 "m.key.verification.mac" => super::key::verification::mac,
133 "m.key.verification.done" => super::key::verification::done,
134 #[cfg(feature = "unstable-msc3488")]
135 "m.location" => super::location,
136 #[cfg(feature = "unstable-msc1767")]
137 #[ruma_enum(alias = "m.message")]
138 "org.matrix.msc1767.message" => super::message,
139 #[cfg(feature = "unstable-msc3381")]
140 "m.poll.start" => super::poll::start,
141 #[cfg(feature = "unstable-msc3381")]
142 #[ruma_enum(ident = UnstablePollStart)]
143 "org.matrix.msc3381.poll.start" => super::poll::unstable_start,
144 #[cfg(feature = "unstable-msc3381")]
145 "m.poll.response" => super::poll::response,
146 #[cfg(feature = "unstable-msc3381")]
147 #[ruma_enum(ident = UnstablePollResponse)]
148 "org.matrix.msc3381.poll.response" => super::poll::unstable_response,
149 #[cfg(feature = "unstable-msc3381")]
150 "m.poll.end" => super::poll::end,
151 #[cfg(feature = "unstable-msc3381")]
152 #[ruma_enum(ident = UnstablePollEnd)]
153 "org.matrix.msc3381.poll.end" => super::poll::unstable_end,
154 #[cfg(feature = "unstable-msc3489")]
155 #[ruma_enum(alias = "m.beacon")]
156 "org.matrix.msc3672.beacon" => super::beacon,
157 "m.reaction" => super::reaction,
158 "m.room.encrypted" => super::room::encrypted,
159 "m.room.message" => super::room::message,
160 "m.room.redaction" => super::room::redaction,
161 "m.sticker" => super::sticker,
162 #[cfg(feature = "unstable-msc3553")]
163 #[ruma_enum(alias = "m.video")]
164 "org.matrix.msc1767.video" => super::video,
165 #[cfg(feature = "unstable-msc3245")]
166 #[ruma_enum(alias = "m.voice")]
167 "org.matrix.msc3245.voice.v2" => super::voice,
168 #[cfg(feature = "unstable-msc4075")]
169 #[ruma_enum(alias = "m.call.notify")]
170 #[allow(deprecated)]
171 "org.matrix.msc4075.call.notify" => super::call::notify,
172 #[cfg(feature = "unstable-msc4075")]
173 #[ruma_enum(alias = "m.rtc.notification")]
174 "org.matrix.msc4075.rtc.notification" => super::rtc::notification,
175 #[cfg(feature = "unstable-msc4310")]
176 #[ruma_enum(alias = "m.rtc.decline")]
177 "org.matrix.msc4310.rtc.decline" => super::rtc::decline,
178 }
179
180 enum State {
182 "m.policy.rule.room" => super::policy::rule::room,
183 "m.policy.rule.server" => super::policy::rule::server,
184 "m.policy.rule.user" => super::policy::rule::user,
185 "m.room.avatar" => super::room::avatar,
186 "m.room.canonical_alias" => super::room::canonical_alias,
187 "m.room.create" => super::room::create,
188 "m.room.encryption" => super::room::encryption,
189 #[cfg(feature = "unstable-msc4362")]
190 "m.room.encrypted" => super::room::encrypted::unstable_state,
191 "m.room.guest_access" => super::room::guest_access,
192 "m.room.history_visibility" => super::room::history_visibility,
193 "m.room.join_rules" => super::room::join_rules,
194 #[cfg(feature = "unstable-msc4334")]
195 #[ruma_enum(alias = "m.room.language")]
196 "org.matrix.msc4334.room.language" => super::room::language,
197 "m.room.member" => super::room::member,
198 "m.room.name" => super::room::name,
199 "m.room.pinned_events" => super::room::pinned_events,
200 "m.room.policy" => super::room::policy,
201 "m.room.power_levels" => super::room::power_levels,
202 "m.room.server_acl" => super::room::server_acl,
203 "m.room.third_party_invite" => super::room::third_party_invite,
204 "m.room.tombstone" => super::room::tombstone,
205 "m.room.topic" => super::room::topic,
206 "m.space.child" => super::space::child,
207 "m.space.parent" => super::space::parent,
208 #[cfg(feature = "unstable-msc2545")]
209 #[ruma_enum(ident = RoomImagePack, alias = "m.image_pack")]
210 "im.ponies.room_emotes" => super::image_pack,
211 #[cfg(feature = "unstable-msc3489")]
212 #[ruma_enum(alias = "m.beacon_info")]
213 "org.matrix.msc3672.beacon_info" => super::beacon_info,
214 #[cfg(feature = "unstable-msc3401")]
215 #[ruma_enum(alias = "m.call.member")]
216 "org.matrix.msc3401.call.member" => super::call::member,
217 #[cfg(feature = "unstable-msc4171")]
218 #[ruma_enum(alias = "m.member_hints")]
219 "io.element.functional_members" => super::member_hints,
220 }
221
222 enum ToDevice {
224 "m.dummy" => super::dummy,
225 "m.room_key" => super::room_key,
226 #[cfg(feature = "unstable-msc4268")]
227 #[ruma_enum(alias = "m.room_key_bundle")]
228 "io.element.msc4268.room_key_bundle" => super::room_key_bundle,
229 "m.room_key_request" => super::room_key_request,
230 "m.room_key.withheld" => super::room_key::withheld,
231 "m.forwarded_room_key" => super::forwarded_room_key,
232 "m.key.verification.request" => super::key::verification::request,
233 "m.key.verification.ready" => super::key::verification::ready,
234 "m.key.verification.start" => super::key::verification::start,
235 "m.key.verification.cancel" => super::key::verification::cancel,
236 "m.key.verification.accept" => super::key::verification::accept,
237 "m.key.verification.key" => super::key::verification::key,
238 "m.key.verification.mac" => super::key::verification::mac,
239 "m.key.verification.done" => super::key::verification::done,
240 "m.room.encrypted" => super::room::encrypted,
241 "m.secret.request"=> super::secret::request,
242 "m.secret.send" => super::secret::send,
243 #[cfg(feature = "unstable-msc4385")]
244 #[ruma_enum(alias = "m.secret.push")]
245 "io.element.msc4385.secret.push" => super::secret::push,
246 #[cfg(feature = "unstable-msc4471")]
247 #[ruma_enum(alias = "m.stream.subscribe")]
248 "org.matrix.msc4471.stream.subscribe" => super::stream::subscribe,
249 #[cfg(feature = "unstable-msc4471")]
250 #[ruma_enum(alias = "m.stream.cancel")]
251 "org.matrix.msc4471.stream.cancel" => super::stream::cancel,
252 #[cfg(feature = "unstable-msc4471")]
253 #[ruma_enum(alias = "m.stream.update")]
254 "org.matrix.msc4471.stream.update" => super::stream::update,
255 }
256}
257
258macro_rules! timeline_event_accessors {
259 (
260 $(
261 #[doc = $docs:literal]
262 pub fn $field:ident(&self) -> $ty:ty;
263 )*
264 ) => {
265 $(
266 #[doc = $docs]
267 pub fn $field(&self) -> $ty {
268 match self {
269 Self::MessageLike(ev) => ev.$field(),
270 Self::State(ev) => ev.$field(),
271 }
272 }
273 )*
274 };
275}
276
277#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
279#[derive(Clone, Debug, EventEnumFromEvent)]
280pub enum AnyTimelineEvent {
281 MessageLike(AnyMessageLikeEvent),
283
284 State(AnyStateEvent),
286}
287
288impl AnyTimelineEvent {
289 timeline_event_accessors! {
290 pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
292
293 pub fn room_id(&self) -> &RoomId;
295
296 pub fn event_id(&self) -> &EventId;
298
299 pub fn sender(&self) -> &UserId;
301
302 pub fn transaction_id(&self) -> Option<&TransactionId>;
304
305 pub fn is_redacted(&self) -> bool;
307 }
308
309 pub fn event_type(&self) -> TimelineEventType {
311 match self {
312 Self::MessageLike(e) => e.event_type().into(),
313 Self::State(e) => e.event_type().into(),
314 }
315 }
316}
317
318#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
322#[derive(Clone, Debug, EventEnumFromEvent)]
323pub enum AnySyncTimelineEvent {
324 MessageLike(AnySyncMessageLikeEvent),
326
327 State(AnySyncStateEvent),
329}
330
331impl AnySyncTimelineEvent {
332 timeline_event_accessors! {
333 pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
335
336 pub fn event_id(&self) -> &EventId;
338
339 pub fn sender(&self) -> &UserId;
341
342 pub fn transaction_id(&self) -> Option<&TransactionId>;
344
345 pub fn is_redacted(&self) -> bool;
347 }
348
349 pub fn event_type(&self) -> TimelineEventType {
351 match self {
352 Self::MessageLike(e) => e.event_type().into(),
353 Self::State(e) => e.event_type().into(),
354 }
355 }
356
357 pub fn into_full_event(self, room_id: OwnedRoomId) -> AnyTimelineEvent {
359 match self {
360 Self::MessageLike(ev) => AnyTimelineEvent::MessageLike(ev.into_full_event(room_id)),
361 Self::State(ev) => AnyTimelineEvent::State(ev.into_full_event(room_id)),
362 }
363 }
364}
365
366impl From<AnyTimelineEvent> for AnySyncTimelineEvent {
367 fn from(ev: AnyTimelineEvent) -> Self {
368 match ev {
369 AnyTimelineEvent::MessageLike(ev) => Self::MessageLike(ev.into()),
370 AnyTimelineEvent::State(ev) => Self::State(ev.into()),
371 }
372 }
373}
374
375#[allow(clippy::large_enum_variant, clippy::exhaustive_enums)]
377#[derive(Clone, Debug, EventEnumFromEvent, Serialize)]
378#[serde(untagged)]
379pub enum AnyTimelineEventContent {
380 MessageLike(AnyMessageLikeEventContent),
382
383 State(AnyStateEventContent),
385}
386
387impl AnyTimelineEventContent {
388 pub fn event_type(&self) -> TimelineEventType {
390 match self {
391 Self::MessageLike(content) => {
392 <AnyMessageLikeEventContent as crate::MessageLikeEventContent>::event_type(content)
393 .into()
394 }
395 Self::State(content) => {
396 <AnyStateEventContent as crate::StateEventContent>::event_type(content).into()
397 }
398 }
399 }
400}
401
402impl JsonCastable<AnyTimelineEventContent> for AnyMessageLikeEventContent {}
403impl JsonCastable<AnyTimelineEventContent> for AnyStateEventContent {}
404
405#[derive(Deserialize)]
406#[allow(clippy::exhaustive_structs)]
407struct EventDeHelper {
408 state_key: Option<de::IgnoredAny>,
409}
410
411impl<'de> Deserialize<'de> for AnyTimelineEvent {
412 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
413 where
414 D: de::Deserializer<'de>,
415 {
416 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
417 let EventDeHelper { state_key } = from_raw_json_value(&json)?;
418
419 if state_key.is_some() {
420 Ok(AnyTimelineEvent::State(from_raw_json_value(&json)?))
421 } else {
422 Ok(AnyTimelineEvent::MessageLike(from_raw_json_value(&json)?))
423 }
424 }
425}
426
427impl<'de> Deserialize<'de> for AnySyncTimelineEvent {
428 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
429 where
430 D: de::Deserializer<'de>,
431 {
432 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
433 let EventDeHelper { state_key } = from_raw_json_value(&json)?;
434
435 if state_key.is_some() {
436 Ok(AnySyncTimelineEvent::State(from_raw_json_value(&json)?))
437 } else {
438 Ok(AnySyncTimelineEvent::MessageLike(from_raw_json_value(&json)?))
439 }
440 }
441}
442
443impl AnyMessageLikeEventContent {
444 pub fn relation(&self) -> Option<encrypted::Relation> {
449 #[cfg(feature = "unstable-msc3489")]
450 use super::beacon::BeaconEventContent;
451 use super::key::verification::{
452 accept::KeyVerificationAcceptEventContent, cancel::KeyVerificationCancelEventContent,
453 done::KeyVerificationDoneEventContent, key::KeyVerificationKeyEventContent,
454 mac::KeyVerificationMacEventContent, ready::KeyVerificationReadyEventContent,
455 start::KeyVerificationStartEventContent,
456 };
457 #[cfg(feature = "unstable-msc3381")]
458 use super::poll::{
459 end::PollEndEventContent, response::PollResponseEventContent,
460 unstable_end::UnstablePollEndEventContent,
461 unstable_response::UnstablePollResponseEventContent,
462 };
463
464 match self {
465 #[rustfmt::skip]
466 Self::KeyVerificationReady(KeyVerificationReadyEventContent { relates_to, .. })
467 | Self::KeyVerificationStart(KeyVerificationStartEventContent { relates_to, .. })
468 | Self::KeyVerificationCancel(KeyVerificationCancelEventContent { relates_to, .. })
469 | Self::KeyVerificationAccept(KeyVerificationAcceptEventContent { relates_to, .. })
470 | Self::KeyVerificationKey(KeyVerificationKeyEventContent { relates_to, .. })
471 | Self::KeyVerificationMac(KeyVerificationMacEventContent { relates_to, .. })
472 | Self::KeyVerificationDone(KeyVerificationDoneEventContent { relates_to, .. }) => {
473 Some(encrypted::Relation::Reference(relates_to.clone()))
474 },
475 Self::Reaction(ev) => Some(encrypted::Relation::Annotation(ev.relates_to.clone())),
476 Self::RoomEncrypted(ev) => ev.relates_to.clone(),
477 Self::RoomMessage(ev) => ev.relates_to.clone().map(Into::into),
478 #[cfg(feature = "unstable-msc1767")]
479 Self::Message(ev) => ev.relates_to.clone().map(Into::into),
480 #[cfg(feature = "unstable-msc3954")]
481 Self::Emote(ev) => ev.relates_to.clone().map(Into::into),
482 #[cfg(feature = "unstable-msc3956")]
483 Self::Encrypted(ev) => ev.relates_to.clone(),
484 #[cfg(feature = "unstable-msc3245")]
485 Self::Voice(ev) => ev.relates_to.clone().map(Into::into),
486 #[cfg(feature = "unstable-msc3927")]
487 Self::Audio(ev) => ev.relates_to.clone().map(Into::into),
488 #[cfg(feature = "unstable-msc3488")]
489 Self::Location(ev) => ev.relates_to.clone().map(Into::into),
490 #[cfg(feature = "unstable-msc3551")]
491 Self::File(ev) => ev.relates_to.clone().map(Into::into),
492 #[cfg(feature = "unstable-msc3552")]
493 Self::Image(ev) => ev.relates_to.clone().map(Into::into),
494 #[cfg(feature = "unstable-msc3553")]
495 Self::Video(ev) => ev.relates_to.clone().map(Into::into),
496 #[cfg(feature = "unstable-msc3381")]
497 Self::PollResponse(PollResponseEventContent { relates_to, .. })
498 | Self::UnstablePollResponse(UnstablePollResponseEventContent { relates_to, .. })
499 | Self::PollEnd(PollEndEventContent { relates_to, .. })
500 | Self::UnstablePollEnd(UnstablePollEndEventContent { relates_to, .. }) => {
501 Some(encrypted::Relation::Reference(relates_to.clone()))
502 }
503 #[cfg(feature = "unstable-msc3489")]
504 Self::Beacon(BeaconEventContent { relates_to, .. }) => {
505 Some(encrypted::Relation::Reference(relates_to.clone()))
506 }
507 #[cfg(feature = "unstable-msc3381")]
508 Self::UnstablePollStart(UnstablePollStartEventContent::New(content)) => {
509 content.relates_to.clone().map(Into::into)
510 }
511 #[cfg(feature = "unstable-msc3381")]
512 Self::UnstablePollStart(UnstablePollStartEventContent::Replacement(content)) => {
513 Some(encrypted::Relation::Replacement(Replacement::new(
514 content.relates_to.event_id.clone(),
515 )))
516 }
517 #[cfg(feature = "unstable-msc3381")]
518 Self::PollStart(PollStartEventContent { relates_to, .. }) => {
519 relates_to.clone().map(Into::into)
520 }
521 #[cfg(feature = "unstable-msc4075")]
522 Self::CallNotify(_) => None,
523 #[cfg(feature = "unstable-msc4075")]
524 Self::RtcNotification(ev) => ev.relates_to.clone().map(encrypted::Relation::Reference),
525 #[cfg(feature = "unstable-msc4310")]
526 Self::RtcDecline(ev) => Some(encrypted::Relation::Reference(ev.relates_to.clone())),
527 Self::CallSdpStreamMetadataChanged(_)
528 | Self::CallNegotiate(_)
529 | Self::CallReject(_)
530 | Self::CallSelectAnswer(_)
531 | Self::CallAnswer(_)
532 | Self::CallInvite(_)
533 | Self::CallHangup(_)
534 | Self::CallCandidates(_)
535 | Self::RoomRedaction(_)
536 | Self::Sticker(_)
537 | Self::_Custom { .. } => None,
538 }
539 }
540}