Skip to content

Commit

Permalink
client-api: Fix deserialization of KnockedRoom
Browse files Browse the repository at this point in the history
`knock_state` in `KnockedRoom` and `events` in `KnockState` are no longer
required during deserialization and are no longer serialized if they are empty.

This was a deviation from the spec, those fields were never required.
  • Loading branch information
zecakeh authored and girlbossceo committed Jan 2, 2025
1 parent ec42dd4 commit c4f55b3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
14 changes: 14 additions & 0 deletions crates/ruma-client-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# [unreleased]

# 0.20.0

Breaking changes:

- `ErrorKind` does not implement `AsRef<str>` and `Display` anymore. To get the
same result, use `ErrorKind::errcode()`. The `ErrorCode` that is returned
implements those traits.

Bug fixes:

- `knock_state` in `KnockedRoom` and `events` in `KnockState` are no longer
required during deserialization and are no longer serialized if they are empty.
This was a deviation from the spec, those fields were never required.

Improvements:

- Add unstable support for reporting rooms, according to MSC4151.
Expand Down
40 changes: 36 additions & 4 deletions crates/ruma-client-api/src/sync/sync_events/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,54 @@ impl JoinedRoom {
}
}

/// Updates to knocked rooms.
/// Updates to a room that the user has knocked upon.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct KnockedRoom {
/// The knock state.
/// Updates to the stripped state of the room.
#[serde(default, skip_serializing_if = "KnockState::is_empty")]
pub knock_state: KnockState,
}

/// A mapping from a key `events` to a list of `StrippedStateEvent`.
impl KnockedRoom {
/// Creates an empty `KnockedRoom`.
pub fn new() -> Self {
Default::default()
}

/// Whether there are updates for this room.
pub fn is_empty(&self) -> bool {
self.knock_state.is_empty()
}
}

impl From<KnockState> for KnockedRoom {
fn from(knock_state: KnockState) -> Self {
KnockedRoom { knock_state, ..Default::default() }
}
}

/// Stripped state updates of a room that the user has knocked upon.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct KnockState {
/// The list of events.
/// The stripped state of a room that the user has knocked upon.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub events: Vec<Raw<AnyStrippedStateEvent>>,
}

impl KnockState {
/// Creates an empty `KnockState`.
pub fn new() -> Self {
Default::default()
}

/// Whether there are stripped state updates in this room.
pub fn is_empty(&self) -> bool {
self.events.is_empty()
}
}

/// Events in the room.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
Expand Down

0 comments on commit c4f55b3

Please sign in to comment.