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

Shields: Add a code field for easier mapping of the state. #3786

Merged
merged 3 commits into from
Aug 5, 2024

Conversation

pixlwave
Copy link
Member

@pixlwave pixlwave commented Jul 31, 2024

Following on from the conversation over on element-hq/element-x-ios#3051 (comment) this PR adds a code field to the ShieldState so that it can be easily mapped by apps that need to provide a translated description to the user.

Note: This PR also adds uniffi to the matrix_sdk_common crate to avoid a duplicate type in the FFI for the code.

@pixlwave pixlwave requested a review from BillCarsonFr August 1, 2024 08:28
Base automatically changed from doug/sent-in-clear to main August 1, 2024 08:47
@BillCarsonFr
Copy link
Member

Draft for now more for discussion, this is an exploration following the conversation over on element-hq/element-x-ios#3051 (comment)

It makes the ShieldState strongly typed so clients don't need to switch over raw strings until a decision is made about how i18n should be handled. The string is still available via ShieldState::message (or message_for_shield_state in the FFI) so we're not losing anything here, plus the tests now assert that the right message is being chosen.

If this is considered a valid direction, then I would also like to suggest the idea of renaming ShieldState to something like EncryptionAuthenticity, considering shields are basically a UI element and it doesn't sound like we'll be using a shield icon for these warnings in Element.

Note: This PR also adds uniffi to the matrix_sdk_common crate to remove the duplicate mapping in the FFI.

As an alternative I was thinking about having the shield state having two fields, a code(machine readable) and a description human_readable string.
Like that you could do i18n on the code and if code is unknown just use the raw human_readable.
Could even be done in a non API breaking way for existing clients?

@pixlwave pixlwave force-pushed the doug/typed-shields branch from be2667b to 187f758 Compare August 1, 2024 09:11
@pixlwave pixlwave changed the title UI Timeline: Strongly typed ShieldStates. Shields: Add a code field for easier mapping of the state. Aug 1, 2024
@pixlwave pixlwave force-pushed the doug/typed-shields branch from db9d8e5 to d5c686b Compare August 1, 2024 10:02
@pixlwave pixlwave marked this pull request as ready for review August 1, 2024 10:03
@pixlwave pixlwave requested a review from a team as a code owner August 1, 2024 10:03
@pixlwave pixlwave requested review from jmartinesp and removed request for a team August 1, 2024 10:03
@pixlwave
Copy link
Member Author

pixlwave commented Aug 1, 2024

Updated to do it that way and have marked as ready for review to let CI run on it.

@pixlwave pixlwave force-pushed the doug/typed-shields branch from d5c686b to ce6ae68 Compare August 1, 2024 10:10
Copy link

codecov bot commented Aug 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.02%. Comparing base (aee8728) to head (d2c553f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3786      +/-   ##
==========================================
+ Coverage   84.00%   84.02%   +0.02%     
==========================================
  Files         259      259              
  Lines       27159    27156       -3     
==========================================
+ Hits        22815    22818       +3     
+ Misses       4344     4338       -6     

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

@pixlwave pixlwave force-pushed the doug/typed-shields branch 2 times, most recently from 01d0028 to e5e8db6 Compare August 1, 2024 10:32
Copy link
Contributor

@jmartinesp jmartinesp left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@jmartinesp
Copy link
Contributor

@BillCarsonFr does the new implementation work for you?

@pixlwave pixlwave force-pushed the doug/typed-shields branch from e5e8db6 to 221ace8 Compare August 1, 2024 11:31
@pixlwave
Copy link
Member Author

pixlwave commented Aug 1, 2024

Force-pushed to rebase on main and add a missing code field in the test from #3788.

Edit: Helps if I actually commit the missing part 🤦‍♂️

@pixlwave pixlwave force-pushed the doug/typed-shields branch from 221ace8 to a666b88 Compare August 1, 2024 11:40
Copy link
Member

@BillCarsonFr BillCarsonFr left a comment

Choose a reason for hiding this comment

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

So yes, I think I prefer the human/machine readable approach.
I just let a comment regarding the sent in clear

@@ -382,7 +382,10 @@ impl EventTimelineItem {
Some(info.verification_state.to_shield_state_lax())
}
}
None => Some(ShieldState::Grey { message: SENT_IN_CLEAR }),
None => Some(ShieldState::Grey {
Copy link
Member

Choose a reason for hiding this comment

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

Sent in clear in an encrypted room is high level warning, should be red

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah my bad, updated 👍

/// A machine-readable representation of the authenticity for a `ShieldState`.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum ShieldStateCode {
Copy link
Member

Choose a reason for hiding this comment

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

If some client app persist that and then later we add a new code, how will this behave? When deserializing an old version of this enum?

Copy link
Member Author

@pixlwave pixlwave Aug 1, 2024

Choose a reason for hiding this comment

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

I'm no Rust expert but the below seems to indicate that it will be fine if serialised to JSON:

use serde::{Serialize, Deserialize};
use serde_json;

#[derive(Serialize, Deserialize, Debug)]
enum Color {
    Red,
}


#[derive(Serialize, Deserialize, Debug)]
enum ColorPop {
    Yellow,
    Red,
    Green,
}


fn main() {
    let color = Color::Red;
    let json_string = serde_json::to_string(&color).unwrap();
    println!("Serialized color: {}", json_string); // Serialized color: "Red"
    
    let color: ColorPop = serde_json::from_str(&json_string).unwrap();
    println!("Deserialized color: {:?}", color); // Deserialized color: Red
}

How come the ShieldState is serialisable in the first place? Shouldn't the app always ask the SDK for the state to be up to date?

Copy link
Contributor

Choose a reason for hiding this comment

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

It might be for the WASM use-case where we sometimes, instead of adding a new type in the bindings which the SDK type gets converted into, we return a JSON object. This is using serde-wasm-bindgen.

If that's not the case, and nobody needs the implementations, perhaps we should just remove the Serialize and Deserialize implementations since they might have been cargo culted.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it, I didn't know that about WASM 👍

@jmartinesp jmartinesp merged commit b453a02 into main Aug 5, 2024
40 checks passed
@jmartinesp jmartinesp deleted the doug/typed-shields branch August 5, 2024 08:48
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.

4 participants