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]
async-trait = "0.1.41"
serde = "1.0.117"
serde = { version = "1.0.117", features = ["rc"]}
dashmap= "*"
serde_json = "1.0.59"
zeroize = "1.1.1"
@ -51,6 +51,8 @@ http = "0.2.1"
tracing-subscriber = "0.2.13"
tempfile = "3.1.0"
mockito = "0.27.0"
rustyline = "7.0.0"
syntect = "4.4.0"
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
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,
AnySyncStateEvent, StateEvent, SyncStateEvent,
},
identifiers::{RoomId, UserId},
identifiers::{RoomId, UserId, room_id},
locks::RwLock,
Raw,
};
@ -49,7 +49,7 @@ use matrix_sdk_crypto::{
Device, EncryptionSettings, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, Sas,
ToDeviceRequest, UserDevices,
};
use tracing::{info, warn};
use tracing::{error, info, warn};
use zeroize::Zeroizing;
use crate::{
@ -58,7 +58,7 @@ use crate::{
AccountData, Ephemeral, JoinedRoom, LeftRoom, Presence, Rooms, State, SyncResponse,
Timeline,
},
rooms::{RoomInfo, Room, RoomType},
rooms::{Room, RoomInfo, RoomType},
session::Session,
store::{StateChanges, Store},
};
@ -515,18 +515,25 @@ impl BaseClient {
let mut state = State::default();
for e in events {
if let Ok(event) = hoist_and_deserialize_state_event(e) {
match &event {
AnySyncStateEvent::RoomMember(member) => {
handle_membership(&mut changes, room_id, member);
}
e => {
summary.handle_state_event(&e);
changes.add_state_event(room_id, e.clone());
}
}
match hoist_and_deserialize_state_event(e) {
Ok(event) => {
match &event {
AnySyncStateEvent::RoomMember(member) => {
handle_membership(&mut changes, room_id, member);
}
e => {
summary.handle_state_event(&e);
state.events.push(event);
changes.add_state_event(room_id, e.clone());
}
}
state.events.push(event);
}
Err(err) => warn!(
"Couldn't deserialize state event for room {}: {:?} {:#?}",
room_id, err, e
),
}
}

View File

@ -110,7 +110,7 @@ impl StateChanges {
self.state
.entry(room_id.to_owned())
.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 {
@ -254,13 +254,13 @@ impl Store {
}
for (room, events) in &changes.state {
for (state_key, event) in events {
for (_, event) in events {
state.insert(
format!(
"{}{}{}",
room.as_str(),
event.content().event_type(),
state_key
event.state_key(),
)
.as_bytes(),
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> {
self.session
.get("session")