From bc2c924c886f698dcac6188672fdc9a576e262ea Mon Sep 17 00:00:00 2001 From: Julian Sparber Date: Fri, 5 Mar 2021 12:00:43 +0100 Subject: [PATCH] matrix-sdk-base: remove InvitedRoom, JoinedRoom, LeftRoom and RoomState They are all replaced by `Room` --- matrix_sdk/examples/autojoin.rs | 6 +- matrix_sdk/examples/command_bot.rs | 10 +- matrix_sdk/examples/image_bot.rs | 10 +- matrix_sdk/examples/login.rs | 10 +- matrix_sdk/src/client.rs | 35 ++-- matrix_sdk/src/lib.rs | 4 +- matrix_sdk_base/src/client.rs | 6 +- matrix_sdk_base/src/event_handler/mod.rs | 229 ++++++++--------------- matrix_sdk_base/src/lib.rs | 4 +- matrix_sdk_base/src/rooms/mod.rs | 128 +------------ matrix_sdk_base/src/rooms/normal.rs | 4 +- matrix_sdk_base/src/store/mod.rs | 43 +---- 12 files changed, 130 insertions(+), 359 deletions(-) diff --git a/matrix_sdk/examples/autojoin.rs b/matrix_sdk/examples/autojoin.rs index b28f33a0..cff3a3bb 100644 --- a/matrix_sdk/examples/autojoin.rs +++ b/matrix_sdk/examples/autojoin.rs @@ -4,7 +4,7 @@ use tokio::time::{sleep, Duration}; use matrix_sdk::{ self, async_trait, events::{room::member::MemberEventContent, StrippedStateEvent}, - Client, ClientConfig, EventHandler, RoomState, SyncSettings, + Client, ClientConfig, EventHandler, Room, RoomType, SyncSettings, }; use url::Url; @@ -22,7 +22,7 @@ impl AutoJoinBot { impl EventHandler for AutoJoinBot { async fn on_stripped_state_member( &self, - room: RoomState, + room: Room, room_member: &StrippedStateEvent, _: Option, ) { @@ -30,7 +30,7 @@ impl EventHandler for AutoJoinBot { return; } - if let RoomState::Invited(room) = room { + if room.room_type() == RoomType::Invited { println!("Autojoining room {}", room.room_id()); let mut delay = 2; diff --git a/matrix_sdk/examples/command_bot.rs b/matrix_sdk/examples/command_bot.rs index ffae25c5..216dad07 100644 --- a/matrix_sdk/examples/command_bot.rs +++ b/matrix_sdk/examples/command_bot.rs @@ -6,7 +6,7 @@ use matrix_sdk::{ room::message::{MessageEventContent, MessageType, TextMessageEventContent}, AnyMessageEventContent, SyncMessageEvent, }, - Client, ClientConfig, EventHandler, RoomState, SyncSettings, + Client, ClientConfig, EventHandler, Room, RoomType, SyncSettings, }; use url::Url; @@ -24,12 +24,8 @@ impl CommandBot { #[async_trait] impl EventHandler for CommandBot { - async fn on_room_message( - &self, - room: RoomState, - event: &SyncMessageEvent, - ) { - if let RoomState::Joined(room) = room { + async fn on_room_message(&self, room: Room, event: &SyncMessageEvent) { + if room.room_type() == RoomType::Joined { let msg_body = if let SyncMessageEvent { content: MessageEventContent { diff --git a/matrix_sdk/examples/image_bot.rs b/matrix_sdk/examples/image_bot.rs index ed38f1fe..4ffd69c6 100644 --- a/matrix_sdk/examples/image_bot.rs +++ b/matrix_sdk/examples/image_bot.rs @@ -14,7 +14,7 @@ use matrix_sdk::{ room::message::{MessageEventContent, MessageType, TextMessageEventContent}, SyncMessageEvent, }, - Client, EventHandler, RoomState, SyncSettings, + Client, EventHandler, Room, RoomType, SyncSettings, }; use url::Url; @@ -32,12 +32,8 @@ impl ImageBot { #[async_trait] impl EventHandler for ImageBot { - async fn on_room_message( - &self, - room: RoomState, - event: &SyncMessageEvent, - ) { - if let RoomState::Joined(room) = room { + async fn on_room_message(&self, room: Room, event: &SyncMessageEvent) { + if room.room_type() == RoomType::Joined { let msg_body = if let SyncMessageEvent { content: MessageEventContent { diff --git a/matrix_sdk/examples/login.rs b/matrix_sdk/examples/login.rs index c8a88f58..b79b95e6 100644 --- a/matrix_sdk/examples/login.rs +++ b/matrix_sdk/examples/login.rs @@ -7,19 +7,15 @@ use matrix_sdk::{ room::message::{MessageEventContent, MessageType, TextMessageEventContent}, SyncMessageEvent, }, - Client, EventHandler, RoomState, SyncSettings, + Client, EventHandler, Room, RoomType, SyncSettings, }; struct EventCallback; #[async_trait] impl EventHandler for EventCallback { - async fn on_room_message( - &self, - room: RoomState, - event: &SyncMessageEvent, - ) { - if let RoomState::Joined(room) = room { + async fn on_room_message(&self, room: Room, event: &SyncMessageEvent) { + if room.room_type() == RoomType::Joined { if let SyncMessageEvent { content: MessageEventContent { diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index baef0a44..d0090c6f 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -41,8 +41,7 @@ use tracing::{error, info, instrument}; use matrix_sdk_base::{ deserialized_responses::{MembersResponse, SyncResponse}, - BaseClient, BaseClientConfig, EventHandler, InvitedRoom, JoinedRoom, LeftRoom, RoomState, - Session, Store, + BaseClient, BaseClientConfig, EventHandler, Room, RoomType, Session, Store, }; #[cfg(feature = "encryption")] @@ -564,34 +563,34 @@ impl Client { /// Get all the rooms the client knows about. /// /// This will return the list of joined, invited, and left rooms. - pub fn rooms(&self) -> Vec { + pub fn rooms(&self) -> Vec { self.store().get_rooms() } /// Returns the joined rooms this client knows about. - pub fn joined_rooms(&self) -> Vec { + pub fn joined_rooms(&self) -> Vec { self.store() .get_rooms() .into_iter() - .filter_map(|r| r.joined()) + .filter(|room| room.room_type() == RoomType::Joined) .collect() } /// Returns the invited rooms this client knows about. - pub fn invited_rooms(&self) -> Vec { + pub fn invited_rooms(&self) -> Vec { self.store() .get_rooms() .into_iter() - .filter_map(|r| r.invited()) + .filter(|room| room.room_type() == RoomType::Invited) .collect() } /// Returns the left rooms this client knows about. - pub fn left_rooms(&self) -> Vec { + pub fn left_rooms(&self) -> Vec { self.store() .get_rooms() .into_iter() - .filter_map(|r| r.left()) + .filter(|room| room.room_type() == RoomType::Left) .collect() } @@ -600,8 +599,10 @@ impl Client { /// # Arguments /// /// `room_id` - The unique id of the room that should be fetched. - pub fn get_joined_room(&self, room_id: &RoomId) -> Option { - self.store().get_joined_room(room_id) + pub fn get_joined_room(&self, room_id: &RoomId) -> Option { + self.store() + .get_room(room_id) + .filter(|room| room.room_type() == RoomType::Joined) } /// Get an invited room with the given room id. @@ -609,8 +610,10 @@ impl Client { /// # Arguments /// /// `room_id` - The unique id of the room that should be fetched. - pub fn get_invited_room(&self, room_id: &RoomId) -> Option { - self.store().get_invited_room(room_id) + pub fn get_invited_room(&self, room_id: &RoomId) -> Option { + self.store() + .get_room(room_id) + .filter(|room| room.room_type() == RoomType::Invited) } /// Get a left room with the given room id. @@ -618,8 +621,10 @@ impl Client { /// # Arguments /// /// `room_id` - The unique id of the room that should be fetched. - pub fn get_left_room(&self, room_id: &RoomId) -> Option { - self.store().get_left_room(room_id) + pub fn get_left_room(&self, room_id: &RoomId) -> Option { + self.store() + .get_room(room_id) + .filter(|room| room.room_type() == RoomType::Left) } /// Login to the server. diff --git a/matrix_sdk/src/lib.rs b/matrix_sdk/src/lib.rs index b0f2002a..9b70e874 100644 --- a/matrix_sdk/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -68,8 +68,8 @@ compile_error!("only one of 'native-tls' or 'rustls-tls' features can be enabled #[cfg_attr(feature = "docs", doc(cfg(encryption)))] pub use matrix_sdk_base::crypto::{EncryptionInfo, LocalTrust}; pub use matrix_sdk_base::{ - CustomEvent, Error as BaseError, EventHandler, InvitedRoom, JoinedRoom, LeftRoom, RoomInfo, - RoomMember, RoomState, RoomType, Session, StateChanges, StoreError, + CustomEvent, Error as BaseError, EventHandler, Room, RoomInfo, RoomMember, RoomType, Session, + StateChanges, StoreError, }; pub use matrix_sdk_common::*; diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 09a1dca8..a5f5b817 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -63,10 +63,10 @@ use zeroize::Zeroizing; use crate::{ error::Result, event_handler::Handler, - rooms::{RoomInfo, RoomType}, + rooms::{Room, RoomInfo, RoomType}, session::Session, store::{ambiguity_map::AmbiguityCache, Result as StoreResult, StateChanges, Store}, - EventHandler, RoomState, + EventHandler, }; pub type Token = String; @@ -1178,7 +1178,7 @@ impl BaseClient { /// # Arguments /// /// * `room_id` - The id of the room that should be fetched. - pub fn get_room(&self, room_id: &RoomId) -> Option { + pub fn get_room(&self, room_id: &RoomId) -> Option { self.store.get_room(room_id) } diff --git a/matrix_sdk_base/src/event_handler/mod.rs b/matrix_sdk_base/src/event_handler/mod.rs index 7d383432..3a9db97d 100644 --- a/matrix_sdk_base/src/event_handler/mod.rs +++ b/matrix_sdk_base/src/event_handler/mod.rs @@ -47,7 +47,7 @@ use crate::{ AnySyncStateEvent, BasicEvent, StrippedStateEvent, SyncEphemeralRoomEvent, SyncMessageEvent, SyncStateEvent, }, - rooms::RoomState, + rooms::Room, Store, }; use matrix_sdk_common::async_trait; @@ -66,7 +66,7 @@ impl Deref for Handler { } impl Handler { - fn get_room(&self, room_id: &RoomId) -> Option { + fn get_room(&self, room_id: &RoomId) -> Option { self.store.get_room(room_id) } @@ -120,7 +120,7 @@ impl Handler { } } - async fn handle_timeline_event(&self, room: RoomState, event: &AnySyncRoomEvent) { + async fn handle_timeline_event(&self, room: Room, event: &AnySyncRoomEvent) { match event { AnySyncRoomEvent::State(event) => match event { AnySyncStateEvent::RoomMember(e) => self.on_room_member(room, e).await, @@ -160,7 +160,7 @@ impl Handler { } } - async fn handle_state_event(&self, room: RoomState, event: &AnySyncStateEvent) { + async fn handle_state_event(&self, room: Room, event: &AnySyncStateEvent) { match event { AnySyncStateEvent::RoomMember(member) => self.on_state_member(room, &member).await, AnySyncStateEvent::RoomName(name) => self.on_state_name(room, &name).await, @@ -188,7 +188,7 @@ impl Handler { pub(crate) async fn handle_stripped_state_event( &self, // TODO these events are only handleted in invited rooms. - room: RoomState, + room: Room, event: &AnyStrippedStateEvent, ) { match event { @@ -216,7 +216,7 @@ impl Handler { } } - pub(crate) async fn handle_account_data_event(&self, room: RoomState, event: &AnyBasicEvent) { + pub(crate) async fn handle_account_data_event(&self, room: Room, event: &AnyBasicEvent) { match event { AnyBasicEvent::Presence(presence) => self.on_non_room_presence(room, &presence).await, AnyBasicEvent::IgnoredUserList(ignored) => { @@ -229,7 +229,7 @@ impl Handler { pub(crate) async fn handle_ephemeral_event( &self, - room: RoomState, + room: Room, event: &AnySyncEphemeralRoomEvent, ) { match event { @@ -276,7 +276,7 @@ pub enum CustomEvent<'c> { /// # room::message::{MessageEventContent, MessageType, TextMessageEventContent}, /// # SyncMessageEvent /// # }, -/// # EventHandler, RoomState +/// # EventHandler, Room, RoomType, /// # }; /// # use matrix_sdk_common::{async_trait, locks::RwLock}; /// @@ -284,8 +284,8 @@ pub enum CustomEvent<'c> { /// /// #[async_trait] /// impl EventHandler for EventCallback { -/// async fn on_room_message(&self, room: RoomState, event: &SyncMessageEvent) { -/// if let RoomState::Joined(room) = room { +/// async fn on_room_message(&self, room: Room, event: &SyncMessageEvent) { +/// if room.room_type() == RoomType::Joined { /// if let SyncMessageEvent { /// content: /// MessageEventContent { @@ -311,165 +311,130 @@ pub enum CustomEvent<'c> { pub trait EventHandler: Send + Sync { // ROOM EVENTS from `IncomingTimeline` /// Fires when `Client` receives a `RoomEvent::RoomMember` event. - async fn on_room_member(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_room_member(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `RoomEvent::RoomName` event. - async fn on_room_name(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_room_name(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `RoomEvent::RoomCanonicalAlias` event. async fn on_room_canonical_alias( &self, - _: RoomState, + _: Room, _: &SyncStateEvent, ) { } /// Fires when `Client` receives a `RoomEvent::RoomAliases` event. - async fn on_room_aliases(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_room_aliases(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `RoomEvent::RoomAvatar` event. - async fn on_room_avatar(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_room_avatar(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `RoomEvent::RoomMessage` event. - async fn on_room_message(&self, _: RoomState, _: &SyncMessageEvent) {} + async fn on_room_message(&self, _: Room, _: &SyncMessageEvent) {} /// Fires when `Client` receives a `RoomEvent::RoomMessageFeedback` event. - async fn on_room_message_feedback( - &self, - _: RoomState, - _: &SyncMessageEvent, - ) { - } + async fn on_room_message_feedback(&self, _: Room, _: &SyncMessageEvent) {} /// Fires when `Client` receives a `RoomEvent::CallInvite` event - async fn on_room_call_invite(&self, _: RoomState, _: &SyncMessageEvent) {} + async fn on_room_call_invite(&self, _: Room, _: &SyncMessageEvent) {} /// Fires when `Client` receives a `RoomEvent::CallAnswer` event - async fn on_room_call_answer(&self, _: RoomState, _: &SyncMessageEvent) {} + async fn on_room_call_answer(&self, _: Room, _: &SyncMessageEvent) {} /// Fires when `Client` receives a `RoomEvent::CallCandidates` event - async fn on_room_call_candidates( - &self, - _: RoomState, - _: &SyncMessageEvent, - ) { + async fn on_room_call_candidates(&self, _: Room, _: &SyncMessageEvent) { } /// Fires when `Client` receives a `RoomEvent::CallHangup` event - async fn on_room_call_hangup(&self, _: RoomState, _: &SyncMessageEvent) {} + async fn on_room_call_hangup(&self, _: Room, _: &SyncMessageEvent) {} /// Fires when `Client` receives a `RoomEvent::RoomRedaction` event. - async fn on_room_redaction(&self, _: RoomState, _: &SyncRedactionEvent) {} + async fn on_room_redaction(&self, _: Room, _: &SyncRedactionEvent) {} /// Fires when `Client` receives a `RoomEvent::RoomPowerLevels` event. - async fn on_room_power_levels( - &self, - _: RoomState, - _: &SyncStateEvent, - ) { - } + async fn on_room_power_levels(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `RoomEvent::Tombstone` event. - async fn on_room_join_rules(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_room_join_rules(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `RoomEvent::Tombstone` event. - async fn on_room_tombstone(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_room_tombstone(&self, _: Room, _: &SyncStateEvent) {} // `RoomEvent`s from `IncomingState` /// Fires when `Client` receives a `StateEvent::RoomMember` event. - async fn on_state_member(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_state_member(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `StateEvent::RoomName` event. - async fn on_state_name(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_state_name(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `StateEvent::RoomCanonicalAlias` event. async fn on_state_canonical_alias( &self, - _: RoomState, + _: Room, _: &SyncStateEvent, ) { } /// Fires when `Client` receives a `StateEvent::RoomAliases` event. - async fn on_state_aliases(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_state_aliases(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `StateEvent::RoomAvatar` event. - async fn on_state_avatar(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_state_avatar(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `StateEvent::RoomPowerLevels` event. - async fn on_state_power_levels( - &self, - _: RoomState, - _: &SyncStateEvent, - ) { - } + async fn on_state_power_levels(&self, _: Room, _: &SyncStateEvent) {} /// Fires when `Client` receives a `StateEvent::RoomJoinRules` event. - async fn on_state_join_rules(&self, _: RoomState, _: &SyncStateEvent) {} + async fn on_state_join_rules(&self, _: Room, _: &SyncStateEvent) {} // `AnyStrippedStateEvent`s /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomMember` event. async fn on_stripped_state_member( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, _: Option, ) { } /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomName` event. - async fn on_stripped_state_name(&self, _: RoomState, _: &StrippedStateEvent) { - } + async fn on_stripped_state_name(&self, _: Room, _: &StrippedStateEvent) {} /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomCanonicalAlias` event. async fn on_stripped_state_canonical_alias( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { } /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAliases` event. async fn on_stripped_state_aliases( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { } /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAvatar` event. - async fn on_stripped_state_avatar( - &self, - _: RoomState, - _: &StrippedStateEvent, - ) { - } + async fn on_stripped_state_avatar(&self, _: Room, _: &StrippedStateEvent) {} /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomPowerLevels` event. async fn on_stripped_state_power_levels( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { } /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomJoinRules` event. async fn on_stripped_state_join_rules( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { } // `NonRoomEvent` (this is a type alias from ruma_events) /// Fires when `Client` receives a `NonRoomEvent::RoomPresence` event. - async fn on_non_room_presence(&self, _: RoomState, _: &PresenceEvent) {} + async fn on_non_room_presence(&self, _: Room, _: &PresenceEvent) {} /// Fires when `Client` receives a `NonRoomEvent::RoomName` event. async fn on_non_room_ignored_users( &self, - _: RoomState, + _: Room, _: &BasicEvent, ) { } /// Fires when `Client` receives a `NonRoomEvent::RoomCanonicalAlias` event. - async fn on_non_room_push_rules(&self, _: RoomState, _: &BasicEvent) {} + async fn on_non_room_push_rules(&self, _: Room, _: &BasicEvent) {} /// Fires when `Client` receives a `NonRoomEvent::RoomAliases` event. async fn on_non_room_fully_read( &self, - _: RoomState, + _: Room, _: &SyncEphemeralRoomEvent, ) { } /// Fires when `Client` receives a `NonRoomEvent::Typing` event. - async fn on_non_room_typing( - &self, - _: RoomState, - _: &SyncEphemeralRoomEvent, - ) { - } + async fn on_non_room_typing(&self, _: Room, _: &SyncEphemeralRoomEvent) {} /// Fires when `Client` receives a `NonRoomEvent::Receipt` event. /// /// This is always a read receipt. - async fn on_non_room_receipt( - &self, - _: RoomState, - _: &SyncEphemeralRoomEvent, - ) { - } + async fn on_non_room_receipt(&self, _: Room, _: &SyncEphemeralRoomEvent) {} // `PresenceEvent` is a struct so there is only the one method /// Fires when `Client` receives a `NonRoomEvent::RoomAliases` event. @@ -479,14 +444,14 @@ pub trait EventHandler: Send + Sync { /// because the event was unknown to ruma. /// /// The only guarantee this method can give about the event is that it is valid JSON. - async fn on_unrecognized_event(&self, _: RoomState, _: &RawJsonValue) {} + async fn on_unrecognized_event(&self, _: Room, _: &RawJsonValue) {} /// Fires when `Client` receives a `Event::Custom` event or if deserialization fails /// because the event was unknown to ruma. /// /// The only guarantee this method can give about the event is that it is in the /// shape of a valid matrix event. - async fn on_custom_event(&self, _: RoomState, _: &CustomEvent<'_>) {} + async fn on_custom_event(&self, _: Room, _: &CustomEvent<'_>) {} } #[cfg(test)] @@ -505,108 +470,88 @@ mod test { #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl EventHandler for EvHandlerTest { - async fn on_room_member(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_room_member(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("member".to_string()) } - async fn on_room_name(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_room_name(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("name".to_string()) } async fn on_room_canonical_alias( &self, - _: RoomState, + _: Room, _: &SyncStateEvent, ) { self.0.lock().await.push("canonical".to_string()) } - async fn on_room_aliases(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_room_aliases(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("aliases".to_string()) } - async fn on_room_avatar(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_room_avatar(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("avatar".to_string()) } - async fn on_room_message(&self, _: RoomState, _: &SyncMessageEvent) { + async fn on_room_message(&self, _: Room, _: &SyncMessageEvent) { self.0.lock().await.push("message".to_string()) } async fn on_room_message_feedback( &self, - _: RoomState, + _: Room, _: &SyncMessageEvent, ) { self.0.lock().await.push("feedback".to_string()) } - async fn on_room_call_invite( - &self, - _: RoomState, - _: &SyncMessageEvent, - ) { + async fn on_room_call_invite(&self, _: Room, _: &SyncMessageEvent) { self.0.lock().await.push("call invite".to_string()) } - async fn on_room_call_answer( - &self, - _: RoomState, - _: &SyncMessageEvent, - ) { + async fn on_room_call_answer(&self, _: Room, _: &SyncMessageEvent) { self.0.lock().await.push("call answer".to_string()) } async fn on_room_call_candidates( &self, - _: RoomState, + _: Room, _: &SyncMessageEvent, ) { self.0.lock().await.push("call candidates".to_string()) } - async fn on_room_call_hangup( - &self, - _: RoomState, - _: &SyncMessageEvent, - ) { + async fn on_room_call_hangup(&self, _: Room, _: &SyncMessageEvent) { self.0.lock().await.push("call hangup".to_string()) } - async fn on_room_redaction(&self, _: RoomState, _: &SyncRedactionEvent) { + async fn on_room_redaction(&self, _: Room, _: &SyncRedactionEvent) { self.0.lock().await.push("redaction".to_string()) } - async fn on_room_power_levels( - &self, - _: RoomState, - _: &SyncStateEvent, - ) { + async fn on_room_power_levels(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("power".to_string()) } - async fn on_room_tombstone(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_room_tombstone(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("tombstone".to_string()) } - async fn on_state_member(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_state_member(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("state member".to_string()) } - async fn on_state_name(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_state_name(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("state name".to_string()) } async fn on_state_canonical_alias( &self, - _: RoomState, + _: Room, _: &SyncStateEvent, ) { self.0.lock().await.push("state canonical".to_string()) } - async fn on_state_aliases(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_state_aliases(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("state aliases".to_string()) } - async fn on_state_avatar(&self, _: RoomState, _: &SyncStateEvent) { + async fn on_state_avatar(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("state avatar".to_string()) } async fn on_state_power_levels( &self, - _: RoomState, + _: Room, _: &SyncStateEvent, ) { self.0.lock().await.push("state power".to_string()) } - async fn on_state_join_rules( - &self, - _: RoomState, - _: &SyncStateEvent, - ) { + async fn on_state_join_rules(&self, _: Room, _: &SyncStateEvent) { self.0.lock().await.push("state rules".to_string()) } @@ -614,7 +559,7 @@ mod test { /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomMember` event. async fn on_stripped_state_member( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, _: Option, ) { @@ -624,17 +569,13 @@ mod test { .push("stripped state member".to_string()) } /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomName` event. - async fn on_stripped_state_name( - &self, - _: RoomState, - _: &StrippedStateEvent, - ) { + async fn on_stripped_state_name(&self, _: Room, _: &StrippedStateEvent) { self.0.lock().await.push("stripped state name".to_string()) } /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomCanonicalAlias` event. async fn on_stripped_state_canonical_alias( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { self.0 @@ -645,7 +586,7 @@ mod test { /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAliases` event. async fn on_stripped_state_aliases( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { self.0 @@ -656,7 +597,7 @@ mod test { /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAvatar` event. async fn on_stripped_state_avatar( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { self.0 @@ -667,7 +608,7 @@ mod test { /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomPowerLevels` event. async fn on_stripped_state_power_levels( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { self.0.lock().await.push("stripped state power".to_string()) @@ -675,46 +616,42 @@ mod test { /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomJoinRules` event. async fn on_stripped_state_join_rules( &self, - _: RoomState, + _: Room, _: &StrippedStateEvent, ) { self.0.lock().await.push("stripped state rules".to_string()) } - async fn on_non_room_presence(&self, _: RoomState, _: &PresenceEvent) { + async fn on_non_room_presence(&self, _: Room, _: &PresenceEvent) { self.0.lock().await.push("presence".to_string()) } async fn on_non_room_ignored_users( &self, - _: RoomState, + _: Room, _: &BasicEvent, ) { self.0.lock().await.push("account ignore".to_string()) } - async fn on_non_room_push_rules( - &self, - _: RoomState, - _: &BasicEvent, - ) { + async fn on_non_room_push_rules(&self, _: Room, _: &BasicEvent) { self.0.lock().await.push("account push rules".to_string()) } async fn on_non_room_fully_read( &self, - _: RoomState, + _: Room, _: &SyncEphemeralRoomEvent, ) { self.0.lock().await.push("account read".to_string()) } async fn on_non_room_typing( &self, - _: RoomState, + _: Room, _: &SyncEphemeralRoomEvent, ) { self.0.lock().await.push("typing event".to_string()) } async fn on_non_room_receipt( &self, - _: RoomState, + _: Room, _: &SyncEphemeralRoomEvent, ) { self.0.lock().await.push("receipt event".to_string()) @@ -722,10 +659,10 @@ mod test { async fn on_presence_event(&self, _: &PresenceEvent) { self.0.lock().await.push("presence event".to_string()) } - async fn on_unrecognized_event(&self, _: RoomState, _: &RawJsonValue) { + async fn on_unrecognized_event(&self, _: Room, _: &RawJsonValue) { self.0.lock().await.push("unrecognized event".to_string()) } - async fn on_custom_event(&self, _: RoomState, _: &CustomEvent<'_>) { + async fn on_custom_event(&self, _: Room, _: &CustomEvent<'_>) { self.0.lock().await.push("custom event".to_string()) } } diff --git a/matrix_sdk_base/src/lib.rs b/matrix_sdk_base/src/lib.rs index 95181d5e..ef1abffa 100644 --- a/matrix_sdk_base/src/lib.rs +++ b/matrix_sdk_base/src/lib.rs @@ -52,9 +52,7 @@ mod session; mod store; pub use event_handler::{CustomEvent, EventHandler}; -pub use rooms::{ - InvitedRoom, JoinedRoom, LeftRoom, Room, RoomInfo, RoomMember, RoomState, RoomType, -}; +pub use rooms::{Room, RoomInfo, RoomMember, RoomType}; pub use store::{StateChanges, StateStore, Store, StoreError}; pub use client::{BaseClient, BaseClientConfig}; diff --git a/matrix_sdk_base/src/rooms/mod.rs b/matrix_sdk_base/src/rooms/mod.rs index 3aef1b44..f536e7b8 100644 --- a/matrix_sdk_base/src/rooms/mod.rs +++ b/matrix_sdk_base/src/rooms/mod.rs @@ -13,7 +13,7 @@ pub use normal::{Room, RoomInfo, RoomType}; pub use members::RoomMember; use serde::{Deserialize, Serialize}; -use std::{cmp::max, ops::Deref}; +use std::cmp::max; use matrix_sdk_common::{ events::{ @@ -23,132 +23,6 @@ use matrix_sdk_common::{ identifiers::RoomAliasId, }; -/// An enum that represents the state of the given `Room`. -/// -/// If the event came from the `join`, `invite` or `leave` rooms map from the server -/// the variant that holds the corresponding room is used. `RoomState` is generic -/// so it can be used to represent a `Room` or an `Arc>` -#[derive(Debug, Clone)] -pub enum RoomState { - /// A room from the `join` section of a sync response. - Joined(JoinedRoom), - /// A room from the `leave` section of a sync response. - Left(LeftRoom), - /// A room from the `invite` section of a sync response. - Invited(InvitedRoom), -} - -impl RoomState { - /// Destructure the room into a `JoinedRoom` if the room is in the joined - /// state. - pub fn joined(self) -> Option { - if let RoomState::Joined(r) = self { - Some(r) - } else { - None - } - } - - /// Destructure the room into an `InvitedRoom` if the room is in the invited - /// state. - pub fn invited(self) -> Option { - if let RoomState::Invited(r) = self { - Some(r) - } else { - None - } - } - - /// Destructure the room into a `LeftRoom` if the room is in the left - /// state. - pub fn left(self) -> Option { - if let RoomState::Left(r) = self { - Some(r) - } else { - None - } - } - - /// Is the room encrypted. - pub fn is_encrypted(&self) -> bool { - match self { - RoomState::Joined(r) => r.inner.is_encrypted(), - RoomState::Left(r) => r.inner.is_encrypted(), - RoomState::Invited(r) => r.inner.is_encrypted(), - } - } - - /// Get the history visibility policy of this room. - pub fn history_visibility(&self) -> HistoryVisibility { - match self { - RoomState::Joined(r) => r.inner.history_visibility(), - RoomState::Left(r) => r.inner.history_visibility(), - RoomState::Invited(r) => r.inner.history_visibility(), - } - } - - /// Get the `m.room.encryption` content that enabled end to end encryption - /// in the room. - pub fn encryption_settings(&self) -> Option { - match self { - RoomState::Joined(r) => r.inner.encryption_settings(), - RoomState::Left(r) => r.inner.encryption_settings(), - RoomState::Invited(r) => r.inner.encryption_settings(), - } - } - - /// Are the members for this room synced. - pub fn are_members_synced(&self) -> bool { - if let RoomState::Joined(r) = self { - r.inner.are_members_synced() - } else { - true - } - } -} - -/// A room in a joined state. -#[derive(Debug, Clone)] -pub struct JoinedRoom { - pub(crate) inner: Room, -} - -impl Deref for JoinedRoom { - type Target = Room; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -/// A room in a left state. -#[derive(Debug, Clone)] -pub struct LeftRoom { - pub(crate) inner: Room, -} - -impl Deref for LeftRoom { - type Target = Room; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -/// A room in an invited state. -#[derive(Debug, Clone)] -pub struct InvitedRoom { - pub(crate) inner: Room, -} - -impl Deref for InvitedRoom { - type Target = Room; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} - /// A base room info struct that is the backbone of normal as well as stripped /// rooms. Holds all the state events that are important to present a room to /// users. diff --git a/matrix_sdk_base/src/rooms/normal.rs b/matrix_sdk_base/src/rooms/normal.rs index 9c8084b3..96fa47fe 100644 --- a/matrix_sdk_base/src/rooms/normal.rs +++ b/matrix_sdk_base/src/rooms/normal.rs @@ -29,7 +29,7 @@ use matrix_sdk_common::{ guest_access::GuestAccess, history_visibility::HistoryVisibility, join_rules::JoinRule, tombstone::TombstoneEventContent, }, - AnySyncStateEvent, AnyStateEventContent, EventType, + AnyStateEventContent, AnySyncStateEvent, EventType, }, identifiers::{RoomAliasId, RoomId, UserId}, }; @@ -67,7 +67,7 @@ pub struct RoomSummary { /// Enum keeping track in which state the room is, e.g. if our own user is /// joined, invited, or has left the room. -#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] pub enum RoomType { /// The room is in a joined state. Joined, diff --git a/matrix_sdk_base/src/store/mod.rs b/matrix_sdk_base/src/store/mod.rs index f40751e4..1f6e3d48 100644 --- a/matrix_sdk_base/src/store/mod.rs +++ b/matrix_sdk_base/src/store/mod.rs @@ -36,7 +36,7 @@ use sled::Db; use crate::{ deserialized_responses::{MemberEvent, StrippedMemberEvent}, rooms::{RoomInfo, RoomType}, - InvitedRoom, JoinedRoom, LeftRoom, Room, RoomState, Session, + Room, Session, }; pub(crate) mod ambiguity_map; @@ -267,51 +267,20 @@ impl Store { } /// Get all the rooms this store knows about. - pub fn get_rooms(&self) -> Vec { + pub fn get_rooms(&self) -> Vec { self.rooms .iter() .filter_map(|r| self.get_room(r.key())) .collect() } - /// Get the joined room with the given room id. - /// - /// *Note*: A room with the given id might exist in a different state, this - /// will only return the room if it's in the joined state. - pub fn get_joined_room(&self, room_id: &RoomId) -> Option { - self.get_room(room_id).and_then(|r| r.joined()) - } - - /// Get the joined room with the given room id. - /// - /// *Note*: A room with the given id might exist in a different state, this - /// will only return the room if it's in the invited state. - pub fn get_invited_room(&self, room_id: &RoomId) -> Option { - self.get_room(room_id).and_then(|r| r.invited()) - } - - /// Get the joined room with the given room id. - /// - /// *Note*: A room with the given id might exist in a different state, this - /// will only return the room if it's in the left state. - pub fn get_left_room(&self, room_id: &RoomId) -> Option { - self.get_room(room_id).and_then(|r| r.left()) - } - /// Get the room with the given room id. - /// - /// *Note*: This will return the room in the `RoomState` enum, a room might - /// turn from an invited room to a joined one between sync requests, this - /// room struct might have stale info in that case and a new one should be - /// pulled out of the store. - pub fn get_room(&self, room_id: &RoomId) -> Option { + pub fn get_room(&self, room_id: &RoomId) -> Option { self.get_bare_room(room_id) .and_then(|r| match r.room_type() { - RoomType::Joined => Some(RoomState::Joined(JoinedRoom { inner: r })), - RoomType::Left => Some(RoomState::Left(LeftRoom { inner: r })), - RoomType::Invited => self - .get_stripped_room(room_id) - .map(|r| RoomState::Invited(InvitedRoom { inner: r })), + RoomType::Joined => Some(r), + RoomType::Left => Some(r), + RoomType::Invited => self.get_stripped_room(room_id), }) }