base: Correctly store the state events of rooms.

master
Damir Jelić 2020-12-10 10:01:53 +01:00
parent ae33904a93
commit a4e7dc1042
3 changed files with 33 additions and 18 deletions

View File

@ -25,7 +25,7 @@ docs = ["encryption", "sqlite_cryptostore", "messages"]
[dependencies] [dependencies]
async-trait = "0.1.41" async-trait = "0.1.41"
serde = "1.0.117" serde = { version = "1.0.117", features = ["rc"]}
dashmap= "*" dashmap= "*"
serde_json = "1.0.59" serde_json = "1.0.59"
zeroize = "1.1.1" zeroize = "1.1.1"
@ -51,6 +51,8 @@ http = "0.2.1"
tracing-subscriber = "0.2.13" tracing-subscriber = "0.2.13"
tempfile = "3.1.0" tempfile = "3.1.0"
mockito = "0.27.0" mockito = "0.27.0"
rustyline = "7.0.0"
syntect = "4.4.0"
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "0.2.22", default-features = false, features = ["rt-threaded", "macros"] } tokio = { version = "0.2.22", default-features = false, features = ["rt-threaded", "macros"] }

View File

@ -32,7 +32,7 @@ use matrix_sdk_common::{
room::member::MemberEventContent, AnyBasicEvent, AnyStrippedStateEvent, AnySyncRoomEvent, room::member::MemberEventContent, AnyBasicEvent, AnyStrippedStateEvent, AnySyncRoomEvent,
AnySyncStateEvent, StateEvent, SyncStateEvent, AnySyncStateEvent, StateEvent, SyncStateEvent,
}, },
identifiers::{RoomId, UserId}, identifiers::{RoomId, UserId, room_id},
locks::RwLock, locks::RwLock,
Raw, Raw,
}; };
@ -49,7 +49,7 @@ use matrix_sdk_crypto::{
Device, EncryptionSettings, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, Sas, Device, EncryptionSettings, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, Sas,
ToDeviceRequest, UserDevices, ToDeviceRequest, UserDevices,
}; };
use tracing::{info, warn}; use tracing::{error, info, warn};
use zeroize::Zeroizing; use zeroize::Zeroizing;
use crate::{ use crate::{
@ -58,7 +58,7 @@ use crate::{
AccountData, Ephemeral, JoinedRoom, LeftRoom, Presence, Rooms, State, SyncResponse, AccountData, Ephemeral, JoinedRoom, LeftRoom, Presence, Rooms, State, SyncResponse,
Timeline, Timeline,
}, },
rooms::{RoomInfo, Room, RoomType}, rooms::{Room, RoomInfo, RoomType},
session::Session, session::Session,
store::{StateChanges, Store}, store::{StateChanges, Store},
}; };
@ -515,19 +515,26 @@ impl BaseClient {
let mut state = State::default(); let mut state = State::default();
for e in events { for e in events {
if let Ok(event) = hoist_and_deserialize_state_event(e) { match hoist_and_deserialize_state_event(e) {
Ok(event) => {
match &event { match &event {
AnySyncStateEvent::RoomMember(member) => { AnySyncStateEvent::RoomMember(member) => {
handle_membership(&mut changes, room_id, member); handle_membership(&mut changes, room_id, member);
} }
e => { e => {
summary.handle_state_event(&e); summary.handle_state_event(&e);
changes.add_state_event(room_id, e.clone()); changes.add_state_event(room_id, e.clone());
} }
} }
state.events.push(event); state.events.push(event);
} }
Err(err) => warn!(
"Couldn't deserialize state event for room {}: {:?} {:#?}",
room_id, err, e
),
}
} }
state state

View File

@ -110,7 +110,7 @@ impl StateChanges {
self.state self.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.content().event_type().to_string(), event);
} }
pub fn from_event(room_id: &RoomId, event: SyncStateEvent<MemberEventContent>) -> Self { pub fn from_event(room_id: &RoomId, event: SyncStateEvent<MemberEventContent>) -> Self {
@ -254,13 +254,13 @@ impl Store {
} }
for (room, events) in &changes.state { for (room, events) in &changes.state {
for (state_key, event) in events { for (_, event) in events {
state.insert( state.insert(
format!( format!(
"{}{}{}", "{}{}{}",
room.as_str(), room.as_str(),
event.content().event_type(), event.content().event_type(),
state_key event.state_key(),
) )
.as_bytes(), .as_bytes(),
serde_json::to_vec(&event).unwrap(), serde_json::to_vec(&event).unwrap(),
@ -335,6 +335,12 @@ impl Store {
) )
} }
pub async fn get_room_infos(&self) -> impl Stream<Item = RoomInfo> {
stream::iter(
self.room_summaries.iter().map(|r| serde_json::from_slice(&r.unwrap().1).unwrap())
)
}
pub fn get_session(&self) -> Option<Session> { pub fn get_session(&self) -> Option<Session> {
self.session self.session
.get("session") .get("session")