matrix-sdk-base: merge StrippedRoom and Room

master
Julian Sparber 2021-03-04 19:23:10 +01:00
parent f6f382e28a
commit 9332c55c8d
8 changed files with 30 additions and 178 deletions

View File

@ -63,7 +63,7 @@ use zeroize::Zeroizing;
use crate::{
error::Result,
event_handler::Handler,
rooms::{RoomInfo, RoomType, StrippedRoomInfo},
rooms::{RoomInfo, RoomType},
session::Session,
store::{ambiguity_map::AmbiguityCache, Result as StoreResult, StateChanges, Store},
EventHandler, RoomState,
@ -481,7 +481,7 @@ impl BaseClient {
}
}
_ => {
room_info.handle_state_event(&s);
room_info.handle_state_event(&s.content());
changes.add_state_event(room_id, s.clone());
}
},
@ -525,7 +525,7 @@ impl BaseClient {
fn handle_invited_state(
&self,
events: Vec<Raw<AnyStrippedStateEvent>>,
room_info: &mut StrippedRoomInfo,
room_info: &mut RoomInfo,
) -> (
InviteState,
BTreeMap<UserId, StrippedMemberEvent>,
@ -549,7 +549,7 @@ impl BaseClient {
),
}
} else {
room_info.handle_state_event(&e);
room_info.handle_state_event(&e.content());
state_events
.entry(e.content().event_type().to_owned())
.or_insert_with(BTreeMap::new)
@ -598,7 +598,7 @@ impl BaseClient {
})
{
state.events.push(event.clone());
room_info.handle_state_event(&event);
room_info.handle_state_event(&event.content());
if let AnySyncStateEvent::RoomMember(member) = event {
match MemberEvent::try_from(member) {

View File

@ -54,7 +54,6 @@ mod store;
pub use event_handler::{CustomEvent, EventHandler};
pub use rooms::{
InvitedRoom, JoinedRoom, LeftRoom, Room, RoomInfo, RoomMember, RoomState, RoomType,
StrippedRoom, StrippedRoomInfo,
};
pub use store::{StateChanges, StateStore, Store, StoreError};

View File

@ -1,6 +1,5 @@
mod members;
mod normal;
mod stripped;
use matrix_sdk_common::{
events::room::{
@ -10,7 +9,6 @@ use matrix_sdk_common::{
identifiers::UserId,
};
pub use normal::{Room, RoomInfo, RoomType};
pub use stripped::{StrippedRoom, StrippedRoomInfo};
pub use members::RoomMember;
@ -140,11 +138,11 @@ impl Deref for LeftRoom {
/// A room in an invited state.
#[derive(Debug, Clone)]
pub struct InvitedRoom {
pub(crate) inner: StrippedRoom,
pub(crate) inner: Room,
}
impl Deref for InvitedRoom {
type Target = StrippedRoom;
type Target = Room;
fn deref(&self) -> &Self::Target {
&self.inner

View File

@ -29,7 +29,7 @@ use matrix_sdk_common::{
guest_access::GuestAccess, history_visibility::HistoryVisibility, join_rules::JoinRule,
tombstone::TombstoneEventContent,
},
AnySyncStateEvent, EventType,
AnySyncStateEvent, AnyStateEventContent, EventType,
},
identifiers::{RoomAliasId, RoomId, UserId},
};
@ -43,7 +43,7 @@ use crate::{
use super::{BaseRoomInfo, RoomMember};
/// The underlying room data structure collecting state for joined and left rooms.
/// The underlying room data structure collecting state for joined, left and invtied rooms.
#[derive(Debug, Clone)]
pub struct Room {
room_id: Arc<RoomId>,
@ -484,8 +484,8 @@ impl RoomInfo {
self.base_info.encryption.is_some()
}
pub(crate) fn handle_state_event(&mut self, event: &AnySyncStateEvent) -> bool {
self.base_info.handle_state_event(&event.content())
pub(crate) fn handle_state_event(&mut self, event: &AnyStateEventContent) -> bool {
self.base_info.handle_state_event(&event)
}
pub(crate) fn update_notification_count(

View File

@ -1,145 +0,0 @@
// 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.
use std::sync::{Arc, Mutex as SyncMutex};
use matrix_sdk_common::{
events::{
room::{encryption::EncryptionEventContent, history_visibility::HistoryVisibility},
AnyStrippedStateEvent,
},
identifiers::{RoomId, UserId},
};
use serde::{Deserialize, Serialize};
use crate::store::StateStore;
use super::BaseRoomInfo;
/// The underlying room data structure collecting state for invited rooms.
#[derive(Debug, Clone)]
pub struct StrippedRoom {
room_id: Arc<RoomId>,
own_user_id: Arc<UserId>,
inner: Arc<SyncMutex<StrippedRoomInfo>>,
store: Arc<Box<dyn StateStore>>,
}
impl StrippedRoom {
pub(crate) fn new(
own_user_id: &UserId,
store: Arc<Box<dyn StateStore>>,
room_id: &RoomId,
) -> Self {
let room_id = Arc::new(room_id.clone());
let info = StrippedRoomInfo {
room_id,
base_info: BaseRoomInfo::new(),
};
Self::restore(own_user_id, store, info)
}
pub(crate) fn restore(
own_user_id: &UserId,
store: Arc<Box<dyn StateStore>>,
room_info: StrippedRoomInfo,
) -> Self {
Self {
own_user_id: Arc::new(own_user_id.clone()),
room_id: room_info.room_id.clone(),
store,
inner: Arc::new(SyncMutex::new(room_info)),
}
}
async fn calculate_name(&self) -> String {
let inner = self.inner.lock().unwrap();
if let Some(name) = &inner.base_info.name {
let name = name.trim();
name.to_string()
} else if let Some(alias) = &inner.base_info.canonical_alias {
let alias = alias.alias().trim();
alias.to_string()
} else {
// TODO do the dance with room members to calculate the name
self.room_id.to_string()
}
}
/// Get the unique room id of the room.
pub fn room_id(&self) -> &RoomId {
&self.room_id
}
/// Get our own user id.
pub fn own_user_id(&self) -> &UserId {
&self.own_user_id
}
pub(crate) fn clone_info(&self) -> StrippedRoomInfo {
(*self.inner.lock().unwrap()).clone()
}
/// Is the room encrypted.
pub fn is_encrypted(&self) -> bool {
self.inner.lock().unwrap().base_info.encryption.is_some()
}
/// Get the `m.room.encryption` content that enabled end to end encryption
/// in the room.
pub fn encryption_settings(&self) -> Option<EncryptionEventContent> {
self.inner.lock().unwrap().base_info.encryption.clone()
}
/// Get the history visibility policy of this room.
pub fn history_visibility(&self) -> HistoryVisibility {
self.inner
.lock()
.unwrap()
.base_info
.history_visibility
.clone()
}
/// Calculate the canonical display name of the room, taking into account
/// its name, aliases and members.
///
/// The display name is calculated according to [this algorithm][spec].
///
/// [spec]: <https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room>
pub async fn display_name(&self) -> String {
self.calculate_name().await
}
}
/// The underlying pure data structure for invited rooms.
///
/// Holds all the info needed to persist a room into the state store.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StrippedRoomInfo {
/// The unique room id of the room.
pub room_id: Arc<RoomId>,
/// Base room info which holds some basic event contents important for the
/// room state.
pub base_info: BaseRoomInfo,
}
impl StrippedRoomInfo {
pub(crate) fn handle_state_event(&mut self, event: &AnyStrippedStateEvent) -> bool {
self.base_info.handle_state_event(&event.content())
}
}

View File

@ -33,7 +33,7 @@ use tracing::info;
use crate::deserialized_responses::{MemberEvent, StrippedMemberEvent};
use super::{Result, RoomInfo, StateChanges, StateStore, StrippedRoomInfo};
use super::{Result, RoomInfo, StateChanges, StateStore};
#[derive(Debug, Clone)]
pub struct MemoryStore {
@ -49,7 +49,7 @@ pub struct MemoryStore {
#[allow(clippy::type_complexity)]
room_state: Arc<DashMap<RoomId, DashMap<String, DashMap<String, AnySyncStateEvent>>>>,
room_account_data: Arc<DashMap<RoomId, DashMap<String, AnyBasicEvent>>>,
stripped_room_info: Arc<DashMap<RoomId, StrippedRoomInfo>>,
stripped_room_info: Arc<DashMap<RoomId, RoomInfo>>,
#[allow(clippy::type_complexity)]
stripped_room_state:
Arc<DashMap<RoomId, DashMap<String, DashMap<String, AnyStrippedStateEvent>>>>,
@ -291,7 +291,7 @@ impl MemoryStore {
self.room_info.iter().map(|r| r.clone()).collect()
}
fn get_stripped_room_infos(&self) -> Vec<StrippedRoomInfo> {
fn get_stripped_room_infos(&self) -> Vec<RoomInfo> {
#[allow(clippy::map_clone)]
self.stripped_room_info.iter().map(|r| r.clone()).collect()
}
@ -357,7 +357,7 @@ impl StateStore for MemoryStore {
Ok(self.get_room_infos())
}
async fn get_stripped_room_infos(&self) -> Result<Vec<StrippedRoomInfo>> {
async fn get_stripped_room_infos(&self) -> Result<Vec<RoomInfo>> {
Ok(self.get_stripped_room_infos())
}

View File

@ -35,7 +35,7 @@ use sled::Db;
use crate::{
deserialized_responses::{MemberEvent, StrippedMemberEvent},
rooms::{RoomInfo, RoomType, StrippedRoom, StrippedRoomInfo},
rooms::{RoomInfo, RoomType},
InvitedRoom, JoinedRoom, LeftRoom, Room, RoomState, Session,
};
@ -164,8 +164,8 @@ pub trait StateStore: AsyncTraitDeps {
/// Get all the pure `RoomInfo`s the store knows about.
async fn get_room_infos(&self) -> Result<Vec<RoomInfo>>;
/// Get all the pure `StrippedRoomInfo`s the store knows about.
async fn get_stripped_room_infos(&self) -> Result<Vec<StrippedRoomInfo>>;
/// Get all the pure `RoomInfo`s the store knows about.
async fn get_stripped_room_infos(&self) -> Result<Vec<RoomInfo>>;
/// Get all the users that use the given display name in the given room.
///
@ -192,7 +192,7 @@ pub struct Store {
pub(crate) session: Arc<RwLock<Option<Session>>>,
pub(crate) sync_token: Arc<RwLock<Option<String>>>,
rooms: Arc<DashMap<RoomId, Room>>,
stripped_rooms: Arc<DashMap<RoomId, StrippedRoom>>,
stripped_rooms: Arc<DashMap<RoomId, Room>>,
}
impl Store {
@ -216,7 +216,7 @@ impl Store {
}
for info in self.inner.get_stripped_room_infos().await? {
let room = StrippedRoom::restore(&session.user_id, self.inner.clone(), info);
let room = Room::restore(&session.user_id, self.inner.clone(), info);
self.stripped_rooms.insert(room.room_id().to_owned(), room);
}
@ -315,12 +315,12 @@ impl Store {
})
}
fn get_stripped_room(&self, room_id: &RoomId) -> Option<StrippedRoom> {
fn get_stripped_room(&self, room_id: &RoomId) -> Option<Room> {
#[allow(clippy::map_clone)]
self.stripped_rooms.get(room_id).map(|r| r.clone())
}
pub(crate) async fn get_or_create_stripped_room(&self, room_id: &RoomId) -> StrippedRoom {
pub(crate) async fn get_or_create_stripped_room(&self, room_id: &RoomId) -> Room {
let session = self.session.read().await;
let user_id = &session
.as_ref()
@ -329,7 +329,7 @@ impl Store {
self.stripped_rooms
.entry(room_id.clone())
.or_insert_with(|| StrippedRoom::new(user_id, self.inner.clone(), room_id))
.or_insert_with(|| Room::new(user_id, self.inner.clone(), room_id, RoomType::Invited))
.clone()
}
@ -384,8 +384,8 @@ pub struct StateChanges {
pub stripped_state: BTreeMap<RoomId, BTreeMap<String, BTreeMap<String, AnyStrippedStateEvent>>>,
/// A mapping of `RoomId` to a map of users and their `StrippedMemberEvent`.
pub stripped_members: BTreeMap<RoomId, BTreeMap<UserId, StrippedMemberEvent>>,
/// A map of `RoomId` to `StrippedRoomInfo`.
pub invited_room_info: BTreeMap<RoomId, StrippedRoomInfo>,
/// A map of `RoomId` to `RoomInfo`.
pub invited_room_info: BTreeMap<RoomId, RoomInfo>,
}
impl StateChanges {
@ -408,8 +408,8 @@ impl StateChanges {
.insert(room.room_id.as_ref().to_owned(), room);
}
/// Update the `StateChanges` struct with the given `StrippedRoomInfo`.
pub fn add_stripped_room(&mut self, room: StrippedRoomInfo) {
/// Update the `StateChanges` struct with the given `RoomInfo`.
pub fn add_stripped_room(&mut self, room: RoomInfo) {
self.invited_room_info
.insert(room.room_id.as_ref().to_owned(), room);
}

View File

@ -43,7 +43,7 @@ use sled::{
};
use tracing::info;
use crate::{deserialized_responses::MemberEvent, rooms::StrippedRoomInfo};
use crate::deserialized_responses::MemberEvent;
use self::store_key::{EncryptedEvent, StoreKey};
@ -560,7 +560,7 @@ impl SledStore {
)
}
pub async fn get_stripped_room_infos(&self) -> impl Stream<Item = Result<StrippedRoomInfo>> {
pub async fn get_stripped_room_infos(&self) -> impl Stream<Item = Result<RoomInfo>> {
let db = self.clone();
stream::iter(
self.stripped_room_info
@ -644,7 +644,7 @@ impl StateStore for SledStore {
self.get_room_infos().await.try_collect().await
}
async fn get_stripped_room_infos(&self) -> Result<Vec<StrippedRoomInfo>> {
async fn get_stripped_room_infos(&self) -> Result<Vec<RoomInfo>> {
self.get_stripped_room_infos().await.try_collect().await
}