base: WIP start to split out the steps collect changes, save changes,

apply changes.
master
Damir Jelić 2020-10-25 21:05:18 +01:00
parent dc57873687
commit 3f57ba57d0
4 changed files with 87 additions and 64 deletions

View File

@ -86,7 +86,7 @@ pub struct AdditionalUnsignedData {
/// ///
/// [synapse-bug]: <https://github.com/matrix-org/matrix-doc/issues/684#issuecomment-641182668> /// [synapse-bug]: <https://github.com/matrix-org/matrix-doc/issues/684#issuecomment-641182668>
/// [discussion]: <https://github.com/matrix-org/matrix-doc/issues/684#issuecomment-641182668> /// [discussion]: <https://github.com/matrix-org/matrix-doc/issues/684#issuecomment-641182668>
fn hoist_and_deserialize_state_event( pub fn hoist_and_deserialize_state_event(
event: &Raw<AnySyncStateEvent>, event: &Raw<AnySyncStateEvent>,
) -> StdResult<AnySyncStateEvent, serde_json::Error> { ) -> 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())?
@ -444,6 +444,9 @@ impl BaseClient {
} }
} }
// TODO we'll want a flow where we calculate changes using a room
// summary snapshot, then we store the new snapshots, only then do we
// apply and emit the new events and rooms.
let mut changes = StateChanges::default(); let mut changes = StateChanges::default();
let handle_membership = let handle_membership =
@ -472,45 +475,43 @@ impl BaseClient {
for (room_id, room_info) in &response.rooms.join { for (room_id, room_info) in &response.rooms.join {
let room = self.get_or_create_room(room_id, RoomType::Joined); let room = self.get_or_create_room(room_id, RoomType::Joined);
if room.update_summary(&room_info.summary) { // if room.update_summary(&room_info.summary) {
changes.add_room(room.clone()) // changes.add_room(room.clone())
} // }
for e in &room_info.state.events { // for e in &room_info.state.events {
if let Ok(event) = hoist_and_deserialize_state_event(e) { // if let Ok(event) = hoist_and_deserialize_state_event(e) {
match event { // match event {
AnySyncStateEvent::RoomMember(member) => { // AnySyncStateEvent::RoomMember(member) => {
handle_membership(&mut changes, room_id, member); // handle_membership(&mut changes, room_id, member);
} // }
_ => { // _ => {
if room.handle_state_event(&event) { // changes.add_room(room.handle_state_event(&event));
changes.add_room(room.clone()); // changes.add_state_event(room_id, event);
} // }
changes.add_state_event(room_id, event); // }
} // }
} // }
}
}
if room.set_prev_batch(room_info.timeline.prev_batch.clone()) { // if room.set_prev_batch(room_info.timeline.prev_batch.clone()) {
changes.add_room(room.clone()); // changes.add_room(room.clone());
} // }
for event in &room_info.timeline.events { for event in &room_info.timeline.events {
if let Ok(e) = hoist_room_event_prev_content(event) { if let Ok(e) = hoist_room_event_prev_content(event) {
match e { match e {
AnySyncRoomEvent::State(s) => { AnySyncRoomEvent::State(s) => {
match s { // match s {
AnySyncStateEvent::RoomMember(member) => { // AnySyncStateEvent::RoomMember(member) => {
handle_membership(&mut changes, room_id, member); // handle_membership(&mut changes, room_id, member);
} // }
_ => { // _ => {
if room.handle_state_event(&s) { // if room.handle_state_event(&s) {
changes.add_room(room.clone()); // changes.add_room(room.clone());
} // }
changes.add_state_event(room_id, s); // changes.add_state_event(room_id, s);
} // }
} // }
} }
AnySyncRoomEvent::Message(_) => { AnySyncRoomEvent::Message(_) => {
// TODO decrypt the event if it's an encrypted one. // TODO decrypt the event if it's an encrypted one.

View File

@ -37,13 +37,14 @@ pub struct Store {
} }
use crate::Session; use crate::Session;
use crate::client::hoist_and_deserialize_state_event;
#[derive(Debug, Default)] #[derive(Debug, Default)]
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>>, state: BTreeMap<RoomId, BTreeMap<String, AnySyncStateEvent>>,
room_summaries: BTreeMap<RoomId, Room>, room_summaries: BTreeMap<RoomId, InnerSummary>,
// display_names: BTreeMap<RoomId, BTreeMap<String, BTreeMap<UserId, ()>>>, // display_names: BTreeMap<RoomId, BTreeMap<String, BTreeMap<UserId, ()>>>,
joined_user_ids: BTreeMap<RoomId, UserId>, joined_user_ids: BTreeMap<RoomId, UserId>,
invited_user_ids: BTreeMap<RoomId, UserId>, invited_user_ids: BTreeMap<RoomId, UserId>,
@ -79,8 +80,8 @@ impl StateChanges {
.insert(user_id, event); .insert(user_id, event);
} }
pub fn add_room(&mut self, room: Room) { pub fn add_room(&mut self, room: InnerSummary) {
self.room_summaries.insert(room.room_id().to_owned(), room); self.room_summaries.insert(room.room_id.as_ref().to_owned(), room);
} }
pub fn add_state_event(&mut self, room_id: &RoomId, event: AnySyncStateEvent) { pub fn add_state_event(&mut self, room_id: &RoomId, event: AnySyncStateEvent) {
@ -153,22 +154,34 @@ impl Room {
} }
pub fn handle_state_events(&self, state_events: &[Raw<AnySyncStateEvent>]) -> InnerSummary { pub fn handle_state_events(&self, state_events: &[Raw<AnySyncStateEvent>]) -> InnerSummary {
// let summary = (&*self.inner.lock().unwrap()).clone();
// for e in state_events {
// if let Ok(event) = hoist_and_deserialize_state_event(e) {
// match event {
// _ => {
// self.handle_state_event(&mut summary, &event);
// }
// }
// }
// }
todo!(); todo!();
} }
pub fn handle_state_event(&self, event: &AnySyncStateEvent) -> bool { pub fn handle_state_event(&self, summary: &mut InnerSummary, event: &AnySyncStateEvent) -> bool {
match event { match event {
AnySyncStateEvent::RoomEncryption(encryption) => { AnySyncStateEvent::RoomEncryption(encryption) => {
info!("MARKING ROOM {} AS ENCRYPTED", self.room_id); info!("MARKING ROOM {} AS ENCRYPTED", self.room_id);
self.mark_as_encrypted(encryption); summary.encryption = Some(encryption.content.clone());
true true
} }
AnySyncStateEvent::RoomName(n) => { AnySyncStateEvent::RoomName(n) => {
self.set_name(&n); summary.name = n.content.name().map(|n| n.to_string());
true true
} }
AnySyncStateEvent::RoomCanonicalAlias(a) => { AnySyncStateEvent::RoomCanonicalAlias(a) => {
self.set_canonical_alias(&a); summary.canonical_alias = a.content.alias.clone();
true true
} }
_ => false, _ => false,
@ -211,31 +224,10 @@ impl Room {
} }
} }
pub fn update_summary(&self, summary: &RumaSummary) -> bool { pub fn update_summary(&self, summary: InnerSummary) {
let mut inner = self.inner.lock().unwrap(); let mut inner = self.inner.lock().unwrap();
let mut changed = false;
info!("UPDAGING SUMMARY FOR {} WITH {:#?}", self.room_id, summary); info!("UPDAGING SUMMARY FOR {} WITH {:#?}", self.room_id, summary);
*inner = summary;
if !summary.is_empty() {
if !summary.heroes.is_empty() {
inner.summary.heroes = summary.heroes.clone();
changed = true;
}
if let Some(joined) = summary.joined_member_count {
inner.summary.joined_member_count = joined.into();
changed = true;
}
if let Some(invited) = summary.invited_member_count {
inner.summary.invited_member_count = invited.into();
changed = true;
}
}
changed
} }
pub fn get_member(&self, user_id: &UserId) -> Option<RoomMember> { pub fn get_member(&self, user_id: &UserId) -> Option<RoomMember> {
@ -354,6 +346,35 @@ impl InnerSummary {
// } // }
} }
} }
fn update(&mut self, summary: RumaSummary) -> bool {
let mut changed = false;
info!("UPDAGING SUMMARY FOR {} WITH {:#?}", self.room_id, summary);
if !summary.is_empty() {
if !summary.heroes.is_empty() {
self.summary.heroes = summary.heroes.clone();
changed = true;
}
if let Some(joined) = summary.joined_member_count {
self.summary.joined_member_count = joined.into();
changed = true;
}
if let Some(invited) = summary.invited_member_count {
self.summary.invited_member_count = invited.into();
changed = true;
}
}
changed
}
fn serialize(&self) -> Vec<u8> {
serde_json::to_vec(&self).unwrap()
}
} }
impl Store { impl Store {

View File

@ -25,7 +25,7 @@ async-trait = "0.1.41"
matrix-sdk-common-macros = { version = "0.1.0", path = "../matrix_sdk_common_macros" } matrix-sdk-common-macros = { version = "0.1.0", path = "../matrix_sdk_common_macros" }
matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" }
olm-rs = { version = "1.0.0", features = ["serde"] } olm-rs = { path = "/home/poljar/werk/priv/olm-rs", version = "1.0.0", features = ["serde"] }
getrandom = "0.2.0" getrandom = "0.2.0"
serde = { version = "1.0.116", features = ["derive", "rc"] } serde = { version = "1.0.116", features = ["derive", "rc"] }
serde_json = "1.0.59" serde_json = "1.0.59"

View File

@ -313,6 +313,7 @@ impl KeyRequestMachine {
} }
// We ignore cancellations here since there's nothing to serve. // We ignore cancellations here since there's nothing to serve.
Action::CancelRequest => return Ok(None), Action::CancelRequest => return Ok(None),
Action::_Custom(_) => return Ok(None),
}; };
let session = self let session = self