Merge branch 'add_room_enum'

This commit is contained in:
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::{
self, async_trait,
events::{room::member::MemberEventContent, StrippedStateEvent},
room, Client, ClientConfig, EventHandler, Room, SyncSettings,
room, BaseRoom, Client, ClientConfig, EventHandler, SyncSettings,
};
use url::Url;
@ -22,7 +22,7 @@ impl AutoJoinBot {
impl EventHandler for AutoJoinBot {
async fn on_stripped_state_member(
&self,
room: Room,
room: BaseRoom,
room_member: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>,
) {

View file

@ -7,7 +7,7 @@ use matrix_sdk::{
AnyMessageEventContent, SyncMessageEvent,
},
room::Joined,
Client, ClientConfig, EventHandler, Room, SyncSettings,
BaseRoom, Client, ClientConfig, EventHandler, SyncSettings,
};
use url::Url;
@ -25,7 +25,7 @@ impl CommandBot {
#[async_trait]
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) {
let msg_body = if let SyncMessageEvent {
content:

View file

@ -15,7 +15,7 @@ use matrix_sdk::{
SyncMessageEvent,
},
room::Joined,
Client, EventHandler, Room, SyncSettings,
BaseRoom, Client, EventHandler, SyncSettings,
};
use url::Url;
@ -33,7 +33,7 @@ impl ImageBot {
#[async_trait]
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) {
let msg_body = if let SyncMessageEvent {
content:

View file

@ -7,14 +7,14 @@ use matrix_sdk::{
room::message::{MessageEventContent, MessageType, TextMessageEventContent},
SyncMessageEvent,
},
Client, EventHandler, Room, RoomType, SyncSettings,
BaseRoom, Client, EventHandler, RoomType, SyncSettings,
};
struct EventCallback;
#[async_trait]
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 let SyncMessageEvent {
content:

View file

@ -547,11 +547,11 @@ 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<room::Common> {
pub fn rooms(&self) -> Vec<room::Room> {
self.store()
.get_rooms()
.into_iter()
.map(|room| room::Common::new(self.clone(), room))
.map(|room| room::Common::new(self.clone(), room).into())
.collect()
}
@ -587,10 +587,10 @@ impl Client {
/// # Arguments
///
/// `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()
.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.

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

View file

@ -6,17 +6,17 @@ use matrix_sdk_common::locks::Mutex;
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
#[derive(Debug, Clone)]
pub struct Common {
inner: Room,
inner: BaseRoom,
pub(crate) client: Client,
}
impl Deref for Common {
type Target = Room;
type Target = BaseRoom;
fn deref(&self) -> &Self::Target {
&self.inner
@ -30,7 +30,7 @@ impl Common {
/// * `client` - The client used to make requests.
///
/// * `room` - The underlaying room.
pub fn new(client: Client, room: Room) -> Self {
pub fn new(client: Client, room: BaseRoom) -> Self {
// TODO: Make this private
Self {
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;
/// A room in the invited state.
@ -7,7 +7,7 @@ use std::ops::Deref;
/// Operations may fail once the underlaying `Room` changes `RoomType`.
#[derive(Debug, Clone)]
pub struct Invited {
inner: Common,
pub(crate) inner: Common,
}
impl Invited {
@ -17,7 +17,7 @@ impl Invited {
/// * `client` - The client used to make requests.
///
/// * `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
if room.room_type() == RoomType::Invited {
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 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`.
#[derive(Debug, Clone)]
pub struct Joined {
inner: Common,
pub(crate) inner: Common,
}
impl Deref for Joined {
@ -63,13 +63,13 @@ impl Deref for 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
/// * `client` - The client used to make requests.
///
/// * `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
if room.room_type() == RoomType::Joined {
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 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`.
#[derive(Debug, Clone)]
pub struct Left {
inner: Common,
pub(crate) inner: Common,
}
impl Left {
@ -19,7 +19,7 @@ impl Left {
/// * `client` - The client used to make requests.
///
/// * `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
if room.room_type() == RoomType::Left {
Some(Self {

View file

@ -1,3 +1,7 @@
use std::ops::Deref;
use crate::RoomType;
mod common;
mod invited;
mod joined;
@ -7,3 +11,69 @@ pub use self::common::Common;
pub use self::invited::Invited;
pub use self::joined::Joined;
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 @@