Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk-ui: make room encryption optional to create a timeline #4021

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jmartinesp
Copy link
Contributor

@jmartinesp jmartinesp commented Sep 19, 2024

Instead of forcing the room encryption to be known when the timeline is created and failing if it's not known, take the latest room encryption info as a base value and update it when processing timeline events.

At the time of writing this PR, the encryption info is only used to decide whether shields should be calculated for timeline items or not.

Fixes #3850 (I hope).

  • Public API changes documented in changelogs (optional)

Signed-off-by:

@jmartinesp jmartinesp requested a review from a team as a code owner September 19, 2024 08:37
@jmartinesp jmartinesp requested review from poljar and removed request for a team September 19, 2024 08:37
Copy link

codecov bot commented Sep 19, 2024

Codecov Report

Attention: Patch coverage is 95.45455% with 2 lines in your changes missing coverage. Please review.

Project coverage is 84.25%. Comparing base (794dbb3) to head (4182ec2).

Files with missing lines Patch % Lines
...rates/matrix-sdk-ui/src/timeline/controller/mod.rs 92.85% 1 Missing ⚠️
crates/matrix-sdk-ui/src/timeline/event_handler.rs 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4021      +/-   ##
==========================================
- Coverage   84.26%   84.25%   -0.01%     
==========================================
  Files         266      266              
  Lines       28349    28391      +42     
==========================================
+ Hits        23888    23921      +33     
- Misses       4461     4470       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jmartinesp jmartinesp force-pushed the misc/optional-encryption-in-timeline branch from 0bddcfd to f12c24f Compare September 19, 2024 16:20
Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some suggestions and questions. Please feel free to use whitespace more lavishly.

/// [`matrix_sdk_base::RoomInfo`] and applies the new value to the
/// existing timeline items.
pub async fn handle_encryption_state_changes(&self) {
let mut room_info = self.room_data_provider.room_info();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut room_info = self.room_data_provider.room_info();
let mut room_info = self.room_data_provider.room_info();

@@ -345,6 +345,29 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this could e a bit clearer, the function also reloads shield states so we might want to call this out as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this function isn't recalculating the shield states: that is created on the fly every time we call the fn EventTimelineItem::get_shield, based on both is_room_encrypted and the remote event's encryption info.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut changed = false;
let mut changed = false;

changed = true;
*is_room_encrypted = Some(is_encrypted_now);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

@@ -295,6 +300,19 @@ impl TimelineState {
result
}

pub(super) fn reload_shields(&mut self) {
let Ok(is_room_encrypted) = self.meta.is_room_encrypted.read() else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally unwrap() if a lock is poisoned, it's the only place where we allow unwraps.

return;
};

if let Some(is_encrypted) = *is_room_encrypted {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to do this only if it's Some() as well as true? On the other hand, the only place where we call this is in a place where we know the value of this and if it'll be true, why don't we just unconditionally call replace_all_events_encryption()?


fn replace_all_events_encryption(&mut self, is_encrypted: bool) {
for idx in 0..self.items.len() {
let item = &self.items[idx];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let item = &self.items[idx];
let item = &self.items[idx];

@@ -720,6 +738,18 @@ impl TimelineStateTransaction<'_> {
fn adjust_day_dividers(&mut self, mut adjuster: DayDividerAdjuster) {
adjuster.run(&mut self.items, &mut self.meta);
}

fn replace_all_events_encryption(&mut self, is_encrypted: bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably a really poor name for this method, at least I can't really explain from the name what this method does.

Nor is it quite clear that this method does enough, it's just setting a flag on the event that the room is encrypted? How will this trigger shield replacements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add some docs for it and maybe find a new name, but it's kind of difficult, since all it does is replace the is_room_encrypted value for each timeline item and create a VectorDiff::Set for each item so this update is emitted.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cloned_event.is_room_encrypted = Some(is_encrypted);
cloned_event.is_room_encrypted = Some(is_encrypted);

@jmartinesp
Copy link
Contributor Author

I tried following your suggestions in 71a8e24 🤞 .

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a small nit, I think that this is fine now.

@jmartinesp jmartinesp force-pushed the misc/optional-encryption-in-timeline branch from cc9bcbf to e44efb4 Compare September 23, 2024 12:02
Instead of forcing the room encryption to be known when the timeline is created and failing if it's not known, take the latest room encryption info as a base value and update it when processing timeline events.

At the time of writing this commit, the encryption info is only used to decide whether shields should be calculated for timeline items or not.
@jmartinesp jmartinesp force-pushed the misc/optional-encryption-in-timeline branch from e44efb4 to 4182ec2 Compare September 23, 2024 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

timeline: is_room_encrypted should be part of the RoomProvider trait
2 participants