matrix-sdk-base: remove InvitedRoom, JoinedRoom, LeftRoom and RoomState

They are all replaced by `Room`
master
Julian Sparber 2021-03-05 12:00:43 +01:00
parent 9332c55c8d
commit bc2c924c88
12 changed files with 130 additions and 359 deletions

View File

@ -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<MemberEventContent>,
_: Option<MemberEventContent>,
) {
@ -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;

View File

@ -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<MessageEventContent>,
) {
if let RoomState::Joined(room) = room {
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
if room.room_type() == RoomType::Joined {
let msg_body = if let SyncMessageEvent {
content:
MessageEventContent {

View File

@ -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<MessageEventContent>,
) {
if let RoomState::Joined(room) = room {
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
if room.room_type() == RoomType::Joined {
let msg_body = if let SyncMessageEvent {
content:
MessageEventContent {

View File

@ -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<MessageEventContent>,
) {
if let RoomState::Joined(room) = room {
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
if room.room_type() == RoomType::Joined {
if let SyncMessageEvent {
content:
MessageEventContent {

View File

@ -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<RoomState> {
pub fn rooms(&self) -> Vec<Room> {
self.store().get_rooms()
}
/// Returns the joined rooms this client knows about.
pub fn joined_rooms(&self) -> Vec<JoinedRoom> {
pub fn joined_rooms(&self) -> Vec<Room> {
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<InvitedRoom> {
pub fn invited_rooms(&self) -> Vec<Room> {
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<LeftRoom> {
pub fn left_rooms(&self) -> Vec<Room> {
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<JoinedRoom> {
self.store().get_joined_room(room_id)
pub fn get_joined_room(&self, room_id: &RoomId) -> Option<Room> {
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<InvitedRoom> {
self.store().get_invited_room(room_id)
pub fn get_invited_room(&self, room_id: &RoomId) -> Option<Room> {
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<LeftRoom> {
self.store().get_left_room(room_id)
pub fn get_left_room(&self, room_id: &RoomId) -> Option<Room> {
self.store()
.get_room(room_id)
.filter(|room| room.room_type() == RoomType::Left)
}
/// Login to the server.

View File

@ -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::*;

View File

@ -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<RoomState> {
pub fn get_room(&self, room_id: &RoomId) -> Option<Room> {
self.store.get_room(room_id)
}

View File

@ -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<RoomState> {
fn get_room(&self, room_id: &RoomId) -> Option<Room> {
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<MessageEventContent>) {
/// if let RoomState::Joined(room) = room {
/// async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
/// 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<MemberEventContent>) {}
async fn on_room_member(&self, _: Room, _: &SyncStateEvent<MemberEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::RoomName` event.
async fn on_room_name(&self, _: RoomState, _: &SyncStateEvent<NameEventContent>) {}
async fn on_room_name(&self, _: Room, _: &SyncStateEvent<NameEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::RoomCanonicalAlias` event.
async fn on_room_canonical_alias(
&self,
_: RoomState,
_: Room,
_: &SyncStateEvent<CanonicalAliasEventContent>,
) {
}
/// Fires when `Client` receives a `RoomEvent::RoomAliases` event.
async fn on_room_aliases(&self, _: RoomState, _: &SyncStateEvent<AliasesEventContent>) {}
async fn on_room_aliases(&self, _: Room, _: &SyncStateEvent<AliasesEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::RoomAvatar` event.
async fn on_room_avatar(&self, _: RoomState, _: &SyncStateEvent<AvatarEventContent>) {}
async fn on_room_avatar(&self, _: Room, _: &SyncStateEvent<AvatarEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::RoomMessage` event.
async fn on_room_message(&self, _: RoomState, _: &SyncMessageEvent<MsgEventContent>) {}
async fn on_room_message(&self, _: Room, _: &SyncMessageEvent<MsgEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::RoomMessageFeedback` event.
async fn on_room_message_feedback(
&self,
_: RoomState,
_: &SyncMessageEvent<FeedbackEventContent>,
) {
}
async fn on_room_message_feedback(&self, _: Room, _: &SyncMessageEvent<FeedbackEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::CallInvite` event
async fn on_room_call_invite(&self, _: RoomState, _: &SyncMessageEvent<InviteEventContent>) {}
async fn on_room_call_invite(&self, _: Room, _: &SyncMessageEvent<InviteEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::CallAnswer` event
async fn on_room_call_answer(&self, _: RoomState, _: &SyncMessageEvent<AnswerEventContent>) {}
async fn on_room_call_answer(&self, _: Room, _: &SyncMessageEvent<AnswerEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::CallCandidates` event
async fn on_room_call_candidates(
&self,
_: RoomState,
_: &SyncMessageEvent<CandidatesEventContent>,
) {
async fn on_room_call_candidates(&self, _: Room, _: &SyncMessageEvent<CandidatesEventContent>) {
}
/// Fires when `Client` receives a `RoomEvent::CallHangup` event
async fn on_room_call_hangup(&self, _: RoomState, _: &SyncMessageEvent<HangupEventContent>) {}
async fn on_room_call_hangup(&self, _: Room, _: &SyncMessageEvent<HangupEventContent>) {}
/// 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<PowerLevelsEventContent>,
) {
}
async fn on_room_power_levels(&self, _: Room, _: &SyncStateEvent<PowerLevelsEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::Tombstone` event.
async fn on_room_join_rules(&self, _: RoomState, _: &SyncStateEvent<JoinRulesEventContent>) {}
async fn on_room_join_rules(&self, _: Room, _: &SyncStateEvent<JoinRulesEventContent>) {}
/// Fires when `Client` receives a `RoomEvent::Tombstone` event.
async fn on_room_tombstone(&self, _: RoomState, _: &SyncStateEvent<TombstoneEventContent>) {}
async fn on_room_tombstone(&self, _: Room, _: &SyncStateEvent<TombstoneEventContent>) {}
// `RoomEvent`s from `IncomingState`
/// Fires when `Client` receives a `StateEvent::RoomMember` event.
async fn on_state_member(&self, _: RoomState, _: &SyncStateEvent<MemberEventContent>) {}
async fn on_state_member(&self, _: Room, _: &SyncStateEvent<MemberEventContent>) {}
/// Fires when `Client` receives a `StateEvent::RoomName` event.
async fn on_state_name(&self, _: RoomState, _: &SyncStateEvent<NameEventContent>) {}
async fn on_state_name(&self, _: Room, _: &SyncStateEvent<NameEventContent>) {}
/// Fires when `Client` receives a `StateEvent::RoomCanonicalAlias` event.
async fn on_state_canonical_alias(
&self,
_: RoomState,
_: Room,
_: &SyncStateEvent<CanonicalAliasEventContent>,
) {
}
/// Fires when `Client` receives a `StateEvent::RoomAliases` event.
async fn on_state_aliases(&self, _: RoomState, _: &SyncStateEvent<AliasesEventContent>) {}
async fn on_state_aliases(&self, _: Room, _: &SyncStateEvent<AliasesEventContent>) {}
/// Fires when `Client` receives a `StateEvent::RoomAvatar` event.
async fn on_state_avatar(&self, _: RoomState, _: &SyncStateEvent<AvatarEventContent>) {}
async fn on_state_avatar(&self, _: Room, _: &SyncStateEvent<AvatarEventContent>) {}
/// Fires when `Client` receives a `StateEvent::RoomPowerLevels` event.
async fn on_state_power_levels(
&self,
_: RoomState,
_: &SyncStateEvent<PowerLevelsEventContent>,
) {
}
async fn on_state_power_levels(&self, _: Room, _: &SyncStateEvent<PowerLevelsEventContent>) {}
/// Fires when `Client` receives a `StateEvent::RoomJoinRules` event.
async fn on_state_join_rules(&self, _: RoomState, _: &SyncStateEvent<JoinRulesEventContent>) {}
async fn on_state_join_rules(&self, _: Room, _: &SyncStateEvent<JoinRulesEventContent>) {}
// `AnyStrippedStateEvent`s
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomMember` event.
async fn on_stripped_state_member(
&self,
_: RoomState,
_: Room,
_: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>,
) {
}
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomName` event.
async fn on_stripped_state_name(&self, _: RoomState, _: &StrippedStateEvent<NameEventContent>) {
}
async fn on_stripped_state_name(&self, _: Room, _: &StrippedStateEvent<NameEventContent>) {}
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomCanonicalAlias` event.
async fn on_stripped_state_canonical_alias(
&self,
_: RoomState,
_: Room,
_: &StrippedStateEvent<CanonicalAliasEventContent>,
) {
}
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAliases` event.
async fn on_stripped_state_aliases(
&self,
_: RoomState,
_: Room,
_: &StrippedStateEvent<AliasesEventContent>,
) {
}
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAvatar` event.
async fn on_stripped_state_avatar(
&self,
_: RoomState,
_: &StrippedStateEvent<AvatarEventContent>,
) {
}
async fn on_stripped_state_avatar(&self, _: Room, _: &StrippedStateEvent<AvatarEventContent>) {}
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomPowerLevels` event.
async fn on_stripped_state_power_levels(
&self,
_: RoomState,
_: Room,
_: &StrippedStateEvent<PowerLevelsEventContent>,
) {
}
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomJoinRules` event.
async fn on_stripped_state_join_rules(
&self,
_: RoomState,
_: Room,
_: &StrippedStateEvent<JoinRulesEventContent>,
) {
}
// `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<IgnoredUserListEventContent>,
) {
}
/// Fires when `Client` receives a `NonRoomEvent::RoomCanonicalAlias` event.
async fn on_non_room_push_rules(&self, _: RoomState, _: &BasicEvent<PushRulesEventContent>) {}
async fn on_non_room_push_rules(&self, _: Room, _: &BasicEvent<PushRulesEventContent>) {}
/// Fires when `Client` receives a `NonRoomEvent::RoomAliases` event.
async fn on_non_room_fully_read(
&self,
_: RoomState,
_: Room,
_: &SyncEphemeralRoomEvent<FullyReadEventContent>,
) {
}
/// Fires when `Client` receives a `NonRoomEvent::Typing` event.
async fn on_non_room_typing(
&self,
_: RoomState,
_: &SyncEphemeralRoomEvent<TypingEventContent>,
) {
}
async fn on_non_room_typing(&self, _: Room, _: &SyncEphemeralRoomEvent<TypingEventContent>) {}
/// Fires when `Client` receives a `NonRoomEvent::Receipt` event.
///
/// This is always a read receipt.
async fn on_non_room_receipt(
&self,
_: RoomState,
_: &SyncEphemeralRoomEvent<ReceiptEventContent>,
) {
}
async fn on_non_room_receipt(&self, _: Room, _: &SyncEphemeralRoomEvent<ReceiptEventContent>) {}
// `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<MemberEventContent>) {
async fn on_room_member(&self, _: Room, _: &SyncStateEvent<MemberEventContent>) {
self.0.lock().await.push("member".to_string())
}
async fn on_room_name(&self, _: RoomState, _: &SyncStateEvent<NameEventContent>) {
async fn on_room_name(&self, _: Room, _: &SyncStateEvent<NameEventContent>) {
self.0.lock().await.push("name".to_string())
}
async fn on_room_canonical_alias(
&self,
_: RoomState,
_: Room,
_: &SyncStateEvent<CanonicalAliasEventContent>,
) {
self.0.lock().await.push("canonical".to_string())
}
async fn on_room_aliases(&self, _: RoomState, _: &SyncStateEvent<AliasesEventContent>) {
async fn on_room_aliases(&self, _: Room, _: &SyncStateEvent<AliasesEventContent>) {
self.0.lock().await.push("aliases".to_string())
}
async fn on_room_avatar(&self, _: RoomState, _: &SyncStateEvent<AvatarEventContent>) {
async fn on_room_avatar(&self, _: Room, _: &SyncStateEvent<AvatarEventContent>) {
self.0.lock().await.push("avatar".to_string())
}
async fn on_room_message(&self, _: RoomState, _: &SyncMessageEvent<MsgEventContent>) {
async fn on_room_message(&self, _: Room, _: &SyncMessageEvent<MsgEventContent>) {
self.0.lock().await.push("message".to_string())
}
async fn on_room_message_feedback(
&self,
_: RoomState,
_: Room,
_: &SyncMessageEvent<FeedbackEventContent>,
) {
self.0.lock().await.push("feedback".to_string())
}
async fn on_room_call_invite(
&self,
_: RoomState,
_: &SyncMessageEvent<InviteEventContent>,
) {
async fn on_room_call_invite(&self, _: Room, _: &SyncMessageEvent<InviteEventContent>) {
self.0.lock().await.push("call invite".to_string())
}
async fn on_room_call_answer(
&self,
_: RoomState,
_: &SyncMessageEvent<AnswerEventContent>,
) {
async fn on_room_call_answer(&self, _: Room, _: &SyncMessageEvent<AnswerEventContent>) {
self.0.lock().await.push("call answer".to_string())
}
async fn on_room_call_candidates(
&self,
_: RoomState,
_: Room,
_: &SyncMessageEvent<CandidatesEventContent>,
) {
self.0.lock().await.push("call candidates".to_string())
}
async fn on_room_call_hangup(
&self,
_: RoomState,
_: &SyncMessageEvent<HangupEventContent>,
) {
async fn on_room_call_hangup(&self, _: Room, _: &SyncMessageEvent<HangupEventContent>) {
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<PowerLevelsEventContent>,
) {
async fn on_room_power_levels(&self, _: Room, _: &SyncStateEvent<PowerLevelsEventContent>) {
self.0.lock().await.push("power".to_string())
}
async fn on_room_tombstone(&self, _: RoomState, _: &SyncStateEvent<TombstoneEventContent>) {
async fn on_room_tombstone(&self, _: Room, _: &SyncStateEvent<TombstoneEventContent>) {
self.0.lock().await.push("tombstone".to_string())
}
async fn on_state_member(&self, _: RoomState, _: &SyncStateEvent<MemberEventContent>) {
async fn on_state_member(&self, _: Room, _: &SyncStateEvent<MemberEventContent>) {
self.0.lock().await.push("state member".to_string())
}
async fn on_state_name(&self, _: RoomState, _: &SyncStateEvent<NameEventContent>) {
async fn on_state_name(&self, _: Room, _: &SyncStateEvent<NameEventContent>) {
self.0.lock().await.push("state name".to_string())
}
async fn on_state_canonical_alias(
&self,
_: RoomState,
_: Room,
_: &SyncStateEvent<CanonicalAliasEventContent>,
) {
self.0.lock().await.push("state canonical".to_string())
}
async fn on_state_aliases(&self, _: RoomState, _: &SyncStateEvent<AliasesEventContent>) {
async fn on_state_aliases(&self, _: Room, _: &SyncStateEvent<AliasesEventContent>) {
self.0.lock().await.push("state aliases".to_string())
}
async fn on_state_avatar(&self, _: RoomState, _: &SyncStateEvent<AvatarEventContent>) {
async fn on_state_avatar(&self, _: Room, _: &SyncStateEvent<AvatarEventContent>) {
self.0.lock().await.push("state avatar".to_string())
}
async fn on_state_power_levels(
&self,
_: RoomState,
_: Room,
_: &SyncStateEvent<PowerLevelsEventContent>,
) {
self.0.lock().await.push("state power".to_string())
}
async fn on_state_join_rules(
&self,
_: RoomState,
_: &SyncStateEvent<JoinRulesEventContent>,
) {
async fn on_state_join_rules(&self, _: Room, _: &SyncStateEvent<JoinRulesEventContent>) {
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<MemberEventContent>,
_: Option<MemberEventContent>,
) {
@ -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<NameEventContent>,
) {
async fn on_stripped_state_name(&self, _: Room, _: &StrippedStateEvent<NameEventContent>) {
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<CanonicalAliasEventContent>,
) {
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<AliasesEventContent>,
) {
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<AvatarEventContent>,
) {
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<PowerLevelsEventContent>,
) {
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<JoinRulesEventContent>,
) {
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<IgnoredUserListEventContent>,
) {
self.0.lock().await.push("account ignore".to_string())
}
async fn on_non_room_push_rules(
&self,
_: RoomState,
_: &BasicEvent<PushRulesEventContent>,
) {
async fn on_non_room_push_rules(&self, _: Room, _: &BasicEvent<PushRulesEventContent>) {
self.0.lock().await.push("account push rules".to_string())
}
async fn on_non_room_fully_read(
&self,
_: RoomState,
_: Room,
_: &SyncEphemeralRoomEvent<FullyReadEventContent>,
) {
self.0.lock().await.push("account read".to_string())
}
async fn on_non_room_typing(
&self,
_: RoomState,
_: Room,
_: &SyncEphemeralRoomEvent<TypingEventContent>,
) {
self.0.lock().await.push("typing event".to_string())
}
async fn on_non_room_receipt(
&self,
_: RoomState,
_: Room,
_: &SyncEphemeralRoomEvent<ReceiptEventContent>,
) {
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())
}
}

View File

@ -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};

View File

@ -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<RwLock<Room>>`
#[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<JoinedRoom> {
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<InvitedRoom> {
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<LeftRoom> {
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<EncryptionEventContent> {
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.

View File

@ -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,

View File

@ -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<RoomState> {
pub fn get_rooms(&self) -> Vec<Room> {
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<JoinedRoom> {
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<InvitedRoom> {
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<LeftRoom> {
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<RoomState> {
pub fn get_room(&self, room_id: &RoomId) -> Option<Room> {
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),
})
}