Merge branch 'add_room_enum'

master
Damir Jelić 2021-03-17 15:08:02 +01:00
commit e9dff24ba7
12 changed files with 99 additions and 28 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},
room, Client, ClientConfig, EventHandler, Room, SyncSettings, room, BaseRoom, Client, ClientConfig, EventHandler, 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: Room, room: BaseRoom,
room_member: &StrippedStateEvent<MemberEventContent>, room_member: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>, _: Option<MemberEventContent>,
) { ) {

View File

@ -7,7 +7,7 @@ use matrix_sdk::{
AnyMessageEventContent, SyncMessageEvent, AnyMessageEventContent, SyncMessageEvent,
}, },
room::Joined, room::Joined,
Client, ClientConfig, EventHandler, Room, SyncSettings, BaseRoom, Client, ClientConfig, EventHandler, SyncSettings,
}; };
use url::Url; use url::Url;
@ -25,7 +25,7 @@ impl CommandBot {
#[async_trait] #[async_trait]
impl EventHandler for CommandBot { impl EventHandler for CommandBot {
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) { async fn on_room_message(&self, room: BaseRoom, event: &SyncMessageEvent<MessageEventContent>) {
if let Some(room) = Joined::new(self.client.clone(), room) { if let Some(room) = Joined::new(self.client.clone(), room) {
let msg_body = if let SyncMessageEvent { let msg_body = if let SyncMessageEvent {
content: content:

View File

@ -15,7 +15,7 @@ use matrix_sdk::{
SyncMessageEvent, SyncMessageEvent,
}, },
room::Joined, room::Joined,
Client, EventHandler, Room, SyncSettings, BaseRoom, Client, EventHandler, SyncSettings,
}; };
use url::Url; use url::Url;
@ -33,7 +33,7 @@ impl ImageBot {
#[async_trait] #[async_trait]
impl EventHandler for ImageBot { impl EventHandler for ImageBot {
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) { async fn on_room_message(&self, room: BaseRoom, event: &SyncMessageEvent<MessageEventContent>) {
if let Some(room) = Joined::new(self.client.clone(), room) { if let Some(room) = Joined::new(self.client.clone(), room) {
let msg_body = if let SyncMessageEvent { let msg_body = if let SyncMessageEvent {
content: content:

View File

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

View File

@ -547,11 +547,11 @@ 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<room::Common> { pub fn rooms(&self) -> Vec<room::Room> {
self.store() self.store()
.get_rooms() .get_rooms()
.into_iter() .into_iter()
.map(|room| room::Common::new(self.clone(), room)) .map(|room| room::Common::new(self.clone(), room).into())
.collect() .collect()
} }
@ -587,10 +587,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_room(&self, room_id: &RoomId) -> Option<room::Common> { pub fn get_room(&self, room_id: &RoomId) -> Option<room::Room> {
self.store() self.store()
.get_room(room_id) .get_room(room_id)
.map(|room| room::Common::new(self.clone(), room)) .map(|room| room::Common::new(self.clone(), room).into())
} }
/// Get a joined room with the given room id. /// Get a joined room with the given room id.

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, Room, RoomInfo, RoomMember, RoomType, Session, CustomEvent, Error as BaseError, EventHandler, Room as BaseRoom, RoomInfo, RoomMember,
StateChanges, StoreError, RoomType, Session, StateChanges, StoreError,
}; };
pub use matrix_sdk_common::*; pub use matrix_sdk_common::*;

View File

@ -6,17 +6,17 @@ use matrix_sdk_common::locks::Mutex;
use std::{ops::Deref, sync::Arc}; use std::{ops::Deref, sync::Arc};
use crate::{Client, Result, Room, RoomMember}; use crate::{BaseRoom, Client, Result, RoomMember};
/// A struct containing methodes that are common for Joined, Invited and Left Rooms /// A struct containing methodes that are common for Joined, Invited and Left Rooms
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Common { pub struct Common {
inner: Room, inner: BaseRoom,
pub(crate) client: Client, pub(crate) client: Client,
} }
impl Deref for Common { impl Deref for Common {
type Target = Room; type Target = BaseRoom;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.inner &self.inner
@ -30,7 +30,7 @@ impl Common {
/// * `client` - The client used to make requests. /// * `client` - The client used to make requests.
/// ///
/// * `room` - The underlaying room. /// * `room` - The underlaying room.
pub fn new(client: Client, room: Room) -> Self { pub fn new(client: Client, room: BaseRoom) -> Self {
// TODO: Make this private // TODO: Make this private
Self { Self {
inner: room, inner: room,

View File

@ -1,4 +1,4 @@
use crate::{room::Common, Client, Result, Room, RoomType}; use crate::{room::Common, BaseRoom, Client, Result, RoomType};
use std::ops::Deref; use std::ops::Deref;
/// A room in the invited state. /// A room in the invited state.
@ -7,7 +7,7 @@ use std::ops::Deref;
/// Operations may fail once the underlaying `Room` changes `RoomType`. /// Operations may fail once the underlaying `Room` changes `RoomType`.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Invited { pub struct Invited {
inner: Common, pub(crate) inner: Common,
} }
impl Invited { impl Invited {
@ -17,7 +17,7 @@ impl Invited {
/// * `client` - The client used to make requests. /// * `client` - The client used to make requests.
/// ///
/// * `room` - The underlaying room. /// * `room` - The underlaying room.
pub fn new(client: Client, room: Room) -> Option<Self> { pub fn new(client: Client, room: BaseRoom) -> Option<Self> {
// TODO: Make this private // TODO: Make this private
if room.room_type() == RoomType::Invited { if room.room_type() == RoomType::Invited {
Some(Self { Some(Self {

View File

@ -1,4 +1,4 @@
use crate::{room::Common, Client, Result, Room, RoomType}; use crate::{room::Common, BaseRoom, Client, Result, RoomType};
use std::{io::Read, ops::Deref, sync::Arc}; use std::{io::Read, ops::Deref, sync::Arc};
use matrix_sdk_common::{ use matrix_sdk_common::{
@ -51,7 +51,7 @@ const TYPING_NOTICE_RESEND_TIMEOUT: Duration = Duration::from_secs(3);
/// Operations may fail once the underlaying `Room` changes `RoomType`. /// Operations may fail once the underlaying `Room` changes `RoomType`.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Joined { pub struct Joined {
inner: Common, pub(crate) inner: Common,
} }
impl Deref for Joined { impl Deref for Joined {
@ -63,13 +63,13 @@ impl Deref for Joined {
} }
impl Joined { impl Joined {
/// Create a new `room::Joined` if the underlaying `Room` has type `RoomType::Joined`. /// Create a new `room::Joined` if the underlaying `BaseRoom` has type `RoomType::Joined`.
/// ///
/// # Arguments /// # Arguments
/// * `client` - The client used to make requests. /// * `client` - The client used to make requests.
/// ///
/// * `room` - The underlaying room. /// * `room` - The underlaying room.
pub fn new(client: Client, room: Room) -> Option<Self> { pub fn new(client: Client, room: BaseRoom) -> Option<Self> {
// TODO: Make this private // TODO: Make this private
if room.room_type() == RoomType::Joined { if room.room_type() == RoomType::Joined {
Some(Self { Some(Self {

View File

@ -1,4 +1,4 @@
use crate::{room::Common, Client, Result, Room, RoomType}; use crate::{room::Common, BaseRoom, Client, Result, RoomType};
use std::ops::Deref; use std::ops::Deref;
use matrix_sdk_common::api::r0::membership::forget_room; use matrix_sdk_common::api::r0::membership::forget_room;
@ -9,7 +9,7 @@ use matrix_sdk_common::api::r0::membership::forget_room;
/// Operations may fail once the underlaying `Room` changes `RoomType`. /// Operations may fail once the underlaying `Room` changes `RoomType`.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Left { pub struct Left {
inner: Common, pub(crate) inner: Common,
} }
impl Left { impl Left {
@ -19,7 +19,7 @@ impl Left {
/// * `client` - The client used to make requests. /// * `client` - The client used to make requests.
/// ///
/// * `room` - The underlaying room. /// * `room` - The underlaying room.
pub fn new(client: Client, room: Room) -> Option<Self> { pub fn new(client: Client, room: BaseRoom) -> Option<Self> {
// TODO: Make this private // TODO: Make this private
if room.room_type() == RoomType::Left { if room.room_type() == RoomType::Left {
Some(Self { Some(Self {

View File

@ -1,3 +1,7 @@
use std::ops::Deref;
use crate::RoomType;
mod common; mod common;
mod invited; mod invited;
mod joined; mod joined;
@ -7,3 +11,69 @@ pub use self::common::Common;
pub use self::invited::Invited; pub use self::invited::Invited;
pub use self::joined::Joined; pub use self::joined::Joined;
pub use self::left::Left; pub use self::left::Left;
/// An enum that abstracts over the different states a room can be in.
#[derive(Debug, Clone)]
pub enum Room {
/// The room in the `join` state.
Joined(Joined),
/// The room in the `left` state.
Left(Left),
/// The room in the `invited` state.
Invited(Invited),
}
impl Deref for Room {
type Target = Common;
fn deref(&self) -> &Self::Target {
match self {
Self::Joined(room) => &*room,
Self::Left(room) => &*room,
Self::Invited(room) => &*room,
}
}
}
impl From<Common> for Room {
fn from(room: Common) -> Self {
match room.room_type() {
RoomType::Joined => Self::Joined(Joined { inner: room }),
RoomType::Left => Self::Left(Left { inner: room }),
RoomType::Invited => Self::Invited(Invited { inner: room }),
}
}
}
impl From<Joined> for Room {
fn from(room: Joined) -> Self {
let room = (*room).clone();
match room.room_type() {
RoomType::Joined => Self::Joined(Joined { inner: room }),
RoomType::Left => Self::Left(Left { inner: room }),
RoomType::Invited => Self::Invited(Invited { inner: room }),
}
}
}
impl From<Left> for Room {
fn from(room: Left) -> Self {
let room = (*room).clone();
match room.room_type() {
RoomType::Joined => Self::Joined(Joined { inner: room }),
RoomType::Left => Self::Left(Left { inner: room }),
RoomType::Invited => Self::Invited(Invited { inner: room }),
}
}
}
impl From<Invited> for Room {
fn from(room: Invited) -> Self {
let room = (*room).clone();
match room.room_type() {
RoomType::Joined => Self::Joined(Joined { inner: room }),
RoomType::Left => Self::Left(Left { inner: room }),
RoomType::Invited => Self::Invited(Invited { inner: room }),
}
}
}

View File

@ -0,0 +1 @@