Merge branch 'room_merge'

master
Damir Jelić 2021-03-08 13:39:46 +01:00
commit e5585b57e8
15 changed files with 156 additions and 533 deletions

View File

@ -4,7 +4,7 @@ use tokio::time::{sleep, Duration};
use matrix_sdk::{ use matrix_sdk::{
self, async_trait, self, async_trait,
events::{room::member::MemberEventContent, StrippedStateEvent}, events::{room::member::MemberEventContent, StrippedStateEvent},
Client, ClientConfig, EventHandler, RoomState, SyncSettings, Client, ClientConfig, EventHandler, Room, RoomType, SyncSettings,
}; };
use url::Url; use url::Url;
@ -22,7 +22,7 @@ impl AutoJoinBot {
impl EventHandler for AutoJoinBot { impl EventHandler for AutoJoinBot {
async fn on_stripped_state_member( async fn on_stripped_state_member(
&self, &self,
room: RoomState, room: Room,
room_member: &StrippedStateEvent<MemberEventContent>, room_member: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>, _: Option<MemberEventContent>,
) { ) {
@ -30,7 +30,7 @@ impl EventHandler for AutoJoinBot {
return; return;
} }
if let RoomState::Invited(room) = room { if room.room_type() == RoomType::Invited {
println!("Autojoining room {}", room.room_id()); println!("Autojoining room {}", room.room_id());
let mut delay = 2; let mut delay = 2;

View File

@ -6,7 +6,7 @@ use matrix_sdk::{
room::message::{MessageEventContent, MessageType, TextMessageEventContent}, room::message::{MessageEventContent, MessageType, TextMessageEventContent},
AnyMessageEventContent, SyncMessageEvent, AnyMessageEventContent, SyncMessageEvent,
}, },
Client, ClientConfig, EventHandler, RoomState, SyncSettings, Client, ClientConfig, EventHandler, Room, RoomType, SyncSettings,
}; };
use url::Url; use url::Url;
@ -24,12 +24,8 @@ impl CommandBot {
#[async_trait] #[async_trait]
impl EventHandler for CommandBot { impl EventHandler for CommandBot {
async fn on_room_message( async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
&self, if room.room_type() == RoomType::Joined {
room: RoomState,
event: &SyncMessageEvent<MessageEventContent>,
) {
if let RoomState::Joined(room) = room {
let msg_body = if let SyncMessageEvent { let msg_body = if let SyncMessageEvent {
content: content:
MessageEventContent { MessageEventContent {

View File

@ -14,7 +14,7 @@ use matrix_sdk::{
room::message::{MessageEventContent, MessageType, TextMessageEventContent}, room::message::{MessageEventContent, MessageType, TextMessageEventContent},
SyncMessageEvent, SyncMessageEvent,
}, },
Client, EventHandler, RoomState, SyncSettings, Client, EventHandler, Room, RoomType, SyncSettings,
}; };
use url::Url; use url::Url;
@ -32,12 +32,8 @@ impl ImageBot {
#[async_trait] #[async_trait]
impl EventHandler for ImageBot { impl EventHandler for ImageBot {
async fn on_room_message( async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
&self, if room.room_type() == RoomType::Joined {
room: RoomState,
event: &SyncMessageEvent<MessageEventContent>,
) {
if let RoomState::Joined(room) = room {
let msg_body = if let SyncMessageEvent { let msg_body = if let SyncMessageEvent {
content: content:
MessageEventContent { MessageEventContent {

View File

@ -7,19 +7,15 @@ use matrix_sdk::{
room::message::{MessageEventContent, MessageType, TextMessageEventContent}, room::message::{MessageEventContent, MessageType, TextMessageEventContent},
SyncMessageEvent, SyncMessageEvent,
}, },
Client, EventHandler, RoomState, SyncSettings, Client, EventHandler, Room, RoomType, SyncSettings,
}; };
struct EventCallback; struct EventCallback;
#[async_trait] #[async_trait]
impl EventHandler for EventCallback { impl EventHandler for EventCallback {
async fn on_room_message( async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
&self, if room.room_type() == RoomType::Joined {
room: RoomState,
event: &SyncMessageEvent<MessageEventContent>,
) {
if let RoomState::Joined(room) = room {
if let SyncMessageEvent { if let SyncMessageEvent {
content: content:
MessageEventContent { MessageEventContent {

View File

@ -41,8 +41,7 @@ use tracing::{error, info, instrument};
use matrix_sdk_base::{ use matrix_sdk_base::{
deserialized_responses::{MembersResponse, SyncResponse}, deserialized_responses::{MembersResponse, SyncResponse},
BaseClient, BaseClientConfig, EventHandler, InvitedRoom, JoinedRoom, LeftRoom, RoomState, BaseClient, BaseClientConfig, EventHandler, Room, RoomType, Session, Store,
Session, Store,
}; };
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
@ -564,34 +563,34 @@ impl Client {
/// Get all the rooms the client knows about. /// Get all the rooms the client knows about.
/// ///
/// This will return the list of joined, invited, and left rooms. /// 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() self.store().get_rooms()
} }
/// Returns the joined rooms this client knows about. /// Returns the joined rooms this client knows about.
pub fn joined_rooms(&self) -> Vec<JoinedRoom> { pub fn joined_rooms(&self) -> Vec<Room> {
self.store() self.store()
.get_rooms() .get_rooms()
.into_iter() .into_iter()
.filter_map(|r| r.joined()) .filter(|room| room.room_type() == RoomType::Joined)
.collect() .collect()
} }
/// Returns the invited rooms this client knows about. /// Returns the invited rooms this client knows about.
pub fn invited_rooms(&self) -> Vec<InvitedRoom> { pub fn invited_rooms(&self) -> Vec<Room> {
self.store() self.store()
.get_rooms() .get_rooms()
.into_iter() .into_iter()
.filter_map(|r| r.invited()) .filter(|room| room.room_type() == RoomType::Invited)
.collect() .collect()
} }
/// Returns the left rooms this client knows about. /// Returns the left rooms this client knows about.
pub fn left_rooms(&self) -> Vec<LeftRoom> { pub fn left_rooms(&self) -> Vec<Room> {
self.store() self.store()
.get_rooms() .get_rooms()
.into_iter() .into_iter()
.filter_map(|r| r.left()) .filter(|room| room.room_type() == RoomType::Left)
.collect() .collect()
} }
@ -600,8 +599,10 @@ impl Client {
/// # Arguments /// # Arguments
/// ///
/// `room_id` - The unique id of the room that should be fetched. /// `room_id` - The unique id of the room that should be fetched.
pub fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> { pub fn get_joined_room(&self, room_id: &RoomId) -> Option<Room> {
self.store().get_joined_room(room_id) self.store()
.get_room(room_id)
.filter(|room| room.room_type() == RoomType::Joined)
} }
/// Get an invited room with the given room id. /// Get an invited room with the given room id.
@ -609,8 +610,10 @@ impl Client {
/// # Arguments /// # Arguments
/// ///
/// `room_id` - The unique id of the room that should be fetched. /// `room_id` - The unique id of the room that should be fetched.
pub fn get_invited_room(&self, room_id: &RoomId) -> Option<InvitedRoom> { pub fn get_invited_room(&self, room_id: &RoomId) -> Option<Room> {
self.store().get_invited_room(room_id) self.store()
.get_room(room_id)
.filter(|room| room.room_type() == RoomType::Invited)
} }
/// Get a left room with the given room id. /// Get a left room with the given room id.
@ -618,8 +621,10 @@ impl Client {
/// # Arguments /// # Arguments
/// ///
/// `room_id` - The unique id of the room that should be fetched. /// `room_id` - The unique id of the room that should be fetched.
pub fn get_left_room(&self, room_id: &RoomId) -> Option<LeftRoom> { pub fn get_left_room(&self, room_id: &RoomId) -> Option<Room> {
self.store().get_left_room(room_id) self.store()
.get_room(room_id)
.filter(|room| room.room_type() == RoomType::Left)
} }
/// Login to the server. /// 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)))] #[cfg_attr(feature = "docs", doc(cfg(encryption)))]
pub use matrix_sdk_base::crypto::{EncryptionInfo, LocalTrust}; pub use matrix_sdk_base::crypto::{EncryptionInfo, LocalTrust};
pub use matrix_sdk_base::{ pub use matrix_sdk_base::{
CustomEvent, Error as BaseError, EventHandler, InvitedRoom, JoinedRoom, LeftRoom, RoomInfo, CustomEvent, Error as BaseError, EventHandler, Room, RoomInfo, RoomMember, RoomType, Session,
RoomMember, RoomState, RoomType, Session, StateChanges, StoreError, StateChanges, StoreError,
}; };
pub use matrix_sdk_common::*; pub use matrix_sdk_common::*;

View File

@ -63,10 +63,10 @@ use zeroize::Zeroizing;
use crate::{ use crate::{
error::Result, error::Result,
event_handler::Handler, event_handler::Handler,
rooms::{RoomInfo, RoomType, StrippedRoomInfo}, rooms::{Room, RoomInfo, RoomType},
session::Session, session::Session,
store::{ambiguity_map::AmbiguityCache, Result as StoreResult, StateChanges, Store}, store::{ambiguity_map::AmbiguityCache, Result as StoreResult, StateChanges, Store},
EventHandler, RoomState, EventHandler,
}; };
pub type Token = String; pub type Token = String;
@ -481,7 +481,7 @@ impl BaseClient {
} }
} }
_ => { _ => {
room_info.handle_state_event(&s); room_info.handle_state_event(&s.content());
changes.add_state_event(room_id, s.clone()); changes.add_state_event(room_id, s.clone());
} }
}, },
@ -525,7 +525,7 @@ impl BaseClient {
fn handle_invited_state( fn handle_invited_state(
&self, &self,
events: Vec<Raw<AnyStrippedStateEvent>>, events: Vec<Raw<AnyStrippedStateEvent>>,
room_info: &mut StrippedRoomInfo, room_info: &mut RoomInfo,
) -> ( ) -> (
InviteState, InviteState,
BTreeMap<UserId, StrippedMemberEvent>, BTreeMap<UserId, StrippedMemberEvent>,
@ -549,7 +549,7 @@ impl BaseClient {
), ),
} }
} else { } else {
room_info.handle_state_event(&e); room_info.handle_state_event(&e.content());
state_events state_events
.entry(e.content().event_type().to_owned()) .entry(e.content().event_type().to_owned())
.or_insert_with(BTreeMap::new) .or_insert_with(BTreeMap::new)
@ -598,7 +598,7 @@ impl BaseClient {
}) })
{ {
state.events.push(event.clone()); state.events.push(event.clone());
room_info.handle_state_event(&event); room_info.handle_state_event(&event.content());
if let AnySyncStateEvent::RoomMember(member) = event { if let AnySyncStateEvent::RoomMember(member) = event {
match MemberEvent::try_from(member) { match MemberEvent::try_from(member) {
@ -1178,7 +1178,7 @@ impl BaseClient {
/// # Arguments /// # Arguments
/// ///
/// * `room_id` - The id of the room that should be fetched. /// * `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) self.store.get_room(room_id)
} }

View File

@ -47,7 +47,7 @@ use crate::{
AnySyncStateEvent, BasicEvent, StrippedStateEvent, SyncEphemeralRoomEvent, AnySyncStateEvent, BasicEvent, StrippedStateEvent, SyncEphemeralRoomEvent,
SyncMessageEvent, SyncStateEvent, SyncMessageEvent, SyncStateEvent,
}, },
rooms::RoomState, rooms::Room,
Store, Store,
}; };
use matrix_sdk_common::async_trait; use matrix_sdk_common::async_trait;
@ -66,7 +66,7 @@ impl Deref for Handler {
} }
impl 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) 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 { match event {
AnySyncRoomEvent::State(event) => match event { AnySyncRoomEvent::State(event) => match event {
AnySyncStateEvent::RoomMember(e) => self.on_room_member(room, e).await, 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 { match event {
AnySyncStateEvent::RoomMember(member) => self.on_state_member(room, &member).await, AnySyncStateEvent::RoomMember(member) => self.on_state_member(room, &member).await,
AnySyncStateEvent::RoomName(name) => self.on_state_name(room, &name).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( pub(crate) async fn handle_stripped_state_event(
&self, &self,
// TODO these events are only handleted in invited rooms. // TODO these events are only handleted in invited rooms.
room: RoomState, room: Room,
event: &AnyStrippedStateEvent, event: &AnyStrippedStateEvent,
) { ) {
match event { 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 { match event {
AnyBasicEvent::Presence(presence) => self.on_non_room_presence(room, &presence).await, AnyBasicEvent::Presence(presence) => self.on_non_room_presence(room, &presence).await,
AnyBasicEvent::IgnoredUserList(ignored) => { AnyBasicEvent::IgnoredUserList(ignored) => {
@ -229,7 +229,7 @@ impl Handler {
pub(crate) async fn handle_ephemeral_event( pub(crate) async fn handle_ephemeral_event(
&self, &self,
room: RoomState, room: Room,
event: &AnySyncEphemeralRoomEvent, event: &AnySyncEphemeralRoomEvent,
) { ) {
match event { match event {
@ -276,7 +276,7 @@ pub enum CustomEvent<'c> {
/// # room::message::{MessageEventContent, MessageType, TextMessageEventContent}, /// # room::message::{MessageEventContent, MessageType, TextMessageEventContent},
/// # SyncMessageEvent /// # SyncMessageEvent
/// # }, /// # },
/// # EventHandler, RoomState /// # EventHandler, Room, RoomType,
/// # }; /// # };
/// # use matrix_sdk_common::{async_trait, locks::RwLock}; /// # use matrix_sdk_common::{async_trait, locks::RwLock};
/// ///
@ -284,8 +284,8 @@ pub enum CustomEvent<'c> {
/// ///
/// #[async_trait] /// #[async_trait]
/// impl EventHandler for EventCallback { /// impl EventHandler for EventCallback {
/// async fn on_room_message(&self, room: RoomState, event: &SyncMessageEvent<MessageEventContent>) { /// async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
/// if let RoomState::Joined(room) = room { /// if room.room_type() == RoomType::Joined {
/// if let SyncMessageEvent { /// if let SyncMessageEvent {
/// content: /// content:
/// MessageEventContent { /// MessageEventContent {
@ -311,165 +311,130 @@ pub enum CustomEvent<'c> {
pub trait EventHandler: Send + Sync { pub trait EventHandler: Send + Sync {
// ROOM EVENTS from `IncomingTimeline` // ROOM EVENTS from `IncomingTimeline`
/// Fires when `Client` receives a `RoomEvent::RoomMember` event. /// 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. /// 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. /// Fires when `Client` receives a `RoomEvent::RoomCanonicalAlias` event.
async fn on_room_canonical_alias( async fn on_room_canonical_alias(
&self, &self,
_: RoomState, _: Room,
_: &SyncStateEvent<CanonicalAliasEventContent>, _: &SyncStateEvent<CanonicalAliasEventContent>,
) { ) {
} }
/// Fires when `Client` receives a `RoomEvent::RoomAliases` event. /// 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. /// 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. /// 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. /// Fires when `Client` receives a `RoomEvent::RoomMessageFeedback` event.
async fn on_room_message_feedback( async fn on_room_message_feedback(&self, _: Room, _: &SyncMessageEvent<FeedbackEventContent>) {}
&self,
_: RoomState,
_: &SyncMessageEvent<FeedbackEventContent>,
) {
}
/// Fires when `Client` receives a `RoomEvent::CallInvite` event /// 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 /// 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 /// Fires when `Client` receives a `RoomEvent::CallCandidates` event
async fn on_room_call_candidates( async fn on_room_call_candidates(&self, _: Room, _: &SyncMessageEvent<CandidatesEventContent>) {
&self,
_: RoomState,
_: &SyncMessageEvent<CandidatesEventContent>,
) {
} }
/// Fires when `Client` receives a `RoomEvent::CallHangup` event /// 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. /// 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. /// Fires when `Client` receives a `RoomEvent::RoomPowerLevels` event.
async fn on_room_power_levels( async fn on_room_power_levels(&self, _: Room, _: &SyncStateEvent<PowerLevelsEventContent>) {}
&self,
_: RoomState,
_: &SyncStateEvent<PowerLevelsEventContent>,
) {
}
/// Fires when `Client` receives a `RoomEvent::Tombstone` event. /// 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. /// 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` // `RoomEvent`s from `IncomingState`
/// Fires when `Client` receives a `StateEvent::RoomMember` event. /// 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. /// 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. /// Fires when `Client` receives a `StateEvent::RoomCanonicalAlias` event.
async fn on_state_canonical_alias( async fn on_state_canonical_alias(
&self, &self,
_: RoomState, _: Room,
_: &SyncStateEvent<CanonicalAliasEventContent>, _: &SyncStateEvent<CanonicalAliasEventContent>,
) { ) {
} }
/// Fires when `Client` receives a `StateEvent::RoomAliases` event. /// 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. /// 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. /// Fires when `Client` receives a `StateEvent::RoomPowerLevels` event.
async fn on_state_power_levels( async fn on_state_power_levels(&self, _: Room, _: &SyncStateEvent<PowerLevelsEventContent>) {}
&self,
_: RoomState,
_: &SyncStateEvent<PowerLevelsEventContent>,
) {
}
/// Fires when `Client` receives a `StateEvent::RoomJoinRules` event. /// 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 // `AnyStrippedStateEvent`s
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomMember` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomMember` event.
async fn on_stripped_state_member( async fn on_stripped_state_member(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<MemberEventContent>, _: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>, _: Option<MemberEventContent>,
) { ) {
} }
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomName` event. /// 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. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomCanonicalAlias` event.
async fn on_stripped_state_canonical_alias( async fn on_stripped_state_canonical_alias(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<CanonicalAliasEventContent>, _: &StrippedStateEvent<CanonicalAliasEventContent>,
) { ) {
} }
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAliases` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAliases` event.
async fn on_stripped_state_aliases( async fn on_stripped_state_aliases(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<AliasesEventContent>, _: &StrippedStateEvent<AliasesEventContent>,
) { ) {
} }
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAvatar` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAvatar` event.
async fn on_stripped_state_avatar( async fn on_stripped_state_avatar(&self, _: Room, _: &StrippedStateEvent<AvatarEventContent>) {}
&self,
_: RoomState,
_: &StrippedStateEvent<AvatarEventContent>,
) {
}
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomPowerLevels` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomPowerLevels` event.
async fn on_stripped_state_power_levels( async fn on_stripped_state_power_levels(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<PowerLevelsEventContent>, _: &StrippedStateEvent<PowerLevelsEventContent>,
) { ) {
} }
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomJoinRules` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomJoinRules` event.
async fn on_stripped_state_join_rules( async fn on_stripped_state_join_rules(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<JoinRulesEventContent>, _: &StrippedStateEvent<JoinRulesEventContent>,
) { ) {
} }
// `NonRoomEvent` (this is a type alias from ruma_events) // `NonRoomEvent` (this is a type alias from ruma_events)
/// Fires when `Client` receives a `NonRoomEvent::RoomPresence` event. /// 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. /// Fires when `Client` receives a `NonRoomEvent::RoomName` event.
async fn on_non_room_ignored_users( async fn on_non_room_ignored_users(
&self, &self,
_: RoomState, _: Room,
_: &BasicEvent<IgnoredUserListEventContent>, _: &BasicEvent<IgnoredUserListEventContent>,
) { ) {
} }
/// Fires when `Client` receives a `NonRoomEvent::RoomCanonicalAlias` event. /// 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. /// Fires when `Client` receives a `NonRoomEvent::RoomAliases` event.
async fn on_non_room_fully_read( async fn on_non_room_fully_read(
&self, &self,
_: RoomState, _: Room,
_: &SyncEphemeralRoomEvent<FullyReadEventContent>, _: &SyncEphemeralRoomEvent<FullyReadEventContent>,
) { ) {
} }
/// Fires when `Client` receives a `NonRoomEvent::Typing` event. /// Fires when `Client` receives a `NonRoomEvent::Typing` event.
async fn on_non_room_typing( async fn on_non_room_typing(&self, _: Room, _: &SyncEphemeralRoomEvent<TypingEventContent>) {}
&self,
_: RoomState,
_: &SyncEphemeralRoomEvent<TypingEventContent>,
) {
}
/// Fires when `Client` receives a `NonRoomEvent::Receipt` event. /// Fires when `Client` receives a `NonRoomEvent::Receipt` event.
/// ///
/// This is always a read receipt. /// This is always a read receipt.
async fn on_non_room_receipt( async fn on_non_room_receipt(&self, _: Room, _: &SyncEphemeralRoomEvent<ReceiptEventContent>) {}
&self,
_: RoomState,
_: &SyncEphemeralRoomEvent<ReceiptEventContent>,
) {
}
// `PresenceEvent` is a struct so there is only the one method // `PresenceEvent` is a struct so there is only the one method
/// Fires when `Client` receives a `NonRoomEvent::RoomAliases` event. /// Fires when `Client` receives a `NonRoomEvent::RoomAliases` event.
@ -479,14 +444,14 @@ pub trait EventHandler: Send + Sync {
/// because the event was unknown to ruma. /// because the event was unknown to ruma.
/// ///
/// The only guarantee this method can give about the event is that it is valid JSON. /// 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 /// Fires when `Client` receives a `Event::Custom` event or if deserialization fails
/// because the event was unknown to ruma. /// because the event was unknown to ruma.
/// ///
/// The only guarantee this method can give about the event is that it is in the /// The only guarantee this method can give about the event is that it is in the
/// shape of a valid matrix event. /// shape of a valid matrix event.
async fn on_custom_event(&self, _: RoomState, _: &CustomEvent<'_>) {} async fn on_custom_event(&self, _: Room, _: &CustomEvent<'_>) {}
} }
#[cfg(test)] #[cfg(test)]
@ -505,108 +470,88 @@ mod test {
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl EventHandler for EvHandlerTest { 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()) 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()) self.0.lock().await.push("name".to_string())
} }
async fn on_room_canonical_alias( async fn on_room_canonical_alias(
&self, &self,
_: RoomState, _: Room,
_: &SyncStateEvent<CanonicalAliasEventContent>, _: &SyncStateEvent<CanonicalAliasEventContent>,
) { ) {
self.0.lock().await.push("canonical".to_string()) 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()) 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()) 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()) self.0.lock().await.push("message".to_string())
} }
async fn on_room_message_feedback( async fn on_room_message_feedback(
&self, &self,
_: RoomState, _: Room,
_: &SyncMessageEvent<FeedbackEventContent>, _: &SyncMessageEvent<FeedbackEventContent>,
) { ) {
self.0.lock().await.push("feedback".to_string()) self.0.lock().await.push("feedback".to_string())
} }
async fn on_room_call_invite( async fn on_room_call_invite(&self, _: Room, _: &SyncMessageEvent<InviteEventContent>) {
&self,
_: RoomState,
_: &SyncMessageEvent<InviteEventContent>,
) {
self.0.lock().await.push("call invite".to_string()) self.0.lock().await.push("call invite".to_string())
} }
async fn on_room_call_answer( async fn on_room_call_answer(&self, _: Room, _: &SyncMessageEvent<AnswerEventContent>) {
&self,
_: RoomState,
_: &SyncMessageEvent<AnswerEventContent>,
) {
self.0.lock().await.push("call answer".to_string()) self.0.lock().await.push("call answer".to_string())
} }
async fn on_room_call_candidates( async fn on_room_call_candidates(
&self, &self,
_: RoomState, _: Room,
_: &SyncMessageEvent<CandidatesEventContent>, _: &SyncMessageEvent<CandidatesEventContent>,
) { ) {
self.0.lock().await.push("call candidates".to_string()) self.0.lock().await.push("call candidates".to_string())
} }
async fn on_room_call_hangup( async fn on_room_call_hangup(&self, _: Room, _: &SyncMessageEvent<HangupEventContent>) {
&self,
_: RoomState,
_: &SyncMessageEvent<HangupEventContent>,
) {
self.0.lock().await.push("call hangup".to_string()) 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()) self.0.lock().await.push("redaction".to_string())
} }
async fn on_room_power_levels( async fn on_room_power_levels(&self, _: Room, _: &SyncStateEvent<PowerLevelsEventContent>) {
&self,
_: RoomState,
_: &SyncStateEvent<PowerLevelsEventContent>,
) {
self.0.lock().await.push("power".to_string()) 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()) 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()) 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()) self.0.lock().await.push("state name".to_string())
} }
async fn on_state_canonical_alias( async fn on_state_canonical_alias(
&self, &self,
_: RoomState, _: Room,
_: &SyncStateEvent<CanonicalAliasEventContent>, _: &SyncStateEvent<CanonicalAliasEventContent>,
) { ) {
self.0.lock().await.push("state canonical".to_string()) 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()) 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()) self.0.lock().await.push("state avatar".to_string())
} }
async fn on_state_power_levels( async fn on_state_power_levels(
&self, &self,
_: RoomState, _: Room,
_: &SyncStateEvent<PowerLevelsEventContent>, _: &SyncStateEvent<PowerLevelsEventContent>,
) { ) {
self.0.lock().await.push("state power".to_string()) self.0.lock().await.push("state power".to_string())
} }
async fn on_state_join_rules( async fn on_state_join_rules(&self, _: Room, _: &SyncStateEvent<JoinRulesEventContent>) {
&self,
_: RoomState,
_: &SyncStateEvent<JoinRulesEventContent>,
) {
self.0.lock().await.push("state rules".to_string()) self.0.lock().await.push("state rules".to_string())
} }
@ -614,7 +559,7 @@ mod test {
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomMember` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomMember` event.
async fn on_stripped_state_member( async fn on_stripped_state_member(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<MemberEventContent>, _: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>, _: Option<MemberEventContent>,
) { ) {
@ -624,17 +569,13 @@ mod test {
.push("stripped state member".to_string()) .push("stripped state member".to_string())
} }
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomName` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomName` event.
async fn on_stripped_state_name( async fn on_stripped_state_name(&self, _: Room, _: &StrippedStateEvent<NameEventContent>) {
&self,
_: RoomState,
_: &StrippedStateEvent<NameEventContent>,
) {
self.0.lock().await.push("stripped state name".to_string()) self.0.lock().await.push("stripped state name".to_string())
} }
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomCanonicalAlias` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomCanonicalAlias` event.
async fn on_stripped_state_canonical_alias( async fn on_stripped_state_canonical_alias(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<CanonicalAliasEventContent>, _: &StrippedStateEvent<CanonicalAliasEventContent>,
) { ) {
self.0 self.0
@ -645,7 +586,7 @@ mod test {
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAliases` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAliases` event.
async fn on_stripped_state_aliases( async fn on_stripped_state_aliases(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<AliasesEventContent>, _: &StrippedStateEvent<AliasesEventContent>,
) { ) {
self.0 self.0
@ -656,7 +597,7 @@ mod test {
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAvatar` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomAvatar` event.
async fn on_stripped_state_avatar( async fn on_stripped_state_avatar(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<AvatarEventContent>, _: &StrippedStateEvent<AvatarEventContent>,
) { ) {
self.0 self.0
@ -667,7 +608,7 @@ mod test {
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomPowerLevels` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomPowerLevels` event.
async fn on_stripped_state_power_levels( async fn on_stripped_state_power_levels(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<PowerLevelsEventContent>, _: &StrippedStateEvent<PowerLevelsEventContent>,
) { ) {
self.0.lock().await.push("stripped state power".to_string()) self.0.lock().await.push("stripped state power".to_string())
@ -675,46 +616,42 @@ mod test {
/// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomJoinRules` event. /// Fires when `Client` receives a `AnyStrippedStateEvent::StrippedRoomJoinRules` event.
async fn on_stripped_state_join_rules( async fn on_stripped_state_join_rules(
&self, &self,
_: RoomState, _: Room,
_: &StrippedStateEvent<JoinRulesEventContent>, _: &StrippedStateEvent<JoinRulesEventContent>,
) { ) {
self.0.lock().await.push("stripped state rules".to_string()) 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()) self.0.lock().await.push("presence".to_string())
} }
async fn on_non_room_ignored_users( async fn on_non_room_ignored_users(
&self, &self,
_: RoomState, _: Room,
_: &BasicEvent<IgnoredUserListEventContent>, _: &BasicEvent<IgnoredUserListEventContent>,
) { ) {
self.0.lock().await.push("account ignore".to_string()) self.0.lock().await.push("account ignore".to_string())
} }
async fn on_non_room_push_rules( async fn on_non_room_push_rules(&self, _: Room, _: &BasicEvent<PushRulesEventContent>) {
&self,
_: RoomState,
_: &BasicEvent<PushRulesEventContent>,
) {
self.0.lock().await.push("account push rules".to_string()) self.0.lock().await.push("account push rules".to_string())
} }
async fn on_non_room_fully_read( async fn on_non_room_fully_read(
&self, &self,
_: RoomState, _: Room,
_: &SyncEphemeralRoomEvent<FullyReadEventContent>, _: &SyncEphemeralRoomEvent<FullyReadEventContent>,
) { ) {
self.0.lock().await.push("account read".to_string()) self.0.lock().await.push("account read".to_string())
} }
async fn on_non_room_typing( async fn on_non_room_typing(
&self, &self,
_: RoomState, _: Room,
_: &SyncEphemeralRoomEvent<TypingEventContent>, _: &SyncEphemeralRoomEvent<TypingEventContent>,
) { ) {
self.0.lock().await.push("typing event".to_string()) self.0.lock().await.push("typing event".to_string())
} }
async fn on_non_room_receipt( async fn on_non_room_receipt(
&self, &self,
_: RoomState, _: Room,
_: &SyncEphemeralRoomEvent<ReceiptEventContent>, _: &SyncEphemeralRoomEvent<ReceiptEventContent>,
) { ) {
self.0.lock().await.push("receipt event".to_string()) self.0.lock().await.push("receipt event".to_string())
@ -722,10 +659,10 @@ mod test {
async fn on_presence_event(&self, _: &PresenceEvent) { async fn on_presence_event(&self, _: &PresenceEvent) {
self.0.lock().await.push("presence event".to_string()) 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()) 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()) self.0.lock().await.push("custom event".to_string())
} }
} }

View File

@ -52,10 +52,7 @@ mod session;
mod store; mod store;
pub use event_handler::{CustomEvent, EventHandler}; pub use event_handler::{CustomEvent, EventHandler};
pub use rooms::{ pub use rooms::{Room, RoomInfo, RoomMember, RoomType};
InvitedRoom, JoinedRoom, LeftRoom, Room, RoomInfo, RoomMember, RoomState, RoomType,
StrippedRoom, StrippedRoomInfo,
};
pub use store::{StateChanges, StateStore, Store, StoreError}; pub use store::{StateChanges, StateStore, Store, StoreError};
pub use client::{BaseClient, BaseClientConfig}; pub use client::{BaseClient, BaseClientConfig};

View File

@ -1,6 +1,5 @@
mod members; mod members;
mod normal; mod normal;
mod stripped;
use matrix_sdk_common::{ use matrix_sdk_common::{
events::room::{ events::room::{
@ -10,12 +9,11 @@ use matrix_sdk_common::{
identifiers::UserId, identifiers::UserId,
}; };
pub use normal::{Room, RoomInfo, RoomType}; pub use normal::{Room, RoomInfo, RoomType};
pub use stripped::{StrippedRoom, StrippedRoomInfo};
pub use members::RoomMember; pub use members::RoomMember;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{cmp::max, ops::Deref}; use std::cmp::max;
use matrix_sdk_common::{ use matrix_sdk_common::{
events::{ events::{
@ -25,132 +23,6 @@ use matrix_sdk_common::{
identifiers::RoomAliasId, 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: StrippedRoom,
}
impl Deref for InvitedRoom {
type Target = StrippedRoom;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
/// A base room info struct that is the backbone of normal as well as stripped /// 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 /// rooms. Holds all the state events that are important to present a room to
/// users. /// users.

View File

@ -29,7 +29,7 @@ use matrix_sdk_common::{
guest_access::GuestAccess, history_visibility::HistoryVisibility, join_rules::JoinRule, guest_access::GuestAccess, history_visibility::HistoryVisibility, join_rules::JoinRule,
tombstone::TombstoneEventContent, tombstone::TombstoneEventContent,
}, },
AnySyncStateEvent, EventType, AnyStateEventContent, AnySyncStateEvent, EventType,
}, },
identifiers::{RoomAliasId, RoomId, UserId}, identifiers::{RoomAliasId, RoomId, UserId},
}; };
@ -43,7 +43,7 @@ use crate::{
use super::{BaseRoomInfo, RoomMember}; use super::{BaseRoomInfo, RoomMember};
/// The underlying room data structure collecting state for joined and left rooms. /// The underlying room data structure collecting state for joined, left and invtied rooms.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Room { pub struct Room {
room_id: Arc<RoomId>, room_id: Arc<RoomId>,
@ -67,7 +67,7 @@ pub struct RoomSummary {
/// Enum keeping track in which state the room is, e.g. if our own user is /// Enum keeping track in which state the room is, e.g. if our own user is
/// joined, invited, or has left the room. /// joined, invited, or has left the room.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum RoomType { pub enum RoomType {
/// The room is in a joined state. /// The room is in a joined state.
Joined, Joined,
@ -484,8 +484,8 @@ impl RoomInfo {
self.base_info.encryption.is_some() self.base_info.encryption.is_some()
} }
pub(crate) fn handle_state_event(&mut self, event: &AnySyncStateEvent) -> bool { pub(crate) fn handle_state_event(&mut self, event: &AnyStateEventContent) -> bool {
self.base_info.handle_state_event(&event.content()) self.base_info.handle_state_event(&event)
} }
pub(crate) fn update_notification_count( pub(crate) fn update_notification_count(

View File

@ -1,145 +0,0 @@
// Copyright 2020 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::sync::{Arc, Mutex as SyncMutex};
use matrix_sdk_common::{
events::{
room::{encryption::EncryptionEventContent, history_visibility::HistoryVisibility},
AnyStrippedStateEvent,
},
identifiers::{RoomId, UserId},
};
use serde::{Deserialize, Serialize};
use crate::store::StateStore;
use super::BaseRoomInfo;
/// The underlying room data structure collecting state for invited rooms.
#[derive(Debug, Clone)]
pub struct StrippedRoom {
room_id: Arc<RoomId>,
own_user_id: Arc<UserId>,
inner: Arc<SyncMutex<StrippedRoomInfo>>,
store: Arc<Box<dyn StateStore>>,
}
impl StrippedRoom {
pub(crate) fn new(
own_user_id: &UserId,
store: Arc<Box<dyn StateStore>>,
room_id: &RoomId,
) -> Self {
let room_id = Arc::new(room_id.clone());
let info = StrippedRoomInfo {
room_id,
base_info: BaseRoomInfo::new(),
};
Self::restore(own_user_id, store, info)
}
pub(crate) fn restore(
own_user_id: &UserId,
store: Arc<Box<dyn StateStore>>,
room_info: StrippedRoomInfo,
) -> Self {
Self {
own_user_id: Arc::new(own_user_id.clone()),
room_id: room_info.room_id.clone(),
store,
inner: Arc::new(SyncMutex::new(room_info)),
}
}
async fn calculate_name(&self) -> String {
let inner = self.inner.lock().unwrap();
if let Some(name) = &inner.base_info.name {
let name = name.trim();
name.to_string()
} else if let Some(alias) = &inner.base_info.canonical_alias {
let alias = alias.alias().trim();
alias.to_string()
} else {
// TODO do the dance with room members to calculate the name
self.room_id.to_string()
}
}
/// Get the unique room id of the room.
pub fn room_id(&self) -> &RoomId {
&self.room_id
}
/// Get our own user id.
pub fn own_user_id(&self) -> &UserId {
&self.own_user_id
}
pub(crate) fn clone_info(&self) -> StrippedRoomInfo {
(*self.inner.lock().unwrap()).clone()
}
/// Is the room encrypted.
pub fn is_encrypted(&self) -> bool {
self.inner.lock().unwrap().base_info.encryption.is_some()
}
/// Get the `m.room.encryption` content that enabled end to end encryption
/// in the room.
pub fn encryption_settings(&self) -> Option<EncryptionEventContent> {
self.inner.lock().unwrap().base_info.encryption.clone()
}
/// Get the history visibility policy of this room.
pub fn history_visibility(&self) -> HistoryVisibility {
self.inner
.lock()
.unwrap()
.base_info
.history_visibility
.clone()
}
/// Calculate the canonical display name of the room, taking into account
/// its name, aliases and members.
///
/// The display name is calculated according to [this algorithm][spec].
///
/// [spec]: <https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room>
pub async fn display_name(&self) -> String {
self.calculate_name().await
}
}
/// The underlying pure data structure for invited rooms.
///
/// Holds all the info needed to persist a room into the state store.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StrippedRoomInfo {
/// The unique room id of the room.
pub room_id: Arc<RoomId>,
/// Base room info which holds some basic event contents important for the
/// room state.
pub base_info: BaseRoomInfo,
}
impl StrippedRoomInfo {
pub(crate) fn handle_state_event(&mut self, event: &AnyStrippedStateEvent) -> bool {
self.base_info.handle_state_event(&event.content())
}
}

View File

@ -33,7 +33,7 @@ use tracing::info;
use crate::deserialized_responses::{MemberEvent, StrippedMemberEvent}; use crate::deserialized_responses::{MemberEvent, StrippedMemberEvent};
use super::{Result, RoomInfo, StateChanges, StateStore, StrippedRoomInfo}; use super::{Result, RoomInfo, StateChanges, StateStore};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MemoryStore { pub struct MemoryStore {
@ -49,7 +49,7 @@ pub struct MemoryStore {
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
room_state: Arc<DashMap<RoomId, DashMap<String, DashMap<String, AnySyncStateEvent>>>>, room_state: Arc<DashMap<RoomId, DashMap<String, DashMap<String, AnySyncStateEvent>>>>,
room_account_data: Arc<DashMap<RoomId, DashMap<String, AnyBasicEvent>>>, room_account_data: Arc<DashMap<RoomId, DashMap<String, AnyBasicEvent>>>,
stripped_room_info: Arc<DashMap<RoomId, StrippedRoomInfo>>, stripped_room_info: Arc<DashMap<RoomId, RoomInfo>>,
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
stripped_room_state: stripped_room_state:
Arc<DashMap<RoomId, DashMap<String, DashMap<String, AnyStrippedStateEvent>>>>, Arc<DashMap<RoomId, DashMap<String, DashMap<String, AnyStrippedStateEvent>>>>,
@ -291,7 +291,7 @@ impl MemoryStore {
self.room_info.iter().map(|r| r.clone()).collect() self.room_info.iter().map(|r| r.clone()).collect()
} }
fn get_stripped_room_infos(&self) -> Vec<StrippedRoomInfo> { fn get_stripped_room_infos(&self) -> Vec<RoomInfo> {
#[allow(clippy::map_clone)] #[allow(clippy::map_clone)]
self.stripped_room_info.iter().map(|r| r.clone()).collect() self.stripped_room_info.iter().map(|r| r.clone()).collect()
} }
@ -357,7 +357,7 @@ impl StateStore for MemoryStore {
Ok(self.get_room_infos()) Ok(self.get_room_infos())
} }
async fn get_stripped_room_infos(&self) -> Result<Vec<StrippedRoomInfo>> { async fn get_stripped_room_infos(&self) -> Result<Vec<RoomInfo>> {
Ok(self.get_stripped_room_infos()) Ok(self.get_stripped_room_infos())
} }

View File

@ -35,8 +35,8 @@ use sled::Db;
use crate::{ use crate::{
deserialized_responses::{MemberEvent, StrippedMemberEvent}, deserialized_responses::{MemberEvent, StrippedMemberEvent},
rooms::{RoomInfo, RoomType, StrippedRoom, StrippedRoomInfo}, rooms::{RoomInfo, RoomType},
InvitedRoom, JoinedRoom, LeftRoom, Room, RoomState, Session, Room, Session,
}; };
pub(crate) mod ambiguity_map; pub(crate) mod ambiguity_map;
@ -164,8 +164,8 @@ pub trait StateStore: AsyncTraitDeps {
/// Get all the pure `RoomInfo`s the store knows about. /// Get all the pure `RoomInfo`s the store knows about.
async fn get_room_infos(&self) -> Result<Vec<RoomInfo>>; async fn get_room_infos(&self) -> Result<Vec<RoomInfo>>;
/// Get all the pure `StrippedRoomInfo`s the store knows about. /// Get all the pure `RoomInfo`s the store knows about.
async fn get_stripped_room_infos(&self) -> Result<Vec<StrippedRoomInfo>>; async fn get_stripped_room_infos(&self) -> Result<Vec<RoomInfo>>;
/// Get all the users that use the given display name in the given room. /// Get all the users that use the given display name in the given room.
/// ///
@ -192,7 +192,7 @@ pub struct Store {
pub(crate) session: Arc<RwLock<Option<Session>>>, pub(crate) session: Arc<RwLock<Option<Session>>>,
pub(crate) sync_token: Arc<RwLock<Option<String>>>, pub(crate) sync_token: Arc<RwLock<Option<String>>>,
rooms: Arc<DashMap<RoomId, Room>>, rooms: Arc<DashMap<RoomId, Room>>,
stripped_rooms: Arc<DashMap<RoomId, StrippedRoom>>, stripped_rooms: Arc<DashMap<RoomId, Room>>,
} }
impl Store { impl Store {
@ -216,7 +216,7 @@ impl Store {
} }
for info in self.inner.get_stripped_room_infos().await? { for info in self.inner.get_stripped_room_infos().await? {
let room = StrippedRoom::restore(&session.user_id, self.inner.clone(), info); let room = Room::restore(&session.user_id, self.inner.clone(), info);
self.stripped_rooms.insert(room.room_id().to_owned(), room); self.stripped_rooms.insert(room.room_id().to_owned(), room);
} }
@ -267,60 +267,29 @@ impl Store {
} }
/// Get all the rooms this store knows about. /// Get all the rooms this store knows about.
pub fn get_rooms(&self) -> Vec<RoomState> { pub fn get_rooms(&self) -> Vec<Room> {
self.rooms self.rooms
.iter() .iter()
.filter_map(|r| self.get_room(r.key())) .filter_map(|r| self.get_room(r.key()))
.collect() .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. /// Get the room with the given room id.
/// pub fn get_room(&self, room_id: &RoomId) -> Option<Room> {
/// *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> {
self.get_bare_room(room_id) self.get_bare_room(room_id)
.and_then(|r| match r.room_type() { .and_then(|r| match r.room_type() {
RoomType::Joined => Some(RoomState::Joined(JoinedRoom { inner: r })), RoomType::Joined => Some(r),
RoomType::Left => Some(RoomState::Left(LeftRoom { inner: r })), RoomType::Left => Some(r),
RoomType::Invited => self RoomType::Invited => self.get_stripped_room(room_id),
.get_stripped_room(room_id)
.map(|r| RoomState::Invited(InvitedRoom { inner: r })),
}) })
} }
fn get_stripped_room(&self, room_id: &RoomId) -> Option<StrippedRoom> { fn get_stripped_room(&self, room_id: &RoomId) -> Option<Room> {
#[allow(clippy::map_clone)] #[allow(clippy::map_clone)]
self.stripped_rooms.get(room_id).map(|r| r.clone()) self.stripped_rooms.get(room_id).map(|r| r.clone())
} }
pub(crate) async fn get_or_create_stripped_room(&self, room_id: &RoomId) -> StrippedRoom { pub(crate) async fn get_or_create_stripped_room(&self, room_id: &RoomId) -> Room {
let session = self.session.read().await; let session = self.session.read().await;
let user_id = &session let user_id = &session
.as_ref() .as_ref()
@ -329,7 +298,7 @@ impl Store {
self.stripped_rooms self.stripped_rooms
.entry(room_id.clone()) .entry(room_id.clone())
.or_insert_with(|| StrippedRoom::new(user_id, self.inner.clone(), room_id)) .or_insert_with(|| Room::new(user_id, self.inner.clone(), room_id, RoomType::Invited))
.clone() .clone()
} }
@ -384,8 +353,8 @@ pub struct StateChanges {
pub stripped_state: BTreeMap<RoomId, BTreeMap<String, BTreeMap<String, AnyStrippedStateEvent>>>, pub stripped_state: BTreeMap<RoomId, BTreeMap<String, BTreeMap<String, AnyStrippedStateEvent>>>,
/// A mapping of `RoomId` to a map of users and their `StrippedMemberEvent`. /// A mapping of `RoomId` to a map of users and their `StrippedMemberEvent`.
pub stripped_members: BTreeMap<RoomId, BTreeMap<UserId, StrippedMemberEvent>>, pub stripped_members: BTreeMap<RoomId, BTreeMap<UserId, StrippedMemberEvent>>,
/// A map of `RoomId` to `StrippedRoomInfo`. /// A map of `RoomId` to `RoomInfo`.
pub invited_room_info: BTreeMap<RoomId, StrippedRoomInfo>, pub invited_room_info: BTreeMap<RoomId, RoomInfo>,
} }
impl StateChanges { impl StateChanges {
@ -408,8 +377,8 @@ impl StateChanges {
.insert(room.room_id.as_ref().to_owned(), room); .insert(room.room_id.as_ref().to_owned(), room);
} }
/// Update the `StateChanges` struct with the given `StrippedRoomInfo`. /// Update the `StateChanges` struct with the given `RoomInfo`.
pub fn add_stripped_room(&mut self, room: StrippedRoomInfo) { pub fn add_stripped_room(&mut self, room: RoomInfo) {
self.invited_room_info self.invited_room_info
.insert(room.room_id.as_ref().to_owned(), room); .insert(room.room_id.as_ref().to_owned(), room);
} }

View File

@ -43,7 +43,7 @@ use sled::{
}; };
use tracing::info; use tracing::info;
use crate::{deserialized_responses::MemberEvent, rooms::StrippedRoomInfo}; use crate::deserialized_responses::MemberEvent;
use self::store_key::{EncryptedEvent, StoreKey}; use self::store_key::{EncryptedEvent, StoreKey};
@ -560,7 +560,7 @@ impl SledStore {
) )
} }
pub async fn get_stripped_room_infos(&self) -> impl Stream<Item = Result<StrippedRoomInfo>> { pub async fn get_stripped_room_infos(&self) -> impl Stream<Item = Result<RoomInfo>> {
let db = self.clone(); let db = self.clone();
stream::iter( stream::iter(
self.stripped_room_info self.stripped_room_info
@ -644,7 +644,7 @@ impl StateStore for SledStore {
self.get_room_infos().await.try_collect().await self.get_room_infos().await.try_collect().await
} }
async fn get_stripped_room_infos(&self) -> Result<Vec<StrippedRoomInfo>> { async fn get_stripped_room_infos(&self) -> Result<Vec<RoomInfo>> {
self.get_stripped_room_infos().await.try_collect().await self.get_stripped_room_infos().await.try_collect().await
} }