2021-01-19 15:48:37 +00:00
|
|
|
// Copyright 2021 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.
|
|
|
|
|
2021-01-26 12:22:06 +00:00
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, BTreeSet},
|
|
|
|
ops::Deref,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-01-19 15:48:37 +00:00
|
|
|
|
2021-04-21 08:47:33 +00:00
|
|
|
#[cfg(feature = "sled_state_store")]
|
|
|
|
use std::path::Path;
|
|
|
|
|
2021-01-19 15:48:37 +00:00
|
|
|
use dashmap::DashMap;
|
|
|
|
use matrix_sdk_common::{
|
2021-04-12 09:09:39 +00:00
|
|
|
api::r0::push::get_notifications::Notification,
|
2021-01-21 11:08:16 +00:00
|
|
|
async_trait,
|
2021-01-19 15:48:37 +00:00
|
|
|
events::{
|
2021-05-12 14:21:44 +00:00
|
|
|
presence::PresenceEvent, room::member::MemberEventContent, AnyGlobalAccountDataEvent,
|
|
|
|
AnyRoomAccountDataEvent, AnyStrippedStateEvent, AnySyncStateEvent, EventContent, EventType,
|
2021-01-19 15:48:37 +00:00
|
|
|
},
|
|
|
|
identifiers::{RoomId, UserId},
|
|
|
|
locks::RwLock,
|
2021-04-28 14:46:58 +00:00
|
|
|
AsyncTraitDeps, Raw,
|
2021-01-19 15:48:37 +00:00
|
|
|
};
|
2021-01-22 17:10:17 +00:00
|
|
|
#[cfg(feature = "sled_state_store")]
|
2021-01-22 10:33:06 +00:00
|
|
|
use sled::Db;
|
2021-01-19 15:48:37 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
deserialized_responses::{MemberEvent, StrippedMemberEvent},
|
2021-03-04 18:23:10 +00:00
|
|
|
rooms::{RoomInfo, RoomType},
|
2021-03-05 11:00:43 +00:00
|
|
|
Room, Session,
|
2021-01-19 15:48:37 +00:00
|
|
|
};
|
|
|
|
|
2021-01-27 10:42:13 +00:00
|
|
|
pub(crate) mod ambiguity_map;
|
2021-01-21 15:31:33 +00:00
|
|
|
mod memory_store;
|
2021-01-22 17:10:17 +00:00
|
|
|
#[cfg(feature = "sled_state_store")]
|
2021-01-19 15:48:37 +00:00
|
|
|
mod sled_store;
|
2021-01-21 15:31:33 +00:00
|
|
|
|
2021-01-28 13:10:26 +00:00
|
|
|
#[cfg(not(feature = "sled_state_store"))]
|
2021-01-22 10:33:06 +00:00
|
|
|
use self::memory_store::MemoryStore;
|
2021-01-22 17:10:17 +00:00
|
|
|
#[cfg(feature = "sled_state_store")]
|
2021-01-22 10:33:06 +00:00
|
|
|
use self::sled_store::SledStore;
|
2021-01-19 15:48:37 +00:00
|
|
|
|
2021-01-28 13:10:26 +00:00
|
|
|
/// State store specific error type.
|
2021-01-19 15:48:37 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum StoreError {
|
2021-01-28 13:10:26 +00:00
|
|
|
/// An error happened in the underlying sled database.
|
2021-01-22 17:10:17 +00:00
|
|
|
#[cfg(feature = "sled_state_store")]
|
2021-01-19 15:48:37 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
Sled(#[from] sled::Error),
|
2021-01-28 13:10:26 +00:00
|
|
|
/// An error happened while serializing or deserializing some data.
|
2021-01-19 15:48:37 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
Json(#[from] serde_json::Error),
|
2021-01-28 13:10:26 +00:00
|
|
|
/// An error happened while deserializing a Matrix identifier, e.g. an user
|
|
|
|
/// id.
|
2021-01-19 15:48:37 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
Identifier(#[from] matrix_sdk_common::identifiers::Error),
|
2021-01-28 13:10:26 +00:00
|
|
|
/// The store is locked with a passphrase and an incorrect passphrase was
|
|
|
|
/// given.
|
2021-01-20 15:27:59 +00:00
|
|
|
#[error("The store failed to be unlocked")]
|
|
|
|
StoreLocked,
|
2021-01-28 13:10:26 +00:00
|
|
|
/// An unencrypted store was tried to be unlocked with a passphrase.
|
2021-01-20 15:27:59 +00:00
|
|
|
#[error("The store is not encrypted but was tried to be opened with a passphrase")]
|
|
|
|
UnencryptedStore,
|
2021-01-28 13:10:26 +00:00
|
|
|
/// The store failed to encrypt or decrypt some data.
|
2021-01-20 15:27:59 +00:00
|
|
|
#[error("Error encrypting or decrypting data from the store: {0}")]
|
|
|
|
Encryption(String),
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A `StateStore` specific result type.
|
2021-04-01 17:35:09 +00:00
|
|
|
pub type Result<T, E = StoreError> = std::result::Result<T, E>;
|
2021-01-19 15:48:37 +00:00
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// An abstract state store trait that can be used to implement different stores
|
|
|
|
/// for the SDK.
|
2021-01-21 11:08:16 +00:00
|
|
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
|
|
|
pub trait StateStore: AsyncTraitDeps {
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Save the given filter id under the given name.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `filter_name` - The name that should be used to store the filter id.
|
|
|
|
///
|
|
|
|
/// * `filter_id` - The filter id that should be stored in the state store.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn save_filter(&self, filter_name: &str, filter_id: &str) -> Result<()>;
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Save the set of state changes in the store.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn save_changes(&self, changes: &StateChanges) -> Result<()>;
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get the filter id that was stored under the given filter name.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `filter_name` - The name that was used to store the filter id.
|
|
|
|
async fn get_filter(&self, filter_name: &str) -> Result<Option<String>>;
|
2021-01-21 11:08:16 +00:00
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get the last stored sync token.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn get_sync_token(&self) -> Result<Option<String>>;
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get the stored presence event for the given user.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `user_id` - The id of the user for which we wish to fetch the presence
|
|
|
|
/// event for.
|
2021-04-28 14:46:58 +00:00
|
|
|
async fn get_presence_event(&self, user_id: &UserId) -> Result<Option<Raw<PresenceEvent>>>;
|
2021-01-21 11:08:16 +00:00
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get a state event out of the state store.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The id of the room the state event was received for.
|
|
|
|
///
|
|
|
|
/// * `event_type` - The event type of the state event.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn get_state_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
event_type: EventType,
|
|
|
|
state_key: &str,
|
2021-04-28 14:46:58 +00:00
|
|
|
) -> Result<Option<Raw<AnySyncStateEvent>>>;
|
2021-01-21 11:08:16 +00:00
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get the current profile for the given user in the given room.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The room id the profile is used in.
|
|
|
|
///
|
|
|
|
/// * `user_id` - The id of the user the profile belongs to.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn get_profile(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
user_id: &UserId,
|
|
|
|
) -> Result<Option<MemberEventContent>>;
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get a raw `MemberEvent` for the given state key in the given room id.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The room id the member event belongs to.
|
|
|
|
///
|
|
|
|
/// * `state_key` - The user id that the member event defines the state for.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn get_member_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
state_key: &UserId,
|
|
|
|
) -> Result<Option<MemberEvent>>;
|
|
|
|
|
2021-03-12 11:20:18 +00:00
|
|
|
/// Get all the user ids of members for a given room.
|
|
|
|
async fn get_user_ids(&self, room_id: &RoomId) -> Result<Vec<UserId>>;
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get all the user ids of members that are in the invited state for a
|
|
|
|
/// given room.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn get_invited_user_ids(&self, room_id: &RoomId) -> Result<Vec<UserId>>;
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get all the user ids of members that are in the joined state for a
|
|
|
|
/// given room.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn get_joined_user_ids(&self, room_id: &RoomId) -> Result<Vec<UserId>>;
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get all the pure `RoomInfo`s the store knows about.
|
2021-01-21 11:08:16 +00:00
|
|
|
async fn get_room_infos(&self) -> Result<Vec<RoomInfo>>;
|
2021-01-23 16:29:43 +00:00
|
|
|
|
2021-03-04 18:23:10 +00:00
|
|
|
/// Get all the pure `RoomInfo`s the store knows about.
|
|
|
|
async fn get_stripped_room_infos(&self) -> Result<Vec<RoomInfo>>;
|
2021-01-26 12:22:06 +00:00
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get all the users that use the given display name in the given room.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The id of the room for which the display name users should
|
|
|
|
/// be fetched for.
|
|
|
|
///
|
|
|
|
/// * `display_name` - The display name that the users use.
|
2021-01-26 12:22:06 +00:00
|
|
|
async fn get_users_with_display_name(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
display_name: &str,
|
|
|
|
) -> Result<BTreeSet<UserId>>;
|
2021-04-12 09:09:39 +00:00
|
|
|
|
|
|
|
/// Get an event out of the account data store.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `event_type` - The event type of the account data event.
|
2021-04-28 14:46:58 +00:00
|
|
|
async fn get_account_data_event(
|
|
|
|
&self,
|
|
|
|
event_type: EventType,
|
2021-05-12 14:21:44 +00:00
|
|
|
) -> Result<Option<Raw<AnyGlobalAccountDataEvent>>>;
|
2021-05-07 17:11:27 +00:00
|
|
|
|
|
|
|
/// Get an event out of the room account data store.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `room_id` - The id of the room for which the room account data event should
|
|
|
|
/// be fetched.
|
|
|
|
///
|
|
|
|
/// * `event_type` - The event type of the room account data event.
|
|
|
|
async fn get_room_account_data_event(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
event_type: EventType,
|
2021-05-12 14:21:44 +00:00
|
|
|
) -> Result<Option<Raw<AnyRoomAccountDataEvent>>>;
|
2021-01-21 11:08:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// A state store wrapper for the SDK.
|
|
|
|
///
|
|
|
|
/// This adds additional higher level store functionality on top of a
|
|
|
|
/// `StateStore` implementation.
|
2021-01-19 15:48:37 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Store {
|
2021-01-21 11:08:16 +00:00
|
|
|
inner: Arc<Box<dyn StateStore>>,
|
2021-01-21 15:31:33 +00:00
|
|
|
pub(crate) session: Arc<RwLock<Option<Session>>>,
|
|
|
|
pub(crate) sync_token: Arc<RwLock<Option<String>>>,
|
2021-01-19 15:48:37 +00:00
|
|
|
rooms: Arc<DashMap<RoomId, Room>>,
|
2021-03-04 18:23:10 +00:00
|
|
|
stripped_rooms: Arc<DashMap<RoomId, Room>>,
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Store {
|
2021-01-21 15:31:33 +00:00
|
|
|
fn new(inner: Box<dyn StateStore>) -> Self {
|
|
|
|
let session = Arc::new(RwLock::new(None));
|
|
|
|
let sync_token = Arc::new(RwLock::new(None));
|
|
|
|
|
2021-01-19 15:48:37 +00:00
|
|
|
Self {
|
2021-01-21 15:31:33 +00:00
|
|
|
inner: inner.into(),
|
2021-01-19 15:48:37 +00:00
|
|
|
session,
|
|
|
|
sync_token,
|
|
|
|
rooms: DashMap::new().into(),
|
|
|
|
stripped_rooms: DashMap::new().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn restore_session(&self, session: Session) -> Result<()> {
|
2021-01-23 16:29:43 +00:00
|
|
|
for info in self.inner.get_room_infos().await? {
|
2021-01-21 11:08:16 +00:00
|
|
|
let room = Room::restore(&session.user_id, self.inner.clone(), info);
|
2021-01-19 15:48:37 +00:00
|
|
|
self.rooms.insert(room.room_id().to_owned(), room);
|
|
|
|
}
|
|
|
|
|
2021-01-23 16:29:43 +00:00
|
|
|
for info in self.inner.get_stripped_room_infos().await? {
|
2021-03-04 18:23:10 +00:00
|
|
|
let room = Room::restore(&session.user_id, self.inner.clone(), info);
|
2021-01-23 16:29:43 +00:00
|
|
|
self.stripped_rooms.insert(room.room_id().to_owned(), room);
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:48:37 +00:00
|
|
|
let token = self.get_sync_token().await?;
|
|
|
|
|
|
|
|
*self.sync_token.write().await = token;
|
|
|
|
*self.session.write().await = Some(session);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:10:26 +00:00
|
|
|
#[cfg(not(feature = "sled_state_store"))]
|
|
|
|
pub(crate) fn open_memory_store() -> Self {
|
2021-01-21 15:31:33 +00:00
|
|
|
let inner = Box::new(MemoryStore::new());
|
|
|
|
|
|
|
|
Self::new(inner)
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Open the default Sled store.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `path` - The path where the store should reside in.
|
|
|
|
///
|
|
|
|
/// * `passphrase` - A passphrase that should be used to encrypt the state
|
|
|
|
/// store.
|
2021-01-22 17:10:17 +00:00
|
|
|
#[cfg(feature = "sled_state_store")]
|
2021-01-22 10:33:06 +00:00
|
|
|
pub fn open_default(path: impl AsRef<Path>, passphrase: Option<&str>) -> Result<(Self, Db)> {
|
2021-01-21 15:31:33 +00:00
|
|
|
let inner = if let Some(passphrase) = passphrase {
|
2021-01-22 10:33:06 +00:00
|
|
|
SledStore::open_with_passphrase(path, passphrase)?
|
2021-01-21 15:31:33 +00:00
|
|
|
} else {
|
2021-01-22 10:33:06 +00:00
|
|
|
SledStore::open_with_path(path)?
|
2021-01-21 15:31:33 +00:00
|
|
|
};
|
|
|
|
|
2021-01-22 10:33:06 +00:00
|
|
|
Ok((Self::new(Box::new(inner.clone())), inner.inner))
|
2021-01-21 15:31:33 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 17:10:17 +00:00
|
|
|
#[cfg(feature = "sled_state_store")]
|
2021-01-28 13:10:26 +00:00
|
|
|
pub(crate) fn open_temporary() -> Result<(Self, Db)> {
|
2021-01-22 10:33:06 +00:00
|
|
|
let inner = SledStore::open()?;
|
2021-01-21 15:31:33 +00:00
|
|
|
|
2021-01-22 10:33:06 +00:00
|
|
|
Ok((Self::new(Box::new(inner.clone())), inner.inner))
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_bare_room(&self, room_id: &RoomId) -> Option<Room> {
|
|
|
|
#[allow(clippy::map_clone)]
|
|
|
|
self.rooms.get(room_id).map(|r| r.clone())
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get all the rooms this store knows about.
|
2021-03-05 11:00:43 +00:00
|
|
|
pub fn get_rooms(&self) -> Vec<Room> {
|
2021-01-19 15:48:37 +00:00
|
|
|
self.rooms
|
|
|
|
.iter()
|
|
|
|
.filter_map(|r| self.get_room(r.key()))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:51:34 +00:00
|
|
|
/// Get the room with the given room id.
|
2021-03-05 11:00:43 +00:00
|
|
|
pub fn get_room(&self, room_id: &RoomId) -> Option<Room> {
|
2021-01-19 15:48:37 +00:00
|
|
|
self.get_bare_room(room_id)
|
2021-01-26 18:28:17 +00:00
|
|
|
.and_then(|r| match r.room_type() {
|
2021-03-05 11:00:43 +00:00
|
|
|
RoomType::Joined => Some(r),
|
|
|
|
RoomType::Left => Some(r),
|
|
|
|
RoomType::Invited => self.get_stripped_room(room_id),
|
2021-01-19 15:48:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:23:10 +00:00
|
|
|
fn get_stripped_room(&self, room_id: &RoomId) -> Option<Room> {
|
2021-01-19 15:48:37 +00:00
|
|
|
#[allow(clippy::map_clone)]
|
|
|
|
self.stripped_rooms.get(room_id).map(|r| r.clone())
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:23:10 +00:00
|
|
|
pub(crate) async fn get_or_create_stripped_room(&self, room_id: &RoomId) -> Room {
|
2021-01-19 15:48:37 +00:00
|
|
|
let session = self.session.read().await;
|
|
|
|
let user_id = &session
|
|
|
|
.as_ref()
|
|
|
|
.expect("Creating room while not being logged in")
|
|
|
|
.user_id;
|
|
|
|
|
|
|
|
self.stripped_rooms
|
|
|
|
.entry(room_id.clone())
|
2021-03-04 18:23:10 +00:00
|
|
|
.or_insert_with(|| Room::new(user_id, self.inner.clone(), room_id, RoomType::Invited))
|
2021-01-19 15:48:37 +00:00
|
|
|
.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn get_or_create_room(&self, room_id: &RoomId, room_type: RoomType) -> Room {
|
|
|
|
let session = self.session.read().await;
|
|
|
|
let user_id = &session
|
|
|
|
.as_ref()
|
|
|
|
.expect("Creating room while not being logged in")
|
|
|
|
.user_id;
|
|
|
|
|
|
|
|
self.rooms
|
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_insert_with(|| Room::new(user_id, self.inner.clone(), room_id, room_type))
|
|
|
|
.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Store {
|
2021-01-21 11:08:16 +00:00
|
|
|
type Target = Box<dyn StateStore>;
|
2021-01-19 15:48:37 +00:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// Store state changes and pass them to the StateStore.
|
2021-01-19 15:48:37 +00:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct StateChanges {
|
2021-02-04 20:54:20 +00:00
|
|
|
/// The sync token that relates to this update.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub sync_token: Option<String>,
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A user session, containing an access token and information about the associated user account.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub session: Option<Session>,
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A mapping of event type string to `AnyBasicEvent`.
|
2021-05-12 14:21:44 +00:00
|
|
|
pub account_data: BTreeMap<String, Raw<AnyGlobalAccountDataEvent>>,
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A mapping of `UserId` to `PresenceEvent`.
|
2021-04-28 14:46:58 +00:00
|
|
|
pub presence: BTreeMap<UserId, Raw<PresenceEvent>>,
|
2021-01-19 15:48:37 +00:00
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A mapping of `RoomId` to a map of users and their `MemberEvent`.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub members: BTreeMap<RoomId, BTreeMap<UserId, MemberEvent>>,
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A mapping of `RoomId` to a map of users and their `MemberEventContent`.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub profiles: BTreeMap<RoomId, BTreeMap<UserId, MemberEventContent>>,
|
2021-02-04 20:54:20 +00:00
|
|
|
|
|
|
|
/// A mapping of `RoomId` to a map of event type string to a state key and `AnySyncStateEvent`.
|
2021-04-28 14:46:58 +00:00
|
|
|
pub state: BTreeMap<RoomId, BTreeMap<String, BTreeMap<String, Raw<AnySyncStateEvent>>>>,
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A mapping of `RoomId` to a map of event type string to `AnyBasicEvent`.
|
2021-05-12 14:21:44 +00:00
|
|
|
pub room_account_data: BTreeMap<RoomId, BTreeMap<String, Raw<AnyRoomAccountDataEvent>>>,
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A map of `RoomId` to `RoomInfo`.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub room_infos: BTreeMap<RoomId, RoomInfo>,
|
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A mapping of `RoomId` to a map of event type to a map of state key to `AnyStrippedStateEvent`.
|
2021-04-28 14:46:58 +00:00
|
|
|
pub stripped_state:
|
|
|
|
BTreeMap<RoomId, BTreeMap<String, BTreeMap<String, Raw<AnyStrippedStateEvent>>>>,
|
2021-02-04 20:54:20 +00:00
|
|
|
/// A mapping of `RoomId` to a map of users and their `StrippedMemberEvent`.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub stripped_members: BTreeMap<RoomId, BTreeMap<UserId, StrippedMemberEvent>>,
|
2021-03-04 18:23:10 +00:00
|
|
|
/// A map of `RoomId` to `RoomInfo`.
|
|
|
|
pub invited_room_info: BTreeMap<RoomId, RoomInfo>,
|
2021-04-12 09:09:39 +00:00
|
|
|
|
2021-04-28 14:46:58 +00:00
|
|
|
/// A map from room id to a map of a display name and a set of user ids that
|
|
|
|
/// share that display name in the given room.
|
|
|
|
pub ambiguity_maps: BTreeMap<RoomId, BTreeMap<String, BTreeSet<UserId>>>,
|
2021-04-12 09:09:39 +00:00
|
|
|
/// A map of `RoomId` to a vector of `Notification`s
|
|
|
|
pub notifications: BTreeMap<RoomId, Vec<Notification>>,
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StateChanges {
|
2021-02-04 20:54:20 +00:00
|
|
|
/// Create a new `StateChanges` struct with the given sync_token.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub fn new(sync_token: String) -> Self {
|
|
|
|
Self {
|
|
|
|
sync_token: Some(sync_token),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// Update the `StateChanges` struct with the given `PresenceEvent`.
|
2021-04-28 14:46:58 +00:00
|
|
|
pub fn add_presence_event(&mut self, event: PresenceEvent, raw_event: Raw<PresenceEvent>) {
|
|
|
|
self.presence.insert(event.sender, raw_event);
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// Update the `StateChanges` struct with the given `RoomInfo`.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub fn add_room(&mut self, room: RoomInfo) {
|
|
|
|
self.room_infos
|
|
|
|
.insert(room.room_id.as_ref().to_owned(), room);
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:23:10 +00:00
|
|
|
/// Update the `StateChanges` struct with the given `RoomInfo`.
|
|
|
|
pub fn add_stripped_room(&mut self, room: RoomInfo) {
|
2021-01-21 14:12:13 +00:00
|
|
|
self.invited_room_info
|
|
|
|
.insert(room.room_id.as_ref().to_owned(), room);
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// Update the `StateChanges` struct with the given `AnyBasicEvent`.
|
2021-05-12 14:21:44 +00:00
|
|
|
pub fn add_account_data(
|
|
|
|
&mut self,
|
|
|
|
event: AnyGlobalAccountDataEvent,
|
|
|
|
raw_event: Raw<AnyGlobalAccountDataEvent>,
|
|
|
|
) {
|
2021-01-19 15:48:37 +00:00
|
|
|
self.account_data
|
2021-04-28 14:46:58 +00:00
|
|
|
.insert(event.content().event_type().to_owned(), raw_event);
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// Update the `StateChanges` struct with the given room with a new `AnyBasicEvent`.
|
2021-04-28 14:46:58 +00:00
|
|
|
pub fn add_room_account_data(
|
|
|
|
&mut self,
|
|
|
|
room_id: &RoomId,
|
2021-05-12 14:21:44 +00:00
|
|
|
event: AnyRoomAccountDataEvent,
|
|
|
|
raw_event: Raw<AnyRoomAccountDataEvent>,
|
2021-04-28 14:46:58 +00:00
|
|
|
) {
|
2021-01-19 15:48:37 +00:00
|
|
|
self.room_account_data
|
|
|
|
.entry(room_id.to_owned())
|
|
|
|
.or_insert_with(BTreeMap::new)
|
2021-04-28 14:46:58 +00:00
|
|
|
.insert(event.content().event_type().to_owned(), raw_event);
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// Update the `StateChanges` struct with the given room with a new `StrippedMemberEvent`.
|
2021-01-19 15:48:37 +00:00
|
|
|
pub fn add_stripped_member(&mut self, room_id: &RoomId, event: StrippedMemberEvent) {
|
|
|
|
let user_id = event.state_key.clone();
|
|
|
|
|
|
|
|
self.stripped_members
|
|
|
|
.entry(room_id.to_owned())
|
|
|
|
.or_insert_with(BTreeMap::new)
|
|
|
|
.insert(user_id, event);
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:54:20 +00:00
|
|
|
/// Update the `StateChanges` struct with the given room with a new `AnySyncStateEvent`.
|
2021-04-28 14:46:58 +00:00
|
|
|
pub fn add_state_event(
|
|
|
|
&mut self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
event: AnySyncStateEvent,
|
|
|
|
raw_event: Raw<AnySyncStateEvent>,
|
|
|
|
) {
|
2021-01-19 15:48:37 +00:00
|
|
|
self.state
|
|
|
|
.entry(room_id.to_owned())
|
|
|
|
.or_insert_with(BTreeMap::new)
|
|
|
|
.entry(event.content().event_type().to_string())
|
|
|
|
.or_insert_with(BTreeMap::new)
|
2021-04-28 14:46:58 +00:00
|
|
|
.insert(event.state_key().to_string(), raw_event);
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|
2021-04-27 09:16:02 +00:00
|
|
|
|
|
|
|
/// Update the `StateChanges` struct with the given room with a new `Notification`.
|
|
|
|
pub fn add_notification(&mut self, room_id: &RoomId, notification: Notification) {
|
|
|
|
self.notifications
|
|
|
|
.entry(room_id.to_owned())
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push(notification);
|
|
|
|
}
|
2021-01-19 15:48:37 +00:00
|
|
|
}
|