Skip to content

Commit

Permalink
feat(room): Separate RoomState::Ban from RoomState::Left.
Browse files Browse the repository at this point in the history
This is needed to tell apart rooms in left and banned state in places like `RoomInfo` or `RoomPreview`.

The banned rooms will still count as left rooms in the sync processes.
  • Loading branch information
jmartinesp committed Dec 13, 2024
1 parent 6dcefe4 commit 6376cee
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub enum Membership {
Joined,
Left,
Knocked,
Banned,
}

impl From<RoomState> for Membership {
Expand All @@ -59,6 +60,7 @@ impl From<RoomState> for Membership {
RoomState::Joined => Membership::Joined,
RoomState::Left => Membership::Left,
RoomState::Knocked => Membership::Knocked,
RoomState::Ban => Membership::Banned,
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ pub enum RoomState {
Invited,
/// The room is in a knocked state.
Knocked,
/// The room is in a banned state.
Ban,
}

impl From<&MembershipState> for RoomState {
fn from(membership_state: &MembershipState) -> Self {
// We consider Ban, Knock and Leave to be Left, because they all mean we are not
// in the room.
match membership_state {
MembershipState::Ban => Self::Left,
MembershipState::Ban => Self::Ban,
MembershipState::Invite => Self::Invited,
MembershipState::Join => Self::Joined,
MembershipState::Knock => Self::Knocked,
Expand Down Expand Up @@ -480,6 +480,7 @@ impl Room {
},
}
}
RoomState::Ban => Ok(false),

// TODO: implement logic once we have the stripped events as we'd have with an Invite
RoomState::Knocked => Ok(false),
Expand Down Expand Up @@ -1886,6 +1887,8 @@ bitflags! {
const LEFT = 0b00000100;
/// The room is in a knocked state.
const KNOCKED = 0b00001000;
/// The room is in a banned state.
const BANNED = 0b00010000;
}
}

Expand All @@ -1901,6 +1904,7 @@ impl RoomStateFilter {
RoomState::Left => Self::LEFT,
RoomState::Invited => Self::INVITED,
RoomState::Knocked => Self::KNOCKED,
RoomState::Ban => Self::BANNED,
};

self.contains(bit_state)
Expand All @@ -1919,6 +1923,12 @@ impl RoomStateFilter {
if self.contains(Self::INVITED) {
states.push(RoomState::Invited);
}
if self.contains(Self::KNOCKED) {
states.push(RoomState::Knocked);
}
if self.contains(Self::BANNED) {
states.push(RoomState::Ban);
}

states
}
Expand Down
19 changes: 14 additions & 5 deletions crates/matrix-sdk-base/src/sliding_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl BaseClient {
.or_insert_with(JoinedRoomUpdate::default)
.account_data
.append(&mut raw.to_vec()),
RoomState::Left => new_rooms
RoomState::Left | RoomState::Ban => new_rooms
.leave
.entry(room_id.to_owned())
.or_insert_with(LeftRoomUpdate::default)
Expand Down Expand Up @@ -546,7 +546,7 @@ impl BaseClient {
))
}

RoomState::Left => Ok((
RoomState::Left | RoomState::Ban => Ok((
room_info,
None,
Some(LeftRoomUpdate::new(
Expand Down Expand Up @@ -1247,7 +1247,7 @@ mod tests {
room.required_state.push(make_state_event(
user_b_id,
user_a_id.as_str(),
RoomMemberEventContent::new(membership),
RoomMemberEventContent::new(membership.clone()),
None,
));
let response = response_with_room(room_id, room);
Expand All @@ -1256,8 +1256,17 @@ mod tests {
.await
.expect("Failed to process sync");

// The room is left.
assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Left);
match membership {
MembershipState::Leave => {
// The room is left.
assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Left);
}
MembershipState::Ban => {
// The room is banned.
assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Ban);
}
_ => panic!("Unexpected membership state found: {membership}"),
}

// And it is added to the list of left rooms only.
assert!(!sync_resp.rooms.join.contains_key(room_id));
Expand Down

0 comments on commit 6376cee

Please sign in to comment.