@@ -56,6 +56,8 @@ use matrix_sdk_common::{
5656} ;
5757use mime:: Mime ;
5858use reply:: Reply ;
59+ #[ cfg( feature = "unstable-msc4274" ) ]
60+ use ruma:: events:: room:: message:: GalleryItemType ;
5961#[ cfg( feature = "e2e-encryption" ) ]
6062use ruma:: events:: {
6163 room:: encrypted:: OriginalSyncRoomEncryptedEvent , AnySyncMessageLikeEvent , AnySyncTimelineEvent ,
@@ -2160,7 +2162,7 @@ impl Room {
21602162 }
21612163
21622164 let content = self
2163- . make_attachment_event (
2165+ . make_message_event (
21642166 self . make_attachment_type (
21652167 content_type,
21662168 filename,
@@ -2276,7 +2278,7 @@ impl Room {
22762278
22772279 /// Creates the [`RoomMessageEventContent`] based on the message type,
22782280 /// mentions and reply information.
2279- pub ( crate ) async fn make_attachment_event (
2281+ pub ( crate ) async fn make_message_event (
22802282 & self ,
22812283 msg_type : MessageType ,
22822284 mentions : Option < Mentions > ,
@@ -2294,6 +2296,99 @@ impl Room {
22942296 Ok ( content)
22952297 }
22962298
2299+ /// Creates the inner [`GalleryItemType`] for an already-uploaded media file
2300+ /// provided by its source.
2301+ #[ cfg( feature = "unstable-msc4274" ) ]
2302+ #[ allow( clippy:: too_many_arguments) ]
2303+ pub ( crate ) fn make_gallery_item_type (
2304+ & self ,
2305+ content_type : & Mime ,
2306+ filename : String ,
2307+ source : MediaSource ,
2308+ caption : Option < String > ,
2309+ formatted_caption : Option < FormattedBody > ,
2310+ info : Option < AttachmentInfo > ,
2311+ thumbnail : Option < ( MediaSource , Box < ThumbnailInfo > ) > ,
2312+ ) -> GalleryItemType {
2313+ // If caption is set, use it as body, and filename as the file name; otherwise,
2314+ // body is the filename, and the filename is not set.
2315+ // https://github.com/matrix-org/matrix-spec-proposals/blob/main/proposals/2530-body-as-caption.md
2316+ let ( body, filename) = match caption {
2317+ Some ( caption) => ( caption, Some ( filename) ) ,
2318+ None => ( filename, None ) ,
2319+ } ;
2320+
2321+ let ( thumbnail_source, thumbnail_info) = thumbnail. unzip ( ) ;
2322+
2323+ match content_type. type_ ( ) {
2324+ mime:: IMAGE => {
2325+ let info = assign ! ( info. map( ImageInfo :: from) . unwrap_or_default( ) , {
2326+ mimetype: Some ( content_type. as_ref( ) . to_owned( ) ) ,
2327+ thumbnail_source,
2328+ thumbnail_info
2329+ } ) ;
2330+ let content = assign ! ( ImageMessageEventContent :: new( body, source) , {
2331+ info: Some ( Box :: new( info) ) ,
2332+ formatted: formatted_caption,
2333+ filename
2334+ } ) ;
2335+ GalleryItemType :: Image ( content)
2336+ }
2337+
2338+ mime:: AUDIO => {
2339+ let mut content = assign ! ( AudioMessageEventContent :: new( body, source) , {
2340+ formatted: formatted_caption,
2341+ filename
2342+ } ) ;
2343+
2344+ if let Some ( AttachmentInfo :: Voice { audio_info, waveform : Some ( waveform_vec) } ) =
2345+ & info
2346+ {
2347+ if let Some ( duration) = audio_info. duration {
2348+ let waveform = waveform_vec. iter ( ) . map ( |v| ( * v) . into ( ) ) . collect ( ) ;
2349+ content. audio =
2350+ Some ( UnstableAudioDetailsContentBlock :: new ( duration, waveform) ) ;
2351+ }
2352+ content. voice = Some ( UnstableVoiceContentBlock :: new ( ) ) ;
2353+ }
2354+
2355+ let mut audio_info = info. map ( AudioInfo :: from) . unwrap_or_default ( ) ;
2356+ audio_info. mimetype = Some ( content_type. as_ref ( ) . to_owned ( ) ) ;
2357+ let content = content. info ( Box :: new ( audio_info) ) ;
2358+
2359+ GalleryItemType :: Audio ( content)
2360+ }
2361+
2362+ mime:: VIDEO => {
2363+ let info = assign ! ( info. map( VideoInfo :: from) . unwrap_or_default( ) , {
2364+ mimetype: Some ( content_type. as_ref( ) . to_owned( ) ) ,
2365+ thumbnail_source,
2366+ thumbnail_info
2367+ } ) ;
2368+ let content = assign ! ( VideoMessageEventContent :: new( body, source) , {
2369+ info: Some ( Box :: new( info) ) ,
2370+ formatted: formatted_caption,
2371+ filename
2372+ } ) ;
2373+ GalleryItemType :: Video ( content)
2374+ }
2375+
2376+ _ => {
2377+ let info = assign ! ( info. map( FileInfo :: from) . unwrap_or_default( ) , {
2378+ mimetype: Some ( content_type. as_ref( ) . to_owned( ) ) ,
2379+ thumbnail_source,
2380+ thumbnail_info
2381+ } ) ;
2382+ let content = assign ! ( FileMessageEventContent :: new( body, source) , {
2383+ info: Some ( Box :: new( info) ) ,
2384+ formatted: formatted_caption,
2385+ filename,
2386+ } ) ;
2387+ GalleryItemType :: File ( content)
2388+ }
2389+ }
2390+ }
2391+
22972392 /// Update the power levels of a select set of users of this room.
22982393 ///
22992394 /// Issue a `power_levels` state event request to the server, changing the
0 commit comments