2019-10-23 20:47:00 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use crate::api::r0 as api;
|
2019-10-23 21:36:57 +00:00
|
|
|
use crate::events::collections::all::{RoomEvent, StateEvent};
|
2019-10-23 20:47:00 +00:00
|
|
|
use crate::events::room::member::{MemberEvent, MembershipState};
|
|
|
|
use crate::session::Session;
|
2019-10-31 08:17:13 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-10-23 20:47:00 +00:00
|
|
|
|
|
|
|
pub type Token = String;
|
|
|
|
pub type RoomId = String;
|
|
|
|
pub type UserId = String;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// A Matrix room member.
|
|
|
|
pub struct RoomMember {
|
|
|
|
/// The unique mxid of the user.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub user_id: UserId,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// The human readable name of the user.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub display_name: Option<String>,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// The matrix url of the users avatar.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub avatar_url: Option<String>,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// The users power level.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub power_level: u8,
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// A Matrix rooom.
|
|
|
|
pub struct Room {
|
|
|
|
/// The unique id of the room.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub room_id: RoomId,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// The mxid of our own user.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub own_user_id: UserId,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// The mxid of the room creator.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub creator: Option<UserId>,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// The map of room members.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub members: HashMap<UserId, RoomMember>,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// A list of users that are currently typing.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub typing_users: Vec<UserId>,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// A flag indicating if the room is encrypted.
|
2019-10-30 22:26:26 +00:00
|
|
|
pub encrypted: bool,
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Room {
|
|
|
|
/// Create a new room.
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The unique id of the room.
|
|
|
|
/// * `own_user_id` - The mxid of our own user.
|
|
|
|
pub fn new(room_id: &str, own_user_id: &UserId) -> Self {
|
|
|
|
Room {
|
|
|
|
room_id: room_id.to_string(),
|
|
|
|
own_user_id: own_user_id.clone(),
|
|
|
|
creator: None,
|
|
|
|
members: HashMap::new(),
|
|
|
|
typing_users: Vec::new(),
|
|
|
|
encrypted: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_member(&mut self, event: &MemberEvent) -> bool {
|
|
|
|
if self.members.contains_key(&event.state_key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let member = RoomMember {
|
|
|
|
user_id: event.state_key.clone(),
|
|
|
|
display_name: event.content.displayname.clone(),
|
|
|
|
avatar_url: event.content.avatar_url.clone(),
|
|
|
|
power_level: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.members.insert(event.state_key.clone(), member);
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_joined_member(&mut self, event: &MemberEvent) -> bool {
|
|
|
|
if let Some(member) = self.members.get_mut(&event.state_key) {
|
|
|
|
member.display_name = event.content.displayname.clone();
|
|
|
|
member.avatar_url = event.content.avatar_url.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_join(&mut self, event: &MemberEvent) -> bool {
|
|
|
|
match &event.prev_content {
|
|
|
|
Some(c) => match c.membership {
|
|
|
|
MembershipState::Join => self.update_joined_member(event),
|
|
|
|
MembershipState::Invite => self.add_member(event),
|
|
|
|
MembershipState::Leave => self.add_member(event),
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
None => self.add_member(event),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_leave(&mut self, event: &MemberEvent) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_membership(&mut self, event: &MemberEvent) -> bool {
|
|
|
|
match event.content.membership {
|
|
|
|
MembershipState::Join => self.handle_join(event),
|
|
|
|
MembershipState::Leave => self.handle_leave(event),
|
|
|
|
MembershipState::Ban => self.handle_leave(event),
|
|
|
|
MembershipState::Invite => false,
|
|
|
|
MembershipState::Knock => false,
|
2019-10-30 18:30:55 +00:00
|
|
|
_ => false,
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 21:36:57 +00:00
|
|
|
/// Receive a timeline event for this room and update the room state.
|
2019-10-23 20:47:00 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `event` - The event of the room.
|
|
|
|
///
|
|
|
|
/// Returns true if the joined member list changed, false otherwise.
|
2019-10-23 21:36:57 +00:00
|
|
|
pub fn receive_timeline_event(&mut self, event: &RoomEvent) -> bool {
|
2019-10-23 20:47:00 +00:00
|
|
|
match event {
|
|
|
|
RoomEvent::RoomMember(m) => self.handle_membership(m),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 21:36:57 +00:00
|
|
|
|
|
|
|
/// Receive a state event for this room and update the room state.
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `event` - The event of the room.
|
|
|
|
///
|
|
|
|
/// Returns true if the joined member list changed, false otherwise.
|
|
|
|
pub fn receive_state_event(&mut self, event: &StateEvent) -> bool {
|
|
|
|
match event {
|
|
|
|
StateEvent::RoomMember(m) => self.handle_membership(m),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// A no IO Client implementation.
|
|
|
|
///
|
|
|
|
/// This Client is a state machine that receives responses and events and
|
|
|
|
/// accordingly updates it's state.
|
|
|
|
pub struct Client {
|
|
|
|
/// The current client session containing our user id, device id and access
|
|
|
|
/// token.
|
|
|
|
pub session: Option<Session>,
|
|
|
|
/// The current sync token that should be used for the next sync call.
|
|
|
|
pub sync_token: Option<Token>,
|
|
|
|
/// A map of the rooms our user is joined in.
|
2019-10-31 08:17:13 +00:00
|
|
|
pub joined_rooms: HashMap<RoomId, Arc<Mutex<Room>>>,
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
/// Create a new client.
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `session` - An optional session if the user already has one from a
|
|
|
|
/// previous login call.
|
|
|
|
pub fn new(session: Option<Session>) -> Self {
|
|
|
|
Client {
|
|
|
|
session,
|
|
|
|
sync_token: None,
|
|
|
|
joined_rooms: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is the client logged in.
|
|
|
|
pub fn logged_in(&self) -> bool {
|
|
|
|
self.session.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a login response and update the session of the client.
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `response` - A successful login response that contains our access token
|
|
|
|
/// and device id.
|
|
|
|
pub fn receive_login_response(&mut self, response: &api::session::login::Response) {
|
|
|
|
let session = Session {
|
|
|
|
access_token: response.access_token.clone(),
|
|
|
|
device_id: response.device_id.clone(),
|
|
|
|
user_id: response.user_id.clone(),
|
|
|
|
};
|
|
|
|
self.session = Some(session);
|
|
|
|
}
|
|
|
|
|
2019-10-31 08:17:13 +00:00
|
|
|
fn get_or_create_room(&mut self, room_id: &RoomId) -> &mut Arc<Mutex<Room>> {
|
2019-10-23 21:36:57 +00:00
|
|
|
self.joined_rooms
|
2019-10-23 20:47:00 +00:00
|
|
|
.entry(room_id.to_string())
|
2019-10-30 22:26:26 +00:00
|
|
|
.or_insert(
|
2019-10-31 08:17:13 +00:00
|
|
|
Arc::new(Mutex::new(Room::new(
|
2019-10-30 22:26:26 +00:00
|
|
|
room_id,
|
|
|
|
&self
|
|
|
|
.session
|
|
|
|
.as_ref()
|
|
|
|
.expect("Receiving events while not being logged in")
|
|
|
|
.user_id
|
|
|
|
.to_string(),
|
|
|
|
))))
|
2019-10-23 21:36:57 +00:00
|
|
|
}
|
2019-10-23 20:47:00 +00:00
|
|
|
|
2019-10-23 21:36:57 +00:00
|
|
|
/// Receive a timeline event for a joined room and update the client state.
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `room_id` - The unique id of the room the event belongs to.
|
|
|
|
/// `event` - The event that should be handled by the client.
|
|
|
|
///
|
|
|
|
/// Returns true if the membership list of the room changed, false
|
|
|
|
/// otherwise.
|
|
|
|
pub fn receive_joined_timeline_event(&mut self, room_id: &RoomId, event: &RoomEvent) -> bool {
|
2019-10-31 08:17:13 +00:00
|
|
|
let mut room = self.get_or_create_room(room_id).lock().unwrap();
|
2019-10-23 21:36:57 +00:00
|
|
|
room.receive_timeline_event(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a state event for a joined room and update the client state.
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `room_id` - The unique id of the room the event belongs to.
|
|
|
|
/// `event` - The event that should be handled by the client.
|
|
|
|
///
|
|
|
|
/// Returns true if the membership list of the room changed, false
|
|
|
|
/// otherwise.
|
|
|
|
pub fn receive_joined_state_event(&mut self, room_id: &RoomId, event: &StateEvent) -> bool {
|
2019-10-31 08:17:13 +00:00
|
|
|
let mut room = self.get_or_create_room(room_id).lock().unwrap();
|
2019-10-23 21:36:57 +00:00
|
|
|
room.receive_state_event(event)
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
}
|