ruwuma/crates/ruma-state-res
gnieto fca0f6a22b
Minor fix on join member auth rules
Previous code was not checking/enforcing rule 4.3.1
(https://spec.matrix.org/v1.2/rooms/v9/), which states that a room
member event (with a join membership) must be accepted only if it contains
a previous event which `state_key` is the room creator.

On top of that, it simplifies the public interface for `auth_rules`,
removing the need of (externally) compute `prev_event`, which, as a side
effect, should reduce the amount of times the previous event needs to be
fetched: It will only load it if the authorized event is a `m.room_member`
with a `Join` state.

Finally, I've splitted the join conditions so it's (hopefully) more
readable and apply auth rules in the same order as they
appear in the spec.
2022-03-08 08:27:47 +01:00
..
2022-03-08 08:27:47 +01:00
2021-11-25 22:34:40 +01:00

Matrix State Resolution in Rust!

/// Abstraction of a PDU so users can have their own PDU types.
pub trait Event {
    /// The `EventId` of this event.
    fn event_id(&self) -> &EventId;
    /// The `RoomId` of this event.
    fn room_id(&self) -> &RoomId;
    /// The `UserId` of this event.
    fn sender(&self) -> &UserId;
    // and so on...
}

/// A mapping of event type and state_key to some value `T`, usually an `EventId`.
pub type StateMap<T> = BTreeMap<(EventType, Option<String>), T>;

/// A mapping of `EventId` to `T`, usually a `StateEvent`.
pub type EventMap<T> = BTreeMap<Box<EventId>, T>;

struct StateResolution {
    // For now the StateResolution struct is empty. If "caching" `event_map`
    // between `resolve` calls ends up being more efficient (probably not, as this would eat memory)
    // it may have an `event_map` field. The `event_map` is all the events
    // `StateResolution` has to know about to resolve state.
}

impl StateResolution {
    /// The point of this all, resolve the possibly conflicting sets of events.
    pub fn resolve<E: Event>(
        room_id: &RoomId,
        room_version: &RoomVersionId,
        state_sets: &[StateMap<Box<EventId>>],
        auth_events: Vec<Vec<Box<EventId>>>,
        event_map: &mut EventMap<Arc<E>>,
    ) -> Result<StateMap<Box<EventId>>> {;
}

The StateStore trait is an abstraction around what ever database your server (or maybe even client) uses to store Persistant Data Units.

We use rumas types when deserializing any PDU or it's contents which helps avoid a lot of type checking logic synapse must do while authenticating event chains.