Skip to content

Add experimental support for MSC4274 (inline media galleries) #4838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ reqwest = { version = "0.12.12", default-features = false }
rmp-serde = "1.3.0"
# Be careful to use commits from the https://github.com/ruma/ruma/tree/ruma-0.12
# branch until a proper release with breaking changes happens.
ruma = { git = "https://github.com/ruma/ruma", rev = "b1cb83544faafaef92be56c53cd98af4c51da858", features = [
ruma = { git = "https://github.com/ruma/ruma", rev = "25b70024889340633fd0e1f856349ddd5b5a687a", features = [
"client-api-c",
"compat-upload-signatures",
"compat-user-id",
Expand All @@ -74,8 +74,9 @@ ruma = { git = "https://github.com/ruma/ruma", rev = "b1cb83544faafaef92be56c53c
"unstable-msc4075",
"unstable-msc4140",
"unstable-msc4171",
"unstable-msc4274"
] }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "b1cb83544faafaef92be56c53cd98af4c51da858" }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "25b70024889340633fd0e1f856349ddd5b5a687a" }
serde = "1.0.217"
serde_html_form = "0.2.7"
serde_json = "1.0.138"
Expand Down
2 changes: 2 additions & 0 deletions bindings/matrix-sdk-ffi/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ pub enum RoomMessageEventMessageType {
Audio,
Emote,
File,
Gallery,
Image,
Location,
Notice,
Expand All @@ -381,6 +382,7 @@ impl From<RumaMessageType> for RoomMessageEventMessageType {
RumaMessageType::Audio { .. } => Self::Audio,
RumaMessageType::Emote { .. } => Self::Emote,
RumaMessageType::File { .. } => Self::File,
RumaMessageType::Gallery { .. } => Self::Gallery,
RumaMessageType::Image { .. } => Self::Image,
RumaMessageType::Location { .. } => Self::Location,
RumaMessageType::Notice { .. } => Self::Notice,
Expand Down
147 changes: 146 additions & 1 deletion bindings/matrix-sdk-ffi/src/ruma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use ruma::{
AudioMessageEventContent as RumaAudioMessageEventContent,
EmoteMessageEventContent as RumaEmoteMessageEventContent, FileInfo as RumaFileInfo,
FileMessageEventContent as RumaFileMessageEventContent,
FormattedBody as RumaFormattedBody,
FormattedBody as RumaFormattedBody, GalleryItemType as RumaGalleryItemType,
GalleryMessageEventContent as RumaGalleryMessageEventContent,
ImageMessageEventContent as RumaImageMessageEventContent,
LocationMessageEventContent as RumaLocationMessageEventContent,
MessageType as RumaMessageType,
Expand Down Expand Up @@ -313,6 +314,7 @@ pub enum MessageType {
Audio { content: AudioMessageContent },
Video { content: VideoMessageContent },
File { content: FileMessageContent },
Gallery { content: GalleryMessageContent },
Notice { content: NoticeMessageContent },
Text { content: TextMessageContent },
Location { content: LocationContent },
Expand Down Expand Up @@ -382,6 +384,18 @@ impl TryFrom<MessageType> for RumaMessageType {
event_content.filename = filename;
Self::File(event_content)
}
MessageType::Gallery { content } => {
let event_content = RumaGalleryMessageEventContent::new(
content.body,
content.formatted.map(Into::into),
content
.itemtypes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()?,
);
Self::Gallery(event_content)
}
MessageType::Notice { content } => {
Self::Notice(assign!(RumaNoticeMessageEventContent::plain(content.body), {
formatted: content.formatted.map(Into::into),
Expand Down Expand Up @@ -452,6 +466,17 @@ impl TryFrom<RumaMessageType> for MessageType {
info: c.info.as_deref().map(TryInto::try_into).transpose()?,
},
},
RumaMessageType::Gallery(c) => MessageType::Gallery {
content: GalleryMessageContent {
body: c.body,
formatted: c.formatted.as_ref().map(Into::into),
itemtypes: c
.itemtypes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()?,
},
},
RumaMessageType::Notice(c) => MessageType::Notice {
content: NoticeMessageContent {
body: c.body.clone(),
Expand Down Expand Up @@ -489,6 +514,105 @@ impl TryFrom<RumaMessageType> for MessageType {
}
}

impl TryFrom<GalleryItemType> for RumaGalleryItemType {
type Error = ClientError;

fn try_from(value: GalleryItemType) -> Result<Self, Self::Error> {
Ok(match value {
GalleryItemType::Image { content } => {
let (body, filename) = get_body_and_filename(content.filename, content.caption);
let mut event_content =
RumaImageMessageEventContent::new(body, (*content.source).clone().into())
.info(content.info.map(Into::into).map(Box::new));
event_content.formatted = content.formatted_caption.map(Into::into);
event_content.filename = filename;
Self::Image(event_content)
}
GalleryItemType::Audio { content } => {
let (body, filename) = get_body_and_filename(content.filename, content.caption);
let mut event_content =
RumaAudioMessageEventContent::new(body, (*content.source).clone().into())
.info(content.info.map(Into::into).map(Box::new));
event_content.formatted = content.formatted_caption.map(Into::into);
event_content.filename = filename;
Self::Audio(event_content)
}
GalleryItemType::Video { content } => {
let (body, filename) = get_body_and_filename(content.filename, content.caption);
let mut event_content =
RumaVideoMessageEventContent::new(body, (*content.source).clone().into())
.info(content.info.map(Into::into).map(Box::new));
event_content.formatted = content.formatted_caption.map(Into::into);
event_content.filename = filename;
Self::Video(event_content)
}
GalleryItemType::File { content } => {
let (body, filename) = get_body_and_filename(content.filename, content.caption);
let mut event_content =
RumaFileMessageEventContent::new(body, (*content.source).clone().into())
.info(content.info.map(Into::into).map(Box::new));
event_content.formatted = content.formatted_caption.map(Into::into);
event_content.filename = filename;
Self::File(event_content)
}
GalleryItemType::Other { itemtype, body } => {
Self::new(&itemtype, body, JsonObject::default())?
}
})
}
}

impl TryFrom<RumaGalleryItemType> for GalleryItemType {
type Error = ClientError;

fn try_from(value: RumaGalleryItemType) -> Result<Self, Self::Error> {
Ok(match value {
RumaGalleryItemType::Image(c) => GalleryItemType::Image {
content: ImageMessageContent {
filename: c.filename().to_owned(),
caption: c.caption().map(ToString::to_string),
formatted_caption: c.formatted_caption().map(Into::into),
source: Arc::new(c.source.try_into()?),
info: c.info.as_deref().map(TryInto::try_into).transpose()?,
},
},
RumaGalleryItemType::Audio(c) => GalleryItemType::Audio {
content: AudioMessageContent {
filename: c.filename().to_owned(),
caption: c.caption().map(ToString::to_string),
formatted_caption: c.formatted_caption().map(Into::into),
source: Arc::new(c.source.try_into()?),
info: c.info.as_deref().map(Into::into),
audio: c.audio.map(Into::into),
voice: c.voice.map(Into::into),
},
},
RumaGalleryItemType::Video(c) => GalleryItemType::Video {
content: VideoMessageContent {
filename: c.filename().to_owned(),
caption: c.caption().map(ToString::to_string),
formatted_caption: c.formatted_caption().map(Into::into),
source: Arc::new(c.source.try_into()?),
info: c.info.as_deref().map(TryInto::try_into).transpose()?,
},
},
RumaGalleryItemType::File(c) => GalleryItemType::File {
content: FileMessageContent {
filename: c.filename().to_owned(),
caption: c.caption().map(ToString::to_string),
formatted_caption: c.formatted_caption().map(Into::into),
source: Arc::new(c.source.try_into()?),
info: c.info.as_deref().map(TryInto::try_into).transpose()?,
},
},
_ => GalleryItemType::Other {
itemtype: value.itemtype().to_owned(),
body: value.body().to_owned(),
},
})
}
}

#[derive(Clone, uniffi::Enum)]
pub enum NotifyType {
Ring,
Expand Down Expand Up @@ -561,6 +685,22 @@ pub struct FileMessageContent {
pub info: Option<FileInfo>,
}

#[derive(Clone, uniffi::Record)]
pub struct GalleryMessageContent {
pub body: String,
pub formatted: Option<FormattedBody>,
pub itemtypes: Vec<GalleryItemType>,
}

#[derive(Clone, uniffi::Enum)]
pub enum GalleryItemType {
Image { content: ImageMessageContent },
Audio { content: AudioMessageContent },
Video { content: VideoMessageContent },
File { content: FileMessageContent },
Other { itemtype: String, body: String },
}

#[derive(Clone, uniffi::Record)]
pub struct ImageInfo {
pub height: Option<u64>,
Expand Down Expand Up @@ -808,6 +948,11 @@ pub struct FormattedBody {
pub body: String,
}

#[uniffi::export]
pub fn formatted_body_from_html(body: String) -> FormattedBody {
FormattedBody::from(&RumaFormattedBody::html(body))
}

impl From<FormattedBody> for RumaFormattedBody {
fn from(f: FormattedBody) -> Self {
Self {
Expand Down
Loading