#client-server #ruma #messaging #chat #matrix-chat

ruma-events

Serializable types for the events in the Matrix specification

61 releases

0.27.11 Nov 27, 2023
0.27.7 Oct 23, 2023
0.26.1 May 4, 2022
0.26.0 Feb 19, 2022
0.2.0 Dec 21, 2016

#7 in #ruma

Download history 882/week @ 2023-12-12 668/week @ 2023-12-19 682/week @ 2023-12-26 1097/week @ 2024-01-02 877/week @ 2024-01-09 778/week @ 2024-01-16 536/week @ 2024-01-23 428/week @ 2024-01-30 401/week @ 2024-02-06 390/week @ 2024-02-13 434/week @ 2024-02-20 679/week @ 2024-02-27 677/week @ 2024-03-05 520/week @ 2024-03-12 616/week @ 2024-03-19 505/week @ 2024-03-26

2,460 downloads per month
Used in 34 crates (9 directly)

MIT license

1MB
23K SLoC

ruma-events

crates.io page docs.rs page license: MIT

Serializable types for the events in the Matrix specification that can be shared by client and server code.


lib.rs:

(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 its kind.

use ruma_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::OwnedEventId;
use ruma_events::{macros::EventContent, OriginalSyncMessageLikeEvent};
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!(
    serde_json::from_value::<OriginalSyncMessageLikeEvent<ReactionEventContent>>(json),
    Ok(OriginalSyncMessageLikeEvent {
        content: ReactionEventContent {
            relates_to: RelatesTo::Annotation { key, .. },
        },
        ..
    }) if key == "👍"
));

Dependencies

~8–21MB
~309K SLoC