base: More work on the new state store.

master
Damir Jelić 2020-10-22 09:46:12 +02:00
parent 5323e6e270
commit bdf32eecc7
2 changed files with 53 additions and 17 deletions

View File

@ -14,6 +14,7 @@
// limitations under the License. // limitations under the License.
use std::{ use std::{
convert::TryFrom,
fmt, fmt,
path::{Path, PathBuf}, path::{Path, PathBuf},
result::Result as StdResult, result::Result as StdResult,
@ -47,7 +48,11 @@ use matrix_sdk_crypto::{
}; };
use zeroize::Zeroizing; use zeroize::Zeroizing;
use crate::{error::Result, session::Session, store::Store}; use crate::{
error::Result,
session::Session,
store::{StateChanges, Store},
};
pub type Token = String; pub type Token = String;
@ -103,23 +108,22 @@ fn hoist_room_event_prev_content(event: &Raw<AnySyncRoomEvent>) -> Option<Raw<An
/// Transform state event by hoisting `prev_content` field from `unsigned` to the top level. /// Transform state event by hoisting `prev_content` field from `unsigned` to the top level.
/// ///
/// See comment of `hoist_room_event_prev_content`. /// See comment of `hoist_room_event_prev_content`.
fn hoist_state_event_prev_content( fn hoist_and_deserialize_state_event(
event: &Raw<AnySyncStateEvent>, event: &Raw<AnySyncStateEvent>,
) -> Option<Raw<AnySyncStateEvent>> { ) -> StdResult<AnySyncStateEvent, serde_json::Error> {
let prev_content = serde_json::from_str::<AdditionalEventData>(event.json().get()) let prev_content = serde_json::from_str::<AdditionalEventData>(event.json().get())?
.map(|more_unsigned| more_unsigned.unsigned) .unsigned
.map(|additional| additional.prev_content) .prev_content;
.ok()
.flatten()?;
let mut ev = event.deserialize().ok()?; let mut ev = event.deserialize()?;
match &mut ev {
AnySyncStateEvent::RoomMember(ref mut member) if member.prev_content.is_none() => { if let AnySyncStateEvent::RoomMember(ref mut member) = ev {
member.prev_content = Some(prev_content.deserialize().ok()?); if member.prev_content.is_none() {
Some(Raw::from(ev)) member.prev_content = prev_content.map(|e| e.deserialize().ok()).flatten();
} }
_ => None,
} }
Ok(ev)
} }
fn stripped_deserialize_prev_content( fn stripped_deserialize_prev_content(
@ -418,6 +422,34 @@ impl BaseClient {
} }
} }
let changes = StateChanges::default();
for (room_id, room) in &response.rooms.join {
for e in &room.state.events {
if let Ok(event) = hoist_and_deserialize_state_event(e) {
match event {
AnySyncStateEvent::RoomMember(member) => {
let member_id = UserId::try_from(member.state_key).unwrap();
let prev_member =
self.store.get_member_event(room_id, &member_id).await;
use matrix_sdk_common::events::room::member::MembershipState::*;
match member.content.membership {
Join => {
// TODO check if the display name is
// ambigous
}
_ => (),
}
}
_ => (),
}
}
}
}
self.store.save_changes(&changes).await;
Ok(()) Ok(())
} }

View File

@ -28,7 +28,11 @@ pub struct StateChanges {
} }
impl StateChanges { impl StateChanges {
pub fn add_event(&mut self, room_id: &RoomId, event: SyncStateEvent<MemberEventContent>) { pub fn add_joined_member(
&mut self,
room_id: &RoomId,
event: SyncStateEvent<MemberEventContent>,
) {
let user_id = UserId::try_from(event.state_key.as_str()).unwrap(); let user_id = UserId::try_from(event.state_key.as_str()).unwrap();
self.members self.members
.entry(room_id.to_owned()) .entry(room_id.to_owned())
@ -38,7 +42,7 @@ impl StateChanges {
pub fn from_event(room_id: &RoomId, event: SyncStateEvent<MemberEventContent>) -> Self { pub fn from_event(room_id: &RoomId, event: SyncStateEvent<MemberEventContent>) -> Self {
let mut changes = Self::default(); let mut changes = Self::default();
changes.add_event(room_id, event); changes.add_joined_member(room_id, event);
changes changes
} }
@ -90,7 +94,7 @@ impl Store {
self.inner.flush_async().await.unwrap(); self.inner.flush_async().await.unwrap();
} }
pub fn get_member_event( pub async fn get_member_event(
&self, &self,
room_id: &RoomId, room_id: &RoomId,
state_key: &UserId, state_key: &UserId,