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