base: Wip.

master
Damir Jelić 2020-10-23 09:39:08 +02:00
parent bdf32eecc7
commit 9ce7feea1a
3 changed files with 26 additions and 14 deletions

View File

@ -36,7 +36,7 @@ matrix-sdk-crypto = { version = "0.1.0", path = "../matrix_sdk_crypto", optional
# Misc dependencies # Misc dependencies
thiserror = "1.0.21" thiserror = "1.0.21"
sled = "*" sled = "0.34.4"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio] [target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio]
version = "0.2.22" version = "0.2.22"

View File

@ -27,7 +27,7 @@ use matrix_sdk_common::{
api::r0 as api, api::r0 as api,
events::{ events::{
room::member::MemberEventContent, AnyStrippedStateEvent, AnySyncRoomEvent, room::member::MemberEventContent, AnyStrippedStateEvent, AnySyncRoomEvent,
AnySyncStateEvent, AnySyncStateEvent, SyncStateEvent,
}, },
identifiers::{RoomId, UserId}, identifiers::{RoomId, UserId},
locks::RwLock, locks::RwLock,
@ -126,6 +126,13 @@ fn hoist_and_deserialize_state_event(
Ok(ev) Ok(ev)
} }
fn calculate_membership_change(
existing_event: SyncStateEvent<MemberEventContent>,
new_event: SyncStateEvent<MemberEventContent>,
) -> bool {
false
}
fn stripped_deserialize_prev_content( fn stripped_deserialize_prev_content(
event: &Raw<AnyStrippedStateEvent>, event: &Raw<AnyStrippedStateEvent>,
) -> Option<AdditionalUnsignedData> { ) -> Option<AdditionalUnsignedData> {

View File

@ -1,7 +1,7 @@
use std::{collections::BTreeMap, convert::TryFrom}; use std::{collections::BTreeMap, convert::TryFrom};
use matrix_sdk_common::{ use matrix_sdk_common::{
events::{room::member::MemberEventContent, SyncStateEvent}, events::{room::member::MemberEventContent, AnySyncStateEvent, SyncStateEvent},
identifiers::{RoomId, UserId}, identifiers::{RoomId, UserId},
}; };
use serde_json; use serde_json;
@ -11,8 +11,9 @@ use sled::{transaction::TransactionResult, Config, Db, Transactional, Tree};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Store { pub struct Store {
inner: Db, inner: Db,
session_tree: Tree, session: Tree,
member_tree: Tree, members: Tree,
room_state: Tree,
} }
use crate::Session; use crate::Session;
@ -21,6 +22,8 @@ use crate::Session;
pub struct StateChanges { pub struct StateChanges {
session: Option<Session>, session: Option<Session>,
members: BTreeMap<RoomId, BTreeMap<UserId, SyncStateEvent<MemberEventContent>>>, members: BTreeMap<RoomId, BTreeMap<UserId, SyncStateEvent<MemberEventContent>>>,
state: BTreeMap<RoomId, BTreeMap<String, AnySyncStateEvent>>,
display_names: BTreeMap<RoomId, BTreeMap<String, BTreeMap<UserId, ()>>>,
added_user_ids: BTreeMap<RoomId, UserId>, added_user_ids: BTreeMap<RoomId, UserId>,
invited_user_ids: BTreeMap<RoomId, UserId>, invited_user_ids: BTreeMap<RoomId, UserId>,
@ -60,19 +63,21 @@ impl From<Session> for StateChanges {
impl Store { impl Store {
pub fn open() -> Self { pub fn open() -> Self {
let db = Config::new().temporary(true).open().unwrap(); let db = Config::new().temporary(true).open().unwrap();
let session_tree = db.open_tree("session").unwrap(); let session = db.open_tree("session").unwrap();
let member_tree = db.open_tree("members").unwrap(); let members = db.open_tree("members").unwrap();
let room_state = db.open_tree("members").unwrap();
Self { Self {
inner: db, inner: db,
session_tree, session,
member_tree, members,
room_state,
} }
} }
pub async fn save_changes(&self, changes: &StateChanges) { pub async fn save_changes(&self, changes: &StateChanges) {
let ret: TransactionResult<()> = let ret: TransactionResult<()> =
(&self.session_tree, &self.member_tree).transaction(|(session, members)| { (&self.session, &self.members).transaction(|(session, members)| {
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())?;
} }
@ -99,14 +104,14 @@ impl Store {
room_id: &RoomId, room_id: &RoomId,
state_key: &UserId, state_key: &UserId,
) -> Option<SyncStateEvent<MemberEventContent>> { ) -> Option<SyncStateEvent<MemberEventContent>> {
self.member_tree self.members
.get(format!("{}{}", room_id.as_str(), state_key.as_str())) .get(format!("{}{}", room_id.as_str(), state_key.as_str()))
.unwrap() .unwrap()
.map(|v| serde_json::from_slice(&v).unwrap()) .map(|v| serde_json::from_slice(&v).unwrap())
} }
pub fn get_session(&self) -> Option<Session> { pub fn get_session(&self) -> Option<Session> {
self.session_tree self.session
.get("session") .get("session")
.unwrap() .unwrap()
.map(|s| serde_json::from_slice(&s).unwrap()) .map(|s| serde_json::from_slice(&s).unwrap())
@ -179,10 +184,10 @@ mod test {
let room_id = room_id!("!test:localhost"); let room_id = room_id!("!test:localhost");
let user_id = user_id(); let user_id = user_id();
assert!(store.get_member_event(&room_id, &user_id).is_none()); assert!(store.get_member_event(&room_id, &user_id).await.is_none());
let changes = StateChanges::from_event(&room_id!("!test:localhost"), membership_event()); let changes = StateChanges::from_event(&room_id!("!test:localhost"), membership_event());
store.save_changes(&changes).await; store.save_changes(&changes).await;
assert!(store.get_member_event(&room_id, &user_id).is_some()); assert!(store.get_member_event(&room_id, &user_id).await.is_some());
} }
} }