Skip to content

Commit

Permalink
fixup! sdk-ui: use RoomInfo to update the room encryption for the `…
Browse files Browse the repository at this point in the history
…Timeline` and its items
  • Loading branch information
jmartinesp committed Sep 20, 2024
1 parent f12c24f commit 71a8e24
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
24 changes: 14 additions & 10 deletions crates/matrix-sdk-ui/src/timeline/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,23 +347,27 @@ impl<P: RoomDataProvider> TimelineController<P> {

/// Listens to encryption state changes for the room in
/// [`matrix_sdk_base::RoomInfo`] and applies the new value to the
/// existing timeline items.
/// existing timeline items. This will then cause a refresh of those
/// timeline items.
pub async fn handle_encryption_state_changes(&self) {
let mut room_info = self.room_data_provider.room_info();

while let Some(info) = room_info.next().await {
let mut changed = false;
if let Ok(mut is_room_encrypted) =
self.state.read().await.meta.is_room_encrypted.write()
{
let changed = {
let state = self.state.read().await;
let mut old_is_room_encrypted = state.meta.is_room_encrypted.write().unwrap();
let is_encrypted_now = info.is_encrypted();
if is_room_encrypted.is_none() || is_room_encrypted.unwrap() != is_encrypted_now {
changed = true;
*is_room_encrypted = Some(is_encrypted_now);
if *old_is_room_encrypted != Some(is_encrypted_now) {
*old_is_room_encrypted = Some(is_encrypted_now);
true
} else {
false
}
}
};

if changed {
let mut state = self.state.write().await;
state.reload_shields();
state.update_all_events_is_room_encrypted();
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions crates/matrix-sdk-ui/src/timeline/controller/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,14 @@ impl TimelineState {
result
}

pub(super) fn reload_shields(&mut self) {
let Ok(is_room_encrypted) = self.meta.is_room_encrypted.read() else {
return;
};
pub(super) fn update_all_events_is_room_encrypted(&mut self) {
let is_room_encrypted = *self.meta.is_room_encrypted.read().unwrap();

if let Some(is_encrypted) = *is_room_encrypted {
drop(is_room_encrypted);
let mut txn = self.transaction();
txn.replace_all_events_encryption(is_encrypted);
txn.commit();
}
// When this transaction finishes, all items in the timeline will be emitted
// again with the updated encryption value
let mut txn = self.transaction();
txn.update_all_events_is_room_encrypted(is_room_encrypted);
txn.commit();
}

pub(super) fn transaction(&mut self) -> TimelineStateTransaction<'_> {
Expand Down Expand Up @@ -739,12 +736,18 @@ impl TimelineStateTransaction<'_> {
adjuster.run(&mut self.items, &mut self.meta);
}

fn replace_all_events_encryption(&mut self, is_encrypted: bool) {
/// This method replaces the `is_room_encrypted` value for all timeline
/// items to its updated version and creates a `VectorDiff::Set` operation
/// for each item which will be added to this transaction.
fn update_all_events_is_room_encrypted(&mut self, is_encrypted: Option<bool>) {
for idx in 0..self.items.len() {
let item = &self.items[idx];

if let Some(event) = item.as_event() {
let mut cloned_event = event.clone();
cloned_event.is_room_encrypted = Some(is_encrypted);
cloned_event.is_room_encrypted = is_encrypted;

// Replace the existing item with a new version with the right encryption flag
let item = item.with_kind(cloned_event);
self.items.set(idx, item);
}
Expand Down

0 comments on commit 71a8e24

Please sign in to comment.