Skip to content

Commit ca397dc

Browse files
committed
feat(ffi): wrap Ruma MediaSources and run validations before passing them over FFI
Ruma doesn't currently validate mxuri's and as such `MediaSource`s passed over FFI can contain invalid/empty URLs. This change introduces a wrapper type around Ruma's and failable transformations so that appropiate actions can be taken beforehand e.g. returning a `TimelineItemContent::FailedToParseMessageLike` or nil-ing out the thumbnail info.
1 parent 1fbe681 commit ca397dc

File tree

7 files changed

+163
-78
lines changed

7 files changed

+163
-78
lines changed

bindings/matrix-sdk-ffi/src/api.udl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,3 @@ interface RoomMessageEventContentWithoutRelation {
1313
interface ClientError {
1414
Generic(string msg);
1515
};
16-
17-
interface MediaSource {
18-
[Name=from_json, Throws=ClientError]
19-
constructor(string json);
20-
string to_json();
21-
string url();
22-
};

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ use matrix_sdk::{
3232
user_directory::search_users,
3333
},
3434
events::{
35-
room::{
36-
avatar::RoomAvatarEventContent, encryption::RoomEncryptionEventContent, MediaSource,
37-
},
35+
room::{avatar::RoomAvatarEventContent, encryption::RoomEncryptionEventContent},
3836
AnyInitialStateEvent, AnyToDeviceEvent, InitialStateEvent,
3937
},
4038
serde::Raw,
@@ -81,7 +79,7 @@ use crate::{
8179
notification_settings::NotificationSettings,
8280
room_directory_search::RoomDirectorySearch,
8381
room_preview::RoomPreview,
84-
ruma::AuthData,
82+
ruma::{AuthData, MediaSource},
8583
sync_service::{SyncService, SyncServiceBuilder},
8684
task_handle::TaskHandle,
8785
utils::AsyncRuntimeDropped,
@@ -455,7 +453,7 @@ impl Client {
455453
.inner
456454
.media()
457455
.get_media_file(
458-
&MediaRequestParameters { source, format: MediaFormat::File },
456+
&MediaRequestParameters { source: source.media_source, format: MediaFormat::File },
459457
filename,
460458
&mime_type,
461459
use_cache,
@@ -728,7 +726,7 @@ impl Client {
728726
&self,
729727
media_source: Arc<MediaSource>,
730728
) -> Result<Vec<u8>, ClientError> {
731-
let source = (*media_source).clone();
729+
let source = (*media_source).clone().media_source;
732730

733731
debug!(?source, "requesting media file");
734732
Ok(self
@@ -744,9 +742,9 @@ impl Client {
744742
width: u64,
745743
height: u64,
746744
) -> Result<Vec<u8>, ClientError> {
747-
let source = (*media_source).clone();
745+
let source = (*media_source).clone().media_source;
748746

749-
debug!(source = ?media_source, width, height, "requesting media thumbnail");
747+
debug!(?source, width, height, "requesting media thumbnail");
750748
Ok(self
751749
.inner
752750
.media()

bindings/matrix-sdk-ffi/src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl TryFrom<AnySyncMessageLikeEvent> for MessageLikeEventContent {
202202
_ => None,
203203
});
204204
MessageLikeEventContent::RoomMessage {
205-
message_type: original_content.msgtype.into(),
205+
message_type: original_content.msgtype.try_into()?,
206206
in_reply_to_event_id,
207207
}
208208
}

bindings/matrix-sdk-ffi/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ mod utils;
3333
mod widget;
3434

3535
use async_compat::TOKIO1 as RUNTIME;
36-
use matrix_sdk::ruma::events::room::{
37-
message::RoomMessageEventContentWithoutRelation, MediaSource,
38-
};
36+
use matrix_sdk::ruma::events::room::message::RoomMessageEventContentWithoutRelation;
3937

4038
use self::{
4139
error::ClientError,
42-
ruma::{MediaSourceExt, Mentions, RoomMessageEventContentWithoutRelationExt},
40+
ruma::{Mentions, RoomMessageEventContentWithoutRelationExt},
4341
task_handle::TaskHandle,
4442
};
4543

bindings/matrix-sdk-ffi/src/room.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ impl TryFrom<ImageInfo> for RumaAvatarImageInfo {
973973

974974
fn try_from(value: ImageInfo) -> Result<Self, MediaInfoError> {
975975
let thumbnail_url = if let Some(media_source) = value.thumbnail_source {
976-
match media_source.as_ref() {
976+
match &media_source.as_ref().media_source {
977977
MediaSource::Plain(mxc_uri) => Some(mxc_uri.clone()),
978978
MediaSource::Encrypted(_) => return Err(MediaInfoError::InvalidField),
979979
}

0 commit comments

Comments
 (0)