From 9332c55c8d0fc943e6914029fcecec2d97f5d000 Mon Sep 17 00:00:00 2001 From: Julian Sparber Date: Thu, 4 Mar 2021 19:23:10 +0100 Subject: [PATCH] matrix-sdk-base: merge StrippedRoom and Room --- matrix_sdk_base/src/client.rs | 10 +- matrix_sdk_base/src/lib.rs | 1 - matrix_sdk_base/src/rooms/mod.rs | 6 +- matrix_sdk_base/src/rooms/normal.rs | 8 +- matrix_sdk_base/src/rooms/stripped.rs | 145 -------------------- matrix_sdk_base/src/store/memory_store.rs | 8 +- matrix_sdk_base/src/store/mod.rs | 24 ++-- matrix_sdk_base/src/store/sled_store/mod.rs | 6 +- 8 files changed, 30 insertions(+), 178 deletions(-) delete mode 100644 matrix_sdk_base/src/rooms/stripped.rs diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 4de856e1..09a1dca8 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -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>, - room_info: &mut StrippedRoomInfo, + room_info: &mut RoomInfo, ) -> ( InviteState, BTreeMap, @@ -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) { diff --git a/matrix_sdk_base/src/lib.rs b/matrix_sdk_base/src/lib.rs index 7a8e6b42..95181d5e 100644 --- a/matrix_sdk_base/src/lib.rs +++ b/matrix_sdk_base/src/lib.rs @@ -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}; diff --git a/matrix_sdk_base/src/rooms/mod.rs b/matrix_sdk_base/src/rooms/mod.rs index 5186da5f..3aef1b44 100644 --- a/matrix_sdk_base/src/rooms/mod.rs +++ b/matrix_sdk_base/src/rooms/mod.rs @@ -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 diff --git a/matrix_sdk_base/src/rooms/normal.rs b/matrix_sdk_base/src/rooms/normal.rs index 2220b7f9..9c8084b3 100644 --- a/matrix_sdk_base/src/rooms/normal.rs +++ b/matrix_sdk_base/src/rooms/normal.rs @@ -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, @@ -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( diff --git a/matrix_sdk_base/src/rooms/stripped.rs b/matrix_sdk_base/src/rooms/stripped.rs deleted file mode 100644 index 8b94a7af..00000000 --- a/matrix_sdk_base/src/rooms/stripped.rs +++ /dev/null @@ -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, - own_user_id: Arc, - inner: Arc>, - store: Arc>, -} - -impl StrippedRoom { - pub(crate) fn new( - own_user_id: &UserId, - store: Arc>, - 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>, - 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 { - 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]: - 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, - /// 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()) - } -} diff --git a/matrix_sdk_base/src/store/memory_store.rs b/matrix_sdk_base/src/store/memory_store.rs index dbe3100e..014c20de 100644 --- a/matrix_sdk_base/src/store/memory_store.rs +++ b/matrix_sdk_base/src/store/memory_store.rs @@ -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>>>, room_account_data: Arc>>, - stripped_room_info: Arc>, + stripped_room_info: Arc>, #[allow(clippy::type_complexity)] stripped_room_state: Arc>>>, @@ -291,7 +291,7 @@ impl MemoryStore { self.room_info.iter().map(|r| r.clone()).collect() } - fn get_stripped_room_infos(&self) -> Vec { + fn get_stripped_room_infos(&self) -> Vec { #[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> { + async fn get_stripped_room_infos(&self) -> Result> { Ok(self.get_stripped_room_infos()) } diff --git a/matrix_sdk_base/src/store/mod.rs b/matrix_sdk_base/src/store/mod.rs index 698efa13..f40751e4 100644 --- a/matrix_sdk_base/src/store/mod.rs +++ b/matrix_sdk_base/src/store/mod.rs @@ -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>; - /// Get all the pure `StrippedRoomInfo`s the store knows about. - async fn get_stripped_room_infos(&self) -> Result>; + /// Get all the pure `RoomInfo`s the store knows about. + async fn get_stripped_room_infos(&self) -> Result>; /// 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>>, pub(crate) sync_token: Arc>>, rooms: Arc>, - stripped_rooms: Arc>, + stripped_rooms: Arc>, } 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 { + fn get_stripped_room(&self, room_id: &RoomId) -> Option { #[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>>, /// A mapping of `RoomId` to a map of users and their `StrippedMemberEvent`. pub stripped_members: BTreeMap>, - /// A map of `RoomId` to `StrippedRoomInfo`. - pub invited_room_info: BTreeMap, + /// A map of `RoomId` to `RoomInfo`. + pub invited_room_info: BTreeMap, } 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); } diff --git a/matrix_sdk_base/src/store/sled_store/mod.rs b/matrix_sdk_base/src/store/sled_store/mod.rs index 5e8662c5..495b9477 100644 --- a/matrix_sdk_base/src/store/sled_store/mod.rs +++ b/matrix_sdk_base/src/store/sled_store/mod.rs @@ -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> { + pub async fn get_stripped_room_infos(&self) -> impl Stream> { 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> { + async fn get_stripped_room_infos(&self) -> Result> { self.get_stripped_room_infos().await.try_collect().await }