logo
Available on crate feature events only.
Expand description

(De)serializable types for the events in the Matrix specification. These types are used by other Ruma crates.

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. This module contains Rust types for all of the event types defined by the specification and facilities for extending the event system for custom event types.

Core event types

This module includes Rust types for all 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::RoomMessageEvent. 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.

Extending Ruma with custom events

For our examples we will start with a simple custom state event. ruma_event specifies the state event’s type and it’s kind.

use ruma_common::events::macros::EventContent;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "org.example.event", kind = State, state_key_type = String)]
pub struct ExampleContent {
    field: String,
}

This can be used with events structs, such as passing it into ruma::api::client::state::send_state_event’s Request.

As a more advanced example we create a reaction message event. For this event we will use a OriginalSyncMessageLikeEvent struct but any OriginalMessageLikeEvent struct would work.

use ruma_common::{
    events::{macros::EventContent, OriginalSyncMessageLikeEvent},
    OwnedEventId,
};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "rel_type")]
pub enum RelatesTo {
    #[serde(rename = "m.annotation")]
    Annotation {
        /// The event this reaction relates to.
        event_id: OwnedEventId,
        /// The displayable content of the reaction.
        key: String,
    },

    /// Since this event is not fully specified in the Matrix spec
    /// it may change or types may be added, we are ready!
    #[serde(rename = "m.whatever")]
    Whatever,
}

/// The payload for our reaction event.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "m.reaction", kind = MessageLike)]
pub struct ReactionEventContent {
    #[serde(rename = "m.relates_to")]
    pub relates_to: RelatesTo,
}

let json = serde_json::json!({
    "content": {
        "m.relates_to": {
            "event_id": "$xxxx-xxxx",
            "key": "👍",
            "rel_type": "m.annotation"
        }
    },
    "event_id": "$xxxx-xxxx",
    "origin_server_ts": 1,
    "sender": "@someone:example.org",
    "type": "m.reaction",
    "unsigned": {
        "age": 85
    }
});

// The downside of this event is we cannot use it with event enums,
// but could be deserialized from a `Raw<_>` that has failed to deserialize.
assert_matches::assert_matches!(
    serde_json::from_value::<OriginalSyncMessageLikeEvent<ReactionEventContent>>(json),
    Ok(OriginalSyncMessageLikeEvent {
        content: ReactionEventContent {
            relates_to: RelatesTo::Annotation { key, .. },
        },
        ..
    }) if key == "👍"
);

Re-exports

pub use self::relation::Relations;

Modules

audiounstable-msc3246
Types for extensible audio message events (MSC3246).
Modules for events in the m.call namespace.
Types for the m.direct event.
Types for the m.dummy event.
emoteunstable-msc1767
Types for extensible emote message events (MSC1767).
fileunstable-msc3551
Types for extensible file message events (MSC3551).
Types for the m.fully_read event.
imageunstable-msc3552
Types for extensible image message events (MSC3552).
Modules for events in the m.key namespace.
locationunstable-msc3488
Types for extensible location message events (MSC3488).
Re-export of all the derives needed to create your own event types.
messageunstable-msc1767
Types for extensible text message events (MSC1767).
noticeunstable-msc1767
Types for extensible notice message events (MSC1767).
pduunstable-pdu
Types for persistent data unit schemas
Modules for events in the m.policy namespace.
pollunstable-msc3381
Modules for events in the m.poll namespace (MSC3381).
A presence event is represented by a struct with a set content field.
Types for the m.push_rules event.
reactionunstable-msc2677
Types for the m.reaction event.
Types for the m.receipt event.
Modules for events in the m.room namespace.
Types for the m.room_key event.
Module for events in the m.secret namespace.
Module for events in the m.secret_storage namespace.
Types for the m.space events.
Types for the m.sticker event.
Types for the m.tag event.
Types for the m.typing event.
videounstable-msc3553
Types for extensible video message events (MSC3553).
voiceunstable-msc3245
Types for voice message events (MSC3245).

Structs

The decrypted payload of an m.megolm.v1.aes-sha2 event.
The decrypted payload of an m.olm.v1.curve25519-aes-sha2 event.
A type that can be used as the state_key for event types where that field is always empty.
An ephemeral room event.
A global account data event.
A minimal state event, used for creating a new room.
Extra information about a message event that is not incorporated into the event’s hash.
Public keys used for an m.olm.v1.curve25519-aes-sha2 event.
An unredacted message-like event.
An unredacted state event.
An unredacted message-like event without a room_id.
An unredacted state event without a room_id.
A redacted message-like event.
A redacted state event.
A redacted message-like event without a room_id.
A redacted state event without a room_id.
Extra information about a redacted event that is not incorporated into the event’s hash.
A room account data event.
Extra information about a state event that is not incorporated into the event’s hash.
A stripped-down state event, used for previews of rooms the user has been invited to.
An ephemeral room event without a room_id.
An event sent using send-to-device messaging.

Enums

Any ephemeral room event.
Any ephemeral room event.
Any global account data event.
Any global account data event.
Any state event.
Any message-like event.
Any message-like event.
Any room account data event.
Any room account data event.
Any state event.
Any state event.
Any state event.
Any ephemeral room event.
Any message-like event.
Any state event.
Any sync room event.
Any room event.
Any to-device event.
Any to-device event.
The type of EphemeralRoomEvent this is.
The “kind” of an event.
The type of GlobalAccountDataEvent this is.
A possibly-redacted message-like event.
The type of MessageLikeEvent this is.
The type of RoomAccountDataEvent this is.
The type of RoomEvent this is.
A possibly-redacted state event.
The type of StateEvent this is.
A possibly-redacted message-like event without a room_id.
A possibly-redacted state event without a room_id.
The type of ToDeviceEvent this is.

Traits

Content of an ephemeral room event.
The base trait that all event content types implement.
Content of a global account-data event.
Content of a non-redacted message-like event.
Trait to define the behavior of redacting an event.
Trait to define the behavior of redact an event’s content object.
The base trait that all redacted event content types implement.
Content of a redacted message-like event.
Content of a non-redacted state event.
Content of a room-specific account-data event.
Content of a redacted state event.
Trait for abstracting over event content structs.
Content of a to-device event.