1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Crate `ruma_events` contains serializable types for the events in the [Matrix](https://matrix.org)
//! specification that can be shared by client and server code.
//!
//! All data exchanged over Matrix is expressed as an event.
//! Different event types represent different actions, such as joining a room or sending a message.
//! Events are stored and transmitted as simple JSON structures.
//! While anyone can create a new event type for their own purposes, the Matrix specification
//! defines a number of event types which are considered core to the protocol, and Matrix clients
//! and servers must understand their semantics.
//! ruma-events contains Rust types for each of the event types defined by the specification and
//! facilities for extending the event system for custom event types.
//!
//! # Event types
//!
//! ruma-events includes a Rust enum called `EventType`, which provides a simple enumeration of
//! all the event types defined by the Matrix specification. Matrix event types are serialized to
//! JSON strings in [reverse domain name
//! notation](https://en.wikipedia.org/wiki/Reverse_domain_name_notation), although the core event
//! types all use the special "m" TLD, e.g. *m.room.message*.
//! `EventType` also includes a variant called `Custom`, which is a catch-all that stores a string
//! containing the name of any event type that isn't part of the specification.
//! `EventType` is used throughout ruma-events to identify and differentiate between events of
//! different types.
//!
//! # Event kinds
//!
//! Matrix defines three "kinds" of events:
//!
//! 1.  **Events**, which are arbitrary JSON structures that have two required keys:
//!     *   `type`, which specifies the event's type
//!     *   `content`, which is a JSON object containing the "payload" of the event
//! 2.  **Room events**, which are a superset of events and represent actions that occurred within
//!     the context of a Matrix room.
//!     They have at least the following additional keys:
//!     *   `event_id`, which is a unique identifier for the event
//!     *   `room_id`, which is a unique identifier for the room in which the event occurred
//!     *   `sender`, which is the unique identifier of the Matrix user who created the event
//!     *   Optionally, `unsigned`, which is a JSON object containing arbitrary additional metadata
//!     that is not digitally signed by Matrix homeservers.
//! 3.  **State events**, which are a superset of room events and represent persistent state
//!     specific to a room, such as the room's member list or topic.
//!     Within a single room, state events of the same type and with the same "state key" will
//!     effectively "replace" the previous one, updating the room's state.
//!     They have at least the following additional keys:
//!     *   `state_key`, a string which serves as a sort of "sub-type."
//!         The state key allows a room to persist multiple state events of the same type.
//!         You can think of a room's state events as being a `HashMap` where the keys are the tuple
//!         `(event_type, state_key)`.
//!     *   Optionally, `prev_content`, a JSON object containing the `content` object from the
//!     previous event of the given `(event_type, state_key)` tuple in the given room.
//!
//! ruma-events represents these three event kinds as traits, allowing any Rust type to serve as a
//! Matrix event so long as it upholds the contract expected of its kind.
//!
//! # Core event types
//!
//! ruma-events includes Rust types for every one of the event types in the Matrix specification.
//! To better organize the crate, these types live in separate modules with a hierarchy that
//! matches the reverse domain name notation of the event type.
//! For example, the *m.room.message* event lives at `ruma_events::room::message::MessageEvent`.
//! Each type's module also contains a Rust type for that event type's `content` field, and any
//! other supporting types required by the event's other fields.
//!
//! # Custom event types
//!
//! Although any Rust type that implements `Event`, `RoomEvent`, or `StateEvent` can serve as a
//! Matrix event type, ruma-events also includes a few convenience types for representing events
//! that are not covered by the spec and not otherwise known by the application.
//! `CustomEvent`, `CustomRoomEvent`, and `CustomStateEvent` are simple implementations of their
//! respective event traits whose `content` field is simply a `serde_json::Value` value, which
//! represents arbitrary JSON.
//!
//! # Serialization and deserialization
//!
//! All concrete event types in ruma-events can be serialized via the `Serialize` trait from
//! [serde](https://serde.rs/) and can be deserialized from as `EventResult<EventType>`. In order to
//! handle incoming data that may not conform to `ruma-events`' strict definitions of event
//! structures, deserialization will return `EventResult::Err` on error. This error covers both
//! structurally invalid JSON data as well as structurally valid JSON that doesn't fulfill
//! additional constraints the matrix specification defines for some event types. The error exposes
//! the deserialized `serde_json::Value` so that developers can still work with the received
//! event data. This makes it possible to deserialize a collection of events without the entire
//! collection failing to deserialize due to a single invalid event. The "content" type for each
//! event also implements `Serialize` and either `TryFromRaw` (enabling usage as
//! `EventResult<ContentType>` for dedicated content types) or `Deserialize` (when the content is a
//! type alias), allowing content to be converted to and from JSON indepedently of the surrounding
//! event structure, if needed.
//!
//! # Collections
//!
//! With the trait-based approach to events, it's easy to write generic collection types like
//! `Vec<Box<R: RoomEvent>>`.
//! However, there are APIs in the Matrix specification that involve heterogeneous collections of
//! events, i.e. a list of events of different event types.
//! Because Rust does not have a facility for arrays, vectors, or slices containing multiple
//! concrete types, ruma-events provides special collection types for this purpose.
//! The collection types are enums which effectively "wrap" each possible event type of a
//! particular event "kind."
//!
//! Because of the hierarchical nature of event kinds in Matrix, these collection types are divied
//! into two modules, `ruma_events::collections::all` and `ruma_events::collections::only`.
//! The "all" versions include every event type that implements the relevant event trait as well as
//! more specific event traits.
//! The "only" versions include only the event types that implement "at most" the relevant event
//! trait.
//!
//! For example, the `ruma_events::collections::all::Event` enum includes *m.room.message*, because
//! that event type is both an event and a room event.
//! However, the `ruma_events::collections::only::Event` enum does *not* include *m.room.message*,
//! because *m.room.message* implements a *more specific* event trait than `Event`.

#![warn(rust_2018_idioms)]
#![deny(missing_debug_implementations, missing_docs)]
// Since we support Rust 1.36.0, we can't apply this suggestion yet
#![allow(clippy::use_self)]

use std::{
    error::Error,
    fmt::{Debug, Display, Formatter, Result as FmtResult},
};

use js_int::UInt;
use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{
    de::{MapAccess, Visitor},
    ser::SerializeMap,
    Deserialize, Deserializer, Serialize, Serializer,
};
use serde_json::{Map, Value};

pub use self::{custom::CustomEvent, custom_room::CustomRoomEvent, custom_state::CustomStateEvent};

#[macro_use]
mod macros;
mod algorithm;
mod event_type;
mod from_raw;
#[doc(hidden)] // only public for external tests
pub mod util;

// Hack to allow both ruma-events itself and external crates (or tests) to use procedural macros
// that expect `ruma_events` to exist in the prelude.
extern crate self as ruma_events;

pub mod call;
/// Enums for heterogeneous collections of events.
pub mod collections {
    pub mod all;
    pub mod only;

    mod raw {
        pub mod all;
        pub mod only;
    }
}
pub mod direct;
pub mod dummy;
pub mod forwarded_room_key;
pub mod fully_read;
pub mod ignored_user_list;
pub mod key;
pub mod presence;
pub mod push_rules;
pub mod receipt;
pub mod room;
pub mod room_key;
pub mod room_key_request;
pub mod sticker;
pub mod stripped;
pub mod tag;
pub mod to_device;
pub mod typing;

pub use self::{
    algorithm::Algorithm,
    event_type::EventType,
    from_raw::{FromRaw, TryFromRaw},
};

/// An event that is malformed or otherwise invalid.
///
/// When attempting to deserialize an [`EventResult`](enum.EventResult.html), an error in the input
/// data may cause deserialization to fail, or the JSON structure may be correct, but additional
/// constraints defined in the matrix specification are not upheld. This type provides an error
/// message and a `serde_json::Value` representation of the invalid event, as well as a flag for
/// which type of error was encountered.
#[derive(Clone, Debug)]
pub struct InvalidEvent {
    message: String,
    json: Value,
    kind: InvalidEventKind,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum InvalidEventKind {
    Deserialization,
    Validation,
}

impl InvalidEvent {
    /// A message describing why the event is invalid.
    pub fn message(&self) -> String {
        self.message.clone()
    }

    /// The `serde_json::Value` representation of the invalid event.
    pub fn json(&self) -> &Value {
        &self.json
    }

    /// Returns whether this is a deserialization error.
    pub fn is_deserialization(&self) -> bool {
        self.kind == InvalidEventKind::Deserialization
    }

    /// Returns whether this is a validation error.
    pub fn is_validation(&self) -> bool {
        self.kind == InvalidEventKind::Validation
    }
}

impl Display for InvalidEvent {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}", self.message())
    }
}

impl Error for InvalidEvent {}

/// An error returned when attempting to create an event with data that would make it invalid.
///
/// This type is similar to [`InvalidEvent`](struct.InvalidEvent.html), but used during the
/// construction of a new event, as opposed to deserialization of an existing event from JSON.
#[derive(Clone, Debug, PartialEq)]
pub struct InvalidInput(String);

impl Display for InvalidInput {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}", self.0)
    }
}

impl Error for InvalidInput {}

/// The result of deserializing an event, which may or may not be valid.
///
/// When data is successfully deserialized and validated, this structure will contain the
/// deserialized value `T`. When deserialization succeeds, but the event is invalid for any reason,
/// this structure will contain an [`InvalidEvent`](struct.InvalidEvent.html). See the documentation
/// for [`InvalidEvent`](struct.InvalidEvent.html) for more details.
#[derive(Clone, Debug)]
pub enum EventResult<T: TryFromRaw> {
    /// `T` deserialized and validated successfully.
    Ok(T),

    /// `T` failed either deserialization or validation.
    ///
    /// [`InvalidEvent`](struct.InvalidEvent.html) contains the error message and the raw data.
    Err(InvalidEvent),
}

impl<T: TryFromRaw> EventResult<T> {
    /// Convert `EventResult<T>` into the equivalent `std::result::Result<T, InvalidEvent>`.
    pub fn into_result(self) -> Result<T, InvalidEvent> {
        match self {
            EventResult::Ok(t) => Ok(t),
            EventResult::Err(invalid_event) => Err(invalid_event),
        }
    }
}

impl<'de, T> Deserialize<'de> for EventResult<T>
where
    T: TryFromRaw,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let json = serde_json::Value::deserialize(deserializer)?;

        let raw_data: T::Raw = match serde_json::from_value(json.clone()) {
            Ok(raw) => raw,
            Err(error) => {
                return Ok(EventResult::Err(InvalidEvent {
                    json,
                    message: error.to_string(),
                    kind: InvalidEventKind::Deserialization,
                }));
            }
        };

        match T::try_from_raw(raw_data) {
            Ok(value) => Ok(EventResult::Ok(value)),
            Err(err) => Ok(EventResult::Err(InvalidEvent {
                message: err.to_string(),
                json,
                kind: InvalidEventKind::Validation,
            })),
        }
    }
}

/// An error when attempting to create a value from a string via the `FromStr` trait.
#[derive(Clone, Copy, Eq, Debug, Hash, PartialEq)]
pub struct FromStrError;

impl Display for FromStrError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "failed to parse type from string")
    }
}

impl Error for FromStrError {}

/// A meaningless value that serializes to an empty JSON object.
///
/// This type is used in a few places where the Matrix specification requires an empty JSON object,
/// but it's wasteful to represent it as a `HashMap` in Rust code.
#[derive(Clone, Debug, PartialEq)]
pub struct Empty;

impl Serialize for Empty {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_map(Some(0))?.end()
    }
}

impl<'de> Deserialize<'de> for Empty {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct EmptyMapVisitor;

        impl<'de> Visitor<'de> for EmptyMapVisitor {
            type Value = Empty;

            fn expecting(&self, f: &mut Formatter<'_>) -> FmtResult {
                write!(f, "an object/map")
            }

            fn visit_map<A>(self, _map: A) -> Result<Self::Value, A::Error>
            where
                A: MapAccess<'de>,
            {
                Ok(Empty)
            }
        }

        deserializer.deserialize_map(EmptyMapVisitor)
    }
}

impl FromRaw for Empty {
    type Raw = Self;

    fn from_raw(raw: Self) -> Self {
        raw
    }
}

/// A basic event.
pub trait Event: Debug + Serialize + Sized + TryFromRaw {
    /// The type of this event's `content` field.
    type Content: Debug + Serialize;

    /// The event's content.
    fn content(&self) -> &Self::Content;

    /// The type of the event.
    fn event_type(&self) -> EventType;
}

/// An event within the context of a room.
pub trait RoomEvent: Event {
    /// The unique identifier for the event.
    fn event_id(&self) -> &EventId;

    /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this event was
    /// sent.
    fn origin_server_ts(&self) -> UInt;

    /// The unique identifier for the room associated with this event.
    ///
    /// This can be `None` if the event came from a context where there is
    /// no ambiguity which room it belongs to, like a `/sync` response for example.
    fn room_id(&self) -> Option<&RoomId>;

    /// The unique identifier for the user who sent this event.
    fn sender(&self) -> &UserId;

    /// Additional key-value pairs not signed by the homeserver.
    fn unsigned(&self) -> &Map<String, Value>;
}

/// An event that describes persistent state about a room.
pub trait StateEvent: RoomEvent {
    /// The previous content for this state key, if any.
    fn prev_content(&self) -> Option<&Self::Content>;

    /// A key that determines which piece of room state the event represents.
    fn state_key(&self) -> &str;
}

/// A basic custom event outside of the Matrix specification.
mod custom {
    use super::{Event, EventType};

    use ruma_events_macros::FromRaw;
    use serde::{Deserialize, Serialize};
    use serde_json::Value;

    /// A custom event not covered by the Matrix specification.
    #[derive(Clone, Debug, FromRaw, PartialEq, Serialize)]
    pub struct CustomEvent {
        /// The event's content.
        pub content: CustomEventContent,
        /// The custom type of the event.
        #[serde(rename = "type")]
        pub event_type: String,
    }

    /// The payload for `CustomEvent`.
    pub type CustomEventContent = Value;

    impl Event for CustomEvent {
        /// The type of this event's `content` field.
        type Content = CustomEventContent;
        /// The event's content.
        fn content(&self) -> &Self::Content {
            &self.content
        }
        /// The type of the event.
        fn event_type(&self) -> EventType {
            EventType::Custom(self.event_type.clone())
        }
    }

    /// "Raw" versions of the event and its content which implement `serde::Deserialize`.
    pub mod raw {
        use super::*;

        /// A custom event not covered by the Matrix specification.
        #[derive(Clone, Debug, PartialEq, Deserialize)]
        pub struct CustomEvent {
            /// The event's content.
            pub content: CustomEventContent,
            /// The custom type of the event.
            #[serde(rename = "type")]
            pub event_type: String,
        }
    }
}

mod custom_room {
    use super::{Event, EventType, RoomEvent};

    use ruma_events_macros::FromRaw;
    use serde::{Deserialize, Serialize};
    use serde_json::{Map, Value};

    /// A custom room event not covered by the Matrix specification.
    #[derive(Clone, Debug, FromRaw, PartialEq, Serialize)]
    pub struct CustomRoomEvent {
        /// The event's content.
        pub content: CustomRoomEventContent,
        /// The unique identifier for the event.
        pub event_id: ruma_identifiers::EventId,
        /// The custom type of the event.
        #[serde(rename = "type")]
        pub event_type: String,
        /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this
        /// event was sent.
        pub origin_server_ts: js_int::UInt,
        /// The unique identifier for the room associated with this event.
        pub room_id: Option<ruma_identifiers::RoomId>,
        /// The unique identifier for the user who sent this event.
        pub sender: ruma_identifiers::UserId,
        /// Additional key-value pairs not signed by the homeserver.
        #[serde(skip_serializing_if = "serde_json::Map::is_empty")]
        pub unsigned: Map<String, Value>,
    }

    /// The payload for `CustomRoomEvent`.
    pub type CustomRoomEventContent = Value;

    impl Event for CustomRoomEvent {
        /// The type of this event's `content` field.
        type Content = CustomRoomEventContent;
        /// The event's content.
        fn content(&self) -> &Self::Content {
            &self.content
        }
        /// The type of the event.
        fn event_type(&self) -> EventType {
            EventType::Custom(self.event_type.clone())
        }
    }

    impl RoomEvent for CustomRoomEvent {
        /// The unique identifier for the event.
        fn event_id(&self) -> &ruma_identifiers::EventId {
            &self.event_id
        }
        /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this event was
        /// sent.
        fn origin_server_ts(&self) -> js_int::UInt {
            self.origin_server_ts
        }
        /// The unique identifier for the room associated with this event.
        ///
        /// This can be `None` if the event came from a context where there is
        /// no ambiguity which room it belongs to, like a `/sync` response for example.
        fn room_id(&self) -> Option<&ruma_identifiers::RoomId> {
            self.room_id.as_ref()
        }
        /// The unique identifier for the user who sent this event.
        fn sender(&self) -> &ruma_identifiers::UserId {
            &self.sender
        }
        /// Additional key-value pairs not signed by the homeserver.
        fn unsigned(&self) -> &Map<String, Value> {
            &self.unsigned
        }
    }

    pub mod raw {
        use super::*;

        /// A custom room event not covered by the Matrix specification.
        #[derive(Clone, Debug, PartialEq, Deserialize)]
        pub struct CustomRoomEvent {
            /// The event's content.
            pub content: CustomRoomEventContent,
            /// The unique identifier for the event.
            pub event_id: ruma_identifiers::EventId,
            /// The custom type of the event.
            #[serde(rename = "type")]
            pub event_type: String,
            /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this
            /// event was sent.
            pub origin_server_ts: js_int::UInt,
            /// The unique identifier for the room associated with this event.
            pub room_id: Option<ruma_identifiers::RoomId>,
            /// The unique identifier for the user who sent this event.
            pub sender: ruma_identifiers::UserId,
            /// Additional key-value pairs not signed by the homeserver.
            #[serde(default)]
            pub unsigned: Map<String, Value>,
        }
    }
}

mod custom_state {
    use super::{Event, EventType, RoomEvent, StateEvent};

    use ruma_events_macros::FromRaw;
    use serde::{Deserialize, Serialize};
    use serde_json::{Map, Value};

    /// A custom state event not covered by the Matrix specification.
    #[derive(Clone, Debug, FromRaw, PartialEq, Serialize)]
    pub struct CustomStateEvent {
        /// The event's content.
        pub content: CustomStateEventContent,
        /// The unique identifier for the event.
        pub event_id: ruma_identifiers::EventId,
        /// The custom type of the event.
        #[serde(rename = "type")]
        pub event_type: String,
        /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this
        /// event was sent.
        pub origin_server_ts: js_int::UInt,
        /// The previous content for this state key, if any.
        pub prev_content: Option<CustomStateEventContent>,
        /// The unique identifier for the room associated with this event.
        pub room_id: Option<ruma_identifiers::RoomId>,
        /// The unique identifier for the user who sent this event.
        pub sender: ruma_identifiers::UserId,
        /// A key that determines which piece of room state the event represents.
        pub state_key: String,
        /// Additional key-value pairs not signed by the homeserver.
        #[serde(skip_serializing_if = "serde_json::Map::is_empty")]
        pub unsigned: Map<String, Value>,
    }

    /// The payload for `CustomStateEvent`.
    pub type CustomStateEventContent = Value;

    impl Event for CustomStateEvent {
        /// The type of this event's `content` field.
        type Content = CustomStateEventContent;
        /// The event's content.
        fn content(&self) -> &Self::Content {
            &self.content
        }
        /// The type of the event.
        fn event_type(&self) -> EventType {
            EventType::Custom(self.event_type.clone())
        }
    }

    impl RoomEvent for CustomStateEvent {
        /// The unique identifier for the event.
        fn event_id(&self) -> &ruma_identifiers::EventId {
            &self.event_id
        }
        /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this event was
        /// sent.
        fn origin_server_ts(&self) -> js_int::UInt {
            self.origin_server_ts
        }
        /// The unique identifier for the room associated with this event.
        ///
        /// This can be `None` if the event came from a context where there is
        /// no ambiguity which room it belongs to, like a `/sync` response for example.
        fn room_id(&self) -> Option<&ruma_identifiers::RoomId> {
            self.room_id.as_ref()
        }
        /// The unique identifier for the user who sent this event.
        fn sender(&self) -> &ruma_identifiers::UserId {
            &self.sender
        }
        /// Additional key-value pairs not signed by the homeserver.
        fn unsigned(&self) -> &Map<String, Value> {
            &self.unsigned
        }
    }

    impl StateEvent for CustomStateEvent {
        /// The previous content for this state key, if any.
        fn prev_content(&self) -> Option<&Self::Content> {
            self.prev_content.as_ref()
        }
        /// A key that determines which piece of room state the event represents.
        fn state_key(&self) -> &str {
            &self.state_key
        }
    }

    pub mod raw {
        use super::*;

        /// A custom state event not covered by the Matrix specification.
        #[derive(Clone, Debug, PartialEq, Deserialize)]
        pub struct CustomStateEvent {
            /// The event's content.
            pub content: CustomStateEventContent,
            /// The unique identifier for the event.
            pub event_id: ruma_identifiers::EventId,
            /// The custom type of the event.
            #[serde(rename = "type")]
            pub event_type: String,
            /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver when this
            /// event was sent.
            pub origin_server_ts: js_int::UInt,
            /// The previous content for this state key, if any.
            pub prev_content: Option<CustomStateEventContent>,
            /// The unique identifier for the room associated with this event.
            pub room_id: Option<ruma_identifiers::RoomId>,
            /// The unique identifier for the user who sent this event.
            pub sender: ruma_identifiers::UserId,
            /// A key that determines which piece of room state the event represents.
            pub state_key: String,
            /// Additional key-value pairs not signed by the homeserver.
            #[serde(default)]
            pub unsigned: Map<String, Value>,
        }
    }
}