base: Change the way we're saving our room summary updates.

master
Damir Jelić 2020-11-12 11:21:37 +01:00
parent 3a1eeb6a16
commit 133b230964
2 changed files with 98 additions and 102 deletions

View File

@ -469,50 +469,44 @@ impl BaseClient {
info!("ADDING INVITED MEMBER {} to {}", event.state_key, room_id);
changes.add_invited_member(room_id, event)
}
_ => (),
_ => info!("UNHANDLED MEMBERSHIP"),
}
};
for (room_id, room_info) in &response.rooms.join {
let room = self.get_or_create_room(room_id, RoomType::Joined);
// if room.update_summary(&room_info.summary) {
// changes.add_room(room.clone())
// }
let mut summary = room.clone_summary();
summary.update(&room_info.summary);
summary.set_prev_batch(room_info.timeline.prev_batch.as_deref());
// for e in &room_info.state.events {
// if let Ok(event) = hoist_and_deserialize_state_event(e) {
// match event {
// AnySyncStateEvent::RoomMember(member) => {
// handle_membership(&mut changes, room_id, member);
// }
// _ => {
// changes.add_room(room.handle_state_event(&event));
// changes.add_state_event(room_id, event);
// }
// }
// }
// }
// if room.set_prev_batch(room_info.timeline.prev_batch.clone()) {
// changes.add_room(room.clone());
// }
for e in &room_info.state.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);
}
}
}
}
for event in &room_info.timeline.events {
if let Ok(e) = hoist_room_event_prev_content(event) {
match e {
AnySyncRoomEvent::State(s) => {
// match s {
// AnySyncStateEvent::RoomMember(member) => {
// handle_membership(&mut changes, room_id, member);
// }
// _ => {
// if room.handle_state_event(&s) {
// changes.add_room(room.clone());
// }
// changes.add_state_event(room_id, s);
// }
// }
match s {
AnySyncStateEvent::RoomMember(member) => {
handle_membership(&mut changes, room_id, member);
}
_ => {
summary.handle_state_event(&s);
changes.add_state_event(room_id, s);
}
}
}
AnySyncRoomEvent::Message(_) => {
// TODO decrypt the event if it's an encrypted one.
@ -521,11 +515,21 @@ impl BaseClient {
}
}
}
changes.add_room(summary);
}
self.store.save_changes(&changes).await;
*self.sync_token.write().await = Some(response.next_batch.clone());
// TODO emit room changes here
for (room_id, summary) in changes.room_summaries {
if let Some(room) = self.get_joined_room(&room_id) {
room.update_summary(summary)
}
}
Ok(())
}

View File

@ -40,7 +40,7 @@ pub struct StateChanges {
session: Option<Session>,
members: BTreeMap<RoomId, BTreeMap<UserId, SyncStateEvent<MemberEventContent>>>,
state: BTreeMap<RoomId, BTreeMap<String, AnySyncStateEvent>>,
room_summaries: BTreeMap<RoomId, InnerSummary>,
pub room_summaries: BTreeMap<RoomId, InnerSummary>,
// display_names: BTreeMap<RoomId, BTreeMap<String, BTreeMap<UserId, ()>>>,
joined_user_ids: BTreeMap<RoomId, UserId>,
invited_user_ids: BTreeMap<RoomId, UserId>,
@ -112,7 +112,7 @@ pub struct Room {
store: Store,
}
#[derive(Debug, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SomeSummary {
heroes: Vec<String>,
joined_member_count: u64,
@ -120,7 +120,7 @@ pub struct SomeSummary {
}
/// Signals to the `BaseClient` which `RoomState` to send to `EventEmitter`.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum RoomType {
/// Represents a joined room, the `joined_rooms` HashMap will be used.
Joined,
@ -150,48 +150,8 @@ impl Room {
}
}
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!();
}
pub fn handle_state_event(
&self,
summary: &mut InnerSummary,
event: &AnySyncStateEvent,
) -> bool {
match event {
AnySyncStateEvent::RoomEncryption(encryption) => {
info!("MARKING ROOM {} AS ENCRYPTED", self.room_id);
summary.encryption = Some(encryption.content.clone());
true
}
AnySyncStateEvent::RoomName(n) => {
summary.name = n.content.name().map(|n| n.to_string());
true
}
AnySyncStateEvent::RoomCanonicalAlias(a) => {
summary.canonical_alias = a.content.alias.clone();
true
}
_ => false,
}
}
fn serialize(&self) -> Vec<u8> {
let inner = self.inner.lock().unwrap();
serde_json::to_vec(&*inner).unwrap()
pub(crate) fn clone_summary(&self) -> InnerSummary {
(*self.inner.lock().unwrap()).clone()
}
pub async fn joined_user_ids(&self) -> Vec<UserId> {
@ -202,29 +162,6 @@ impl Room {
self.inner.lock().unwrap().encryption.is_some()
}
pub fn mark_as_encrypted(&self, event: &SyncStateEvent<EncryptionEventContent>) {
self.inner.lock().unwrap().encryption = Some(event.content.clone());
}
pub fn set_name(&self, event: &SyncStateEvent<NameEventContent>) {
self.inner.lock().unwrap().name = event.content.name().map(|n| n.to_string());
}
pub fn set_canonical_alias(&self, event: &SyncStateEvent<CanonicalAliasEventContent>) {
self.inner.lock().unwrap().canonical_alias = event.content.alias.clone();
}
pub fn set_prev_batch(&self, prev_batch: Option<String>) -> bool {
let mut inner = self.inner.lock().unwrap();
if inner.last_prev_batch != prev_batch {
inner.last_prev_batch = prev_batch;
true
} else {
false
}
}
pub fn update_summary(&self, summary: InnerSummary) {
let mut inner = self.inner.lock().unwrap();
info!("UPDAGING SUMMARY FOR {} WITH {:#?}", self.room_id, summary);
@ -275,7 +212,7 @@ impl From<SyncStateEvent<MemberEventContent>> for RoomMember {
}
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InnerSummary {
room_id: Arc<RoomId>,
room_type: RoomType,
@ -348,7 +285,62 @@ impl InnerSummary {
}
}
fn update(&mut self, summary: RumaSummary) -> bool {
pub fn handle_state_events(&mut self, state_events: &[Raw<AnySyncStateEvent>]) {
for e in state_events {
if let Ok(event) = hoist_and_deserialize_state_event(e) {
match event {
_ => {
self.handle_state_event(&event);
}
}
}
}
}
pub fn mark_as_encrypted(&mut self, event: &SyncStateEvent<EncryptionEventContent>) {
self.encryption = Some(event.content.clone());
}
pub fn set_name(&mut self, event: &SyncStateEvent<NameEventContent>) {
self.name = event.content.name().map(|n| n.to_string());
}
pub fn set_canonical_alias(&mut self, event: &SyncStateEvent<CanonicalAliasEventContent>) {
self.canonical_alias = event.content.alias.clone();
}
pub fn set_prev_batch(&mut self, prev_batch: Option<&str>) -> bool {
if self.last_prev_batch.as_deref() != prev_batch {
self.last_prev_batch = prev_batch.map(|p| p.to_string());
true
} else {
false
}
}
pub fn handle_state_event(
&mut self,
event: &AnySyncStateEvent,
) -> bool {
match event {
AnySyncStateEvent::RoomEncryption(encryption) => {
info!("MARKING ROOM {} AS ENCRYPTED", self.room_id);
self.encryption = Some(encryption.content.clone());
true
}
AnySyncStateEvent::RoomName(n) => {
self.name = n.content.name().map(|n| n.to_string());
true
}
AnySyncStateEvent::RoomCanonicalAlias(a) => {
self.canonical_alias = a.content.alias.clone();
true
}
_ => false,
}
}
pub(crate) fn update(&mut self, summary: &RumaSummary) -> bool {
let mut changed = false;
info!("UPDAGING SUMMARY FOR {} WITH {:#?}", self.room_id, summary);