Skip to content

Commit

Permalink
feat(ffi): wrap Ruma MediaSources and run validations before passing …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
stefanceriu committed Nov 26, 2024
1 parent 1fbe681 commit ca397dc
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 78 deletions.
7 changes: 0 additions & 7 deletions bindings/matrix-sdk-ffi/src/api.udl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,3 @@ interface RoomMessageEventContentWithoutRelation {
interface ClientError {
Generic(string msg);
};

interface MediaSource {
[Name=from_json, Throws=ClientError]
constructor(string json);
string to_json();
string url();
};
14 changes: 6 additions & 8 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ use matrix_sdk::{
user_directory::search_users,
},
events::{
room::{
avatar::RoomAvatarEventContent, encryption::RoomEncryptionEventContent, MediaSource,
},
room::{avatar::RoomAvatarEventContent, encryption::RoomEncryptionEventContent},
AnyInitialStateEvent, AnyToDeviceEvent, InitialStateEvent,
},
serde::Raw,
Expand Down Expand Up @@ -81,7 +79,7 @@ use crate::{
notification_settings::NotificationSettings,
room_directory_search::RoomDirectorySearch,
room_preview::RoomPreview,
ruma::AuthData,
ruma::{AuthData, MediaSource},
sync_service::{SyncService, SyncServiceBuilder},
task_handle::TaskHandle,
utils::AsyncRuntimeDropped,
Expand Down Expand Up @@ -455,7 +453,7 @@ impl Client {
.inner
.media()
.get_media_file(
&MediaRequestParameters { source, format: MediaFormat::File },
&MediaRequestParameters { source: source.media_source, format: MediaFormat::File },
filename,
&mime_type,
use_cache,
Expand Down Expand Up @@ -728,7 +726,7 @@ impl Client {
&self,
media_source: Arc<MediaSource>,
) -> Result<Vec<u8>, ClientError> {
let source = (*media_source).clone();
let source = (*media_source).clone().media_source;

debug!(?source, "requesting media file");
Ok(self
Expand All @@ -744,9 +742,9 @@ impl Client {
width: u64,
height: u64,
) -> Result<Vec<u8>, ClientError> {
let source = (*media_source).clone();
let source = (*media_source).clone().media_source;

debug!(source = ?media_source, width, height, "requesting media thumbnail");
debug!(?source, width, height, "requesting media thumbnail");
Ok(self
.inner
.media()
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl TryFrom<AnySyncMessageLikeEvent> for MessageLikeEventContent {
_ => None,
});
MessageLikeEventContent::RoomMessage {
message_type: original_content.msgtype.into(),
message_type: original_content.msgtype.try_into()?,
in_reply_to_event_id,
}
}
Expand Down
6 changes: 2 additions & 4 deletions bindings/matrix-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ mod utils;
mod widget;

use async_compat::TOKIO1 as RUNTIME;
use matrix_sdk::ruma::events::room::{
message::RoomMessageEventContentWithoutRelation, MediaSource,
};
use matrix_sdk::ruma::events::room::message::RoomMessageEventContentWithoutRelation;

use self::{
error::ClientError,
ruma::{MediaSourceExt, Mentions, RoomMessageEventContentWithoutRelationExt},
ruma::{Mentions, RoomMessageEventContentWithoutRelationExt},
task_handle::TaskHandle,
};

Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ impl TryFrom<ImageInfo> for RumaAvatarImageInfo {

fn try_from(value: ImageInfo) -> Result<Self, MediaInfoError> {
let thumbnail_url = if let Some(media_source) = value.thumbnail_source {
match media_source.as_ref() {
match &media_source.as_ref().media_source {
MediaSource::Plain(mxc_uri) => Some(mxc_uri.clone()),
MediaSource::Encrypted(_) => return Err(MediaInfoError::InvalidField),
}
Expand Down
Loading

0 comments on commit ca397dc

Please sign in to comment.