Skip to content

Commit d9b2b53

Browse files
authored
feat(timeline): Expose shield state for EventTimelineItem (#3679)
1 parent bacf85d commit d9b2b53

File tree

2 files changed

+47
-1
lines changed
  • bindings/matrix-sdk-ffi/src/timeline
  • crates/matrix-sdk-ui/src/timeline/event_item

2 files changed

+47
-1
lines changed

bindings/matrix-sdk-ffi/src/timeline/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use matrix_sdk::{
2424
AttachmentConfig, AttachmentInfo, BaseAudioInfo, BaseFileInfo, BaseImageInfo,
2525
BaseThumbnailInfo, BaseVideoInfo, Thumbnail,
2626
},
27+
deserialized_responses::ShieldState as RustShieldState,
2728
Error,
2829
};
2930
use matrix_sdk_ui::timeline::{
@@ -922,6 +923,30 @@ impl From<&matrix_sdk_ui::timeline::EventSendState> for EventSendState {
922923
}
923924
}
924925

926+
/// Recommended decorations for decrypted messages, representing the message's
927+
/// authenticity properties.
928+
#[derive(uniffi::Enum)]
929+
pub enum ShieldState {
930+
/// A red shield with a tooltip containing the associated message should be
931+
/// presented.
932+
Red { message: String },
933+
/// A grey shield with a tooltip containing the associated message should be
934+
/// presented.
935+
Grey { message: String },
936+
/// No shield should be presented.
937+
None,
938+
}
939+
940+
impl From<RustShieldState> for ShieldState {
941+
fn from(value: RustShieldState) -> Self {
942+
match value {
943+
RustShieldState::Red { message } => Self::Red { message: message.to_owned() },
944+
RustShieldState::Grey { message } => Self::Grey { message: message.to_owned() },
945+
RustShieldState::None => Self::None,
946+
}
947+
}
948+
}
949+
925950
#[derive(uniffi::Object)]
926951
pub struct EventTimelineItem(pub(crate) matrix_sdk_ui::timeline::EventTimelineItem);
927952

@@ -1008,6 +1033,12 @@ impl EventTimelineItem {
10081033
pub fn can_be_replied_to(&self) -> bool {
10091034
self.0.can_be_replied_to()
10101035
}
1036+
1037+
/// Gets the [`ShieldState`] which can be used to decorate messages in the
1038+
/// recommended way.
1039+
pub fn get_shield(&self, strict: bool) -> Option<ShieldState> {
1040+
self.0.get_shield(strict).map(Into::into)
1041+
}
10111042
}
10121043

10131044
#[derive(uniffi::Record)]

crates/matrix-sdk-ui/src/timeline/event_item/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use std::sync::Arc;
1616

1717
use as_variant::as_variant;
1818
use indexmap::IndexMap;
19-
use matrix_sdk::{deserialized_responses::EncryptionInfo, Client, Error};
19+
use matrix_sdk::{
20+
deserialized_responses::{EncryptionInfo, ShieldState},
21+
Client, Error,
22+
};
2023
use matrix_sdk_base::{deserialized_responses::SyncTimelineEvent, latest_event::LatestEvent};
2124
use once_cell::sync::Lazy;
2225
use ruma::{
@@ -335,6 +338,18 @@ impl EventTimelineItem {
335338
}
336339
}
337340

341+
/// Gets the [`ShieldState`] which can be used to decorate messages in the
342+
/// recommended way.
343+
pub fn get_shield(&self, strict: bool) -> Option<ShieldState> {
344+
self.encryption_info().map(|info| {
345+
if strict {
346+
info.verification_state.to_shield_state_strict()
347+
} else {
348+
info.verification_state.to_shield_state_lax()
349+
}
350+
})
351+
}
352+
338353
/// Check whether this item can be replied to.
339354
pub fn can_be_replied_to(&self) -> bool {
340355
// This must be in sync with the early returns of `Timeline::send_reply`

0 commit comments

Comments
 (0)