From 147cd970c4f9ea5841e4941a41c18272e01bd475 Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Mon, 3 Jul 2023 15:34:07 +0200 Subject: [PATCH] Map MSC3488 fields for UniFFI (#2187) * Update ruma * Refactor send_location * Add timeline parser * Fix code format * Add zoom_level * Update ruma * Map zoom_level * Format code * Cleanup * Refactor LocationContent * Apply suggestion * Fix format issue --- Cargo.lock | 16 +++---- Cargo.toml | 4 +- bindings/matrix-sdk-ffi/Cargo.toml | 2 +- bindings/matrix-sdk-ffi/src/room.rs | 40 +++++++++++++++++- bindings/matrix-sdk-ffi/src/timeline.rs | 55 ++++++++++++++++++------- 5 files changed, 88 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7ffd0483..e2e6085c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4204,7 +4204,7 @@ dependencies = [ [[package]] name = "ruma" version = "0.8.2" -source = "git+https://github.com/ruma/ruma?rev=cf32036df4c9daca736dcd7f0d9d65debcf9897f#cf32036df4c9daca736dcd7f0d9d65debcf9897f" +source = "git+https://github.com/ruma/ruma?rev=5fde57460aaa71e16ac68f79376843089f9c3dfc#5fde57460aaa71e16ac68f79376843089f9c3dfc" dependencies = [ "assign", "js_int", @@ -4219,7 +4219,7 @@ dependencies = [ [[package]] name = "ruma-appservice-api" version = "0.8.1" -source = "git+https://github.com/ruma/ruma?rev=cf32036df4c9daca736dcd7f0d9d65debcf9897f#cf32036df4c9daca736dcd7f0d9d65debcf9897f" +source = "git+https://github.com/ruma/ruma?rev=5fde57460aaa71e16ac68f79376843089f9c3dfc#5fde57460aaa71e16ac68f79376843089f9c3dfc" dependencies = [ "js_int", "ruma-common", @@ -4230,7 +4230,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.16.2" -source = "git+https://github.com/ruma/ruma?rev=cf32036df4c9daca736dcd7f0d9d65debcf9897f#cf32036df4c9daca736dcd7f0d9d65debcf9897f" +source = "git+https://github.com/ruma/ruma?rev=5fde57460aaa71e16ac68f79376843089f9c3dfc#5fde57460aaa71e16ac68f79376843089f9c3dfc" dependencies = [ "assign", "bytes", @@ -4247,7 +4247,7 @@ dependencies = [ [[package]] name = "ruma-common" version = "0.11.3" -source = "git+https://github.com/ruma/ruma?rev=cf32036df4c9daca736dcd7f0d9d65debcf9897f#cf32036df4c9daca736dcd7f0d9d65debcf9897f" +source = "git+https://github.com/ruma/ruma?rev=5fde57460aaa71e16ac68f79376843089f9c3dfc#5fde57460aaa71e16ac68f79376843089f9c3dfc" dependencies = [ "base64 0.21.2", "bytes", @@ -4280,7 +4280,7 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.7.1" -source = "git+https://github.com/ruma/ruma?rev=cf32036df4c9daca736dcd7f0d9d65debcf9897f#cf32036df4c9daca736dcd7f0d9d65debcf9897f" +source = "git+https://github.com/ruma/ruma?rev=5fde57460aaa71e16ac68f79376843089f9c3dfc#5fde57460aaa71e16ac68f79376843089f9c3dfc" dependencies = [ "js_int", "ruma-common", @@ -4291,7 +4291,7 @@ dependencies = [ [[package]] name = "ruma-identifiers-validation" version = "0.9.1" -source = "git+https://github.com/ruma/ruma?rev=cf32036df4c9daca736dcd7f0d9d65debcf9897f#cf32036df4c9daca736dcd7f0d9d65debcf9897f" +source = "git+https://github.com/ruma/ruma?rev=5fde57460aaa71e16ac68f79376843089f9c3dfc#5fde57460aaa71e16ac68f79376843089f9c3dfc" dependencies = [ "js_int", "thiserror", @@ -4300,7 +4300,7 @@ dependencies = [ [[package]] name = "ruma-macros" version = "0.11.3" -source = "git+https://github.com/ruma/ruma?rev=cf32036df4c9daca736dcd7f0d9d65debcf9897f#cf32036df4c9daca736dcd7f0d9d65debcf9897f" +source = "git+https://github.com/ruma/ruma?rev=5fde57460aaa71e16ac68f79376843089f9c3dfc#5fde57460aaa71e16ac68f79376843089f9c3dfc" dependencies = [ "once_cell", "proc-macro-crate", @@ -4315,7 +4315,7 @@ dependencies = [ [[package]] name = "ruma-push-gateway-api" version = "0.7.1" -source = "git+https://github.com/ruma/ruma?rev=cf32036df4c9daca736dcd7f0d9d65debcf9897f#cf32036df4c9daca736dcd7f0d9d65debcf9897f" +source = "git+https://github.com/ruma/ruma?rev=5fde57460aaa71e16ac68f79376843089f9c3dfc#5fde57460aaa71e16ac68f79376843089f9c3dfc" dependencies = [ "js_int", "ruma-common", diff --git a/Cargo.toml b/Cargo.toml index f7bcffc0a..5ea1b99f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,8 +36,8 @@ futures-executor = "0.3.21" futures-util = { version = "0.3.26", default-features = false, features = ["alloc"] } http = "0.2.6" itertools = "0.10.5" -ruma = { git = "https://github.com/ruma/ruma", rev = "cf32036df4c9daca736dcd7f0d9d65debcf9897f", features = ["client-api-c", "compat-user-id"] } -ruma-common = { git = "https://github.com/ruma/ruma", rev = "cf32036df4c9daca736dcd7f0d9d65debcf9897f" } +ruma = { git = "https://github.com/ruma/ruma", rev = "5fde57460aaa71e16ac68f79376843089f9c3dfc", features = ["client-api-c", "compat-user-id"] } +ruma-common = { git = "https://github.com/ruma/ruma", rev = "5fde57460aaa71e16ac68f79376843089f9c3dfc" } once_cell = "1.16.0" serde = "1.0.151" serde_html_form = "0.2.0" diff --git a/bindings/matrix-sdk-ffi/Cargo.toml b/bindings/matrix-sdk-ffi/Cargo.toml index aea3f881c..b94de89c4 100644 --- a/bindings/matrix-sdk-ffi/Cargo.toml +++ b/bindings/matrix-sdk-ffi/Cargo.toml @@ -36,7 +36,7 @@ mime = "0.3.16" once_cell = { workspace = true } opentelemetry = { version = "0.19.0", features = ["rt-tokio"] } opentelemetry-otlp = { version = "0.12.0", features = ["tokio", "reqwest-client", "http-proto"] } -ruma = { workspace = true, features = ["unstable-sanitize", "unstable-unspecified"] } +ruma = { workspace = true, features = ["unstable-sanitize", "unstable-unspecified", "unstable-msc3488"] } sanitize-filename-reader-friendly = "2.2.1" serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/bindings/matrix-sdk-ffi/src/room.rs b/bindings/matrix-sdk-ffi/src/room.rs index 9e5190f37..d06f197bd 100644 --- a/bindings/matrix-sdk-ffi/src/room.rs +++ b/bindings/matrix-sdk-ffi/src/room.rs @@ -11,6 +11,7 @@ use matrix_sdk::{ ruma::{ api::client::{receipt::create_receipt::v3::ReceiptType, room::report_content}, events::{ + location::{AssetType as RumaAssetType, LocationContent, ZoomLevel}, receipt::ReceiptThread, relation::{Annotation, Replacement}, room::message::{ @@ -806,8 +807,28 @@ impl Room { }); } - pub fn send_location(&self, body: String, geo_uri: String, txn_id: Option) { - let location_event_message_content = LocationMessageEventContent::new(body, geo_uri); + pub fn send_location( + &self, + body: String, + geo_uri: String, + description: Option, + zoom_level: Option, + asset_type: Option, + txn_id: Option, + ) { + let mut location_event_message_content = + LocationMessageEventContent::new(body, geo_uri.clone()); + + if let Some(asset_type) = asset_type { + location_event_message_content = + location_event_message_content.with_asset_type(RumaAssetType::from(asset_type)); + } + + let mut location_content = LocationContent::new(geo_uri); + location_content.description = description; + location_content.zoom_level = zoom_level.and_then(ZoomLevel::new); + location_event_message_content.location = Some(location_content); + let room_message_event_content = RoomMessageEventContent::new(MessageType::Location(location_event_message_content)); self.send(Arc::new(room_message_event_content), txn_id) @@ -967,3 +988,18 @@ impl From for matrix_sdk_ui::timeline::PaginationOptions<'sta opts } } + +#[derive(Clone, uniffi::Enum)] +pub enum AssetType { + Sender, + Pin, +} + +impl From for RumaAssetType { + fn from(value: AssetType) -> Self { + match value { + AssetType::Sender => Self::Self_, + AssetType::Pin => Self::Pin, + } + } +} diff --git a/bindings/matrix-sdk-ffi/src/timeline.rs b/bindings/matrix-sdk-ffi/src/timeline.rs index e5a81fe69..65c82f550 100644 --- a/bindings/matrix-sdk-ffi/src/timeline.rs +++ b/bindings/matrix-sdk-ffi/src/timeline.rs @@ -5,20 +5,25 @@ use extension_trait::extension_trait; use eyeball_im::VectorDiff; use matrix_sdk::{ attachment::{BaseAudioInfo, BaseFileInfo, BaseImageInfo, BaseThumbnailInfo, BaseVideoInfo}, - ruma::events::room::{ - message::{ - AudioInfo as RumaAudioInfo, AudioMessageEventContent as RumaAudioMessageEventContent, - EmoteMessageEventContent as RumaEmoteMessageEventContent, FileInfo as RumaFileInfo, - FileMessageEventContent as RumaFileMessageEventContent, - FormattedBody as RumaFormattedBody, - ImageMessageEventContent as RumaImageMessageEventContent, - LocationMessageEventContent as RumaLocationMessageEventContent, - MessageType as RumaMessageType, - NoticeMessageEventContent as RumaNoticeMessageEventContent, RoomMessageEventContent, - TextMessageEventContent as RumaTextMessageEventContent, VideoInfo as RumaVideoInfo, - VideoMessageEventContent as RumaVideoMessageEventContent, + ruma::events::{ + location::AssetType as RumaAssetType, + room::{ + message::{ + AudioInfo as RumaAudioInfo, + AudioMessageEventContent as RumaAudioMessageEventContent, + EmoteMessageEventContent as RumaEmoteMessageEventContent, FileInfo as RumaFileInfo, + FileMessageEventContent as RumaFileMessageEventContent, + FormattedBody as RumaFormattedBody, + ImageMessageEventContent as RumaImageMessageEventContent, + LocationMessageEventContent as RumaLocationMessageEventContent, + MessageType as RumaMessageType, + NoticeMessageEventContent as RumaNoticeMessageEventContent, + RoomMessageEventContent, TextMessageEventContent as RumaTextMessageEventContent, + VideoInfo as RumaVideoInfo, + VideoMessageEventContent as RumaVideoMessageEventContent, + }, + ImageInfo as RumaImageInfo, MediaSource, ThumbnailInfo as RumaThumbnailInfo, }, - ImageInfo as RumaImageInfo, MediaSource, ThumbnailInfo as RumaThumbnailInfo, }, }; use matrix_sdk_ui::timeline::{Profile, TimelineDetails}; @@ -28,6 +33,7 @@ use tracing::warn; use crate::{ error::{ClientError, TimelineError}, helpers::unwrap_or_clone_arc, + room::AssetType, }; #[uniffi::export] @@ -621,9 +627,23 @@ impl TryFrom for MessageType { formatted: c.formatted.as_ref().map(Into::into), }, }, - RumaMessageType::Location(c) => MessageType::Location { - content: LocationContent { body: c.body, geo_uri: c.geo_uri }, - }, + RumaMessageType::Location(c) => { + let (description, zoom_level) = + c.location.map(|loc| (loc.description, loc.zoom_level)).unwrap_or((None, None)); + MessageType::Location { + content: LocationContent { + body: c.body, + geo_uri: c.geo_uri, + description, + zoom_level: zoom_level.and_then(|z| z.get().try_into().ok()), + asset: c.asset.and_then(|a| match a.type_ { + RumaAssetType::Self_ => Some(AssetType::Sender), + RumaAssetType::Pin => Some(AssetType::Pin), + _ => None, + }), + }, + } + } _ => bail!("Unsupported type"), }; Ok(message_type) @@ -870,6 +890,9 @@ pub struct TextMessageContent { pub struct LocationContent { pub body: String, pub geo_uri: String, + pub description: Option, + pub zoom_level: Option, + pub asset: Option, } #[derive(Clone, uniffi::Record)]