base: Save the stripped state of invited rooms.

master
Damir Jelić 2020-12-11 21:17:27 +01:00
parent b16e3b6bd8
commit e245599913
5 changed files with 349 additions and 28 deletions

View File

@ -55,10 +55,10 @@ use zeroize::Zeroizing;
use crate::{ use crate::{
error::Result, error::Result,
responses::{ responses::{
AccountData, Ephemeral, JoinedRoom, LeftRoom, Presence, Rooms, State, SyncResponse, AccountData, Ephemeral, InviteState, InvitedRoom, JoinedRoom, LeftRoom, Presence, Rooms,
Timeline, State, SyncResponse, Timeline,
}, },
rooms::{Room, RoomInfo, RoomType}, rooms::{Room, RoomInfo, RoomType, StrippedRoom},
session::Session, session::Session,
store::{StateChanges, Store}, store::{StateChanges, Store},
}; };
@ -148,14 +148,6 @@ fn hoist_room_event_prev_content(
Ok(ev) Ok(ev)
} }
fn stripped_deserialize_prev_content(
event: &Raw<AnyStrippedStateEvent>,
) -> Option<AdditionalUnsignedData> {
serde_json::from_str::<AdditionalEventData>(event.json().get())
.map(|more_unsigned| more_unsigned.unsigned)
.ok()
}
fn handle_membership( fn handle_membership(
changes: &mut StateChanges, changes: &mut StateChanges,
room_id: &RoomId, room_id: &RoomId,
@ -217,6 +209,7 @@ pub struct BaseClient {
/// Database /// Database
store: Store, store: Store,
rooms: Arc<DashMap<RoomId, Room>>, rooms: Arc<DashMap<RoomId, Room>>,
stripped_rooms: Arc<DashMap<RoomId, StrippedRoom>>,
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
olm: Arc<Mutex<Option<OlmMachine>>>, olm: Arc<Mutex<Option<OlmMachine>>>,
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
@ -331,6 +324,7 @@ impl BaseClient {
sync_token: Arc::new(RwLock::new(None)), sync_token: Arc::new(RwLock::new(None)),
store, store,
rooms: Arc::new(DashMap::new()), rooms: Arc::new(DashMap::new()),
stripped_rooms: Arc::new(DashMap::new()),
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
olm: Arc::new(Mutex::new(None)), olm: Arc::new(Mutex::new(None)),
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
@ -448,6 +442,19 @@ impl BaseClient {
self.sync_token.read().await.clone() self.sync_token.read().await.clone()
} }
async fn get_or_create_stripped_room(&self, room_id: &RoomId) -> StrippedRoom {
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())
.or_insert_with(|| StrippedRoom::new(user_id, self.store.clone(), room_id))
.clone()
}
async fn get_or_create_room(&self, room_id: &RoomId, room_type: RoomType) -> Room { async fn get_or_create_room(&self, room_id: &RoomId, room_type: RoomType) -> Room {
let session = self.session.read().await; let session = self.session.read().await;
let user_id = &session let user_id = &session
@ -685,6 +692,42 @@ impl BaseClient {
.insert(room_id, LeftRoom::new(timeline, state, account_data)); .insert(room_id, LeftRoom::new(timeline, state, account_data));
} }
for (room_id, invited) in response.rooms.invite {
{
let room = self.get_or_create_room(&room_id, RoomType::Invited).await;
let mut room_info = room.clone_summary();
room_info.mark_as_invited();
changes.add_room(room_info);
}
let mut state = InviteState::default();
let room = self.get_or_create_stripped_room(&room_id).await;
let mut room_info = room.clone_summary();
for event in &invited.invite_state.events {
if let Ok(e) = event.deserialize() {
match &e {
AnyStrippedStateEvent::RoomMember(member) => {
changes.add_stripped_member(&room_id, member.clone());
}
_ => {
room_info.handle_state_event(&e);
changes.add_stripped_state_event(&room_id, e.clone());
}
}
state.events.push(e);
}
}
let room = InvitedRoom {
invite_state: state,
};
rooms.invite.insert(room_id, room);
}
for event in &response.presence.events { for event in &response.presence.events {
if let Ok(e) = event.deserialize() { if let Ok(e) = event.deserialize() {
changes.add_presence_event(e); changes.add_presence_event(e);
@ -723,7 +766,7 @@ impl BaseClient {
async fn apply_changes(&self, changes: &StateChanges) { async fn apply_changes(&self, changes: &StateChanges) {
// TODO emit room changes here // TODO emit room changes here
for (room_id, summary) in &changes.room_summaries { for (room_id, summary) in &changes.room_infos {
if let Some(room) = self.get_room(&room_id) { if let Some(room) = self.get_room(&room_id) {
room.update_summary(summary.clone()) room.update_summary(summary.clone())
} }

View File

@ -1,5 +1,8 @@
mod normal;
mod members; mod members;
mod normal;
mod stripped;
pub use normal::{Room, RoomInfo, RoomType}; pub use normal::{Room, RoomInfo, RoomType};
pub use stripped::{StrippedRoom, StrippedRoomInfo};
pub use members::RoomMember; pub use members::RoomMember;

View File

@ -54,6 +54,8 @@ pub enum RoomType {
Joined, Joined,
/// Represents a left room, the `left_rooms` HashMap will be used. /// Represents a left room, the `left_rooms` HashMap will be used.
Left, Left,
/// Represents an invited room, the `invited_rooms` HashMap will be used.
Invited,
} }
impl Room { impl Room {
@ -292,6 +294,10 @@ impl RoomInfo {
self.room_type = RoomType::Left; self.room_type = RoomType::Left;
} }
pub fn mark_as_invited(&mut self) {
self.room_type = RoomType::Invited;
}
pub fn mark_members_synced(&mut self) { pub fn mark_members_synced(&mut self) {
self.members_synced = true; self.members_synced = true;
} }

View File

@ -0,0 +1,210 @@
// 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::{
convert::TryFrom,
sync::{Arc, Mutex as SyncMutex},
};
use futures::stream::{Stream, StreamExt};
use matrix_sdk_common::{
events::{room::encryption::EncryptionEventContent, AnyStrippedStateEvent, EventType},
identifiers::{RoomAliasId, RoomId, UserId},
};
use serde::{Deserialize, Serialize};
use crate::store::Store;
use super::RoomMember;
#[derive(Debug, Clone)]
pub struct StrippedRoom {
room_id: Arc<RoomId>,
own_user_id: Arc<UserId>,
inner: Arc<SyncMutex<StrippedRoomInfo>>,
store: Store,
}
impl StrippedRoom {
pub fn new(own_user_id: &UserId, store: Store, room_id: &RoomId) -> Self {
let room_id = Arc::new(room_id.clone());
Self {
own_user_id: Arc::new(own_user_id.clone()),
room_id: room_id.clone(),
store,
inner: Arc::new(SyncMutex::new(StrippedRoomInfo {
room_id,
encryption: None,
name: None,
canonical_alias: None,
avatar_url: None,
topic: None,
})),
}
}
/// 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 calculate_name(&self) -> String {
let inner = self.inner.lock().unwrap();
if let Some(name) = &inner.name {
let name = name.trim();
name.to_string()
} else if let Some(alias) = &inner.canonical_alias {
let alias = alias.alias().trim();
alias.to_string()
} else {
todo!()
// let joined = inner.summary.joined_member_count;
// let invited = inner.summary.invited_member_count;
// let heroes_count = inner.summary.heroes.len() as u64;
// let invited_joined = (invited + joined).saturating_sub(1);
// let members = self.get_j_members().await;
// info!(
// "Calculating name for {}, hero count {} members {:#?}",
// self.room_id(),
// heroes_count,
// members
// );
// TODO: This should use `self.heroes` but it is always empty??
//
// let own_user_id = self.own_user_id.clone();
// let is_own_member = |m: &RoomMember| m.user_id() == &*own_user_id;
// if !inner.summary.heroes.is_empty() {
// let mut names = stream::iter(inner.summary.heroes.iter())
// .take(3)
// .filter_map(|u| async move {
// let user_id = UserId::try_from(u.as_str()).ok()?;
// self.get_member(&user_id).await
// })
// .map(|mem| {
// mem.display_name()
// .map(|d| d.to_string())
// .unwrap_or_else(|| mem.user_id().localpart().to_string())
// })
// .collect::<Vec<String>>()
// .await;
// names.sort();
// names.join(", ")
// } else if heroes_count >= invited_joined {
// let mut names = members
// .filter(|m| future::ready(is_own_member(m)))
// .take(3)
// .map(|mem| {
// mem.display_name()
// .map(|d| d.to_string())
// .unwrap_or_else(|| mem.user_id().localpart().to_string())
// })
// .collect::<Vec<String>>()
// .await;
// // stabilize ordering
// names.sort();
// names.join(", ")
// } else if heroes_count < invited_joined && invited + joined > 1 {
// let mut names = members
// .filter(|m| future::ready(is_own_member(m)))
// .take(3)
// .map(|mem| {
// mem.display_name()
// .map(|d| d.to_string())
// .unwrap_or_else(|| mem.user_id().localpart().to_string())
// })
// .collect::<Vec<String>>()
// .await;
// names.sort();
// // TODO: What length does the spec want us to use here and in
// // the `else`?
// format!("{}, and {} others", names.join(", "), (joined + invited))
// } else {
// "Empty room".to_string()
// }
}
}
pub fn own_user_id(&self) -> &UserId {
&self.own_user_id
}
pub(crate) fn clone_summary(&self) -> StrippedRoomInfo {
(*self.inner.lock().unwrap()).clone()
}
pub fn is_encrypted(&self) -> bool {
self.inner.lock().unwrap().encryption.is_some()
}
pub fn room_id(&self) -> &RoomId {
&self.room_id
}
pub async fn display_name(&self) -> String {
self.calculate_name().await
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StrippedRoomInfo {
pub room_id: Arc<RoomId>,
pub name: Option<String>,
pub canonical_alias: Option<RoomAliasId>,
pub avatar_url: Option<String>,
pub topic: Option<String>,
pub encryption: Option<EncryptionEventContent>,
}
impl StrippedRoomInfo {
pub fn handle_state_event(&mut self, event: &AnyStrippedStateEvent) -> bool {
match event {
AnyStrippedStateEvent::RoomEncryption(encryption) => {
self.encryption = Some(encryption.content.clone());
true
}
AnyStrippedStateEvent::RoomName(n) => {
self.name = n.content.name().map(|n| n.to_string());
true
}
AnyStrippedStateEvent::RoomCanonicalAlias(a) => {
self.canonical_alias = a.content.alias.clone();
true
}
AnyStrippedStateEvent::RoomTopic(t) => {
self.topic = Some(t.content.topic.clone());
true
}
_ => false,
}
}
pub fn is_encrypted(&self) -> bool {
self.encryption.is_some()
}
pub fn serialize(&self) -> Vec<u8> {
serde_json::to_vec(&self).unwrap()
}
}

View File

@ -4,7 +4,8 @@ use futures::stream::{self, Stream};
use matrix_sdk_common::{ use matrix_sdk_common::{
events::{ events::{
presence::PresenceEvent, room::member::MemberEventContent, AnyBasicEvent, presence::PresenceEvent, room::member::MemberEventContent, AnyBasicEvent,
AnyStrippedStateEvent, AnySyncStateEvent, EventContent, EventType, SyncStateEvent, AnyStrippedStateEvent, AnySyncStateEvent, EventContent, EventType, StrippedStateEvent,
SyncStateEvent,
}, },
identifiers::{RoomId, UserId}, identifiers::{RoomId, UserId},
}; };
@ -22,9 +23,12 @@ pub struct Store {
members: Tree, members: Tree,
joined_user_ids: Tree, joined_user_ids: Tree,
invited_user_ids: Tree, invited_user_ids: Tree,
room_summaries: Tree, room_info: Tree,
room_state: Tree, room_state: Tree,
room_account_data: Tree, room_account_data: Tree,
stripped_room_info: Tree,
stripped_room_state: Tree,
stripped_members: Tree,
presence: Tree, presence: Tree,
} }
@ -35,13 +39,14 @@ pub struct StateChanges {
pub state: BTreeMap<RoomId, BTreeMap<String, AnySyncStateEvent>>, pub state: BTreeMap<RoomId, BTreeMap<String, AnySyncStateEvent>>,
pub account_data: BTreeMap<String, AnyBasicEvent>, pub account_data: BTreeMap<String, AnyBasicEvent>,
pub room_account_data: BTreeMap<RoomId, BTreeMap<String, AnyBasicEvent>>, pub room_account_data: BTreeMap<RoomId, BTreeMap<String, AnyBasicEvent>>,
pub room_summaries: BTreeMap<RoomId, RoomInfo>, pub room_infos: BTreeMap<RoomId, RoomInfo>,
// display_names: BTreeMap<RoomId, BTreeMap<String, BTreeMap<UserId, ()>>>,
pub joined_user_ids: BTreeMap<RoomId, Vec<UserId>>, pub joined_user_ids: BTreeMap<RoomId, Vec<UserId>>,
pub invited_user_ids: BTreeMap<RoomId, Vec<UserId>>, pub invited_user_ids: BTreeMap<RoomId, Vec<UserId>>,
pub removed_user_ids: BTreeMap<RoomId, UserId>, pub removed_user_ids: BTreeMap<RoomId, UserId>,
pub presence: BTreeMap<UserId, PresenceEvent>, pub presence: BTreeMap<UserId, PresenceEvent>,
pub invitest_state: BTreeMap<RoomId, BTreeMap<String, AnyStrippedStateEvent>>, pub stripped_state: BTreeMap<RoomId, BTreeMap<String, AnyStrippedStateEvent>>,
pub stripped_members:
BTreeMap<RoomId, BTreeMap<UserId, StrippedStateEvent<MemberEventContent>>>,
pub invited_room_info: BTreeMap<RoomId, RoomInfo>, pub invited_room_info: BTreeMap<RoomId, RoomInfo>,
} }
@ -83,7 +88,7 @@ impl StateChanges {
} }
pub fn add_room(&mut self, room: RoomInfo) { pub fn add_room(&mut self, room: RoomInfo) {
self.room_summaries self.room_infos
.insert(room.room_id.as_ref().to_owned(), room); .insert(room.room_id.as_ref().to_owned(), room);
} }
@ -99,13 +104,25 @@ impl StateChanges {
.insert(event.content().event_type().to_owned(), event); .insert(event.content().event_type().to_owned(), event);
} }
pub fn add_invited_state(&mut self, room_id: &RoomId, event: AnyStrippedStateEvent) { pub fn add_stripped_state_event(&mut self, room_id: &RoomId, event: AnyStrippedStateEvent) {
self.invitest_state self.stripped_state
.entry(room_id.to_owned()) .entry(room_id.to_owned())
.or_insert_with(BTreeMap::new) .or_insert_with(BTreeMap::new)
.insert(event.state_key().to_string(), event); .insert(event.state_key().to_string(), event);
} }
pub fn add_stripped_member(
&mut self,
room_id: &RoomId,
event: StrippedStateEvent<MemberEventContent>,
) {
let user_id = UserId::try_from(event.state_key.as_str()).unwrap();
self.stripped_members
.entry(room_id.to_owned())
.or_insert_with(BTreeMap::new)
.insert(user_id, event);
}
pub fn add_state_event(&mut self, room_id: &RoomId, event: AnySyncStateEvent) { pub fn add_state_event(&mut self, room_id: &RoomId, event: AnySyncStateEvent) {
self.state self.state
.entry(room_id.to_owned()) .entry(room_id.to_owned())
@ -140,10 +157,14 @@ impl Store {
let invited_user_ids = db.open_tree("invited_user_ids").unwrap(); let invited_user_ids = db.open_tree("invited_user_ids").unwrap();
let room_state = db.open_tree("room_state").unwrap(); let room_state = db.open_tree("room_state").unwrap();
let room_summaries = db.open_tree("room_summaries").unwrap(); let room_info = db.open_tree("room_infos").unwrap();
let presence = db.open_tree("presence").unwrap(); let presence = db.open_tree("presence").unwrap();
let room_account_data = db.open_tree("room_account_data").unwrap(); let room_account_data = db.open_tree("room_account_data").unwrap();
let stripped_room_info = db.open_tree("stripped_room_info").unwrap();
let stripped_members = db.open_tree("stripped_members").unwrap();
let stripped_room_state = db.open_tree("stripped_room_state").unwrap();
Self { Self {
inner: db, inner: db,
session, session,
@ -154,7 +175,10 @@ impl Store {
room_account_data, room_account_data,
presence, presence,
room_state, room_state,
room_summaries, room_info,
stripped_room_info,
stripped_members,
stripped_room_state,
} }
} }
@ -191,10 +215,13 @@ impl Store {
&self.members, &self.members,
&self.joined_user_ids, &self.joined_user_ids,
&self.invited_user_ids, &self.invited_user_ids,
&self.room_summaries, &self.room_info,
&self.room_state, &self.room_state,
&self.room_account_data, &self.room_account_data,
&self.presence, &self.presence,
&self.stripped_room_info,
&self.stripped_members,
&self.stripped_room_state,
) )
.transaction( .transaction(
|( |(
@ -207,15 +234,18 @@ impl Store {
state, state,
room_account_data, room_account_data,
presence, presence,
striped_rooms,
stripped_members,
stripped_state,
)| { )| {
if let Some(s) = &changes.session { if let Some(s) = &changes.session {
session.insert("session", serde_json::to_vec(s).unwrap())?; session.insert("session", serde_json::to_vec(s).unwrap())?;
} }
for (room, events) in &changes.members { for (room, events) in &changes.members {
for (user_id, event) in events { for (_, event) in events {
members.insert( members.insert(
format!("{}{}", room.as_str(), user_id.as_str()).as_str(), format!("{}{}", room.as_str(), &event.state_key).as_str(),
serde_json::to_vec(&event).unwrap(), serde_json::to_vec(&event).unwrap(),
)?; )?;
} }
@ -268,7 +298,7 @@ impl Store {
} }
} }
for (room_id, summary) in &changes.room_summaries { for (room_id, summary) in &changes.room_infos {
summaries.insert(room_id.as_bytes(), summary.serialize())?; summaries.insert(room_id.as_bytes(), summary.serialize())?;
} }
@ -276,6 +306,35 @@ impl Store {
presence.insert(sender.as_bytes(), serde_json::to_vec(&event).unwrap())?; presence.insert(sender.as_bytes(), serde_json::to_vec(&event).unwrap())?;
} }
for (room_id, info) in &changes.invited_room_info {
striped_rooms
.insert(room_id.as_str(), serde_json::to_vec(&info).unwrap())?;
}
for (room, events) in &changes.stripped_members {
for (_, event) in events {
stripped_members.insert(
format!("{}{}", room.as_str(), &event.state_key).as_str(),
serde_json::to_vec(&event).unwrap(),
)?;
}
}
for (room, events) in &changes.stripped_state {
for (_, event) in events {
stripped_state.insert(
format!(
"{}{}{}",
room.as_str(),
event.content().event_type(),
event.state_key(),
)
.as_bytes(),
serde_json::to_vec(&event).unwrap(),
)?;
}
}
Ok(()) Ok(())
}, },
); );
@ -337,7 +396,7 @@ impl Store {
pub async fn get_room_infos(&self) -> impl Stream<Item = RoomInfo> { pub async fn get_room_infos(&self) -> impl Stream<Item = RoomInfo> {
stream::iter( stream::iter(
self.room_summaries self.room_info
.iter() .iter()
.map(|r| serde_json::from_slice(&r.unwrap().1).unwrap()), .map(|r| serde_json::from_slice(&r.unwrap().1).unwrap()),
) )