2020-03-28 10:58:30 +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-23 08:52:47 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2020-04-03 13:07:40 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-03-28 10:58:30 +00:00
|
|
|
|
2020-05-07 01:17:21 +00:00
|
|
|
#[cfg(feature = "messages")]
|
2020-04-29 11:58:31 +00:00
|
|
|
use super::message::MessageQueue;
|
2020-04-03 13:07:40 +00:00
|
|
|
use super::RoomMember;
|
2020-03-29 19:54:26 +00:00
|
|
|
|
2020-05-07 19:21:06 +00:00
|
|
|
use crate::api::r0::sync::sync_events::{RoomSummary, UnreadNotificationsCount};
|
2020-03-28 10:58:30 +00:00
|
|
|
use crate::events::collections::all::{RoomEvent, StateEvent};
|
2020-03-31 23:34:11 +00:00
|
|
|
use crate::events::presence::PresenceEvent;
|
2020-03-28 10:58:30 +00:00
|
|
|
use crate::events::room::{
|
|
|
|
aliases::AliasesEvent,
|
|
|
|
canonical_alias::CanonicalAliasEvent,
|
2020-04-01 14:06:28 +00:00
|
|
|
encryption::EncryptionEvent,
|
2020-03-29 12:05:40 +00:00
|
|
|
member::{MemberEvent, MembershipChange},
|
2020-03-28 10:58:30 +00:00
|
|
|
name::NameEvent,
|
2020-04-05 16:41:00 +00:00
|
|
|
power_levels::{NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent},
|
2020-04-23 10:45:00 +00:00
|
|
|
tombstone::TombstoneEvent,
|
2020-03-28 10:58:30 +00:00
|
|
|
};
|
2020-05-07 00:46:06 +00:00
|
|
|
use crate::events::stripped::{AnyStrippedStateEvent, StrippedRoomName};
|
2020-05-14 11:58:09 +00:00
|
|
|
use crate::events::{Algorithm, EventType};
|
2020-05-07 01:17:21 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "messages")]
|
|
|
|
use crate::events::room::message::MessageEvent;
|
|
|
|
|
2020-04-03 13:14:46 +00:00
|
|
|
use crate::identifiers::{RoomAliasId, RoomId, UserId};
|
2020-03-28 10:58:30 +00:00
|
|
|
|
2020-04-29 08:40:27 +00:00
|
|
|
use crate::js_int::{Int, UInt};
|
2020-04-16 14:02:59 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
|
2020-04-29 11:58:31 +00:00
|
|
|
#[cfg_attr(test, derive(Clone))]
|
2020-03-28 10:58:30 +00:00
|
|
|
/// `RoomName` allows the calculation of a text room name.
|
|
|
|
pub struct RoomName {
|
|
|
|
/// The displayed name of the room.
|
|
|
|
name: Option<String>,
|
|
|
|
/// The canonical alias of the room ex. `#room-name:example.com` and port number.
|
|
|
|
canonical_alias: Option<RoomAliasId>,
|
|
|
|
/// List of `RoomAliasId`s the room has been given.
|
|
|
|
aliases: Vec<RoomAliasId>,
|
2020-04-17 01:00:50 +00:00
|
|
|
/// Users which can be used to generate a room name if the room does not have
|
|
|
|
/// one. Required if room name or canonical aliases are not set or empty.
|
|
|
|
pub heroes: Vec<String>,
|
|
|
|
/// Number of users whose membership status is `join`.
|
|
|
|
/// Required if field has changed since last sync; otherwise, it may be
|
|
|
|
/// omitted.
|
|
|
|
pub joined_member_count: Option<UInt>,
|
|
|
|
/// Number of users whose membership status is `invite`.
|
|
|
|
/// Required if field has changed since last sync; otherwise, it may be
|
|
|
|
/// omitted.
|
|
|
|
pub invited_member_count: Option<UInt>,
|
2020-03-28 10:58:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 14:02:59 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
2020-04-29 11:58:31 +00:00
|
|
|
#[cfg_attr(test, derive(Clone))]
|
2020-04-05 16:41:00 +00:00
|
|
|
pub struct PowerLevels {
|
|
|
|
/// The level required to ban a user.
|
|
|
|
pub ban: Int,
|
|
|
|
/// The level required to send specific event types.
|
|
|
|
///
|
|
|
|
/// This is a mapping from event type to power level required.
|
2020-04-23 08:52:47 +00:00
|
|
|
pub events: BTreeMap<EventType, Int>,
|
2020-04-05 16:41:00 +00:00
|
|
|
/// The default level required to send message events.
|
|
|
|
pub events_default: Int,
|
|
|
|
/// The level required to invite a user.
|
|
|
|
pub invite: Int,
|
|
|
|
/// The level required to kick a user.
|
|
|
|
pub kick: Int,
|
|
|
|
/// The level required to redact an event.
|
|
|
|
pub redact: Int,
|
|
|
|
/// The default level required to send state events.
|
|
|
|
pub state_default: Int,
|
|
|
|
/// The default power level for every user in the room.
|
|
|
|
pub users_default: Int,
|
|
|
|
/// The power level requirements for specific notification types.
|
|
|
|
///
|
|
|
|
/// This is a mapping from `key` to power level for that notifications key.
|
|
|
|
pub notifications: Int,
|
|
|
|
}
|
|
|
|
|
2020-05-14 11:58:09 +00:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
|
|
|
/// Encryption info of the room.
|
|
|
|
pub struct EncryptionInfo {
|
|
|
|
/// The encryption algorithm that should be used to encrypt messages in the
|
|
|
|
/// room.
|
|
|
|
algorithm: Algorithm,
|
|
|
|
/// How long should a session be used before it is rotated.
|
|
|
|
rotation_period_ms: u64,
|
|
|
|
/// The maximum amount of messages that should be encrypted using the same
|
|
|
|
/// session.
|
|
|
|
rotation_period_messages: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EncryptionInfo {
|
|
|
|
/// The encryption algorithm that should be used to encrypt messages in the
|
|
|
|
/// room.
|
|
|
|
pub fn algorithm(&self) -> &Algorithm {
|
|
|
|
&self.algorithm
|
|
|
|
}
|
|
|
|
|
|
|
|
/// How long should a session be used before it is rotated.
|
|
|
|
pub fn rotation_period(&self) -> u64 {
|
|
|
|
self.rotation_period_ms
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The maximum amount of messages that should be encrypted using the same
|
|
|
|
/// session.
|
|
|
|
pub fn rotation_period_messages(&self) -> u64 {
|
|
|
|
self.rotation_period_messages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&EncryptionEvent> for EncryptionInfo {
|
|
|
|
fn from(event: &EncryptionEvent) -> Self {
|
|
|
|
EncryptionInfo {
|
|
|
|
algorithm: event.content.algorithm.clone(),
|
|
|
|
rotation_period_ms: event
|
|
|
|
.content
|
|
|
|
.rotation_period_ms
|
2020-05-14 12:52:32 +00:00
|
|
|
.map_or(604_800_000, Into::into),
|
2020-05-14 11:58:09 +00:00
|
|
|
rotation_period_messages: event.content.rotation_period_msgs.map_or(100, Into::into),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 00:02:36 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
2020-04-29 11:58:31 +00:00
|
|
|
#[cfg_attr(test, derive(Clone))]
|
2020-04-23 10:45:00 +00:00
|
|
|
pub struct Tombstone {
|
|
|
|
/// A server-defined message.
|
|
|
|
body: String,
|
|
|
|
/// The room that is now active.
|
|
|
|
replacement: RoomId,
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:02:59 +00:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2020-04-29 11:58:31 +00:00
|
|
|
#[cfg_attr(test, derive(Clone))]
|
2020-04-23 10:45:00 +00:00
|
|
|
/// A Matrix room.
|
2020-03-28 10:58:30 +00:00
|
|
|
pub struct Room {
|
|
|
|
/// The unique id of the room.
|
2020-04-03 13:07:40 +00:00
|
|
|
pub room_id: RoomId,
|
2020-03-28 10:58:30 +00:00
|
|
|
/// The name of the room, clients use this to represent a room.
|
|
|
|
pub room_name: RoomName,
|
|
|
|
/// The mxid of our own user.
|
|
|
|
pub own_user_id: UserId,
|
|
|
|
/// The mxid of the room creator.
|
|
|
|
pub creator: Option<UserId>,
|
|
|
|
/// The map of room members.
|
|
|
|
pub members: HashMap<UserId, RoomMember>,
|
2020-04-30 10:28:36 +00:00
|
|
|
/// A queue of messages, holds no more than 10 of the most recent messages.
|
2020-04-29 11:58:31 +00:00
|
|
|
///
|
|
|
|
/// This is helpful when using a `StateStore` to avoid multiple requests
|
|
|
|
/// to the server for messages.
|
2020-05-07 01:17:21 +00:00
|
|
|
#[cfg(feature = "messages")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
|
2020-04-29 11:58:31 +00:00
|
|
|
#[serde(with = "super::message::ser_deser")]
|
|
|
|
pub messages: MessageQueue,
|
2020-03-28 10:58:30 +00:00
|
|
|
/// A list of users that are currently typing.
|
|
|
|
pub typing_users: Vec<UserId>,
|
2020-04-05 16:41:00 +00:00
|
|
|
/// The power level requirements for specific actions in this room
|
|
|
|
pub power_levels: Option<PowerLevels>,
|
2020-05-14 11:58:09 +00:00
|
|
|
/// Optional encryption info, will be `Some` if the room is encrypted.
|
|
|
|
pub encrypted: Option<EncryptionInfo>,
|
2020-03-29 19:54:26 +00:00
|
|
|
/// Number of unread notifications with highlight flag set.
|
|
|
|
pub unread_highlight: Option<UInt>,
|
|
|
|
/// Number of unread notifications.
|
|
|
|
pub unread_notifications: Option<UInt>,
|
2020-04-23 10:45:00 +00:00
|
|
|
/// The tombstone state of this room.
|
|
|
|
pub tombstone: Option<Tombstone>,
|
2020-03-28 10:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RoomName {
|
|
|
|
pub fn push_alias(&mut self, alias: RoomAliasId) -> bool {
|
|
|
|
self.aliases.push(alias);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_canonical(&mut self, alias: RoomAliasId) -> bool {
|
|
|
|
self.canonical_alias = Some(alias);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_name(&mut self, name: &str) -> bool {
|
|
|
|
self.name = Some(name.to_string());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-04-17 01:00:50 +00:00
|
|
|
pub fn calculate_name(&self, members: &HashMap<UserId, RoomMember>) -> String {
|
2020-03-31 10:57:29 +00:00
|
|
|
// https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room.
|
2020-03-28 10:58:30 +00:00
|
|
|
// the order in which we check for a name ^^
|
|
|
|
if let Some(name) = &self.name {
|
2020-04-23 10:53:34 +00:00
|
|
|
let name = name.trim();
|
|
|
|
name.to_string()
|
2020-03-28 10:58:30 +00:00
|
|
|
} else if let Some(alias) = &self.canonical_alias {
|
2020-04-23 10:53:34 +00:00
|
|
|
let alias = alias.alias().trim();
|
|
|
|
alias.to_string()
|
|
|
|
} else if !self.aliases.is_empty() && !self.aliases[0].alias().is_empty() {
|
|
|
|
self.aliases[0].alias().trim().to_string()
|
2020-03-28 10:58:30 +00:00
|
|
|
} else {
|
2020-04-27 10:12:45 +00:00
|
|
|
let joined = self.joined_member_count.unwrap_or(UInt::MIN);
|
|
|
|
let invited = self.invited_member_count.unwrap_or(UInt::MIN);
|
2020-04-17 01:00:50 +00:00
|
|
|
let heroes = UInt::new(self.heroes.len() as u64).unwrap();
|
|
|
|
let one = UInt::new(1).unwrap();
|
2020-03-28 10:58:30 +00:00
|
|
|
|
2020-04-27 10:12:45 +00:00
|
|
|
let invited_joined = if invited + joined == UInt::MIN {
|
|
|
|
UInt::MIN
|
2020-04-23 10:53:34 +00:00
|
|
|
} else {
|
|
|
|
invited + joined - one
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO this should use `self.heroes but it is always empty??
|
|
|
|
if heroes >= invited_joined {
|
|
|
|
let mut names = members
|
|
|
|
.values()
|
|
|
|
.take(3)
|
2020-04-23 13:55:59 +00:00
|
|
|
.map(|mem| {
|
|
|
|
mem.display_name
|
|
|
|
.clone()
|
2020-04-27 10:12:51 +00:00
|
|
|
.unwrap_or_else(|| mem.user_id.localpart().to_string())
|
2020-04-23 13:55:59 +00:00
|
|
|
})
|
2020-04-23 10:53:34 +00:00
|
|
|
.collect::<Vec<String>>();
|
2020-04-23 11:05:59 +00:00
|
|
|
// stabilize ordering
|
2020-03-28 10:58:30 +00:00
|
|
|
names.sort();
|
|
|
|
names.join(", ")
|
2020-04-23 10:53:34 +00:00
|
|
|
} else if heroes < invited_joined && invited + joined > one {
|
|
|
|
let mut names = members
|
|
|
|
.values()
|
|
|
|
.take(3)
|
2020-04-23 13:55:59 +00:00
|
|
|
.map(|mem| {
|
|
|
|
mem.display_name
|
|
|
|
.clone()
|
2020-04-27 10:12:51 +00:00
|
|
|
.unwrap_or_else(|| mem.user_id.localpart().to_string())
|
2020-04-23 13:55:59 +00:00
|
|
|
})
|
2020-04-23 10:53:34 +00:00
|
|
|
.collect::<Vec<String>>();
|
2020-04-17 01:00:50 +00:00
|
|
|
names.sort();
|
2020-05-05 20:13:14 +00:00
|
|
|
// TODO what length does the spec want us to use here and in the `else`
|
2020-04-17 01:00:50 +00:00
|
|
|
format!("{}, and {} others", names.join(", "), (joined + invited))
|
|
|
|
} else {
|
|
|
|
format!("Empty Room (was {} others)", members.len())
|
2020-03-28 10:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Room {
|
|
|
|
/// Create a new room.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The unique id of the room.
|
|
|
|
///
|
|
|
|
/// * `own_user_id` - The mxid of our own user.
|
2020-04-03 13:07:40 +00:00
|
|
|
pub fn new(room_id: &RoomId, own_user_id: &UserId) -> Self {
|
2020-03-28 10:58:30 +00:00
|
|
|
Room {
|
2020-04-03 13:07:40 +00:00
|
|
|
room_id: room_id.clone(),
|
2020-03-28 10:58:30 +00:00
|
|
|
room_name: RoomName::default(),
|
2020-04-03 13:07:40 +00:00
|
|
|
own_user_id: own_user_id.clone(),
|
2020-03-28 10:58:30 +00:00
|
|
|
creator: None,
|
|
|
|
members: HashMap::new(),
|
2020-05-07 01:17:21 +00:00
|
|
|
#[cfg(feature = "messages")]
|
2020-04-29 11:58:31 +00:00
|
|
|
messages: MessageQueue::new(),
|
2020-03-28 10:58:30 +00:00
|
|
|
typing_users: Vec::new(),
|
2020-04-05 16:41:00 +00:00
|
|
|
power_levels: None,
|
2020-05-14 11:58:09 +00:00
|
|
|
encrypted: None,
|
2020-03-29 19:54:26 +00:00
|
|
|
unread_highlight: None,
|
|
|
|
unread_notifications: None,
|
2020-04-23 10:45:00 +00:00
|
|
|
tombstone: None,
|
2020-03-28 10:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 22:22:52 +00:00
|
|
|
/// Return the display name of the room.
|
2020-05-07 10:51:53 +00:00
|
|
|
pub fn display_name(&self) -> String {
|
2020-04-17 01:00:50 +00:00
|
|
|
self.room_name.calculate_name(&self.members)
|
2020-03-31 19:28:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 13:37:00 +00:00
|
|
|
/// Is the room a encrypted room.
|
|
|
|
pub fn is_encrypted(&self) -> bool {
|
2020-05-14 11:58:09 +00:00
|
|
|
self.encrypted.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the encryption info if any of the room.
|
|
|
|
///
|
|
|
|
/// Returns None if the room is not encrypted.
|
|
|
|
pub fn encryption_info(&self) -> Option<&EncryptionInfo> {
|
|
|
|
self.encrypted.as_ref()
|
2020-04-01 13:37:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 10:58:30 +00:00
|
|
|
fn add_member(&mut self, event: &MemberEvent) -> bool {
|
2020-04-03 13:14:46 +00:00
|
|
|
if self
|
|
|
|
.members
|
|
|
|
.contains_key(&UserId::try_from(event.state_key.as_str()).unwrap())
|
|
|
|
{
|
2020-03-28 10:58:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let member = RoomMember::new(event);
|
2020-04-05 16:41:00 +00:00
|
|
|
|
2020-04-03 13:14:46 +00:00
|
|
|
self.members
|
|
|
|
.insert(UserId::try_from(event.state_key.as_str()).unwrap(), member);
|
2020-03-28 10:58:30 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add to the list of `RoomAliasId`s.
|
2020-04-03 13:07:40 +00:00
|
|
|
fn push_room_alias(&mut self, alias: &RoomAliasId) -> bool {
|
2020-03-28 10:58:30 +00:00
|
|
|
self.room_name.push_alias(alias.clone());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RoomAliasId is `#alias:hostname` and `port`
|
|
|
|
fn canonical_alias(&mut self, alias: &RoomAliasId) -> bool {
|
|
|
|
self.room_name.set_canonical(alias.clone());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-04-23 13:55:59 +00:00
|
|
|
fn set_room_name(&mut self, name: &str) -> bool {
|
2020-03-28 10:58:30 +00:00
|
|
|
self.room_name.set_name(name);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:41:00 +00:00
|
|
|
fn set_room_power_level(&mut self, event: &PowerLevelsEvent) -> bool {
|
|
|
|
let PowerLevelsEventContent {
|
|
|
|
ban,
|
|
|
|
events,
|
|
|
|
events_default,
|
|
|
|
invite,
|
|
|
|
kick,
|
|
|
|
redact,
|
|
|
|
state_default,
|
|
|
|
users_default,
|
|
|
|
notifications: NotificationPowerLevels { room },
|
|
|
|
..
|
|
|
|
} = &event.content;
|
|
|
|
|
|
|
|
let power = PowerLevels {
|
|
|
|
ban: *ban,
|
|
|
|
events: events.clone(),
|
|
|
|
events_default: *events_default,
|
|
|
|
invite: *invite,
|
|
|
|
kick: *kick,
|
|
|
|
redact: *redact,
|
|
|
|
state_default: *state_default,
|
|
|
|
users_default: *users_default,
|
|
|
|
notifications: *room,
|
|
|
|
};
|
|
|
|
self.power_levels = Some(power);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-04-17 01:00:50 +00:00
|
|
|
pub(crate) fn set_room_summary(&mut self, summary: &RoomSummary) {
|
|
|
|
let RoomSummary {
|
|
|
|
heroes,
|
|
|
|
joined_member_count,
|
|
|
|
invited_member_count,
|
|
|
|
} = summary;
|
|
|
|
self.room_name.heroes = heroes.clone();
|
2020-04-23 10:45:00 +00:00
|
|
|
self.room_name.invited_member_count = *invited_member_count;
|
|
|
|
self.room_name.joined_member_count = *joined_member_count;
|
2020-04-17 01:00:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 19:21:06 +00:00
|
|
|
pub(crate) fn set_unread_notice_count(&mut self, notifications: &UnreadNotificationsCount) {
|
|
|
|
self.unread_highlight = notifications.highlight_count;
|
|
|
|
self.unread_notifications = notifications.notification_count;
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:54:26 +00:00
|
|
|
/// Handle a room.member updating the room state if necessary.
|
|
|
|
///
|
|
|
|
/// Returns true if the joined member list changed, false otherwise.
|
|
|
|
pub fn handle_membership(&mut self, event: &MemberEvent) -> bool {
|
2020-05-21 14:27:29 +00:00
|
|
|
// TODO this would not be handled correctly as all the MemberEvents have the `prev_content`
|
|
|
|
// inside of `unsigned` field
|
2020-03-29 19:54:26 +00:00
|
|
|
match event.membership_change() {
|
|
|
|
MembershipChange::Invited | MembershipChange::Joined => self.add_member(event),
|
|
|
|
_ => {
|
2020-04-05 12:23:59 +00:00
|
|
|
let user = if let Ok(id) = UserId::try_from(event.state_key.as_str()) {
|
|
|
|
id
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
2020-04-05 16:41:00 +00:00
|
|
|
if let Some(member) = self.members.get_mut(&user) {
|
2020-03-29 19:54:26 +00:00
|
|
|
member.update_member(event)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 11:58:31 +00:00
|
|
|
/// Handle a room.message event and update the `MessageQueue` if necessary.
|
|
|
|
///
|
|
|
|
/// Returns true if `MessageQueue` was added to.
|
2020-05-07 01:17:21 +00:00
|
|
|
#[cfg(feature = "messages")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
|
2020-04-29 11:58:31 +00:00
|
|
|
pub fn handle_message(&mut self, event: &MessageEvent) -> bool {
|
|
|
|
self.messages.push(event.clone())
|
|
|
|
}
|
|
|
|
|
2020-03-28 10:58:30 +00:00
|
|
|
/// Handle a room.aliases event, updating the room state if necessary.
|
2020-03-28 12:58:02 +00:00
|
|
|
///
|
2020-03-28 10:58:30 +00:00
|
|
|
/// Returns true if the room name changed, false otherwise.
|
|
|
|
pub fn handle_room_aliases(&mut self, event: &AliasesEvent) -> bool {
|
|
|
|
match event.content.aliases.as_slice() {
|
2020-04-03 13:07:40 +00:00
|
|
|
[alias] => self.push_room_alias(alias),
|
|
|
|
[alias, ..] => self.push_room_alias(alias),
|
2020-03-28 10:58:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a room.canonical_alias event, updating the room state if necessary.
|
2020-03-28 12:58:02 +00:00
|
|
|
///
|
2020-03-28 10:58:30 +00:00
|
|
|
/// Returns true if the room name changed, false otherwise.
|
|
|
|
pub fn handle_canonical(&mut self, event: &CanonicalAliasEvent) -> bool {
|
|
|
|
match &event.content.alias {
|
|
|
|
Some(name) => self.canonical_alias(&name),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a room.name event, updating the room state if necessary.
|
2020-03-28 12:58:02 +00:00
|
|
|
///
|
2020-03-28 10:58:30 +00:00
|
|
|
/// Returns true if the room name changed, false otherwise.
|
|
|
|
pub fn handle_room_name(&mut self, event: &NameEvent) -> bool {
|
|
|
|
match event.content.name() {
|
2020-04-23 13:55:59 +00:00
|
|
|
Some(name) => self.set_room_name(name),
|
2020-03-28 10:58:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 00:46:06 +00:00
|
|
|
/// Handle a room.name event, updating the room state if necessary.
|
|
|
|
///
|
|
|
|
/// Returns true if the room name changed, false otherwise.
|
|
|
|
pub fn handle_stripped_room_name(&mut self, event: &StrippedRoomName) -> bool {
|
|
|
|
match event.content.name() {
|
|
|
|
Some(name) => self.set_room_name(name),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 19:05:37 +00:00
|
|
|
/// Handle a room.power_levels event, updating the room state if necessary.
|
|
|
|
///
|
|
|
|
/// Returns true if the room name changed, false otherwise.
|
|
|
|
pub fn handle_power_level(&mut self, event: &PowerLevelsEvent) -> bool {
|
2020-04-05 16:41:00 +00:00
|
|
|
// NOTE: this is always true, we assume that if we get an event their is an update.
|
|
|
|
let mut updated = self.set_room_power_level(event);
|
|
|
|
|
|
|
|
let mut max_power = event.content.users_default;
|
|
|
|
for power in event.content.users.values() {
|
|
|
|
max_power = *power.max(&max_power);
|
2020-03-28 19:05:37 +00:00
|
|
|
}
|
2020-04-05 16:41:00 +00:00
|
|
|
|
|
|
|
for user in event.content.users.keys() {
|
|
|
|
if let Some(member) = self.members.get_mut(user) {
|
|
|
|
if member.update_power(event, max_power) {
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updated
|
2020-03-28 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 10:45:00 +00:00
|
|
|
fn handle_tombstone(&mut self, event: &TombstoneEvent) -> bool {
|
|
|
|
self.tombstone = Some(Tombstone {
|
|
|
|
body: event.content.body.clone(),
|
|
|
|
replacement: event.content.replacement_room.clone(),
|
|
|
|
});
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-05-14 11:58:09 +00:00
|
|
|
fn handle_encryption_event(&mut self, event: &EncryptionEvent) -> bool {
|
|
|
|
self.encrypted = Some(event.into());
|
2020-04-01 14:06:28 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-03-28 10:58:30 +00:00
|
|
|
/// Receive a timeline event for this room and update the room state.
|
|
|
|
///
|
|
|
|
/// Returns true if the joined member list changed, false otherwise.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The event of the room.
|
|
|
|
pub fn receive_timeline_event(&mut self, event: &RoomEvent) -> bool {
|
|
|
|
match event {
|
|
|
|
// update to the current members of the room
|
2020-04-29 11:58:31 +00:00
|
|
|
RoomEvent::RoomMember(member) => self.handle_membership(member),
|
2020-04-05 12:23:59 +00:00
|
|
|
// finds all events related to the name of the room for later use
|
2020-04-29 11:58:31 +00:00
|
|
|
RoomEvent::RoomName(name) => self.handle_room_name(name),
|
|
|
|
RoomEvent::RoomCanonicalAlias(c_alias) => self.handle_canonical(c_alias),
|
|
|
|
RoomEvent::RoomAliases(alias) => self.handle_room_aliases(alias),
|
2020-03-28 10:58:30 +00:00
|
|
|
// power levels of the room members
|
2020-04-29 11:58:31 +00:00
|
|
|
RoomEvent::RoomPowerLevels(power) => self.handle_power_level(power),
|
|
|
|
RoomEvent::RoomTombstone(tomb) => self.handle_tombstone(tomb),
|
|
|
|
RoomEvent::RoomEncryption(encrypt) => self.handle_encryption_event(encrypt),
|
2020-05-07 01:17:21 +00:00
|
|
|
#[cfg(feature = "messages")]
|
2020-04-29 11:58:31 +00:00
|
|
|
RoomEvent::RoomMessage(msg) => self.handle_message(msg),
|
2020-03-28 10:58:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a state event for this room and update the room state.
|
|
|
|
///
|
2020-04-07 12:55:42 +00:00
|
|
|
/// Returns true if the state of the `Room` has changed, false otherwise.
|
2020-03-28 10:58:30 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The event of the room.
|
|
|
|
pub fn receive_state_event(&mut self, event: &StateEvent) -> bool {
|
|
|
|
match event {
|
2020-04-29 11:58:31 +00:00
|
|
|
// update to the current members of the room
|
|
|
|
StateEvent::RoomMember(member) => self.handle_membership(member),
|
|
|
|
// finds all events related to the name of the room for later use
|
|
|
|
StateEvent::RoomName(name) => self.handle_room_name(name),
|
|
|
|
StateEvent::RoomCanonicalAlias(c_alias) => self.handle_canonical(c_alias),
|
|
|
|
StateEvent::RoomAliases(alias) => self.handle_room_aliases(alias),
|
|
|
|
// power levels of the room members
|
|
|
|
StateEvent::RoomPowerLevels(power) => self.handle_power_level(power),
|
|
|
|
StateEvent::RoomTombstone(tomb) => self.handle_tombstone(tomb),
|
|
|
|
StateEvent::RoomEncryption(encrypt) => self.handle_encryption_event(encrypt),
|
2020-03-28 10:58:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 20:13:14 +00:00
|
|
|
/// Receive a stripped state event for this room and update the room state.
|
|
|
|
///
|
|
|
|
/// Returns true if the state of the `Room` has changed, false otherwise.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event` - The `AnyStrippedStateEvent` sent by the server for invited but not
|
|
|
|
/// joined rooms.
|
2020-05-07 00:46:06 +00:00
|
|
|
pub fn receive_stripped_state_event(&mut self, event: &AnyStrippedStateEvent) -> bool {
|
|
|
|
match event {
|
|
|
|
AnyStrippedStateEvent::RoomName(n) => self.handle_stripped_room_name(n),
|
|
|
|
_ => false,
|
|
|
|
}
|
2020-05-05 20:13:14 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 10:58:30 +00:00
|
|
|
/// Receive a presence event from an `IncomingResponse` and updates the client state.
|
|
|
|
///
|
2020-05-13 08:06:08 +00:00
|
|
|
/// This will only update the user if found in the current room looped through
|
|
|
|
/// by `Client::sync`.
|
2020-03-29 12:05:40 +00:00
|
|
|
/// Returns true if the specific users presence has changed, false otherwise.
|
2020-03-28 10:58:30 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2020-03-29 12:05:40 +00:00
|
|
|
/// * `event` - The presence event for a specified room member.
|
2020-03-28 10:58:30 +00:00
|
|
|
pub fn receive_presence_event(&mut self, event: &PresenceEvent) -> bool {
|
2020-04-05 12:28:25 +00:00
|
|
|
if let Some(member) = self.members.get_mut(&event.sender) {
|
|
|
|
if member.did_update_presence(event) {
|
2020-03-28 10:58:30 +00:00
|
|
|
false
|
|
|
|
} else {
|
2020-04-05 12:28:25 +00:00
|
|
|
member.update_presence(event);
|
2020-03-28 10:58:30 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// this is probably an error as we have a `PresenceEvent` for a user
|
2020-03-29 12:05:40 +00:00
|
|
|
// we don't know about
|
2020-03-28 10:58:30 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 13:01:48 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-04-03 13:07:40 +00:00
|
|
|
use super::*;
|
2020-05-14 11:58:09 +00:00
|
|
|
use crate::events::{
|
|
|
|
room::{encryption::EncryptionEventContent, member::MembershipState},
|
|
|
|
UnsignedData,
|
|
|
|
};
|
|
|
|
use crate::identifiers::{EventId, UserId};
|
2020-05-11 08:43:21 +00:00
|
|
|
use crate::{BaseClient, Session};
|
2020-05-08 14:12:21 +00:00
|
|
|
use matrix_sdk_test::{async_test, sync_response, EventBuilder, EventsFile, SyncResponseFile};
|
2020-03-31 13:01:48 +00:00
|
|
|
|
2020-05-14 11:58:09 +00:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
use wasm_bindgen_test::*;
|
2020-03-31 13:01:48 +00:00
|
|
|
|
|
|
|
use std::convert::TryFrom;
|
2020-04-05 16:41:00 +00:00
|
|
|
use std::ops::Deref;
|
2020-05-07 10:51:53 +00:00
|
|
|
|
2020-05-22 13:23:58 +00:00
|
|
|
async fn get_client() -> BaseClient {
|
2020-03-31 13:01:48 +00:00
|
|
|
let session = Session {
|
|
|
|
access_token: "1234".to_owned(),
|
2020-03-31 19:18:27 +00:00
|
|
|
user_id: UserId::try_from("@example:localhost").unwrap(),
|
2020-03-31 13:01:48 +00:00
|
|
|
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-07 14:22:18 +00:00
|
|
|
}
|
2020-03-31 13:01:48 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
fn get_room_id() -> RoomId {
|
|
|
|
RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()
|
|
|
|
}
|
2020-03-31 13:01:48 +00:00
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
#[async_test]
|
2020-05-07 14:22:18 +00:00
|
|
|
async fn user_presence() {
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-07 14:22:18 +00:00
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
let mut response = sync_response(SyncResponseFile::Default);
|
2020-03-31 13:01:48 +00:00
|
|
|
|
2020-05-07 10:51:53 +00:00
|
|
|
client.receive_sync_response(&mut response).await.unwrap();
|
2020-03-31 13:01:48 +00:00
|
|
|
|
2020-05-07 10:51:53 +00:00
|
|
|
let rooms_lock = &client.joined_rooms();
|
2020-05-06 11:57:58 +00:00
|
|
|
let rooms = rooms_lock.read().await;
|
2020-03-31 13:01:48 +00:00
|
|
|
let room = &rooms
|
2020-04-03 13:07:40 +00:00
|
|
|
.get(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap())
|
2020-03-31 13:01:48 +00:00
|
|
|
.unwrap()
|
2020-04-14 22:10:10 +00:00
|
|
|
.read()
|
2020-04-01 01:08:25 +00:00
|
|
|
.await;
|
2020-03-31 13:01:48 +00:00
|
|
|
|
|
|
|
assert_eq!(2, room.members.len());
|
2020-04-27 10:12:51 +00:00
|
|
|
for member in room.members.values() {
|
2020-03-31 13:01:48 +00:00
|
|
|
assert_eq!(MembershipState::Join, member.membership);
|
|
|
|
}
|
2020-04-05 16:41:00 +00:00
|
|
|
|
|
|
|
assert!(room.deref().power_levels.is_some())
|
2020-03-31 13:01:48 +00:00
|
|
|
}
|
2020-04-07 00:59:44 +00:00
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
#[async_test]
|
2020-05-07 14:22:18 +00:00
|
|
|
async fn room_events() {
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-07 14:22:18 +00:00
|
|
|
let room_id = get_room_id();
|
|
|
|
let user_id = UserId::try_from("@example:localhost").unwrap();
|
2020-04-07 20:11:35 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
let mut response = EventBuilder::default()
|
2020-05-08 14:12:21 +00:00
|
|
|
.add_room_event(EventsFile::Member, RoomEvent::RoomMember)
|
|
|
|
.add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels)
|
2020-05-07 14:22:18 +00:00
|
|
|
.build_sync_response();
|
|
|
|
|
|
|
|
client.receive_sync_response(&mut response).await.unwrap();
|
2020-04-07 12:55:42 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
let room = client.get_joined_room(&room_id).await.unwrap();
|
|
|
|
let room = room.read().await;
|
2020-04-07 20:11:35 +00:00
|
|
|
|
|
|
|
assert_eq!(room.members.len(), 1);
|
|
|
|
assert!(room.power_levels.is_some());
|
|
|
|
assert_eq!(
|
|
|
|
room.power_levels.as_ref().unwrap().kick,
|
2020-04-29 08:40:27 +00:00
|
|
|
crate::js_int::Int::new(50).unwrap()
|
2020-04-07 20:11:35 +00:00
|
|
|
);
|
2020-05-07 14:22:18 +00:00
|
|
|
let admin = room.members.get(&user_id).unwrap();
|
2020-04-29 08:40:27 +00:00
|
|
|
assert_eq!(
|
|
|
|
admin.power_level.unwrap(),
|
|
|
|
crate::js_int::Int::new(100).unwrap()
|
|
|
|
);
|
2020-04-07 00:59:44 +00:00
|
|
|
}
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
#[async_test]
|
2020-05-07 14:22:18 +00:00
|
|
|
async fn calculate_aliases() {
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-07 14:22:18 +00:00
|
|
|
|
|
|
|
let room_id = get_room_id();
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
let mut response = EventBuilder::default()
|
2020-05-08 14:12:21 +00:00
|
|
|
.add_state_event(EventsFile::Aliases, StateEvent::RoomAliases)
|
2020-05-07 14:22:18 +00:00
|
|
|
.build_sync_response();
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
client.receive_sync_response(&mut response).await.unwrap();
|
|
|
|
|
|
|
|
let room = client.get_joined_room(&room_id).await.unwrap();
|
|
|
|
let room = room.read().await;
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 10:51:53 +00:00
|
|
|
assert_eq!("tutorial", room.display_name());
|
2020-04-23 13:55:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
#[async_test]
|
2020-05-07 14:22:18 +00:00
|
|
|
async fn calculate_alias() {
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-07 14:22:18 +00:00
|
|
|
|
|
|
|
let room_id = get_room_id();
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
let mut response = EventBuilder::default()
|
2020-05-08 14:12:21 +00:00
|
|
|
.add_state_event(EventsFile::Alias, StateEvent::RoomCanonicalAlias)
|
2020-05-07 14:22:18 +00:00
|
|
|
.build_sync_response();
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
client.receive_sync_response(&mut response).await.unwrap();
|
|
|
|
|
|
|
|
let room = client.get_joined_room(&room_id).await.unwrap();
|
|
|
|
let room = room.read().await;
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 10:51:53 +00:00
|
|
|
assert_eq!("tutorial", room.display_name());
|
2020-04-23 13:55:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
#[async_test]
|
2020-05-07 14:22:18 +00:00
|
|
|
async fn calculate_name() {
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = get_client().await;
|
2020-05-07 14:22:18 +00:00
|
|
|
|
|
|
|
let room_id = get_room_id();
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
let mut response = EventBuilder::default()
|
2020-05-08 14:12:21 +00:00
|
|
|
.add_state_event(EventsFile::Name, StateEvent::RoomName)
|
2020-05-07 14:22:18 +00:00
|
|
|
.build_sync_response();
|
|
|
|
|
|
|
|
client.receive_sync_response(&mut response).await.unwrap();
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 14:22:18 +00:00
|
|
|
let room = client.get_joined_room(&room_id).await.unwrap();
|
|
|
|
let room = room.read().await;
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 10:51:53 +00:00
|
|
|
assert_eq!("room name", room.display_name());
|
2020-04-23 13:55:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
#[async_test]
|
2020-04-23 13:55:59 +00:00
|
|
|
async fn calculate_room_names_from_summary() {
|
2020-05-08 14:12:21 +00:00
|
|
|
let mut response = sync_response(SyncResponseFile::DefaultWithSummary);
|
2020-04-23 13:55:59 +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();
|
2020-05-07 10:51:53 +00:00
|
|
|
client.receive_sync_response(&mut response).await.unwrap();
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 10:51:53 +00:00
|
|
|
let mut room_names = vec![];
|
|
|
|
for room in client.joined_rooms().read().await.values() {
|
|
|
|
room_names.push(room.read().await.display_name())
|
2020-05-07 11:09:22 +00:00
|
|
|
}
|
2020-04-23 13:55:59 +00:00
|
|
|
|
2020-05-07 10:51:53 +00:00
|
|
|
assert_eq!(vec!["example, example2"], room_names);
|
2020-04-23 13:55:59 +00:00
|
|
|
}
|
2020-05-14 11:58:09 +00:00
|
|
|
|
|
|
|
#[async_test]
|
2020-05-14 13:12:14 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-05-14 11:58:09 +00:00
|
|
|
async fn encryption_info_test() {
|
|
|
|
let mut response = sync_response(SyncResponseFile::DefaultWithSummary);
|
|
|
|
let user_id = UserId::try_from("@example:localhost").unwrap();
|
|
|
|
|
|
|
|
let session = Session {
|
|
|
|
access_token: "1234".to_owned(),
|
|
|
|
user_id: user_id.clone(),
|
|
|
|
device_id: "DEVICEID".to_owned(),
|
|
|
|
};
|
2020-05-22 13:23:58 +00:00
|
|
|
let client = BaseClient::new().unwrap();
|
|
|
|
client.restore_login(session).await.unwrap();
|
2020-05-14 11:58:09 +00:00
|
|
|
client.receive_sync_response(&mut response).await.unwrap();
|
|
|
|
|
|
|
|
let event = EncryptionEvent {
|
2020-05-14 12:50:20 +00:00
|
|
|
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
2020-05-14 11:58:09 +00:00
|
|
|
origin_server_ts: SystemTime::now(),
|
|
|
|
sender: user_id,
|
|
|
|
state_key: "".into(),
|
|
|
|
unsigned: UnsignedData::default(),
|
|
|
|
content: EncryptionEventContent {
|
|
|
|
algorithm: Algorithm::MegolmV1AesSha2,
|
|
|
|
rotation_period_ms: Some(100_000u32.into()),
|
|
|
|
rotation_period_msgs: Some(100u32.into()),
|
|
|
|
},
|
|
|
|
prev_content: None,
|
|
|
|
room_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let room_id = get_room_id();
|
|
|
|
let room = client.get_joined_room(&room_id).await.unwrap();
|
|
|
|
|
|
|
|
assert!(!room.read().await.is_encrypted());
|
|
|
|
room.write().await.handle_encryption_event(&event);
|
|
|
|
assert!(room.read().await.is_encrypted());
|
|
|
|
|
|
|
|
let room_lock = room.read().await;
|
|
|
|
let encryption_info = room_lock.encryption_info().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(encryption_info.algorithm(), &Algorithm::MegolmV1AesSha2);
|
|
|
|
assert_eq!(encryption_info.rotation_period(), 100_000);
|
|
|
|
assert_eq!(encryption_info.rotation_period_messages(), 100);
|
|
|
|
}
|
2020-03-31 13:01:48 +00:00
|
|
|
}
|