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.
|
|
|
|
|
2020-04-03 08:42:03 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
#[cfg(feature = "encryption")]
|
2020-04-23 08:52:47 +00:00
|
|
|
use std::collections::{BTreeMap, HashSet};
|
2020-03-31 23:34:11 +00:00
|
|
|
use std::fmt;
|
2020-05-25 12:21:04 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-04-01 02:00:46 +00:00
|
|
|
use std::sync::Arc;
|
2020-05-25 12:21:04 +00:00
|
|
|
use zeroize::Zeroizing;
|
2020-03-18 13:17:22 +00:00
|
|
|
|
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;
|
2020-03-28 12:58:02 +00:00
|
|
|
use crate::events::presence::PresenceEvent;
|
2020-03-29 19:54:26 +00:00
|
|
|
// `NonRoomEvent` is what it is aliased as
|
2020-06-01 21:02:12 +00:00
|
|
|
use crate::event_emitter::CustomOrRawEvent;
|
2020-03-29 19:54:26 +00:00
|
|
|
use crate::events::ignored_user_list::IgnoredUserListEvent;
|
2020-06-20 21:18:20 +00:00
|
|
|
use crate::events::push_rules::PushRulesEvent;
|
2020-05-21 14:16:04 +00:00
|
|
|
use crate::events::room::member::MemberEventContent;
|
2020-04-05 12:28:25 +00:00
|
|
|
use crate::identifiers::{RoomId, UserId};
|
2020-03-28 12:58:02 +00:00
|
|
|
use crate::models::Room;
|
2020-06-20 21:18:20 +00:00
|
|
|
use crate::push::Ruleset;
|
2019-10-23 20:47:00 +00:00
|
|
|
use crate::session::Session;
|
2020-05-11 19:32:58 +00:00
|
|
|
use crate::state::{AllRooms, ClientState, StateStore};
|
2020-03-31 23:34:11 +00:00
|
|
|
use crate::EventEmitter;
|
2020-06-20 21:18:20 +00:00
|
|
|
use matrix_sdk_common::events::{
|
2020-06-25 00:30:53 +00:00
|
|
|
AnyBasicEvent, AnyEphemeralRoomEventStub, AnyMessageEventStub, AnyRoomEventStub,
|
|
|
|
AnyStateEventStub, AnyStrippedStateEventStub, EventJson,
|
2020-06-20 21:18:20 +00:00
|
|
|
};
|
2019-10-23 20:47:00 +00:00
|
|
|
|
2020-04-14 22:10:10 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
2020-05-08 14:12:21 +00:00
|
|
|
use matrix_sdk_common::locks::Mutex;
|
|
|
|
use matrix_sdk_common::locks::RwLock;
|
|
|
|
use std::ops::Deref;
|
2020-03-11 09:08:22 +00:00
|
|
|
|
2020-04-09 14:27:09 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
2020-04-29 08:40:27 +00:00
|
|
|
use crate::api::r0::keys::{
|
2020-04-03 08:27:30 +00:00
|
|
|
claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse,
|
|
|
|
upload_keys::Response as KeysUploadResponse, DeviceKeys, KeyAlgorithm,
|
2020-04-01 13:37:00 +00:00
|
|
|
};
|
2020-04-03 08:27:30 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
2020-05-05 13:29:25 +00:00
|
|
|
use crate::api::r0::to_device::send_event_to_device;
|
|
|
|
#[cfg(feature = "encryption")]
|
2020-06-20 21:18:20 +00:00
|
|
|
use crate::events::room::{
|
|
|
|
encrypted::EncryptedEventContent, message::MessageEventContent as MsgEventContent,
|
|
|
|
};
|
2020-04-09 14:27:09 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
2020-04-29 08:40:27 +00:00
|
|
|
use crate::identifiers::DeviceId;
|
2020-05-25 12:21:04 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
use crate::JsonStore;
|
2020-04-29 08:40:27 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
2020-05-25 12:58:06 +00:00
|
|
|
use matrix_sdk_crypto::{CryptoStore, OlmError, OlmMachine, OneTimeKeys};
|
2020-03-11 09:08:22 +00:00
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
pub type Token = String;
|
|
|
|
|
2020-05-21 14:16:04 +00:00
|
|
|
/// A deserialization wrapper for extracting the prev_content field when
|
|
|
|
/// found in an `unsigned` field.
|
|
|
|
///
|
|
|
|
/// Represents the outer `unsigned` field
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
pub struct AdditionalEventData {
|
|
|
|
unsigned: AdditionalUnsignedData,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A deserialization wrapper for extracting the prev_content field when
|
|
|
|
/// found in an `unsigned` field.
|
|
|
|
///
|
|
|
|
/// Represents the inner `prev_content` field
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
pub struct AdditionalUnsignedData {
|
|
|
|
pub prev_content: Option<EventJson<MemberEventContent>>,
|
|
|
|
}
|
|
|
|
|
2020-06-15 15:04:10 +00:00
|
|
|
/// Transform room event by hoisting `prev_content` field from `unsigned` to the top level.
|
|
|
|
///
|
|
|
|
/// Due to a [bug in synapse][synapse-bug], `prev_content` often ends up in `unsigned` contrary to
|
|
|
|
/// the C2S spec. Some more discussion can be found [here][discussion]. Until this is fixed in
|
|
|
|
/// synapse or handled in Ruma, we use this to hoist up `prev_content` to the top level.
|
|
|
|
///
|
|
|
|
/// [synapse-bug]: <https://github.com/matrix-org/matrix-doc/issues/684#issuecomment-641182668>
|
|
|
|
/// [discussion]: <https://github.com/matrix-org/matrix-doc/issues/684#issuecomment-641182668>
|
2020-06-20 21:18:20 +00:00
|
|
|
fn hoist_room_event_prev_content(
|
|
|
|
event: &EventJson<AnyRoomEventStub>,
|
|
|
|
) -> Option<EventJson<AnyRoomEventStub>> {
|
2020-05-22 21:12:58 +00:00
|
|
|
let prev_content = serde_json::from_str::<AdditionalEventData>(event.json().get())
|
2020-05-21 14:16:04 +00:00
|
|
|
.map(|more_unsigned| more_unsigned.unsigned)
|
2020-05-22 21:12:58 +00:00
|
|
|
.map(|additional| additional.prev_content)
|
2020-05-21 14:16:04 +00:00
|
|
|
.ok()
|
2020-05-23 01:29:51 +00:00
|
|
|
.flatten()?;
|
2020-05-22 21:12:58 +00:00
|
|
|
|
2020-05-23 01:29:51 +00:00
|
|
|
let mut ev = event.deserialize().ok()?;
|
2020-06-20 21:18:20 +00:00
|
|
|
|
2020-05-23 01:29:51 +00:00
|
|
|
match &mut ev {
|
2020-06-25 00:30:53 +00:00
|
|
|
AnyRoomEventStub::State(AnyStateEventStub::RoomMember(ref mut member))
|
|
|
|
if member.prev_content.is_none() =>
|
|
|
|
{
|
2020-06-20 10:57:59 +00:00
|
|
|
if let Ok(prev) = prev_content.deserialize() {
|
2020-06-15 15:04:10 +00:00
|
|
|
member.prev_content = Some(prev)
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(EventJson::from(ev))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transform state event by hoisting `prev_content` field from `unsigned` to the top level.
|
|
|
|
///
|
|
|
|
/// See comment of `hoist_room_event_prev_content`.
|
2020-06-20 21:18:20 +00:00
|
|
|
fn hoist_state_event_prev_content(
|
|
|
|
event: &EventJson<AnyStateEventStub>,
|
|
|
|
) -> Option<EventJson<AnyStateEventStub>> {
|
2020-06-15 15:04:10 +00:00
|
|
|
let prev_content = serde_json::from_str::<AdditionalEventData>(event.json().get())
|
|
|
|
.map(|more_unsigned| more_unsigned.unsigned)
|
|
|
|
.map(|additional| additional.prev_content)
|
|
|
|
.ok()
|
|
|
|
.flatten()?;
|
|
|
|
|
|
|
|
let mut ev = event.deserialize().ok()?;
|
|
|
|
match &mut ev {
|
2020-06-25 00:30:53 +00:00
|
|
|
AnyStateEventStub::RoomMember(ref mut member) if member.prev_content.is_none() => {
|
2020-06-20 21:18:20 +00:00
|
|
|
member.prev_content = Some(prev_content.deserialize().ok()?);
|
2020-05-23 01:29:51 +00:00
|
|
|
Some(EventJson::from(ev))
|
2020-05-22 21:12:58 +00:00
|
|
|
}
|
2020-05-23 01:29:51 +00:00
|
|
|
_ => None,
|
2020-05-22 21:12:58 +00:00
|
|
|
}
|
2020-05-21 14:16:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 21:12:58 +00:00
|
|
|
fn stripped_deserialize_prev_content(
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &EventJson<AnyStrippedStateEventStub>,
|
2020-05-21 14:16:04 +00:00
|
|
|
) -> Option<AdditionalUnsignedData> {
|
|
|
|
serde_json::from_str::<AdditionalEventData>(event.json().get())
|
|
|
|
.map(|more_unsigned| more_unsigned.unsigned)
|
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:45:27 +00:00
|
|
|
/// Signals to the `BaseClient` which `RoomState` to send to `EventEmitter`.
|
2020-05-13 10:34:46 +00:00
|
|
|
#[derive(Debug)]
|
2020-05-06 23:45:27 +00:00
|
|
|
pub enum RoomStateType {
|
|
|
|
/// Represents a joined room, the `joined_rooms` HashMap will be used.
|
|
|
|
Joined,
|
|
|
|
/// Represents a left room, the `left_rooms` HashMap will be used.
|
|
|
|
Left,
|
|
|
|
/// Represents an invited room, the `invited_rooms` HashMap will be used.
|
|
|
|
Invited,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
2020-05-11 19:32:58 +00:00
|
|
|
/// 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>>`
|
2020-05-13 10:34:46 +00:00
|
|
|
#[derive(Debug)]
|
2020-05-11 19:32:58 +00:00
|
|
|
pub enum RoomState<R> {
|
2020-05-06 23:45:27 +00:00
|
|
|
/// A room from the `join` section of a sync response.
|
2020-05-11 19:32:58 +00:00
|
|
|
Joined(R),
|
2020-05-06 23:45:27 +00:00
|
|
|
/// A room from the `leave` section of a sync response.
|
2020-05-11 19:32:58 +00:00
|
|
|
Left(R),
|
2020-05-06 23:45:27 +00:00
|
|
|
/// A room from the `invite` section of a sync response.
|
2020-05-11 19:32:58 +00:00
|
|
|
Invited(R),
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
/// A no IO Client implementation.
|
|
|
|
///
|
|
|
|
/// This Client is a state machine that receives responses and events and
|
|
|
|
/// accordingly updates it's state.
|
2020-05-06 13:36:55 +00:00
|
|
|
#[derive(Clone)]
|
2020-05-11 08:43:21 +00:00
|
|
|
pub struct BaseClient {
|
2019-10-23 20:47:00 +00:00
|
|
|
/// The current client session containing our user id, device id and access
|
|
|
|
/// token.
|
2020-05-06 13:55:18 +00:00
|
|
|
session: Arc<RwLock<Option<Session>>>,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// The current sync token that should be used for the next sync call.
|
2020-05-06 13:55:18 +00:00
|
|
|
pub(crate) sync_token: Arc<RwLock<Option<Token>>>,
|
2019-10-23 20:47:00 +00:00
|
|
|
/// A map of the rooms our user is joined in.
|
2020-05-06 13:55:18 +00:00
|
|
|
joined_rooms: Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>>,
|
2020-05-06 23:45:27 +00:00
|
|
|
/// A map of the rooms our user is invited to.
|
2020-05-07 00:35:15 +00:00
|
|
|
invited_rooms: Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>>,
|
2020-05-06 23:45:27 +00:00
|
|
|
/// A map of the rooms our user has left.
|
2020-05-07 00:35:15 +00:00
|
|
|
left_rooms: Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>>,
|
2020-03-29 19:54:26 +00:00
|
|
|
/// A list of ignored users.
|
2020-05-06 13:55:18 +00:00
|
|
|
pub(crate) ignored_users: Arc<RwLock<Vec<UserId>>>,
|
2020-03-29 19:54:26 +00:00
|
|
|
/// The push ruleset for the logged in user.
|
2020-05-06 13:55:18 +00:00
|
|
|
pub(crate) push_ruleset: Arc<RwLock<Option<Ruleset>>>,
|
2020-03-31 23:34:11 +00:00
|
|
|
/// Any implementor of EventEmitter will act as the callbacks for various
|
|
|
|
/// events.
|
2020-05-06 13:55:18 +00:00
|
|
|
event_emitter: Arc<RwLock<Option<Box<dyn EventEmitter>>>>,
|
2020-04-29 11:00:14 +00:00
|
|
|
/// Any implementor of `StateStore` will be called to save `Room` and
|
2020-05-13 08:06:08 +00:00
|
|
|
/// some `BaseClient` state after receiving a sync response.
|
2020-04-18 22:06:30 +00:00
|
|
|
///
|
2020-04-29 11:00:14 +00:00
|
|
|
/// There is a default implementation `JsonStore` that saves JSON to disk.
|
2020-05-06 13:55:18 +00:00
|
|
|
state_store: Arc<RwLock<Option<Box<dyn StateStore>>>>,
|
2020-03-29 19:54:26 +00:00
|
|
|
|
2020-03-11 09:08:22 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
olm: Arc<Mutex<Option<OlmMachine>>>,
|
2020-05-25 12:21:04 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
cryptostore: Arc<Mutex<Option<Box<dyn CryptoStore>>>>,
|
|
|
|
store_path: Arc<Option<PathBuf>>,
|
|
|
|
store_passphrase: Arc<Zeroizing<String>>,
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 09:25:31 +00:00
|
|
|
// #[cfg_attr(tarpaulin, skip)]
|
2020-05-11 08:43:21 +00:00
|
|
|
impl fmt::Debug for BaseClient {
|
2020-03-31 23:34:11 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("Client")
|
|
|
|
.field("session", &self.session)
|
|
|
|
.field("sync_token", &self.sync_token)
|
|
|
|
.field("joined_rooms", &self.joined_rooms)
|
|
|
|
.field("ignored_users", &self.ignored_users)
|
|
|
|
.field("push_ruleset", &self.push_ruleset)
|
|
|
|
.field("event_emitter", &"EventEmitter<...>")
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 12:21:04 +00:00
|
|
|
/// Configuration for the creation of the `BaseClient`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use matrix_sdk_base::BaseClientConfig;
|
|
|
|
///
|
|
|
|
/// let client_config = BaseClientConfig::new()
|
|
|
|
/// .store_path("/home/example/matrix-sdk-client")
|
|
|
|
/// .passphrase("test-passphrase".to_owned());
|
|
|
|
/// ```
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct BaseClientConfig {
|
|
|
|
state_store: Option<Box<dyn StateStore>>,
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
crypto_store: Option<Box<dyn CryptoStore>>,
|
|
|
|
store_path: Option<PathBuf>,
|
|
|
|
passphrase: Option<Zeroizing<String>>,
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:25:31 +00:00
|
|
|
// #[cfg_attr(tarpaulin, skip)]
|
2020-05-25 12:21:04 +00:00
|
|
|
impl std::fmt::Debug for BaseClientConfig {
|
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> StdResult<(), std::fmt::Error> {
|
|
|
|
fmt.debug_struct("BaseClientConfig").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BaseClientConfig {
|
|
|
|
/// Create a new default `BaseClientConfig`.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a custom implementation of a `StateStore`.
|
|
|
|
///
|
|
|
|
/// The state store should be opened before being set.
|
|
|
|
pub fn state_store(mut self, store: Box<dyn StateStore>) -> Self {
|
|
|
|
self.state_store = Some(store);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a custom implementation of a `CryptoStore`.
|
|
|
|
///
|
|
|
|
/// The crypto store should be opened before being set.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
pub fn crypto_store(mut self, store: Box<dyn CryptoStore>) -> Self {
|
|
|
|
self.crypto_store = Some(store);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the path for storage.
|
2020-03-02 10:31:03 +00:00
|
|
|
///
|
2019-10-23 20:47:00 +00:00
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-05-25 12:21:04 +00:00
|
|
|
/// * `path` - The path where the stores should save data in. It is the
|
|
|
|
/// callers responsibility to make sure that the path exists.
|
|
|
|
///
|
|
|
|
/// In the default configuration the client will open default
|
|
|
|
/// implementations for the crypto store and the state store. It will use
|
|
|
|
/// the given path to open the stores. If no path is provided no store will
|
|
|
|
/// be opened
|
|
|
|
pub fn store_path<P: AsRef<Path>>(mut self, path: P) -> Self {
|
|
|
|
self.store_path = Some(path.as_ref().into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the passphrase to encrypt the crypto store.
|
|
|
|
///
|
|
|
|
/// # Argument
|
|
|
|
///
|
|
|
|
/// * `passphrase` - The passphrase that will be used to encrypt the data in
|
|
|
|
/// the cryptostore.
|
|
|
|
///
|
|
|
|
/// This is only used if no custom cryptostore is set.
|
|
|
|
pub fn passphrase(mut self, passphrase: String) -> Self {
|
|
|
|
self.passphrase = Some(Zeroizing::new(passphrase));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BaseClient {
|
|
|
|
/// Create a new default client.
|
2020-05-22 13:23:58 +00:00
|
|
|
pub fn new() -> Result<Self> {
|
2020-05-25 12:21:04 +00:00
|
|
|
BaseClient::new_with_config(BaseClientConfig::default())
|
2020-05-06 13:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new client.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-05-25 12:21:04 +00:00
|
|
|
/// * `config` - An optional session if the user already has one from a
|
2020-05-06 13:20:20 +00:00
|
|
|
/// previous login call.
|
2020-05-25 12:21:04 +00:00
|
|
|
pub fn new_with_config(config: BaseClientConfig) -> Result<Self> {
|
2020-05-11 08:43:21 +00:00
|
|
|
Ok(BaseClient {
|
2020-05-22 13:23:58 +00:00
|
|
|
session: Arc::new(RwLock::new(None)),
|
2020-05-06 11:57:58 +00:00
|
|
|
sync_token: Arc::new(RwLock::new(None)),
|
|
|
|
joined_rooms: Arc::new(RwLock::new(HashMap::new())),
|
2020-05-07 00:35:15 +00:00
|
|
|
invited_rooms: Arc::new(RwLock::new(HashMap::new())),
|
|
|
|
left_rooms: Arc::new(RwLock::new(HashMap::new())),
|
2020-05-06 11:57:58 +00:00
|
|
|
ignored_users: Arc::new(RwLock::new(Vec::new())),
|
|
|
|
push_ruleset: Arc::new(RwLock::new(None)),
|
2020-05-06 12:47:58 +00:00
|
|
|
event_emitter: Arc::new(RwLock::new(None)),
|
2020-05-25 12:21:04 +00:00
|
|
|
state_store: Arc::new(RwLock::new(config.state_store)),
|
2020-03-11 09:08:22 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
2020-05-22 13:23:58 +00:00
|
|
|
olm: Arc::new(Mutex::new(None)),
|
2020-05-25 12:21:04 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
cryptostore: Arc::new(Mutex::new(config.crypto_store)),
|
|
|
|
store_path: Arc::new(config.store_path),
|
|
|
|
store_passphrase: Arc::new(
|
|
|
|
config
|
|
|
|
.passphrase
|
|
|
|
.unwrap_or_else(|| Zeroizing::new("DEFAULT_PASSPHRASE".to_owned())),
|
|
|
|
),
|
2020-03-18 13:15:56 +00:00
|
|
|
})
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 13:55:18 +00:00
|
|
|
/// The current client session containing our user id, device id and access
|
|
|
|
/// token.
|
|
|
|
pub fn session(&self) -> &Arc<RwLock<Option<Session>>> {
|
|
|
|
&self.session
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
/// Is the client logged in.
|
2020-05-06 12:27:53 +00:00
|
|
|
pub async fn logged_in(&self) -> bool {
|
|
|
|
// TODO turn this into a atomic bool so this method doesn't need to be
|
|
|
|
// async.
|
|
|
|
self.session.read().await.is_some()
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 19:59:13 +00:00
|
|
|
/// Add `EventEmitter` to `Client`.
|
|
|
|
///
|
|
|
|
/// The methods of `EventEmitter` are called when the respective `RoomEvents` occur.
|
2020-05-06 12:47:58 +00:00
|
|
|
pub async fn add_event_emitter(&self, emitter: Box<dyn EventEmitter>) {
|
|
|
|
*self.event_emitter.write().await = Some(emitter);
|
2020-04-02 19:59:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 23:37:27 +00:00
|
|
|
/// When a client is provided the state store will load state from the `StateStore`.
|
|
|
|
///
|
2020-04-29 19:42:20 +00:00
|
|
|
/// Returns `true` when a state store sync has successfully completed.
|
2020-05-25 12:21:04 +00:00
|
|
|
async fn sync_with_state_store(&self, session: &Session) -> Result<bool> {
|
2020-05-06 13:20:20 +00:00
|
|
|
let store = self.state_store.read().await;
|
2020-04-23 23:37:27 +00:00
|
|
|
|
2020-05-25 12:21:04 +00:00
|
|
|
let loaded = if let Some(store) = store.as_ref() {
|
|
|
|
if let Some(client_state) = store.load_client_state(session).await? {
|
|
|
|
let ClientState {
|
|
|
|
sync_token,
|
|
|
|
ignored_users,
|
|
|
|
push_ruleset,
|
|
|
|
} = client_state;
|
|
|
|
*self.sync_token.write().await = sync_token;
|
|
|
|
*self.ignored_users.write().await = ignored_users;
|
|
|
|
*self.push_ruleset.write().await = push_ruleset;
|
|
|
|
} else {
|
|
|
|
// return false and continues with a sync request then save the state and create
|
|
|
|
// and populate the files during the sync
|
|
|
|
return Ok(false);
|
2020-04-29 11:00:14 +00:00
|
|
|
}
|
2020-05-25 12:21:04 +00:00
|
|
|
|
|
|
|
let AllRooms {
|
|
|
|
mut joined,
|
|
|
|
mut invited,
|
|
|
|
mut left,
|
|
|
|
} = store.load_all_rooms().await?;
|
|
|
|
|
|
|
|
*self.joined_rooms.write().await = joined
|
|
|
|
.drain()
|
|
|
|
.map(|(k, room)| (k, Arc::new(RwLock::new(room))))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
*self.invited_rooms.write().await = invited
|
|
|
|
.drain()
|
|
|
|
.map(|(k, room)| (k, Arc::new(RwLock::new(room))))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
*self.left_rooms.write().await = left
|
|
|
|
.drain()
|
|
|
|
.map(|(k, room)| (k, Arc::new(RwLock::new(room))))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(loaded)
|
2020-04-23 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 11:33:57 +00:00
|
|
|
/// When a client is provided the state store will load state from the `StateStore`.
|
|
|
|
///
|
|
|
|
/// Returns `true` when a state store sync has successfully completed.
|
|
|
|
pub async fn store_room_state(&self, room_id: &RoomId) -> Result<()> {
|
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
|
|
|
if let Some(room) = self.get_joined_room(room_id).await {
|
|
|
|
let room = room.read().await;
|
2020-05-11 19:32:58 +00:00
|
|
|
store
|
|
|
|
.store_room_state(RoomState::Joined(room.deref()))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
if let Some(room) = self.get_invited_room(room_id).await {
|
|
|
|
let room = room.read().await;
|
|
|
|
store
|
|
|
|
.store_room_state(RoomState::Invited(room.deref()))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
if let Some(room) = self.get_left_room(room_id).await {
|
|
|
|
let room = room.read().await;
|
|
|
|
store
|
|
|
|
.store_room_state(RoomState::Left(room.deref()))
|
|
|
|
.await?;
|
2020-05-09 11:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:47:00 +00:00
|
|
|
/// 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(
|
2020-05-06 13:36:55 +00:00
|
|
|
&self,
|
2020-03-18 13:15:56 +00:00
|
|
|
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(),
|
|
|
|
};
|
2020-05-22 13:23:58 +00:00
|
|
|
self.restore_login(session).await
|
|
|
|
}
|
2020-03-11 09:08:22 +00:00
|
|
|
|
2020-05-22 13:23:58 +00:00
|
|
|
/// Restore a previously logged in session.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `session` - An session that the user already has from a
|
|
|
|
/// previous login call.
|
|
|
|
pub async fn restore_login(&self, session: Session) -> Result<()> {
|
2020-07-06 08:54:01 +00:00
|
|
|
// If there wasn't a state store opened, try to open the default one if
|
|
|
|
// a store path was provided.
|
|
|
|
if self.state_store.read().await.is_none() {
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
{
|
|
|
|
if let Some(path) = &*self.store_path {
|
|
|
|
let store = JsonStore::open(path)?;
|
|
|
|
*self.state_store.write().await = Some(Box::new(store));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.sync_with_state_store(&session).await?;
|
|
|
|
|
2020-03-11 09:08:22 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
{
|
|
|
|
let mut olm = self.olm.lock().await;
|
2020-05-25 12:21:04 +00:00
|
|
|
let store = self.cryptostore.lock().await.take();
|
|
|
|
|
|
|
|
if let Some(store) = store {
|
|
|
|
*olm = Some(
|
|
|
|
OlmMachine::new_with_store(
|
|
|
|
session.user_id.to_owned(),
|
|
|
|
session.device_id.to_owned(),
|
|
|
|
store,
|
|
|
|
)
|
|
|
|
.await
|
2020-05-25 12:58:06 +00:00
|
|
|
.map_err(OlmError::from)?,
|
2020-05-25 12:21:04 +00:00
|
|
|
);
|
|
|
|
} else if let Some(path) = self.store_path.as_ref() {
|
|
|
|
#[cfg(feature = "sqlite-cryptostore")]
|
|
|
|
{
|
|
|
|
*olm = Some(
|
|
|
|
OlmMachine::new_with_default_store(
|
|
|
|
&session.user_id,
|
|
|
|
&session.device_id,
|
|
|
|
path,
|
|
|
|
&self.store_passphrase,
|
|
|
|
)
|
|
|
|
.await
|
2020-05-25 12:58:06 +00:00
|
|
|
.map_err(OlmError::from)?,
|
2020-05-25 12:21:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "sqlite-cryptostore"))]
|
|
|
|
{
|
|
|
|
*olm = Some(OlmMachine::new(&session.user_id, &session.device_id));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*olm = Some(OlmMachine::new(&session.user_id, &session.device_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 13:23:58 +00:00
|
|
|
*self.session.write().await = Some(session);
|
2020-03-18 13:15:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 20:26:27 +00:00
|
|
|
pub(crate) async fn get_or_create_joined_room(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> Result<Arc<RwLock<Room>>> {
|
2020-05-13 11:23:16 +00:00
|
|
|
// If this used to be an invited or left room remove them from our other
|
|
|
|
// hashmaps.
|
2020-05-18 20:26:27 +00:00
|
|
|
if self.invited_rooms.write().await.remove(room_id).is_some() {
|
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
2020-05-22 07:54:42 +00:00
|
|
|
store.delete_room_state(RoomState::Invited(room_id)).await?;
|
2020-05-18 20:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.left_rooms.write().await.remove(room_id).is_some() {
|
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
2020-05-22 07:54:42 +00:00
|
|
|
store.delete_room_state(RoomState::Left(room_id)).await?;
|
2020-05-18 20:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-13 11:23:16 +00:00
|
|
|
|
2020-05-06 11:57:58 +00:00
|
|
|
let mut rooms = self.joined_rooms.write().await;
|
2020-05-08 09:07:08 +00:00
|
|
|
#[allow(clippy::or_fun_call)]
|
2020-05-18 20:26:27 +00:00
|
|
|
Ok(rooms
|
2020-04-03 13:07:40 +00:00
|
|
|
.entry(room_id.clone())
|
2020-04-14 22:10:10 +00:00
|
|
|
.or_insert(Arc::new(RwLock::new(Room::new(
|
2019-11-10 17:33:06 +00:00
|
|
|
room_id,
|
|
|
|
&self
|
|
|
|
.session
|
2020-05-06 12:27:53 +00:00
|
|
|
.read()
|
|
|
|
.await
|
2019-11-10 17:33:06 +00:00
|
|
|
.as_ref()
|
|
|
|
.expect("Receiving events while not being logged in")
|
2020-04-03 13:07:40 +00:00
|
|
|
.user_id,
|
2019-10-30 22:26:26 +00:00
|
|
|
))))
|
2020-05-18 20:26:27 +00:00
|
|
|
.clone())
|
2019-10-23 21:36:57 +00:00
|
|
|
}
|
2019-10-23 20:47:00 +00:00
|
|
|
|
2020-05-06 13:55:18 +00:00
|
|
|
/// Get a joined room with the given room id.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `room_id` - The unique id of the room that should be fetched.
|
2020-05-07 12:57:38 +00:00
|
|
|
pub async fn get_joined_room(&self, room_id: &RoomId) -> Option<Arc<RwLock<Room>>> {
|
2020-05-06 11:57:58 +00:00
|
|
|
self.joined_rooms.read().await.get(room_id).cloned()
|
2020-03-31 10:57:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 13:55:18 +00:00
|
|
|
/// Returns the joined rooms this client knows about.
|
|
|
|
///
|
|
|
|
/// A `HashMap` of room id to `matrix::models::Room`
|
2020-05-08 14:12:21 +00:00
|
|
|
pub fn joined_rooms(&self) -> Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>> {
|
2020-05-06 13:55:18 +00:00
|
|
|
self.joined_rooms.clone()
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:26:27 +00:00
|
|
|
pub(crate) async fn get_or_create_invited_room(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> Result<Arc<RwLock<Room>>> {
|
2020-05-13 11:23:16 +00:00
|
|
|
// Remove the left rooms only here, since a join -> invite action per
|
|
|
|
// spec can't happen.
|
2020-05-18 20:26:27 +00:00
|
|
|
if self.left_rooms.write().await.remove(room_id).is_some() {
|
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
2020-05-22 07:54:42 +00:00
|
|
|
store.delete_room_state(RoomState::Left(room_id)).await?;
|
2020-05-18 20:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-13 11:23:16 +00:00
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
let mut rooms = self.invited_rooms.write().await;
|
2020-05-08 10:39:36 +00:00
|
|
|
#[allow(clippy::or_fun_call)]
|
2020-05-18 20:26:27 +00:00
|
|
|
Ok(rooms
|
2020-05-06 23:45:27 +00:00
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_insert(Arc::new(RwLock::new(Room::new(
|
|
|
|
room_id,
|
|
|
|
&self
|
|
|
|
.session
|
2020-05-07 00:35:15 +00:00
|
|
|
.read()
|
|
|
|
.await
|
2020-05-06 23:45:27 +00:00
|
|
|
.as_ref()
|
|
|
|
.expect("Receiving events while not being logged in")
|
|
|
|
.user_id,
|
|
|
|
))))
|
2020-05-18 20:26:27 +00:00
|
|
|
.clone())
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
/// Get an invited room with the given room id.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `room_id` - The unique id of the room that should be fetched.
|
2020-05-07 12:57:38 +00:00
|
|
|
pub async fn get_invited_room(&self, room_id: &RoomId) -> Option<Arc<RwLock<Room>>> {
|
2020-05-07 00:35:15 +00:00
|
|
|
self.invited_rooms.read().await.get(room_id).cloned()
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
/// Returns the invited rooms this client knows about.
|
|
|
|
///
|
|
|
|
/// A `HashMap` of room id to `matrix::models::Room`
|
2020-05-08 14:12:21 +00:00
|
|
|
pub fn invited_rooms(&self) -> Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>> {
|
2020-05-07 00:35:15 +00:00
|
|
|
self.invited_rooms.clone()
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:26:27 +00:00
|
|
|
pub(crate) async fn get_or_create_left_room(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> Result<Arc<RwLock<Room>>> {
|
2020-05-13 11:23:16 +00:00
|
|
|
// If this used to be an invited or joined room remove them from our other
|
|
|
|
// hashmaps.
|
2020-05-18 20:26:27 +00:00
|
|
|
if self.invited_rooms.write().await.remove(room_id).is_some() {
|
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
2020-05-22 07:54:42 +00:00
|
|
|
store.delete_room_state(RoomState::Invited(room_id)).await?;
|
2020-05-18 20:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.joined_rooms.write().await.remove(room_id).is_some() {
|
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
2020-05-22 07:54:42 +00:00
|
|
|
store.delete_room_state(RoomState::Joined(room_id)).await?;
|
2020-05-18 20:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-13 11:23:16 +00:00
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
let mut rooms = self.left_rooms.write().await;
|
2020-05-08 10:39:36 +00:00
|
|
|
#[allow(clippy::or_fun_call)]
|
2020-05-18 20:26:27 +00:00
|
|
|
Ok(rooms
|
2020-05-06 23:45:27 +00:00
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_insert(Arc::new(RwLock::new(Room::new(
|
|
|
|
room_id,
|
|
|
|
&self
|
|
|
|
.session
|
2020-05-07 00:35:15 +00:00
|
|
|
.read()
|
|
|
|
.await
|
2020-05-06 23:45:27 +00:00
|
|
|
.as_ref()
|
|
|
|
.expect("Receiving events while not being logged in")
|
|
|
|
.user_id,
|
|
|
|
))))
|
2020-05-18 20:26:27 +00:00
|
|
|
.clone())
|
2020-05-07 00:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an left room with the given room id.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// `room_id` - The unique id of the room that should be fetched.
|
2020-05-07 12:57:38 +00:00
|
|
|
pub async fn get_left_room(&self, room_id: &RoomId) -> Option<Arc<RwLock<Room>>> {
|
2020-05-07 00:35:15 +00:00
|
|
|
self.left_rooms.read().await.get(room_id).cloned()
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
/// Returns the left rooms this client knows about.
|
|
|
|
///
|
|
|
|
/// A `HashMap` of room id to `matrix::models::Room`
|
2020-05-08 14:12:21 +00:00
|
|
|
pub fn left_rooms(&self) -> Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>> {
|
2020-05-07 00:35:15 +00:00
|
|
|
self.left_rooms.clone()
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 19:54:26 +00:00
|
|
|
/// Handle a m.ignored_user_list event, updating the room state if necessary.
|
|
|
|
///
|
|
|
|
/// Returns true if the room name changed, false otherwise.
|
2020-05-06 11:57:58 +00:00
|
|
|
pub(crate) async fn handle_ignored_users(&self, event: &IgnoredUserListEvent) -> bool {
|
2020-04-03 13:07:40 +00:00
|
|
|
// this avoids cloning every UserId for the eq check
|
2020-05-06 11:57:58 +00:00
|
|
|
if self.ignored_users.read().await.iter().collect::<Vec<_>>()
|
2020-04-03 13:14:46 +00:00
|
|
|
== event.content.ignored_users.iter().collect::<Vec<_>>()
|
2020-03-30 11:14:33 +00:00
|
|
|
{
|
2020-03-29 19:54:26 +00:00
|
|
|
false
|
|
|
|
} else {
|
2020-05-06 11:57:58 +00:00
|
|
|
*self.ignored_users.write().await = event.content.ignored_users.to_vec();
|
2020-03-29 19:54:26 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a m.ignored_user_list event, updating the room state if necessary.
|
|
|
|
///
|
|
|
|
/// Returns true if the room name changed, false otherwise.
|
2020-05-06 11:57:58 +00:00
|
|
|
pub(crate) async fn handle_push_rules(&self, event: &PushRulesEvent) -> bool {
|
2020-03-29 19:54:26 +00:00
|
|
|
// TODO this is basically a stub
|
2020-05-04 12:22:10 +00:00
|
|
|
// TODO ruma removed PartialEq for evens, so this doesn't work anymore.
|
|
|
|
// Returning always true for now should be ok here since those don't
|
|
|
|
// change often.
|
|
|
|
// if self.push_ruleset.as_ref() == Some(&event.content.global) {
|
|
|
|
// false
|
|
|
|
// } else {
|
2020-05-06 11:57:58 +00:00
|
|
|
*self.push_ruleset.write().await = Some(event.content.global.clone());
|
2020-05-04 12:22:10 +00:00
|
|
|
true
|
|
|
|
// }
|
2020-03-29 19:54:26 +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-04-29 11:00:14 +00:00
|
|
|
/// Returns a tuple of the successfully decrypted event, or None on failure and
|
|
|
|
/// a bool, true when the `Room` state has been updated.
|
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(
|
2020-05-06 11:57:58 +00:00
|
|
|
&self,
|
2020-03-25 14:03:10 +00:00
|
|
|
room_id: &RoomId,
|
2020-07-03 11:02:12 +00:00
|
|
|
event: &mut EventJson<AnyRoomEventStub>,
|
2020-07-02 21:16:56 +00:00
|
|
|
) -> Result<bool> {
|
2020-04-23 08:52:47 +00:00
|
|
|
match event.deserialize() {
|
2020-04-23 09:37:47 +00:00
|
|
|
#[allow(unused_mut)]
|
2020-04-23 08:52:47 +00:00
|
|
|
Ok(mut e) => {
|
2020-03-25 14:03:10 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
{
|
2020-07-03 11:02:12 +00:00
|
|
|
if let AnyRoomEventStub::Message(AnyMessageEventStub::RoomEncrypted(
|
|
|
|
ref mut encrypted_event,
|
|
|
|
)) = e
|
|
|
|
{
|
2020-04-30 12:07:49 +00:00
|
|
|
let mut olm = self.olm.lock().await;
|
2020-03-25 14:03:10 +00:00
|
|
|
|
2020-04-30 12:07:49 +00:00
|
|
|
if let Some(o) = &mut *olm {
|
2020-07-11 18:57:01 +00:00
|
|
|
if let Ok(decrypted) =
|
|
|
|
o.decrypt_room_event(&encrypted_event, room_id).await
|
2020-07-02 21:16:56 +00:00
|
|
|
{
|
|
|
|
if let Ok(d) = decrypted.deserialize() {
|
|
|
|
e = d
|
|
|
|
}
|
|
|
|
*event = decrypted;
|
|
|
|
}
|
2020-03-25 14:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:26:27 +00:00
|
|
|
let room_lock = self.get_or_create_joined_room(&room_id).await?;
|
2020-05-06 11:57:58 +00:00
|
|
|
let mut room = room_lock.write().await;
|
2020-05-14 10:59:36 +00:00
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
if let AnyRoomEventStub::State(AnyStateEventStub::RoomMember(mem_event)) = &mut e {
|
2020-07-03 19:29:10 +00:00
|
|
|
let changed = room.handle_membership(mem_event);
|
2020-05-14 10:59:36 +00:00
|
|
|
|
|
|
|
// The memberlist of the room changed, invalidate the group session
|
|
|
|
// of the room.
|
|
|
|
if changed {
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
self.invalidate_group_session(room_id).await;
|
|
|
|
}
|
|
|
|
|
2020-07-02 21:16:56 +00:00
|
|
|
Ok(changed)
|
2020-05-14 10:59:36 +00:00
|
|
|
} else {
|
2020-07-03 19:29:10 +00:00
|
|
|
Ok(room.receive_timeline_event(&e))
|
2020-05-14 10:59:36 +00:00
|
|
|
}
|
2019-11-26 19:47:02 +00:00
|
|
|
}
|
2020-07-02 21:16:56 +00:00
|
|
|
_ => Ok(false),
|
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-05-06 23:45:27 +00:00
|
|
|
/// Returns true if the state of the room changed, false
|
2020-03-02 10:31:03 +00:00
|
|
|
/// 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-05-18 20:26:27 +00:00
|
|
|
pub async fn receive_joined_state_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &AnyStateEventStub,
|
2020-05-18 20:26:27 +00:00
|
|
|
) -> Result<bool> {
|
|
|
|
let room_lock = self.get_or_create_joined_room(room_id).await?;
|
2020-05-07 00:35:15 +00:00
|
|
|
let mut room = room_lock.write().await;
|
2020-05-14 10:59:36 +00:00
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
if let AnyStateEventStub::RoomMember(e) = event {
|
2020-07-03 19:29:10 +00:00
|
|
|
let changed = room.handle_membership(e);
|
2020-05-14 10:59:36 +00:00
|
|
|
|
|
|
|
// The memberlist of the room changed, invalidate the group session
|
|
|
|
// of the room.
|
|
|
|
if changed {
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
self.invalidate_group_session(room_id).await;
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:26:27 +00:00
|
|
|
Ok(changed)
|
2020-05-14 10:59:36 +00:00
|
|
|
} else {
|
2020-07-03 19:29:10 +00:00
|
|
|
Ok(room.receive_state_event(event))
|
2020-05-14 10:59:36 +00:00
|
|
|
}
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
2019-11-24 21:40:52 +00:00
|
|
|
|
2020-05-05 20:13:14 +00:00
|
|
|
/// Receive a state event for a room the user has been invited to.
|
|
|
|
///
|
|
|
|
/// Returns true if the state of the room changed, false
|
|
|
|
/// otherwise.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The unique id of the room the event belongs to.
|
|
|
|
///
|
|
|
|
/// * `event` - A `AnyStrippedStateEvent` that should be handled by the client.
|
|
|
|
pub async fn receive_invite_state_event(
|
2020-05-07 00:35:15 +00:00
|
|
|
&self,
|
2020-05-05 20:13:14 +00:00
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &AnyStrippedStateEventStub,
|
2020-05-18 20:26:27 +00:00
|
|
|
) -> Result<bool> {
|
|
|
|
let room_lock = self.get_or_create_invited_room(room_id).await?;
|
2020-05-07 00:35:15 +00:00
|
|
|
let mut room = room_lock.write().await;
|
2020-05-18 20:26:27 +00:00
|
|
|
Ok(room.receive_stripped_state_event(event))
|
2020-05-05 20:13:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:45:27 +00:00
|
|
|
/// Receive a timeline event for a room the user has left and update the client state.
|
|
|
|
///
|
|
|
|
/// Returns a tuple of the successfully decrypted event, or None on failure and
|
|
|
|
/// a bool, true when the `Room` state has been updated.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The unique id of the room the event belongs to.
|
|
|
|
///
|
|
|
|
/// * `event` - The event that should be handled by the client.
|
|
|
|
pub async fn receive_left_timeline_event(
|
2020-05-07 00:35:15 +00:00
|
|
|
&self,
|
2020-05-06 23:45:27 +00:00
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &EventJson<AnyRoomEventStub>,
|
2020-05-18 20:26:27 +00:00
|
|
|
) -> Result<bool> {
|
2020-05-06 23:45:27 +00:00
|
|
|
match event.deserialize() {
|
2020-05-07 11:09:22 +00:00
|
|
|
Ok(e) => {
|
2020-05-18 20:26:27 +00:00
|
|
|
let room_lock = self.get_or_create_left_room(room_id).await?;
|
2020-05-07 00:35:15 +00:00
|
|
|
let mut room = room_lock.write().await;
|
2020-07-03 19:29:10 +00:00
|
|
|
Ok(room.receive_timeline_event(&e))
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
2020-05-18 20:26:27 +00:00
|
|
|
_ => Ok(false),
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a state event for a room the user has left and update the client state.
|
|
|
|
///
|
|
|
|
/// Returns true if the state of the room changed, false
|
|
|
|
/// otherwise.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The unique id of the room the event belongs to.
|
|
|
|
///
|
|
|
|
/// * `event` - The event that should be handled by the client.
|
2020-05-18 20:26:27 +00:00
|
|
|
pub async fn receive_left_state_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &AnyStateEventStub,
|
2020-05-18 20:26:27 +00:00
|
|
|
) -> Result<bool> {
|
|
|
|
let room_lock = self.get_or_create_left_room(room_id).await?;
|
2020-05-06 11:57:58 +00:00
|
|
|
let mut room = room_lock.write().await;
|
2020-07-03 19:29:10 +00:00
|
|
|
Ok(room.receive_state_event(event))
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 10:57:29 +00:00
|
|
|
/// Receive a presence event from a sync response and updates the client state.
|
2020-03-28 10:58:30 +00:00
|
|
|
///
|
2020-05-06 23:45:27 +00:00
|
|
|
/// Returns true if the state of the room changed, false
|
2020-03-28 10:58:30 +00:00
|
|
|
/// otherwise.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The unique id of the room the event belongs to.
|
|
|
|
///
|
|
|
|
/// * `event` - The event that should be handled by the client.
|
2020-05-06 11:57:58 +00:00
|
|
|
pub async fn receive_presence_event(&self, room_id: &RoomId, event: &PresenceEvent) -> bool {
|
2020-03-31 10:57:29 +00:00
|
|
|
// this should be the room that was just created in the `Client::sync` loop.
|
2020-05-06 13:55:18 +00:00
|
|
|
if let Some(room) = self.get_joined_room(room_id).await {
|
2020-04-14 22:10:10 +00:00
|
|
|
let mut room = room.write().await;
|
2020-03-31 10:57:29 +00:00
|
|
|
room.receive_presence_event(event)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2020-03-28 10:58:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
/// Receive an account data event from a sync response and updates the client state.
|
|
|
|
///
|
|
|
|
/// Returns true if the state of the `Room` has changed, false otherwise.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The unique id of the room the event belongs to.
|
|
|
|
///
|
|
|
|
/// * `event` - The presence event for a specified room member.
|
2020-06-20 21:18:20 +00:00
|
|
|
pub async fn receive_account_data_event(&self, _: &RoomId, event: &AnyBasicEvent) -> bool {
|
|
|
|
match &event {
|
|
|
|
AnyBasicEvent::IgnoredUserList(event) => self.handle_ignored_users(&event).await,
|
|
|
|
AnyBasicEvent::PushRules(event) => self.handle_push_rules(&event).await,
|
2020-04-07 12:55:42 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive an ephemeral event from a sync response and updates the client state.
|
2020-03-29 19:54:26 +00:00
|
|
|
///
|
2020-04-07 12:55:42 +00:00
|
|
|
/// Returns true if the state of the `Room` has changed, false otherwise.
|
2020-03-29 19:54:26 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-04-02 19:59:13 +00:00
|
|
|
/// * `room_id` - The unique id of the room the event belongs to.
|
|
|
|
///
|
2020-03-29 19:54:26 +00:00
|
|
|
/// * `event` - The presence event for a specified room member.
|
2020-07-03 19:29:10 +00:00
|
|
|
pub async fn receive_ephemeral_event(&self, event: &AnyEphemeralRoomEventStub) -> bool {
|
2020-06-20 21:18:20 +00:00
|
|
|
match &event {
|
2020-06-25 00:30:53 +00:00
|
|
|
AnyEphemeralRoomEventStub::FullyRead(_) => {}
|
|
|
|
AnyEphemeralRoomEventStub::Receipt(_) => {}
|
|
|
|
AnyEphemeralRoomEventStub::Typing(_) => {}
|
2020-06-20 21:18:20 +00:00
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
false
|
2020-03-29 19:54:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 11:57:58 +00:00
|
|
|
/// Get the current, if any, sync token of the client.
|
|
|
|
/// This will be None if the client didn't sync at least once.
|
|
|
|
pub async fn sync_token(&self) -> Option<String> {
|
|
|
|
self.sync_token.read().await.clone()
|
|
|
|
}
|
|
|
|
|
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-04-29 11:00:14 +00:00
|
|
|
pub async fn receive_sync_response(
|
2020-05-06 11:57:58 +00:00
|
|
|
&self,
|
2020-04-29 11:00:14 +00:00
|
|
|
response: &mut api::sync::sync_events::Response,
|
|
|
|
) -> Result<()> {
|
2020-05-13 10:26:07 +00:00
|
|
|
// The server might respond multiple times with the same sync token, in
|
|
|
|
// that case we already received this response and there's nothing to
|
|
|
|
// do.
|
|
|
|
if self.sync_token.read().await.as_ref() == Some(&response.next_batch) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:57:58 +00:00
|
|
|
*self.sync_token.write().await = 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-05-13 10:27:16 +00:00
|
|
|
// Let the crypto machine handle the sync response, this
|
|
|
|
// decryptes to-device events, but leaves room events alone.
|
|
|
|
// This makes sure that we have the deryption keys for the room
|
|
|
|
// events at hand.
|
2020-03-23 15:14:10 +00:00
|
|
|
o.receive_sync_response(response).await;
|
2020-03-12 14:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 11:00:14 +00:00
|
|
|
|
2020-05-09 11:33:57 +00:00
|
|
|
// when events change state, updated_* signals to StateStore to update database
|
2020-05-13 10:27:16 +00:00
|
|
|
self.iter_joined_rooms(response).await?;
|
2020-07-03 10:30:57 +00:00
|
|
|
self.iter_invited_rooms(response).await?;
|
2020-05-13 10:27:16 +00:00
|
|
|
self.iter_left_rooms(response).await?;
|
2020-05-07 00:35:15 +00:00
|
|
|
|
2020-05-13 10:27:16 +00:00
|
|
|
let store = self.state_store.read().await;
|
2020-05-07 00:35:15 +00:00
|
|
|
|
2020-05-13 10:27:16 +00:00
|
|
|
// Store now the new sync token an other client specific state. Since we
|
|
|
|
// know the sync token changed we can assume that this needs to be done
|
|
|
|
// always.
|
|
|
|
if let Some(store) = store.as_ref() {
|
|
|
|
let state = ClientState::from_base_client(&self).await;
|
|
|
|
store.store_client_state(state).await?;
|
2020-04-29 11:00:14 +00:00
|
|
|
}
|
2020-05-13 10:27:16 +00:00
|
|
|
|
2020-04-29 11:00:14 +00:00
|
|
|
Ok(())
|
2019-11-24 21:40:52 +00:00
|
|
|
}
|
2020-03-11 09:10:43 +00:00
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
async fn iter_joined_rooms(
|
|
|
|
&self,
|
|
|
|
response: &mut api::sync::sync_events::Response,
|
|
|
|
) -> Result<bool> {
|
2020-05-06 11:57:58 +00:00
|
|
|
let mut updated = false;
|
2020-05-07 00:35:15 +00:00
|
|
|
for (room_id, joined_room) in &mut response.rooms.join {
|
2020-05-06 11:57:58 +00:00
|
|
|
let matrix_room = {
|
2020-06-15 15:04:10 +00:00
|
|
|
for event in &mut joined_room.state.events {
|
|
|
|
// XXX: Related to `prev_content` and `unsigned`; see the doc comment of
|
|
|
|
// `hoist_room_event_prev_content`
|
|
|
|
if let Some(e) = hoist_state_event_prev_content(event) {
|
|
|
|
*event = e;
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:57:58 +00:00
|
|
|
if let Ok(e) = event.deserialize() {
|
2020-07-03 08:31:47 +00:00
|
|
|
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
|
|
|
|
// should only pass events to receive_* methods and then let *them* emit.
|
2020-05-18 20:26:27 +00:00
|
|
|
if self.receive_joined_state_event(&room_id, &e).await? {
|
2020-05-06 11:57:58 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
2020-05-14 11:00:47 +00:00
|
|
|
self.emit_state_event(&room_id, &e, RoomStateType::Joined)
|
|
|
|
.await;
|
2020-05-06 11:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:26:27 +00:00
|
|
|
self.get_or_create_joined_room(&room_id).await?.clone()
|
2020-05-06 11:57:58 +00:00
|
|
|
};
|
|
|
|
|
2020-06-15 15:05:10 +00:00
|
|
|
// RoomSummary contains information for calculating room name.
|
2020-05-07 00:35:15 +00:00
|
|
|
matrix_room
|
|
|
|
.write()
|
|
|
|
.await
|
|
|
|
.set_room_summary(&joined_room.summary);
|
2020-05-06 11:57:58 +00:00
|
|
|
|
2020-06-15 15:05:10 +00:00
|
|
|
// Set unread notification count.
|
2020-05-07 19:21:06 +00:00
|
|
|
matrix_room
|
|
|
|
.write()
|
|
|
|
.await
|
|
|
|
.set_unread_notice_count(&joined_room.unread_notifications);
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
for mut event in &mut joined_room.timeline.events {
|
2020-07-03 09:53:15 +00:00
|
|
|
// XXX: Related to `prev_content` and `unsigned`; see the doc comment of
|
|
|
|
// `hoist_room_event_prev_content`
|
|
|
|
if let Some(e) = hoist_room_event_prev_content(event) {
|
|
|
|
*event = e;
|
|
|
|
}
|
|
|
|
|
2020-07-03 08:31:47 +00:00
|
|
|
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
|
|
|
|
// should only pass events to receive_* methods and then let *them* emit.
|
2020-07-02 21:16:56 +00:00
|
|
|
let timeline_update = self
|
|
|
|
.receive_joined_timeline_event(room_id, &mut event)
|
|
|
|
.await?;
|
|
|
|
if timeline_update {
|
|
|
|
updated = true;
|
2020-05-06 11:57:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Ok(e) = event.deserialize() {
|
2020-05-07 00:35:15 +00:00
|
|
|
self.emit_timeline_event(&room_id, &e, RoomStateType::Joined)
|
|
|
|
.await;
|
2020-05-29 21:36:58 +00:00
|
|
|
} else {
|
|
|
|
self.emit_unrecognized_event(&room_id, &event, RoomStateType::Joined)
|
|
|
|
.await;
|
2020-05-06 11:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 13:48:28 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
{
|
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
if let Some(o) = &mut *olm {
|
|
|
|
let room = matrix_room.read().await;
|
|
|
|
|
|
|
|
// If the room is encrypted, update the tracked users.
|
|
|
|
if room.is_encrypted() {
|
|
|
|
o.update_tracked_users(room.joined_members.keys()).await;
|
|
|
|
o.update_tracked_users(room.invited_members.keys()).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:57:58 +00:00
|
|
|
// look at AccountData to further cut down users by collecting ignored users
|
2020-07-03 11:02:12 +00:00
|
|
|
for account_data in &joined_room.account_data.events {
|
|
|
|
{
|
|
|
|
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
|
|
|
|
// should only pass events to receive_* methods and then let *them* emit.
|
|
|
|
if let Ok(e) = account_data.deserialize() {
|
|
|
|
if self.receive_account_data_event(&room_id, &e).await {
|
|
|
|
updated = true;
|
2020-05-06 11:57:58 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
self.emit_account_data_event(room_id, &e, RoomStateType::Joined)
|
|
|
|
.await;
|
2020-05-06 11:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// After the room has been created and state/timeline events accounted for we use the room_id of the newly created
|
|
|
|
// room to add any presence events that relate to a user in the current room. This is not super
|
|
|
|
// efficient but we need a room_id so we would loop through now or later.
|
|
|
|
for presence in &mut response.presence.events {
|
|
|
|
{
|
2020-07-03 08:31:47 +00:00
|
|
|
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
|
|
|
|
// should only pass events to receive_* methods and then let *them* emit.
|
2020-05-06 11:57:58 +00:00
|
|
|
if let Ok(e) = presence.deserialize() {
|
|
|
|
if self.receive_presence_event(&room_id, &e).await {
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
self.emit_presence_event(&room_id, &e, RoomStateType::Joined)
|
|
|
|
.await;
|
2020-05-06 11:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
for ephemeral in &mut joined_room.ephemeral.events {
|
2020-05-06 11:57:58 +00:00
|
|
|
{
|
|
|
|
if let Ok(e) = ephemeral.deserialize() {
|
2020-07-03 08:31:47 +00:00
|
|
|
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
|
|
|
|
// should only pass events to receive_* methods and then let *them* emit.
|
2020-07-03 19:29:10 +00:00
|
|
|
if self.receive_ephemeral_event(&e).await {
|
2020-05-06 11:57:58 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
self.emit_ephemeral_event(&room_id, &e, RoomStateType::Joined)
|
|
|
|
.await;
|
2020-05-06 11:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated {
|
2020-05-07 00:35:15 +00:00
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
|
|
|
store
|
2020-05-11 19:32:58 +00:00
|
|
|
.store_room_state(RoomState::Joined(matrix_room.read().await.deref()))
|
2020-05-07 00:35:15 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(updated)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn iter_left_rooms(
|
|
|
|
&self,
|
|
|
|
response: &mut api::sync::sync_events::Response,
|
|
|
|
) -> Result<bool> {
|
|
|
|
let mut updated = false;
|
|
|
|
for (room_id, left_room) in &mut response.rooms.leave {
|
|
|
|
let matrix_room = {
|
2020-06-15 15:04:10 +00:00
|
|
|
for event in &mut left_room.state.events {
|
|
|
|
// XXX: Related to `prev_content` and `unsigned`; see the doc comment of
|
|
|
|
// `hoist_room_event_prev_content`
|
|
|
|
if let Some(e) = hoist_state_event_prev_content(event) {
|
|
|
|
*event = e;
|
|
|
|
}
|
|
|
|
|
2020-07-03 08:31:47 +00:00
|
|
|
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
|
|
|
|
// should only pass events to receive_* methods and then let *them* emit.
|
2020-05-07 00:35:15 +00:00
|
|
|
if let Ok(e) = event.deserialize() {
|
2020-05-18 20:26:27 +00:00
|
|
|
if self.receive_left_state_event(&room_id, &e).await? {
|
2020-05-07 00:35:15 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:26:27 +00:00
|
|
|
self.get_or_create_left_room(&room_id).await?.clone()
|
2020-05-07 00:35:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for event in &mut left_room.state.events {
|
|
|
|
if let Ok(e) = event.deserialize() {
|
|
|
|
self.emit_state_event(&room_id, &e, RoomStateType::Left)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 13:20:20 +00:00
|
|
|
|
2020-05-08 10:39:36 +00:00
|
|
|
for event in &mut left_room.timeline.events {
|
2020-06-15 15:04:10 +00:00
|
|
|
// XXX: Related to `prev_content` and `unsigned`; see the doc comment of
|
|
|
|
// `hoist_room_event_prev_content`
|
|
|
|
if let Some(e) = hoist_room_event_prev_content(event) {
|
2020-05-22 21:12:58 +00:00
|
|
|
*event = e;
|
|
|
|
}
|
|
|
|
|
2020-07-03 08:31:47 +00:00
|
|
|
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
|
|
|
|
// should only pass events to receive_* methods and then let *them* emit.
|
2020-05-18 20:26:27 +00:00
|
|
|
if self.receive_left_timeline_event(room_id, &event).await? {
|
2020-05-07 11:09:22 +00:00
|
|
|
updated = true;
|
2020-05-07 00:35:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Ok(e) = event.deserialize() {
|
|
|
|
self.emit_timeline_event(&room_id, &e, RoomStateType::Left)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated {
|
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
2020-05-06 11:57:58 +00:00
|
|
|
store
|
2020-05-11 19:32:58 +00:00
|
|
|
.store_room_state(RoomState::Left(matrix_room.read().await.deref()))
|
2020-05-06 11:57:58 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 00:35:15 +00:00
|
|
|
Ok(updated)
|
|
|
|
}
|
2020-05-06 11:57:58 +00:00
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
async fn iter_invited_rooms(
|
|
|
|
&self,
|
|
|
|
response: &api::sync::sync_events::Response,
|
|
|
|
) -> Result<bool> {
|
|
|
|
let mut updated = false;
|
|
|
|
for (room_id, invited_room) in &response.rooms.invite {
|
|
|
|
let matrix_room = {
|
|
|
|
for event in &invited_room.invite_state.events {
|
|
|
|
if let Ok(e) = event.deserialize() {
|
2020-07-03 08:31:47 +00:00
|
|
|
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
|
|
|
|
// should only pass events to receive_* methods and then let *them* emit.
|
2020-05-18 20:26:27 +00:00
|
|
|
if self.receive_invite_state_event(&room_id, &e).await? {
|
2020-05-07 00:35:15 +00:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 13:20:20 +00:00
|
|
|
|
2020-05-18 20:26:27 +00:00
|
|
|
self.get_or_create_invited_room(&room_id).await?.clone()
|
2020-05-07 00:35:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for event in &invited_room.invite_state.events {
|
2020-05-21 14:16:04 +00:00
|
|
|
if let Ok(mut e) = event.deserialize() {
|
|
|
|
// if the event is a m.room.member event the server will sometimes
|
|
|
|
// send the `prev_content` field as part of the unsigned field.
|
2020-06-20 21:18:20 +00:00
|
|
|
if let AnyStrippedStateEventStub::RoomMember(_) = &mut e {
|
2020-05-22 21:12:58 +00:00
|
|
|
if let Some(raw_content) = stripped_deserialize_prev_content(event) {
|
2020-05-21 14:16:04 +00:00
|
|
|
let prev_content = match raw_content.prev_content {
|
|
|
|
Some(json) => json.deserialize().ok(),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
self.emit_stripped_state_event(
|
|
|
|
&room_id,
|
|
|
|
&e,
|
|
|
|
prev_content,
|
|
|
|
RoomStateType::Invited,
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.emit_stripped_state_event(&room_id, &e, None, RoomStateType::Invited)
|
2020-05-07 00:35:15 +00:00
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated {
|
|
|
|
if let Some(store) = self.state_store.read().await.as_ref() {
|
|
|
|
store
|
2020-05-11 19:32:58 +00:00
|
|
|
.store_room_state(RoomState::Invited(matrix_room.read().await.deref()))
|
2020-05-07 00:35:15 +00:00
|
|
|
.await?;
|
|
|
|
}
|
2020-04-29 11:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-07 00:35:15 +00:00
|
|
|
Ok(updated)
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 09:47:01 +00:00
|
|
|
/// Should the client share a group session for the given room.
|
|
|
|
///
|
|
|
|
/// Returns true if a session needs to be shared before room messages can be
|
|
|
|
/// encrypted, false if one is already shared and ready to encrypt room
|
|
|
|
/// messages.
|
|
|
|
///
|
|
|
|
/// This should be called every time a new room message wants to be sent out
|
|
|
|
/// since group sessions can expire at any time.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
|
|
|
pub async fn should_share_group_session(&self, room_id: &RoomId) -> bool {
|
|
|
|
let olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
match &*olm {
|
|
|
|
Some(o) => o.should_share_group_session(room_id),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:37:00 +00:00
|
|
|
/// Should users be queried for their device keys.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
|
|
|
pub async fn should_query_keys(&self) -> bool {
|
|
|
|
let olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
match &*olm {
|
|
|
|
Some(o) => o.should_query_keys(),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:27:30 +00:00
|
|
|
/// 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")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
|
|
|
pub async fn get_missing_sessions(
|
|
|
|
&self,
|
2020-04-03 15:00:37 +00:00
|
|
|
users: impl Iterator<Item = &UserId>,
|
2020-04-27 14:26:03 +00:00
|
|
|
) -> Result<BTreeMap<UserId, BTreeMap<DeviceId, KeyAlgorithm>>> {
|
2020-04-03 08:27:30 +00:00
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
match &mut *olm {
|
2020-04-27 14:26:03 +00:00
|
|
|
Some(o) => Ok(o.get_missing_sessions(users).await?),
|
|
|
|
None => Ok(BTreeMap::new()),
|
2020-04-03 08:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:27:09 +00:00
|
|
|
/// Get a to-device request that will share a group session for a room.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
|
|
|
pub async fn share_group_session(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> Result<Vec<send_event_to_device::Request>> {
|
2020-05-06 13:55:18 +00:00
|
|
|
let room = self.get_joined_room(room_id).await.expect("No room found");
|
2020-04-09 14:27:09 +00:00
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
match &mut *olm {
|
|
|
|
Some(o) => {
|
2020-04-14 22:10:10 +00:00
|
|
|
let room = room.write().await;
|
2020-06-20 10:51:02 +00:00
|
|
|
|
|
|
|
// XXX: We construct members in a slightly roundabout way instead of chaining the
|
|
|
|
// iterators directly because of https://github.com/rust-lang/rust/issues/64552
|
|
|
|
let joined_members = room.joined_members.keys();
|
|
|
|
let invited_members = room.joined_members.keys();
|
|
|
|
let members: Vec<&UserId> = joined_members.chain(invited_members).collect();
|
|
|
|
Ok(o.share_group_session(room_id, members.into_iter()).await?)
|
2020-04-09 14:27:09 +00:00
|
|
|
}
|
|
|
|
None => panic!("Olm machine wasn't started"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encrypt a message event content.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
|
|
|
pub async fn encrypt(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
content: MsgEventContent,
|
2020-05-05 13:29:25 +00:00
|
|
|
) -> Result<EncryptedEventContent> {
|
2020-04-09 14:27:09 +00:00
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
2020-05-05 13:29:25 +00:00
|
|
|
match &mut *olm {
|
|
|
|
Some(o) => Ok(o.encrypt(room_id, content).await?),
|
|
|
|
None => panic!("Olm machine wasn't started"),
|
|
|
|
}
|
2020-04-09 14:27:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 09:10:43 +00:00
|
|
|
/// 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(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:37:00 +00:00
|
|
|
/// Get the users that we need to query keys for.
|
|
|
|
///
|
|
|
|
/// Returns an empty error if no keys need to be queried.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
2020-04-03 15:00:37 +00:00
|
|
|
pub async fn users_for_key_query(&self) -> StdResult<HashSet<UserId>, ()> {
|
2020-04-01 13:37:00 +00:00
|
|
|
let olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
match &*olm {
|
|
|
|
Some(o) => Ok(o.users_for_key_query()),
|
|
|
|
None => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 09:10:43 +00:00
|
|
|
/// 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
|
|
|
}
|
2020-04-01 13:37:00 +00:00
|
|
|
|
2020-04-03 08:27:30 +00:00
|
|
|
/// Receive a successful keys claim response.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `response` - The keys claim response of the request that the client
|
|
|
|
/// performed.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Panics if the client hasn't been logged in.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
|
|
|
pub async fn receive_keys_claim_response(&self, response: &KeysClaimResponse) -> Result<()> {
|
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
let o = olm.as_mut().expect("Client isn't logged in.");
|
|
|
|
o.receive_keys_claim_response(response).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:37:00 +00:00
|
|
|
/// Receive a successful keys query response.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `response` - The keys query response of the request that the client
|
|
|
|
/// performed.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Panics if the client hasn't been logged in.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
|
|
|
pub async fn receive_keys_query_response(&self, response: &KeysQueryResponse) -> Result<()> {
|
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
let o = olm.as_mut().expect("Client isn't logged in.");
|
|
|
|
o.receive_keys_query_response(response).await?;
|
|
|
|
// TODO notify our callers of new devices via some callback.
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-02 11:06:21 +00:00
|
|
|
|
2020-05-14 09:55:12 +00:00
|
|
|
/// Invalidate the currently active outbound group session for the given
|
|
|
|
/// room.
|
|
|
|
///
|
|
|
|
/// Returns true if a session was invalidated, false if there was no session
|
|
|
|
/// to invalidate.
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
|
|
|
|
pub async fn invalidate_group_session(&self, room_id: &RoomId) -> bool {
|
|
|
|
let mut olm = self.olm.lock().await;
|
|
|
|
|
|
|
|
match &mut *olm {
|
|
|
|
Some(o) => o.invalidate_group_session(room_id),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:45:27 +00:00
|
|
|
pub(crate) async fn emit_timeline_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &AnyRoomEventStub,
|
2020-05-06 23:45:27 +00:00
|
|
|
room_state: RoomStateType,
|
|
|
|
) {
|
2020-05-06 12:47:58 +00:00
|
|
|
let lock = self.event_emitter.read().await;
|
|
|
|
let event_emitter = if let Some(ee) = lock.as_ref() {
|
|
|
|
ee
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
let room = match room_state {
|
|
|
|
RoomStateType::Invited => {
|
|
|
|
if let Some(room) = self.get_invited_room(&room_id).await {
|
|
|
|
RoomState::Invited(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Joined => {
|
|
|
|
if let Some(room) = self.get_joined_room(&room_id).await {
|
|
|
|
RoomState::Joined(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Left => {
|
|
|
|
if let Some(room) = self.get_left_room(&room_id).await {
|
|
|
|
RoomState::Left(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 12:47:58 +00:00
|
|
|
};
|
|
|
|
|
2020-06-20 21:18:20 +00:00
|
|
|
match &event {
|
|
|
|
AnyRoomEventStub::State(event) => match &event {
|
|
|
|
AnyStateEventStub::RoomMember(e) => event_emitter.on_room_member(room, &e).await,
|
|
|
|
AnyStateEventStub::RoomName(e) => event_emitter.on_room_name(room, &e).await,
|
|
|
|
AnyStateEventStub::RoomCanonicalAlias(e) => {
|
|
|
|
event_emitter.on_room_canonical_alias(room, &e).await
|
|
|
|
}
|
|
|
|
AnyStateEventStub::RoomAliases(e) => event_emitter.on_room_aliases(room, &e).await,
|
|
|
|
AnyStateEventStub::RoomAvatar(e) => event_emitter.on_room_avatar(room, &e).await,
|
|
|
|
AnyStateEventStub::RoomPowerLevels(e) => {
|
|
|
|
event_emitter.on_room_power_levels(room, &e).await
|
|
|
|
}
|
|
|
|
AnyStateEventStub::RoomTombstone(e) => {
|
|
|
|
event_emitter.on_room_tombstone(room, &e).await
|
|
|
|
}
|
2020-06-24 21:56:56 +00:00
|
|
|
AnyStateEventStub::RoomJoinRules(e) => {
|
|
|
|
event_emitter.on_room_join_rules(room, &e).await
|
2020-06-20 21:18:20 +00:00
|
|
|
}
|
|
|
|
AnyStateEventStub::Custom(e) => {
|
|
|
|
event_emitter
|
|
|
|
.on_unrecognized_event(room, &CustomOrRawEvent::State(&e))
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
AnyRoomEventStub::Message(event) => match &event {
|
|
|
|
AnyMessageEventStub::RoomMessage(e) => {
|
|
|
|
event_emitter.on_room_message(room, &e).await
|
|
|
|
}
|
|
|
|
AnyMessageEventStub::RoomMessageFeedback(e) => {
|
|
|
|
event_emitter.on_room_message_feedback(room, &e).await
|
|
|
|
}
|
|
|
|
AnyMessageEventStub::RoomRedaction(e) => {
|
|
|
|
event_emitter.on_room_redaction(room, e).await
|
|
|
|
}
|
|
|
|
AnyMessageEventStub::Custom(e) => {
|
|
|
|
event_emitter
|
|
|
|
.on_unrecognized_event(room, &CustomOrRawEvent::Message(&e))
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
pub(crate) async fn emit_state_event(
|
2020-05-05 20:13:14 +00:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &AnyStateEventStub,
|
2020-05-06 23:45:27 +00:00
|
|
|
room_state: RoomStateType,
|
2020-05-05 20:13:14 +00:00
|
|
|
) {
|
2020-05-06 12:47:58 +00:00
|
|
|
let lock = self.event_emitter.read().await;
|
|
|
|
let event_emitter = if let Some(ee) = lock.as_ref() {
|
|
|
|
ee
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
let room = match room_state {
|
|
|
|
RoomStateType::Invited => {
|
|
|
|
if let Some(room) = self.get_invited_room(&room_id).await {
|
|
|
|
RoomState::Invited(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Joined => {
|
|
|
|
if let Some(room) = self.get_joined_room(&room_id).await {
|
|
|
|
RoomState::Joined(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Left => {
|
|
|
|
if let Some(room) = self.get_left_room(&room_id).await {
|
|
|
|
RoomState::Left(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 12:47:58 +00:00
|
|
|
};
|
|
|
|
|
2020-03-31 23:34:11 +00:00
|
|
|
match event {
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStateEventStub::RoomMember(member) => {
|
|
|
|
event_emitter.on_state_member(room, &member).await
|
|
|
|
}
|
|
|
|
AnyStateEventStub::RoomName(name) => event_emitter.on_state_name(room, &name).await,
|
|
|
|
AnyStateEventStub::RoomCanonicalAlias(canonical) => {
|
2020-05-06 12:47:58 +00:00
|
|
|
event_emitter
|
|
|
|
.on_state_canonical_alias(room, &canonical)
|
|
|
|
.await
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStateEventStub::RoomAliases(aliases) => {
|
2020-05-06 12:47:58 +00:00
|
|
|
event_emitter.on_state_aliases(room, &aliases).await
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStateEventStub::RoomAvatar(avatar) => {
|
|
|
|
event_emitter.on_state_avatar(room, &avatar).await
|
|
|
|
}
|
|
|
|
AnyStateEventStub::RoomPowerLevels(power) => {
|
2020-05-06 12:47:58 +00:00
|
|
|
event_emitter.on_state_power_levels(room, &power).await
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStateEventStub::RoomJoinRules(rules) => {
|
2020-05-06 12:47:58 +00:00
|
|
|
event_emitter.on_state_join_rules(room, &rules).await
|
2020-04-23 11:05:59 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStateEventStub::RoomTombstone(tomb) => {
|
|
|
|
// TODO make `on_state_tombstone` method
|
|
|
|
event_emitter.on_room_tombstone(room, &tomb).await
|
|
|
|
}
|
|
|
|
AnyStateEventStub::Custom(custom) => {
|
2020-06-01 21:02:12 +00:00
|
|
|
event_emitter
|
2020-06-20 21:18:20 +00:00
|
|
|
.on_unrecognized_event(room, &CustomOrRawEvent::State(custom))
|
2020-06-01 21:02:12 +00:00
|
|
|
.await
|
2020-05-29 21:36:58 +00:00
|
|
|
}
|
2020-03-31 23:34:11 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2020-05-05 20:13:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
pub(crate) async fn emit_stripped_state_event(
|
2020-05-06 23:45:27 +00:00
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &AnyStrippedStateEventStub,
|
2020-05-21 14:16:04 +00:00
|
|
|
prev_content: Option<MemberEventContent>,
|
2020-05-06 23:45:27 +00:00
|
|
|
room_state: RoomStateType,
|
|
|
|
) {
|
2020-05-06 12:47:58 +00:00
|
|
|
let lock = self.event_emitter.read().await;
|
|
|
|
let event_emitter = if let Some(ee) = lock.as_ref() {
|
|
|
|
ee
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
let room = match room_state {
|
|
|
|
RoomStateType::Invited => {
|
|
|
|
if let Some(room) = self.get_invited_room(&room_id).await {
|
|
|
|
RoomState::Invited(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Joined => {
|
|
|
|
if let Some(room) = self.get_joined_room(&room_id).await {
|
|
|
|
RoomState::Joined(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Left => {
|
|
|
|
if let Some(room) = self.get_left_room(&room_id).await {
|
|
|
|
RoomState::Left(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStrippedStateEventStub::RoomMember(member) => {
|
2020-05-21 14:16:04 +00:00
|
|
|
event_emitter
|
|
|
|
.on_stripped_state_member(room, &member, prev_content)
|
|
|
|
.await
|
2020-05-07 00:35:15 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStrippedStateEventStub::RoomName(name) => {
|
2020-05-07 00:35:15 +00:00
|
|
|
event_emitter.on_stripped_state_name(room, &name).await
|
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStrippedStateEventStub::RoomCanonicalAlias(canonical) => {
|
2020-05-07 00:35:15 +00:00
|
|
|
event_emitter
|
|
|
|
.on_stripped_state_canonical_alias(room, &canonical)
|
|
|
|
.await
|
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStrippedStateEventStub::RoomAliases(aliases) => {
|
2020-05-07 00:35:15 +00:00
|
|
|
event_emitter
|
|
|
|
.on_stripped_state_aliases(room, &aliases)
|
|
|
|
.await
|
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStrippedStateEventStub::RoomAvatar(avatar) => {
|
2020-05-07 00:35:15 +00:00
|
|
|
event_emitter.on_stripped_state_avatar(room, &avatar).await
|
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStrippedStateEventStub::RoomPowerLevels(power) => {
|
2020-05-07 00:35:15 +00:00
|
|
|
event_emitter
|
|
|
|
.on_stripped_state_power_levels(room, &power)
|
|
|
|
.await
|
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyStrippedStateEventStub::RoomJoinRules(rules) => {
|
2020-05-07 00:35:15 +00:00
|
|
|
event_emitter
|
|
|
|
.on_stripped_state_join_rules(room, &rules)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-05-06 23:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn emit_account_data_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-20 21:18:20 +00:00
|
|
|
event: &AnyBasicEvent,
|
2020-05-06 23:45:27 +00:00
|
|
|
room_state: RoomStateType,
|
|
|
|
) {
|
2020-05-07 00:35:15 +00:00
|
|
|
let lock = self.event_emitter.read().await;
|
|
|
|
let event_emitter = if let Some(ee) = lock.as_ref() {
|
|
|
|
ee
|
2020-05-06 12:47:58 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
let room = match room_state {
|
|
|
|
RoomStateType::Invited => {
|
|
|
|
if let Some(room) = self.get_invited_room(&room_id).await {
|
|
|
|
RoomState::Invited(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Joined => {
|
|
|
|
if let Some(room) = self.get_joined_room(&room_id).await {
|
|
|
|
RoomState::Joined(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Left => {
|
|
|
|
if let Some(room) = self.get_left_room(&room_id).await {
|
|
|
|
RoomState::Left(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-07 12:55:42 +00:00
|
|
|
match event {
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyBasicEvent::Presence(presence) => {
|
2020-06-01 11:13:57 +00:00
|
|
|
event_emitter.on_non_room_presence(room, &presence).await
|
2020-04-07 12:55:42 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyBasicEvent::IgnoredUserList(ignored) => {
|
2020-06-01 11:13:57 +00:00
|
|
|
event_emitter
|
|
|
|
.on_non_room_ignored_users(room, &ignored)
|
|
|
|
.await
|
2020-04-07 12:55:42 +00:00
|
|
|
}
|
2020-06-20 21:18:20 +00:00
|
|
|
AnyBasicEvent::PushRules(rules) => {
|
2020-06-01 11:13:57 +00:00
|
|
|
event_emitter.on_non_room_push_rules(room, &rules).await
|
2020-04-07 12:55:42 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:45:27 +00:00
|
|
|
pub(crate) async fn emit_ephemeral_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
2020-06-25 00:30:53 +00:00
|
|
|
event: &AnyEphemeralRoomEventStub,
|
2020-05-06 23:45:27 +00:00
|
|
|
room_state: RoomStateType,
|
|
|
|
) {
|
2020-05-06 12:47:58 +00:00
|
|
|
let lock = self.event_emitter.read().await;
|
|
|
|
let event_emitter = if let Some(ee) = lock.as_ref() {
|
|
|
|
ee
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2020-05-07 00:35:15 +00:00
|
|
|
let room = match room_state {
|
|
|
|
RoomStateType::Invited => {
|
|
|
|
if let Some(room) = self.get_invited_room(&room_id).await {
|
|
|
|
RoomState::Invited(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Joined => {
|
|
|
|
if let Some(room) = self.get_joined_room(&room_id).await {
|
|
|
|
RoomState::Joined(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Left => {
|
|
|
|
if let Some(room) = self.get_left_room(&room_id).await {
|
|
|
|
RoomState::Left(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 12:47:58 +00:00
|
|
|
};
|
|
|
|
|
2020-03-31 23:34:11 +00:00
|
|
|
match event {
|
2020-06-25 00:30:53 +00:00
|
|
|
AnyEphemeralRoomEventStub::FullyRead(full_read) => {
|
2020-06-20 21:18:20 +00:00
|
|
|
event_emitter.on_non_room_fully_read(room, &full_read).await
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
2020-06-25 00:30:53 +00:00
|
|
|
AnyEphemeralRoomEventStub::Typing(typing) => {
|
2020-06-20 21:18:20 +00:00
|
|
|
event_emitter.on_non_room_typing(room, &typing).await
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
2020-06-25 00:30:53 +00:00
|
|
|
AnyEphemeralRoomEventStub::Receipt(receipt) => {
|
2020-06-20 21:18:20 +00:00
|
|
|
event_emitter.on_non_room_receipt(room, &receipt).await
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-04-07 12:55:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 23:45:27 +00:00
|
|
|
pub(crate) async fn emit_presence_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
event: &PresenceEvent,
|
|
|
|
room_state: RoomStateType,
|
|
|
|
) {
|
2020-05-07 00:35:15 +00:00
|
|
|
let room = match room_state {
|
|
|
|
RoomStateType::Invited => {
|
|
|
|
if let Some(room) = self.get_invited_room(&room_id).await {
|
|
|
|
RoomState::Invited(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-07 00:35:15 +00:00
|
|
|
RoomStateType::Joined => {
|
|
|
|
if let Some(room) = self.get_joined_room(&room_id).await {
|
|
|
|
RoomState::Joined(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
2020-05-07 00:35:15 +00:00
|
|
|
}
|
|
|
|
RoomStateType::Left => {
|
|
|
|
if let Some(room) = self.get_left_room(&room_id).await {
|
|
|
|
RoomState::Left(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-07 00:35:15 +00:00
|
|
|
};
|
|
|
|
if let Some(ee) = &self.event_emitter.read().await.as_ref() {
|
|
|
|
ee.on_presence_event(room, &event).await;
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 21:36:58 +00:00
|
|
|
|
|
|
|
pub(crate) async fn emit_unrecognized_event<T>(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
event: &EventJson<T>,
|
|
|
|
room_state: RoomStateType,
|
|
|
|
) {
|
|
|
|
let room = match room_state {
|
|
|
|
RoomStateType::Invited => {
|
|
|
|
if let Some(room) = self.get_invited_room(&room_id).await {
|
|
|
|
RoomState::Invited(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Joined => {
|
|
|
|
if let Some(room) = self.get_joined_room(&room_id).await {
|
|
|
|
RoomState::Joined(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RoomStateType::Left => {
|
|
|
|
if let Some(room) = self.get_left_room(&room_id).await {
|
|
|
|
RoomState::Left(Arc::clone(&room))
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(ee) = &self.event_emitter.read().await.as_ref() {
|
2020-06-01 21:02:12 +00:00
|
|
|
ee.on_unrecognized_event(room, &CustomOrRawEvent::RawJson(event.json()))
|
|
|
|
.await;
|
2020-05-29 21:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-23 20:47:00 +00:00
|
|
|
}
|
2020-05-13 13:57:09 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::identifiers::{RoomId, UserId};
|
2020-07-04 23:34:45 +00:00
|
|
|
use crate::{BaseClient, BaseClientConfig, Session};
|
2020-07-04 23:08:15 +00:00
|
|
|
use matrix_sdk_common::events::{AnyRoomEventStub, EventJson};
|
2020-06-17 16:52:53 +00:00
|
|
|
use matrix_sdk_common_macros::async_trait;
|
2020-06-25 00:30:53 +00:00
|
|
|
use matrix_sdk_test::{async_test, test_json, EventBuilder, EventsJson};
|
2020-05-13 14:25:42 +00:00
|
|
|
use serde_json::json;
|
2020-05-13 13:57:09 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-07-04 23:08:15 +00:00
|
|
|
use tempfile::tempdir;
|
2020-05-13 13:57:09 +00:00
|
|
|
|
2020-07-04 23:34:45 +00:00
|
|
|
#[cfg(feature = "messages")]
|
|
|
|
use crate::JsonStore;
|
|
|
|
|
2020-05-13 14:25:42 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
use wasm_bindgen_test::*;
|
|
|
|
|
2020-05-22 13:23:58 +00:00
|
|
|
async fn get_client() -> BaseClient {
|
2020-05-13 13:57:09 +00:00
|
|
|
let session = Session {
|
|
|
|
access_token: "1234".to_owned(),
|
|
|
|
user_id: UserId::try_from("@example:localhost").unwrap(),
|
|
|
|
device_id: "DEVICEID".to_owned(),
|
|
|
|
};
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = BaseClient::new().unwrap();
|
|
|
|
client.restore_login(session).await.unwrap();
|
|
|
|
client
|
2020-05-13 13:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_room_id() -> RoomId {
|
|
|
|
RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()
|
|
|
|
}
|
|
|
|
|
2020-05-14 08:57:21 +00:00
|
|
|
fn member_event() -> serde_json::Value {
|
|
|
|
json!({
|
|
|
|
"content": {
|
|
|
|
"displayname": "example",
|
|
|
|
"membership": "join"
|
|
|
|
},
|
|
|
|
"event_id": "$151800140517rfvjc:localhost",
|
|
|
|
"membership": "join",
|
|
|
|
"origin_server_ts": 0,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"state_key": "@example:localhost",
|
2020-06-20 21:18:20 +00:00
|
|
|
"type": "m.room.member"
|
2020-05-14 08:57:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:57:09 +00:00
|
|
|
#[async_test]
|
|
|
|
async fn test_joined_room_creation() {
|
|
|
|
let mut sync_response = EventBuilder::default()
|
2020-06-25 00:30:53 +00:00
|
|
|
.add_state_event(EventsJson::Member)
|
2020-05-13 13:57:09 +00:00
|
|
|
.build_sync_response();
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-13 13:57:09 +00:00
|
|
|
let room_id = get_room_id();
|
|
|
|
|
2020-05-14 08:57:21 +00:00
|
|
|
let room = client.get_joined_room(&room_id).await;
|
|
|
|
assert!(room.is_none());
|
|
|
|
|
2020-05-13 13:57:09 +00:00
|
|
|
client
|
|
|
|
.receive_sync_response(&mut sync_response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-05-14 08:57:21 +00:00
|
|
|
let room = client.get_left_room(&room_id).await;
|
|
|
|
assert!(room.is_none());
|
|
|
|
|
2020-05-13 13:57:09 +00:00
|
|
|
let room = client.get_joined_room(&room_id).await;
|
|
|
|
assert!(room.is_some());
|
2020-05-14 08:57:21 +00:00
|
|
|
|
|
|
|
let mut sync_response = EventBuilder::default()
|
2020-06-25 00:30:53 +00:00
|
|
|
.add_custom_left_event(&room_id, member_event())
|
2020-05-14 08:57:21 +00:00
|
|
|
.build_sync_response();
|
|
|
|
|
|
|
|
sync_response.next_batch = "Hello".to_owned();
|
|
|
|
|
|
|
|
client
|
|
|
|
.receive_sync_response(&mut sync_response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let room = client.get_joined_room(&room_id).await;
|
|
|
|
assert!(room.is_none());
|
|
|
|
|
|
|
|
let room = client.get_left_room(&room_id).await;
|
|
|
|
assert!(room.is_some());
|
2020-05-13 13:57:09 +00:00
|
|
|
}
|
2020-05-13 14:25:42 +00:00
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
async fn test_left_room_creation() {
|
|
|
|
let room_id = RoomId::try_from("!left_room:localhost").unwrap();
|
|
|
|
let mut sync_response = EventBuilder::default()
|
2020-06-25 00:30:53 +00:00
|
|
|
.add_custom_left_event(&room_id, member_event())
|
2020-05-13 14:25:42 +00:00
|
|
|
.build_sync_response();
|
|
|
|
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-13 14:25:42 +00:00
|
|
|
|
2020-05-14 08:57:21 +00:00
|
|
|
let room = client.get_left_room(&room_id).await;
|
|
|
|
assert!(room.is_none());
|
|
|
|
|
2020-05-13 14:25:42 +00:00
|
|
|
client
|
|
|
|
.receive_sync_response(&mut sync_response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let room = client.get_left_room(&room_id).await;
|
|
|
|
assert!(room.is_some());
|
2020-05-14 08:57:21 +00:00
|
|
|
|
2020-06-25 00:30:53 +00:00
|
|
|
let mem = member_event();
|
|
|
|
|
2020-05-14 08:57:21 +00:00
|
|
|
let mut sync_response = EventBuilder::default()
|
2020-06-25 00:30:53 +00:00
|
|
|
.add_custom_joined_event(&room_id, mem)
|
2020-05-14 08:57:21 +00:00
|
|
|
.build_sync_response();
|
|
|
|
|
|
|
|
sync_response.next_batch = "Hello".to_owned();
|
|
|
|
|
|
|
|
client
|
|
|
|
.receive_sync_response(&mut sync_response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let room = client.get_left_room(&room_id).await;
|
|
|
|
assert!(room.is_none());
|
|
|
|
|
|
|
|
let room = client.get_joined_room(&room_id).await;
|
|
|
|
assert!(room.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
async fn test_invited_room_creation() {
|
|
|
|
let room_id = RoomId::try_from("!invited_room:localhost").unwrap();
|
|
|
|
let mut sync_response = EventBuilder::default()
|
2020-06-20 21:18:20 +00:00
|
|
|
.add_custom_invited_event(&room_id, member_event())
|
2020-05-14 08:57:21 +00:00
|
|
|
.build_sync_response();
|
|
|
|
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-14 08:57:21 +00:00
|
|
|
|
|
|
|
let room = client.get_invited_room(&room_id).await;
|
|
|
|
assert!(room.is_none());
|
|
|
|
|
|
|
|
client
|
|
|
|
.receive_sync_response(&mut sync_response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let room = client.get_invited_room(&room_id).await;
|
|
|
|
assert!(room.is_some());
|
|
|
|
|
|
|
|
let mut sync_response = EventBuilder::default()
|
2020-06-25 00:30:53 +00:00
|
|
|
.add_custom_joined_event(&room_id, member_event())
|
2020-05-14 08:57:21 +00:00
|
|
|
.build_sync_response();
|
|
|
|
|
|
|
|
sync_response.next_batch = "Hello".to_owned();
|
|
|
|
|
|
|
|
client
|
|
|
|
.receive_sync_response(&mut sync_response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let room = client.get_invited_room(&room_id).await;
|
|
|
|
assert!(room.is_none());
|
|
|
|
|
|
|
|
let room = client.get_joined_room(&room_id).await;
|
|
|
|
assert!(room.is_some());
|
2020-05-13 14:25:42 +00:00
|
|
|
}
|
2020-05-14 09:55:12 +00:00
|
|
|
|
2020-05-21 14:16:04 +00:00
|
|
|
#[async_test]
|
|
|
|
async fn test_prev_content_from_unsigned() {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::{EventEmitter, SyncRoom};
|
2020-06-20 21:18:20 +00:00
|
|
|
use matrix_sdk_common::events::{
|
|
|
|
room::member::{MemberEventContent, MembershipChange},
|
|
|
|
StateEventStub,
|
|
|
|
};
|
2020-05-21 14:16:04 +00:00
|
|
|
use matrix_sdk_common::locks::RwLock;
|
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EE(Arc<AtomicBool>);
|
2020-06-17 16:52:53 +00:00
|
|
|
#[async_trait]
|
2020-05-21 14:16:04 +00:00
|
|
|
impl EventEmitter for EE {
|
2020-06-20 21:18:20 +00:00
|
|
|
async fn on_room_member(
|
|
|
|
&self,
|
|
|
|
room: SyncRoom,
|
|
|
|
event: &StateEventStub<MemberEventContent>,
|
|
|
|
) {
|
2020-05-21 14:16:04 +00:00
|
|
|
if let SyncRoom::Joined(_) = room {
|
2020-06-20 21:18:20 +00:00
|
|
|
if let MembershipChange::Joined = event.membership_change() {
|
|
|
|
self.0.swap(true, Ordering::SeqCst);
|
|
|
|
}
|
2020-05-21 14:16:04 +00:00
|
|
|
}
|
|
|
|
if event.prev_content.is_none() {
|
|
|
|
self.0.swap(false, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let room_id = get_room_id();
|
2020-07-03 09:53:15 +00:00
|
|
|
let user_id = UserId::try_from("@example:localhost").unwrap();
|
|
|
|
|
2020-05-21 14:16:04 +00:00
|
|
|
let passed = Arc::new(AtomicBool::default());
|
|
|
|
let emitter = EE(Arc::clone(&passed));
|
2020-05-22 21:17:27 +00:00
|
|
|
let mut client = get_client().await;
|
2020-05-21 14:16:04 +00:00
|
|
|
|
|
|
|
client.event_emitter = Arc::new(RwLock::new(Some(Box::new(emitter))));
|
|
|
|
|
2020-07-03 09:53:15 +00:00
|
|
|
// We can't do this through `EventBuilder` since it goes through a de/ser cycle and the
|
|
|
|
// `prev_content` is lost. Luckily, this test won't be needed once ruma fixes
|
|
|
|
// `prev_content` parsing.
|
|
|
|
let join_event: serde_json::Value = serde_json::json!({
|
|
|
|
"content": {
|
|
|
|
"avatar_url": null,
|
|
|
|
"displayname": "example",
|
|
|
|
"membership": "join"
|
|
|
|
},
|
|
|
|
"event_id": "$151800140517rfvjc:localhost",
|
|
|
|
"membership": "join",
|
|
|
|
"origin_server_ts": 151800140,
|
|
|
|
"sender": user_id.as_ref(),
|
|
|
|
"state_key": user_id.as_ref(),
|
|
|
|
"type": "m.room.member",
|
|
|
|
"unsigned": {
|
|
|
|
"age": 297036,
|
|
|
|
"replaces_state": "$151800111315tsynI:localhost",
|
|
|
|
"prev_content": {
|
|
|
|
"avatar_url": null,
|
|
|
|
"displayname": "example",
|
|
|
|
"membership": "invite"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let display_name_change_event: serde_json::Value = serde_json::json!({
|
|
|
|
"content": {
|
|
|
|
"avatar_url": null,
|
|
|
|
"displayname": "changed",
|
|
|
|
"membership": "join"
|
|
|
|
},
|
|
|
|
"event_id": "$191804320221Tallh:localhost",
|
|
|
|
"membership": "join",
|
|
|
|
"origin_server_ts": 151800140,
|
|
|
|
"sender": user_id.as_ref(),
|
|
|
|
"state_key": user_id.as_ref(),
|
|
|
|
"type": "m.room.member",
|
|
|
|
"unsigned": {
|
|
|
|
"age": 297036,
|
|
|
|
"replaces_state": "$151800140517rfvjc:localhost",
|
|
|
|
"prev_content": {
|
|
|
|
"avatar_url": null,
|
|
|
|
"displayname": "example",
|
|
|
|
"membership": "join"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-06-21 18:22:28 +00:00
|
|
|
|
2020-05-22 21:12:58 +00:00
|
|
|
let mut joined_rooms: HashMap<RoomId, serde_json::Value> = HashMap::new();
|
|
|
|
let joined_room = serde_json::json!({
|
|
|
|
"summary": {},
|
|
|
|
"account_data": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"ephemeral": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"state": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"timeline": {
|
2020-07-03 09:53:15 +00:00
|
|
|
"events": vec![ join_event, display_name_change_event ],
|
2020-05-29 21:36:58 +00:00
|
|
|
"limited": true,
|
|
|
|
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1"
|
|
|
|
},
|
|
|
|
"unread_notifications": {
|
|
|
|
"highlight_count": 0,
|
|
|
|
"notification_count": 11
|
|
|
|
}
|
|
|
|
});
|
2020-07-03 09:53:15 +00:00
|
|
|
joined_rooms.insert(room_id.clone(), joined_room);
|
2020-05-29 21:36:58 +00:00
|
|
|
|
|
|
|
let empty_room: HashMap<RoomId, serde_json::Value> = HashMap::new();
|
|
|
|
let body = serde_json::json!({
|
|
|
|
"device_one_time_keys_count": {},
|
|
|
|
"next_batch": "s526_47314_0_7_1_1_1_11444_1",
|
|
|
|
"device_lists": {
|
|
|
|
"changed": [],
|
|
|
|
"left": []
|
|
|
|
},
|
|
|
|
"rooms": {
|
|
|
|
"invite": empty_room,
|
|
|
|
"join": joined_rooms,
|
|
|
|
"leave": empty_room,
|
|
|
|
},
|
|
|
|
"to_device": {
|
|
|
|
"events": []
|
|
|
|
},
|
|
|
|
"presence": {
|
|
|
|
"events": []
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let response = http::Response::builder()
|
|
|
|
.body(serde_json::to_vec(&body).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
let mut sync =
|
|
|
|
matrix_sdk_common::api::r0::sync::sync_events::Response::try_from(response).unwrap();
|
|
|
|
|
|
|
|
client.receive_sync_response(&mut sync).await.unwrap();
|
|
|
|
|
2020-07-03 09:53:15 +00:00
|
|
|
// This is a tricky test. Since we receive and emit the event separately, we have to test
|
|
|
|
// both paths.
|
|
|
|
|
|
|
|
// This first part tests that the event was received correctly (with
|
|
|
|
// `prev_content` hoisted).
|
|
|
|
//
|
|
|
|
// However, we can't simply test that the member is joined since a missing `prev_content`
|
|
|
|
// is considered to be `"membership": "invite"` by default, which would still work out
|
|
|
|
// correctly. Hence we test that his display name was changed.
|
|
|
|
let room = client.get_joined_room(&room_id).await.unwrap();
|
|
|
|
let room = room.read().await;
|
|
|
|
let member = room.joined_members.get(&user_id).unwrap();
|
|
|
|
assert_eq!(*member.display_name.as_ref().unwrap(), "changed");
|
|
|
|
|
|
|
|
// The second part tests that the event is emitted correctly. If `prev_content` was
|
2020-07-03 11:02:12 +00:00
|
|
|
// missing, this bool is reset to false.
|
2020-05-29 21:36:58 +00:00
|
|
|
assert!(passed.load(Ordering::SeqCst))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
async fn test_unrecognized_events() {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::{EventEmitter, SyncRoom};
|
|
|
|
use matrix_sdk_common::locks::RwLock;
|
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EE(Arc<AtomicBool>);
|
2020-06-17 16:52:53 +00:00
|
|
|
#[async_trait]
|
2020-05-29 21:36:58 +00:00
|
|
|
impl EventEmitter for EE {
|
2020-06-01 21:02:12 +00:00
|
|
|
async fn on_unrecognized_event(&self, room: SyncRoom, event: &CustomOrRawEvent<'_>) {
|
2020-05-29 21:36:58 +00:00
|
|
|
if let SyncRoom::Joined(_) = room {
|
2020-06-01 21:02:12 +00:00
|
|
|
if let CustomOrRawEvent::RawJson(raw) = event {
|
|
|
|
let val = serde_json::to_value(raw).unwrap();
|
|
|
|
if val.get("type").unwrap() == &json! { "m.room.message" }
|
|
|
|
&& val.get("content").unwrap().get("m.relates_to").is_some()
|
|
|
|
{
|
|
|
|
self.0.swap(true, Ordering::SeqCst);
|
|
|
|
}
|
2020-05-29 21:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let room_id = get_room_id();
|
|
|
|
let passed = Arc::new(AtomicBool::default());
|
|
|
|
let emitter = EE(Arc::clone(&passed));
|
|
|
|
let mut client = get_client().await;
|
|
|
|
|
|
|
|
client.event_emitter = Arc::new(RwLock::new(Some(Box::new(emitter))));
|
|
|
|
|
|
|
|
// This is needed other wise the `EventBuilder` goes through a de/ser cycle and the `prev_content` is lost.
|
2020-06-21 18:22:28 +00:00
|
|
|
let event: &serde_json::Value = &test_json::MESSAGE_EDIT;
|
2020-05-29 21:36:58 +00:00
|
|
|
|
|
|
|
let mut joined_rooms: HashMap<RoomId, serde_json::Value> = HashMap::new();
|
|
|
|
let joined_room = serde_json::json!({
|
|
|
|
"summary": {},
|
|
|
|
"account_data": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"ephemeral": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"state": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"timeline": {
|
|
|
|
"events": vec![ event ],
|
|
|
|
"limited": true,
|
|
|
|
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1"
|
|
|
|
},
|
|
|
|
"unread_notifications": {
|
|
|
|
"highlight_count": 0,
|
|
|
|
"notification_count": 11
|
|
|
|
}
|
|
|
|
});
|
|
|
|
joined_rooms.insert(room_id, joined_room);
|
|
|
|
|
|
|
|
let empty_room: HashMap<RoomId, serde_json::Value> = HashMap::new();
|
|
|
|
let body = serde_json::json!({
|
|
|
|
"device_one_time_keys_count": {},
|
|
|
|
"next_batch": "s526_47314_0_7_1_1_1_11444_1",
|
|
|
|
"device_lists": {
|
|
|
|
"changed": [],
|
|
|
|
"left": []
|
|
|
|
},
|
|
|
|
"rooms": {
|
|
|
|
"invite": empty_room,
|
|
|
|
"join": joined_rooms,
|
|
|
|
"leave": empty_room,
|
|
|
|
},
|
|
|
|
"to_device": {
|
|
|
|
"events": []
|
|
|
|
},
|
|
|
|
"presence": {
|
|
|
|
"events": []
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let response = http::Response::builder()
|
|
|
|
.body(serde_json::to_vec(&body).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
let mut sync =
|
|
|
|
matrix_sdk_common::api::r0::sync::sync_events::Response::try_from(response).unwrap();
|
|
|
|
|
|
|
|
client.receive_sync_response(&mut sync).await.unwrap();
|
|
|
|
|
|
|
|
assert!(passed.load(Ordering::SeqCst))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_test]
|
|
|
|
async fn test_unrecognized_custom_event() {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::{EventEmitter, SyncRoom};
|
2020-06-20 21:18:20 +00:00
|
|
|
use matrix_sdk_common::api::r0::sync::sync_events;
|
2020-05-29 21:36:58 +00:00
|
|
|
use matrix_sdk_common::locks::RwLock;
|
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EE(Arc<AtomicBool>);
|
2020-06-17 16:52:53 +00:00
|
|
|
#[async_trait]
|
2020-05-29 21:36:58 +00:00
|
|
|
impl EventEmitter for EE {
|
2020-06-01 21:02:12 +00:00
|
|
|
async fn on_unrecognized_event(&self, room: SyncRoom, event: &CustomOrRawEvent<'_>) {
|
2020-05-29 21:36:58 +00:00
|
|
|
if let SyncRoom::Joined(_) = room {
|
2020-06-20 21:18:20 +00:00
|
|
|
if let CustomOrRawEvent::Message(custom) = event {
|
|
|
|
if custom.content.event_type == "m.reaction"
|
|
|
|
&& custom.content.json.get("m.relates_to").is_some()
|
2020-06-01 21:02:12 +00:00
|
|
|
{
|
|
|
|
self.0.swap(true, Ordering::SeqCst);
|
|
|
|
}
|
2020-05-29 21:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let room_id = get_room_id();
|
|
|
|
let passed = Arc::new(AtomicBool::default());
|
|
|
|
let emitter = EE(Arc::clone(&passed));
|
|
|
|
let mut client = get_client().await;
|
|
|
|
|
|
|
|
client.event_emitter = Arc::new(RwLock::new(Some(Box::new(emitter))));
|
|
|
|
|
|
|
|
// This is needed other wise the `EventBuilder` goes through a de/ser cycle and the `prev_content` is lost.
|
2020-06-21 18:22:28 +00:00
|
|
|
let event: &serde_json::Value = &test_json::REACTION;
|
2020-05-29 21:36:58 +00:00
|
|
|
|
|
|
|
let mut joined_rooms: HashMap<RoomId, serde_json::Value> = HashMap::new();
|
|
|
|
let joined_room = serde_json::json!({
|
|
|
|
"summary": {},
|
|
|
|
"account_data": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"ephemeral": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"state": {
|
|
|
|
"events": [],
|
|
|
|
},
|
|
|
|
"timeline": {
|
|
|
|
"events": vec![ event ],
|
2020-05-22 21:12:58 +00:00
|
|
|
"limited": true,
|
|
|
|
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1"
|
|
|
|
},
|
|
|
|
"unread_notifications": {
|
|
|
|
"highlight_count": 0,
|
|
|
|
"notification_count": 11
|
2020-05-21 14:16:04 +00:00
|
|
|
}
|
2020-05-22 21:12:58 +00:00
|
|
|
});
|
|
|
|
joined_rooms.insert(room_id, joined_room);
|
|
|
|
|
|
|
|
let empty_room: HashMap<RoomId, serde_json::Value> = HashMap::new();
|
|
|
|
let body = serde_json::json!({
|
|
|
|
"device_one_time_keys_count": {},
|
|
|
|
"next_batch": "s526_47314_0_7_1_1_1_11444_1",
|
|
|
|
"device_lists": {
|
|
|
|
"changed": [],
|
|
|
|
"left": []
|
|
|
|
},
|
|
|
|
"rooms": {
|
|
|
|
"invite": empty_room,
|
|
|
|
"join": joined_rooms,
|
|
|
|
"leave": empty_room,
|
|
|
|
},
|
|
|
|
"to_device": {
|
|
|
|
"events": []
|
|
|
|
},
|
|
|
|
"presence": {
|
|
|
|
"events": []
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let response = http::Response::builder()
|
|
|
|
.body(serde_json::to_vec(&body).unwrap())
|
|
|
|
.unwrap();
|
2020-06-20 21:18:20 +00:00
|
|
|
let mut sync = sync_events::Response::try_from(response).unwrap();
|
2020-05-22 21:12:58 +00:00
|
|
|
|
|
|
|
client.receive_sync_response(&mut sync).await.unwrap();
|
2020-05-21 14:16:04 +00:00
|
|
|
|
|
|
|
assert!(passed.load(Ordering::SeqCst))
|
|
|
|
}
|
|
|
|
|
2020-07-04 23:08:15 +00:00
|
|
|
#[cfg(feature = "messages")]
|
|
|
|
#[async_test]
|
|
|
|
async fn message_queue_redaction_event_store_deser() {
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
let room_id = RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap();
|
|
|
|
|
|
|
|
let session = Session {
|
|
|
|
access_token: "1234".to_owned(),
|
|
|
|
user_id: UserId::try_from("@cheeky_monkey:matrix.org").unwrap(),
|
|
|
|
device_id: "DEVICEID".to_owned(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let _m = mockito::mock(
|
|
|
|
"GET",
|
|
|
|
mockito::Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
|
|
|
|
)
|
|
|
|
.with_status(200)
|
|
|
|
.with_body(test_json::SYNC.to_string())
|
|
|
|
.create();
|
|
|
|
|
|
|
|
let dir = tempdir().unwrap();
|
|
|
|
// a sync response to populate our JSON store
|
|
|
|
let config =
|
|
|
|
BaseClientConfig::default().state_store(Box::new(JsonStore::open(dir.path()).unwrap()));
|
|
|
|
let client = BaseClient::new_with_config(config).unwrap();
|
|
|
|
client.restore_login(session.clone()).await.unwrap();
|
|
|
|
|
|
|
|
let response = http::Response::builder()
|
|
|
|
.body(serde_json::to_vec(test_json::SYNC.deref()).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
let mut sync =
|
|
|
|
matrix_sdk_common::api::r0::sync::sync_events::Response::try_from(response).unwrap();
|
|
|
|
|
|
|
|
client.receive_sync_response(&mut sync).await.unwrap();
|
|
|
|
|
|
|
|
let json = serde_json::json!({
|
|
|
|
"content": {
|
|
|
|
"reason": "😀"
|
|
|
|
},
|
|
|
|
"event_id": "$XXXX:localhost",
|
|
|
|
"origin_server_ts": 151957878,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"type": "m.room.redaction",
|
|
|
|
"redacts": "$152037280074GZeOm:localhost"
|
|
|
|
});
|
|
|
|
let mut event: EventJson<AnyRoomEventStub> = serde_json::from_value(json).unwrap();
|
|
|
|
client
|
|
|
|
.receive_joined_timeline_event(&room_id, &mut event)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// check that the message has actually been redacted
|
|
|
|
for room in client.joined_rooms().read().await.values() {
|
|
|
|
let queue = &room.read().await.messages;
|
|
|
|
if let crate::events::AnyMessageEventContent::RoomRedaction(content) =
|
|
|
|
&queue.msgs[0].content
|
|
|
|
{
|
|
|
|
assert_eq!(content.reason, Some("😀".to_string()));
|
|
|
|
} else {
|
|
|
|
panic!("[pre store sync] message event in message queue should be redacted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `receive_joined_timeline_event` does not save the state to the store so we must
|
|
|
|
client.store_room_state(&room_id).await.unwrap();
|
|
|
|
|
|
|
|
// we load state from the store only
|
|
|
|
let config =
|
|
|
|
BaseClientConfig::default().state_store(Box::new(JsonStore::open(dir.path()).unwrap()));
|
|
|
|
let client = BaseClient::new_with_config(config).unwrap();
|
|
|
|
client.restore_login(session).await.unwrap();
|
|
|
|
|
|
|
|
// make sure that our redacted message event is redacted and that ser/de works
|
|
|
|
// properly
|
|
|
|
for room in client.joined_rooms().read().await.values() {
|
|
|
|
let queue = &room.read().await.messages;
|
|
|
|
if let crate::events::AnyMessageEventContent::RoomRedaction(content) =
|
|
|
|
&queue.msgs[0].content
|
|
|
|
{
|
|
|
|
assert_eq!(content.reason, Some("😀".to_string()));
|
|
|
|
} else {
|
|
|
|
panic!("[post store sync] message event in message queue should be redacted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 09:55:12 +00:00
|
|
|
#[async_test]
|
2020-05-14 12:01:16 +00:00
|
|
|
#[cfg(feature = "encryption")]
|
2020-05-14 09:55:12 +00:00
|
|
|
async fn test_group_session_invalidation() {
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-14 09:55:12 +00:00
|
|
|
let room_id = get_room_id();
|
|
|
|
|
|
|
|
let mut sync_response = EventBuilder::default()
|
2020-06-25 00:30:53 +00:00
|
|
|
.add_state_event(EventsJson::Member)
|
2020-05-14 09:55:12 +00:00
|
|
|
.build_sync_response();
|
|
|
|
|
|
|
|
client
|
|
|
|
.receive_sync_response(&mut sync_response)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(client.should_share_group_session(&room_id).await);
|
|
|
|
let _ = client.share_group_session(&room_id).await.unwrap();
|
|
|
|
assert!(!client.should_share_group_session(&room_id).await);
|
|
|
|
client.invalidate_group_session(&room_id).await;
|
|
|
|
}
|
2020-05-13 13:57:09 +00:00
|
|
|
}
|