2020-02-21 15:54:05 +00:00
|
|
|
// Copyright 2020 Damir Jelić
|
|
|
|
// 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.
|
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
use std::collections::HashMap;
|
2020-03-18 13:17:22 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "encryption")]
|
2020-03-18 13:15:56 +00:00
|
|
|
use std::result::Result as StdResult;
|
2019-10-23 20:47:00 +00:00
|
|
|
|
|
|
|
use crate::api::r0 as api;
|
2020-03-18 13:15:56 +00:00
|
|
|
use crate::error::Result;
|
2019-10-23 21:36:57 +00:00
|
|
|
use crate::events::collections::all::{RoomEvent, StateEvent};
|
2020-03-27 20:14:16 +00:00
|
|
|
use crate::events::room::{
|
|
|
|
aliases::AliasesEvent,
|
|
|
|
canonical_alias::CanonicalAliasEvent,
|
|
|
|
member::{MemberEvent, MembershipState},
|
|
|
|
name::{NameEvent},
|
|
|
|
};
|
2019-11-26 19:47:02 +00:00
|
|
|
use crate::events::EventResult;
|
2020-03-27 20:14:16 +00:00
|
|
|
use crate::identifiers::{RoomAliasId};
|
2019-10-23 20:47:00 +00:00
|
|
|
use crate::session::Session;
|
2019-11-10 10:44:03 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2019-10-23 20:47:00 +00:00
|
|
|
|
2020-03-11 09:08:22 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
use crate::crypto::{OlmMachine, OneTimeKeys};
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
use ruma_client_api::r0::keys::{upload_keys::Response as KeysUploadResponse, DeviceKeys};
|
2020-03-25 14:03:10 +00:00
|
|
|
use ruma_identifiers::RoomId;
|
2020-03-11 09:08:22 +00:00
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
pub type Token = String;
|
|
|
|
pub type UserId = String;
|
|
|
|
|
2020-03-27 20:14:16 +00:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
/// `RoomName` allows the calculation of a text room name.
|
|
|
|
pub struct RoomName {
|
|
|
|
/// The displayed name of the room.
|
|
|
|
name: Option<String>,
|
|
|
|
/// The canonical alias of the room ex. `#room-name:example.com` and port number.
|
|
|
|
canonical_alias: Option<RoomAliasId>,
|
|
|
|
/// List of `RoomAliasId`s the room has been given.
|
|
|
|
aliases: Vec<RoomAliasId>,
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
#[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.
|
2020-03-27 20:14:16 +00:00
|
|
|
pub room_id: RoomId,
|
|
|
|
/// The name of the room, clients use this to represent a room.
|
|
|
|
pub room_name: RoomName,
|
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
|
|
|
}
|
|
|
|
|
2020-03-27 20:14:16 +00:00
|
|
|
impl RoomName {
|
|
|
|
pub fn push_alias(&mut self, alias: RoomAliasId) -> bool {
|
|
|
|
self.aliases.push(alias);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_canonical(&mut self, alias: RoomAliasId) -> bool {
|
|
|
|
self.canonical_alias = Some(alias);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_name(&mut self, name: &str) -> bool {
|
|
|
|
self.name = Some(name.to_string());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn calculate_name(&self, room_id: &RoomId, members: &HashMap<UserId, RoomMember>) -> String {
|
|
|
|
// https://github.com/matrix-org/matrix-js-sdk/blob/33941eb37bffe41958ba9887fc8070dfb1a0ee76/src/models/room.js#L1823
|
|
|
|
// the order in which we check for a name ^^
|
|
|
|
if let Some(name) = &self.name {
|
|
|
|
name.clone()
|
|
|
|
} else if let Some(alias) = &self.canonical_alias {
|
|
|
|
alias.alias().to_string()
|
|
|
|
} else if !self.aliases.is_empty() {
|
|
|
|
self.aliases[0].alias().to_string()
|
|
|
|
} else {
|
|
|
|
// TODO
|
|
|
|
let mut names = members.values().flat_map(|m| m.display_name.clone()).take(3).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if names.is_empty() {
|
|
|
|
format!("Room {}", room_id)
|
|
|
|
} else {
|
|
|
|
// stablize order
|
|
|
|
names.sort();
|
|
|
|
names.join(", ").to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
impl Room {
|
|
|
|
/// Create a new room.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
2019-10-23 20:47:00 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The unique id of the room.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
2019-10-23 20:47:00 +00:00
|
|
|
/// * `own_user_id` - The mxid of our own user.
|
2020-02-21 13:29:46 +00:00
|
|
|
pub fn new(room_id: &str, own_user_id: &str) -> Self {
|
2019-10-23 20:47:00 +00:00
|
|
|
Room {
|
|
|
|
room_id: room_id.to_string(),
|
2020-03-27 20:14:16 +00:00
|
|
|
room_name: RoomName::default(),
|
2020-02-21 13:29:46 +00:00
|
|
|
own_user_id: own_user_id.to_owned(),
|
2019-10-23 20:47:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:18:31 +00:00
|
|
|
fn remove_member(&mut self, event: &MemberEvent) -> bool {
|
|
|
|
if !self.members.contains_key(&event.state_key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
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),
|
2019-11-14 19:18:31 +00:00
|
|
|
MembershipState::Leave => self.remove_member(event),
|
2019-10-23 20:47:00 +00:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
None => self.add_member(event),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 13:29:25 +00:00
|
|
|
fn handle_leave(&mut self, _event: &MemberEvent) -> bool {
|
2019-10-23 20:47:00 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-02-21 13:29:25 +00:00
|
|
|
/// Handle a room.member updating the room state if necessary.
|
|
|
|
/// Returns true if the joined member list changed, false otherwise.
|
2019-11-10 10:44:03 +00:00
|
|
|
pub fn handle_membership(&mut self, event: &MemberEvent) -> bool {
|
2019-10-23 20:47:00 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:14:16 +00:00
|
|
|
/// Add to the list of `RoomAliasId`s.
|
|
|
|
fn room_aliases(&mut self, alias: &RoomAliasId) -> bool {
|
|
|
|
self.room_name.push_alias(alias.clone());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RoomAliasId is `#alias:hostname` and `port`
|
|
|
|
fn canonical_alias(&mut self, alias: &RoomAliasId) -> bool {
|
|
|
|
self.room_name.set_canonical(alias.clone());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name_room(&mut self, name: &str) -> bool {
|
|
|
|
self.room_name.set_name(name);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a room.aliases event, updating the room state if necessary.
|
|
|
|
/// Returns true if the room name changed, false otherwise.
|
|
|
|
pub fn handle_room_aliases(&mut self, event: &AliasesEvent) -> bool {
|
|
|
|
match event.content.aliases.as_slice() {
|
|
|
|
[alias] => self.room_aliases(alias),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a room.canonical_alias event, updating the room state if necessary.
|
|
|
|
/// Returns true if the room name changed, false otherwise.
|
|
|
|
pub fn handle_canonical(&mut self, event: &CanonicalAliasEvent) -> bool {
|
|
|
|
match &event.content.alias {
|
|
|
|
Some(name) => self.canonical_alias(&name),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a room.name event, updating the room state if necessary.
|
|
|
|
/// Returns true if the room name changed, false otherwise.
|
|
|
|
pub fn handle_room_name(&mut self, event: &NameEvent) -> bool {
|
|
|
|
match event.content.name() {
|
|
|
|
Some(name) => self.name_room(name),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
///
|
|
|
|
/// Returns true if the joined member list changed, false otherwise.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The event of the room.
|
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),
|
2020-03-27 20:14:16 +00:00
|
|
|
RoomEvent::RoomName(n) => self.handle_room_name(n),
|
|
|
|
RoomEvent::RoomCanonicalAlias(ca) => self.handle_canonical(ca),
|
|
|
|
RoomEvent::RoomAliases(a) => self.handle_room_aliases(a),
|
2019-10-23 20:47:00 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 21:36:57 +00:00
|
|
|
|
|
|
|
/// Receive a state event for this room and update the room state.
|
|
|
|
///
|
|
|
|
/// Returns true if the joined member list changed, false otherwise.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The event of the room.
|
2019-10-23 21:36:57 +00:00
|
|
|
pub fn receive_state_event(&mut self, event: &StateEvent) -> bool {
|
|
|
|
match event {
|
|
|
|
StateEvent::RoomMember(m) => self.handle_membership(m),
|
2020-03-27 20:14:16 +00:00
|
|
|
StateEvent::RoomName(n) => self.handle_room_name(n),
|
|
|
|
StateEvent::RoomCanonicalAlias(ca) => self.handle_canonical(ca),
|
|
|
|
StateEvent::RoomAliases(a) => self.handle_room_aliases(a),
|
2019-10-23 21:36:57 +00:00
|
|
|
_ => 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.
|
2020-03-25 14:03:10 +00:00
|
|
|
pub joined_rooms: HashMap<String, Arc<RwLock<Room>>>,
|
2020-03-11 09:08:22 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
olm: Arc<Mutex<Option<OlmMachine>>>,
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
/// Create a new client.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
2019-10-23 20:47:00 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `session` - An optional session if the user already has one from a
|
2019-10-23 20:47:00 +00:00
|
|
|
/// previous login call.
|
2020-03-18 13:15:56 +00:00
|
|
|
pub fn new(session: Option<Session>) -> Result<Self> {
|
2020-03-12 15:28:02 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
let olm = match &session {
|
2020-03-18 13:15:56 +00:00
|
|
|
Some(s) => Some(OlmMachine::new(&s.user_id, &s.device_id)?),
|
2020-03-12 15:28:02 +00:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2020-03-18 13:15:56 +00:00
|
|
|
Ok(Client {
|
2019-10-23 20:47:00 +00:00
|
|
|
session,
|
|
|
|
sync_token: None,
|
|
|
|
joined_rooms: HashMap::new(),
|
2020-03-11 09:08:22 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
2020-03-12 15:28:02 +00:00
|
|
|
olm: Arc::new(Mutex::new(olm)),
|
2020-03-18 13:15:56 +00:00
|
|
|
})
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
2019-10-23 20:47:00 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `response` - A successful login response that contains our access token
|
2019-10-23 20:47:00 +00:00
|
|
|
/// and device id.
|
2020-03-18 13:15:56 +00:00
|
|
|
pub async fn receive_login_response(
|
|
|
|
&mut self,
|
|
|
|
response: &api::session::login::Response,
|
|
|
|
) -> Result<()> {
|
2019-10-23 20:47:00 +00:00
|
|
|
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);
|
2020-03-11 09:08:22 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
{
|
|
|
|
let mut olm = self.olm.lock().await;
|
2020-03-18 13:15:56 +00:00
|
|
|
*olm = Some(OlmMachine::new(&response.user_id, &response.device_id)?);
|
2020-03-11 09:08:22 +00:00
|
|
|
}
|
2020-03-18 13:15:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:47:56 +00:00
|
|
|
pub(crate) fn get_or_create_room(&mut self, room_id: &str) -> &mut Arc<RwLock<Room>> {
|
2020-02-21 13:29:46 +00:00
|
|
|
#[allow(clippy::or_fun_call)]
|
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-11-10 17:33:06 +00:00
|
|
|
.or_insert(Arc::new(RwLock::new(Room::new(
|
|
|
|
room_id,
|
|
|
|
&self
|
|
|
|
.session
|
|
|
|
.as_ref()
|
|
|
|
.expect("Receiving events while not being logged in")
|
|
|
|
.user_id
|
|
|
|
.to_string(),
|
2019-10-30 22:26:26 +00:00
|
|
|
))))
|
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.
|
2020-02-21 13:29:25 +00:00
|
|
|
///
|
2020-03-25 14:03:10 +00:00
|
|
|
/// If the event was a encrypted room event and decryption was successful
|
|
|
|
/// the decrypted event will be returned, otherwise None.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
2019-10-23 21:36:57 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `room_id` - The unique id of the room the event belongs to.
|
2019-10-23 21:36:57 +00:00
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `event` - The event that should be handled by the client.
|
2020-03-25 14:03:10 +00:00
|
|
|
pub async fn receive_joined_timeline_event(
|
2019-11-26 19:47:02 +00:00
|
|
|
&mut self,
|
2020-03-25 14:03:10 +00:00
|
|
|
room_id: &RoomId,
|
|
|
|
event: &mut EventResult<RoomEvent>,
|
|
|
|
) -> Option<EventResult<RoomEvent>> {
|
2019-11-26 19:47:02 +00:00
|
|
|
match event {
|
|
|
|
EventResult::Ok(e) => {
|
2020-03-25 14:03:10 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
let mut decrypted_event = None;
|
|
|
|
#[cfg(not(feature = "encryption"))]
|
|
|
|
let decrypted_event = None;
|
|
|
|
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
{
|
|
|
|
match e {
|
|
|
|
RoomEvent::RoomEncrypted(e) => {
|
|
|
|
e.room_id = Some(room_id.to_owned());
|
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
if let Some(o) = &mut *olm {
|
|
|
|
decrypted_event = o.decrypt_room_event(e).await.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut room = self
|
|
|
|
.get_or_create_room(&room_id.to_string())
|
|
|
|
.write()
|
|
|
|
.unwrap();
|
|
|
|
room.receive_timeline_event(e);
|
|
|
|
decrypted_event
|
2019-11-26 19:47:02 +00:00
|
|
|
}
|
2020-03-25 14:03:10 +00:00
|
|
|
_ => None,
|
2019-11-26 19:47:02 +00:00
|
|
|
}
|
2019-10-23 21:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a state event for a joined room and update the client state.
|
2020-02-21 13:29:25 +00:00
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// Returns true if the membership list of the room changed, false
|
|
|
|
/// otherwise.
|
|
|
|
///
|
2019-10-23 21:36:57 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `room_id` - The unique id of the room the event belongs to.
|
2019-10-23 21:36:57 +00:00
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `event` - The event that should be handled by the client.
|
2020-02-21 13:29:25 +00:00
|
|
|
pub fn receive_joined_state_event(&mut self, room_id: &str, event: &StateEvent) -> bool {
|
2019-11-10 10:44:03 +00:00
|
|
|
let mut room = self.get_or_create_room(room_id).write().unwrap();
|
2019-10-23 21:36:57 +00:00
|
|
|
room.receive_state_event(event)
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
2019-11-24 21:40:52 +00:00
|
|
|
|
2020-02-21 13:29:25 +00:00
|
|
|
/// Receive a response from a sync call.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-03-02 10:31:03 +00:00
|
|
|
/// * `response` - The response that we received after a successful sync.
|
2020-03-12 14:41:11 +00:00
|
|
|
pub async fn receive_sync_response(
|
|
|
|
&mut self,
|
|
|
|
response: &mut api::sync::sync_events::IncomingResponse,
|
|
|
|
) {
|
2019-11-24 21:40:52 +00:00
|
|
|
self.sync_token = Some(response.next_batch.clone());
|
2020-03-12 14:41:11 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
{
|
2020-03-12 15:14:43 +00:00
|
|
|
let mut olm = self.olm.lock().await;
|
2020-03-12 14:41:11 +00:00
|
|
|
|
2020-03-12 15:14:43 +00:00
|
|
|
if let Some(o) = &mut *olm {
|
2020-03-23 15:14:10 +00:00
|
|
|
o.receive_sync_response(response).await;
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 21:40:52 +00:00
|
|
|
}
|
2020-03-11 09:10:43 +00:00
|
|
|
|
|
|
|
/// Should account or one-time keys be uploaded to the server.
|
|
|
|
#[cfg(feature = "encryption")]
|
2020-03-19 12:55:04 +00:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
2020-03-11 09:10:43 +00:00
|
|
|
pub async fn should_upload_keys(&self) -> bool {
|
|
|
|
let olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
match &*olm {
|
2020-03-18 14:50:32 +00:00
|
|
|
Some(o) => o.should_upload_keys().await,
|
2020-03-11 09:10:43 +00:00
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a tuple of device and one-time keys that need to be uploaded.
|
|
|
|
///
|
|
|
|
/// Returns an empty error if no keys need to be uploaded.
|
|
|
|
#[cfg(feature = "encryption")]
|
2020-03-19 12:55:04 +00:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
2020-03-18 13:15:56 +00:00
|
|
|
pub async fn keys_for_upload(
|
|
|
|
&self,
|
|
|
|
) -> StdResult<(Option<DeviceKeys>, Option<OneTimeKeys>), ()> {
|
2020-03-11 09:10:43 +00:00
|
|
|
let olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
match &*olm {
|
2020-03-18 14:50:32 +00:00
|
|
|
Some(o) => o.keys_for_upload().await,
|
2020-03-11 09:10:43 +00:00
|
|
|
None => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a successful keys upload response.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `response` - The keys upload response of the request that the client
|
|
|
|
/// performed.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Panics if the client hasn't been logged in.
|
|
|
|
#[cfg(feature = "encryption")]
|
2020-03-19 12:55:04 +00:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
2020-03-18 14:50:32 +00:00
|
|
|
pub async fn receive_keys_upload_response(&self, response: &KeysUploadResponse) -> Result<()> {
|
2020-03-11 09:10:43 +00:00
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
let o = olm.as_mut().expect("Client isn't logged in.");
|
2020-03-18 14:50:32 +00:00
|
|
|
o.receive_keys_upload_response(response).await?;
|
|
|
|
Ok(())
|
2020-03-11 09:10:43 +00:00
|
|
|
}
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|