From f8a9d12c88fc18e93328201e4d97f4ae442f86f6 Mon Sep 17 00:00:00 2001 From: Daniel Salinas Date: Thu, 12 Dec 2024 16:00:17 -0500 Subject: [PATCH] Use a type alias to allow bindings to take advantage of custom types --- bindings/matrix-sdk-ffi/src/event.rs | 5 +++-- .../matrix-sdk-ffi/src/session_verification.rs | 6 +++--- bindings/matrix-sdk-ffi/src/timeline/content.rs | 7 ++++--- bindings/matrix-sdk-ffi/src/timeline/mod.rs | 15 ++++++++------- bindings/matrix-sdk-ffi/src/utils.rs | 13 ++++++++++++- .../src/timeline/event_item/content/polls.rs | 4 ++-- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/event.rs b/bindings/matrix-sdk-ffi/src/event.rs index 2be9989959e..188c75e9ce4 100644 --- a/bindings/matrix-sdk-ffi/src/event.rs +++ b/bindings/matrix-sdk-ffi/src/event.rs @@ -17,6 +17,7 @@ use ruma::{ use crate::{ room_member::MembershipState, ruma::{MessageType, NotifyType}, + utils::Timestamp, ClientError, }; @@ -33,8 +34,8 @@ impl TimelineEvent { self.0.sender().to_string() } - pub fn timestamp(&self) -> u64 { - self.0.origin_server_ts().0.into() + pub fn timestamp(&self) -> Timestamp { + self.0.origin_server_ts().into() } pub fn event_type(&self) -> Result { diff --git a/bindings/matrix-sdk-ffi/src/session_verification.rs b/bindings/matrix-sdk-ffi/src/session_verification.rs index 6cc4dea8b76..6843c3e2312 100644 --- a/bindings/matrix-sdk-ffi/src/session_verification.rs +++ b/bindings/matrix-sdk-ffi/src/session_verification.rs @@ -13,7 +13,7 @@ use ruma::UserId; use tracing::{error, info}; use super::RUNTIME; -use crate::error::ClientError; +use crate::{error::ClientError, utils::Timestamp}; #[derive(uniffi::Object)] pub struct SessionVerificationEmoji { @@ -46,7 +46,7 @@ pub struct SessionVerificationRequestDetails { device_id: String, display_name: Option, /// First time this device was seen in milliseconds since epoch. - first_seen_timestamp: u64, + first_seen_timestamp: Timestamp, } #[matrix_sdk_ffi_macros::export(callback_interface)] @@ -242,7 +242,7 @@ impl SessionVerificationController { flow_id: request.flow_id().into(), device_id: other_device_data.device_id().into(), display_name: other_device_data.display_name().map(str::to_string), - first_seen_timestamp: other_device_data.first_time_seen_ts().get().into(), + first_seen_timestamp: other_device_data.first_time_seen_ts().into(), }); } } diff --git a/bindings/matrix-sdk-ffi/src/timeline/content.rs b/bindings/matrix-sdk-ffi/src/timeline/content.rs index 3c182c65e82..86624fb1ab5 100644 --- a/bindings/matrix-sdk-ffi/src/timeline/content.rs +++ b/bindings/matrix-sdk-ffi/src/timeline/content.rs @@ -22,6 +22,7 @@ use super::ProfileDetails; use crate::{ error::ClientError, ruma::{ImageInfo, MediaSource, MediaSourceExt, Mentions, MessageType, PollKind}, + utils::Timestamp, }; impl From for TimelineItemContent { @@ -187,7 +188,7 @@ pub enum TimelineItemContent { max_selections: u64, answers: Vec, votes: HashMap>, - end_time: Option, + end_time: Option, has_been_edited: bool, }, CallInvite, @@ -319,7 +320,7 @@ pub struct Reaction { #[derive(Clone, uniffi::Record)] pub struct ReactionSenderData { pub sender_id: String, - pub timestamp: u64, + pub timestamp: Timestamp, } #[derive(Clone, uniffi::Enum)] @@ -481,7 +482,7 @@ impl From for TimelineItemContent { .map(|i| PollAnswer { id: i.id, text: i.text }) .collect(), votes: value.votes, - end_time: value.end_time, + end_time: value.end_time.map(|t| t.into()), has_been_edited: value.has_been_edited, } } diff --git a/bindings/matrix-sdk-ffi/src/timeline/mod.rs b/bindings/matrix-sdk-ffi/src/timeline/mod.rs index 89f226dc6a0..0d0894b6443 100644 --- a/bindings/matrix-sdk-ffi/src/timeline/mod.rs +++ b/bindings/matrix-sdk-ffi/src/timeline/mod.rs @@ -75,6 +75,7 @@ use crate::{ VideoInfo, }, task_handle::TaskHandle, + utils::Timestamp, RUNTIME, }; @@ -986,7 +987,7 @@ impl TimelineItem { pub fn as_virtual(self: Arc) -> Option { use matrix_sdk_ui::timeline::VirtualTimelineItem as VItem; match self.0.as_virtual()? { - VItem::DateDivider(ts) => Some(VirtualTimelineItem::DateDivider { ts: ts.0.into() }), + VItem::DateDivider(ts) => Some(VirtualTimelineItem::DateDivider { ts: (*ts).into() }), VItem::ReadMarker => Some(VirtualTimelineItem::ReadMarker), } } @@ -1081,7 +1082,7 @@ pub struct EventTimelineItem { is_own: bool, is_editable: bool, content: TimelineItemContent, - timestamp: u64, + timestamp: Timestamp, reactions: Vec, local_send_state: Option, read_receipts: HashMap, @@ -1101,7 +1102,7 @@ impl From for EventTimelineItem { .into_iter() .map(|(sender_id, info)| ReactionSenderData { sender_id: sender_id.to_string(), - timestamp: info.timestamp.0.into(), + timestamp: info.timestamp.into(), }) .collect(), }) @@ -1118,7 +1119,7 @@ impl From for EventTimelineItem { is_own: item.is_own(), is_editable: item.is_editable(), content: item.content().clone().into(), - timestamp: item.timestamp().0.into(), + timestamp: item.timestamp().into(), reactions, local_send_state: item.send_state().map(|s| s.into()), read_receipts, @@ -1131,12 +1132,12 @@ impl From for EventTimelineItem { #[derive(Clone, uniffi::Record)] pub struct Receipt { - pub timestamp: Option, + pub timestamp: Option, } impl From for Receipt { fn from(value: ruma::events::receipt::Receipt) -> Self { - Receipt { timestamp: value.ts.map(|ts| ts.0.into()) } + Receipt { timestamp: value.ts.map(|ts| ts.into()) } } } @@ -1260,7 +1261,7 @@ pub enum VirtualTimelineItem { DateDivider { /// A timestamp in milliseconds since Unix Epoch on that day in local /// time. - ts: u64, + ts: Timestamp, }, /// The user's own read marker. diff --git a/bindings/matrix-sdk-ffi/src/utils.rs b/bindings/matrix-sdk-ffi/src/utils.rs index 8868573e344..19e6a4a565f 100644 --- a/bindings/matrix-sdk-ffi/src/utils.rs +++ b/bindings/matrix-sdk-ffi/src/utils.rs @@ -15,9 +15,20 @@ use std::{mem::ManuallyDrop, ops::Deref}; use async_compat::TOKIO1 as RUNTIME; -use ruma::UInt; +use ruma::{MilliSecondsSinceUnixEpoch, UInt}; use tracing::warn; +#[derive(Debug, Clone)] +pub struct Timestamp(u64); + +impl From for Timestamp { + fn from(date: MilliSecondsSinceUnixEpoch) -> Self { + Self(date.0.into()) + } +} + +uniffi::custom_newtype!(Timestamp, u64); + pub(crate) fn u64_to_uint(u: u64) -> UInt { UInt::new(u).unwrap_or_else(|| { warn!("u64 -> UInt conversion overflowed, falling back to UInt::MAX"); diff --git a/crates/matrix-sdk-ui/src/timeline/event_item/content/polls.rs b/crates/matrix-sdk-ui/src/timeline/event_item/content/polls.rs index 8112bc7b147..b758caf124d 100644 --- a/crates/matrix-sdk-ui/src/timeline/event_item/content/polls.rs +++ b/crates/matrix-sdk-ui/src/timeline/event_item/content/polls.rs @@ -146,7 +146,7 @@ impl PollState { .iter() .map(|i| ((*i.0).to_owned(), i.1.iter().map(|i| i.to_string()).collect())) .collect(), - end_time: self.end_event_timestamp.map(|millis| millis.0.into()), + end_time: self.end_event_timestamp, has_been_edited: self.has_been_edited, } } @@ -178,7 +178,7 @@ pub struct PollResult { pub max_selections: u64, pub answers: Vec, pub votes: HashMap>, - pub end_time: Option, + pub end_time: Option, pub has_been_edited: bool, }