@@ -45,6 +45,7 @@ use ruma::{
45
45
client:: {
46
46
account:: whoami,
47
47
alias:: { create_alias, delete_alias, get_alias} ,
48
+ authenticated_media,
48
49
device:: { delete_devices, get_devices, update_device} ,
49
50
directory:: { get_public_rooms, get_public_rooms_filtered} ,
50
51
discovery:: {
@@ -55,6 +56,7 @@ use ruma::{
55
56
error:: ErrorKind ,
56
57
filter:: { create_filter:: v3:: Request as FilterUploadRequest , FilterDefinition } ,
57
58
knock:: knock_room,
59
+ media,
58
60
membership:: { join_room_by_id, join_room_by_id_or_alias} ,
59
61
room:: create_room,
60
62
session:: login:: v3:: DiscoveryInfo ,
@@ -2792,12 +2794,22 @@ impl Client {
2792
2794
return Ok ( data. to_owned ( ) ) ;
2793
2795
}
2794
2796
2795
- let response = self
2796
- . send ( ruma:: api:: client:: authenticated_media:: get_media_config:: v1:: Request :: default ( ) )
2797
- . await ?;
2797
+ // Use the authenticated endpoint when the server supports it.
2798
+ let supported_versions = self . supported_versions ( ) . await ?;
2799
+ let use_auth =
2800
+ authenticated_media:: get_media_config:: v1:: Request :: is_supported ( & supported_versions) ;
2798
2801
2799
- match max_upload_size_lock. set ( response. upload_size ) {
2800
- Ok ( _) => Ok ( response. upload_size ) ,
2802
+ let upload_size = if use_auth {
2803
+ self . send ( authenticated_media:: get_media_config:: v1:: Request :: default ( ) )
2804
+ . await ?
2805
+ . upload_size
2806
+ } else {
2807
+ #[ allow( deprecated) ]
2808
+ self . send ( media:: get_media_config:: v3:: Request :: default ( ) ) . await ?. upload_size
2809
+ } ;
2810
+
2811
+ match max_upload_size_lock. set ( upload_size) {
2812
+ Ok ( _) => Ok ( upload_size) ,
2801
2813
Err ( error) => {
2802
2814
Err ( Error :: Media ( MediaError :: FetchMaxUploadSizeFailed ( error. to_string ( ) ) ) )
2803
2815
}
@@ -3803,7 +3815,9 @@ pub(crate) mod tests {
3803
3815
}
3804
3816
3805
3817
#[ async_test]
3806
- async fn test_load_or_fetch_max_upload_size ( ) {
3818
+ async fn test_load_or_fetch_max_upload_size_with_auth_matrix_version ( ) {
3819
+ // The default Matrix version we use is 1.11 or higher, so authenticated media
3820
+ // is supported.
3807
3821
let server = MatrixMockServer :: new ( ) . await ;
3808
3822
let client = server. client_builder ( ) . build ( ) . await ;
3809
3823
@@ -3815,6 +3829,55 @@ pub(crate) mod tests {
3815
3829
assert_eq ! ( * client. inner. server_max_upload_size. lock( ) . await . get( ) . unwrap( ) , uint!( 2 ) ) ;
3816
3830
}
3817
3831
3832
+ #[ async_test]
3833
+ async fn test_load_or_fetch_max_upload_size_with_auth_stable_feature ( ) {
3834
+ // The server must advertise support for the stable feature for authenticated
3835
+ // media support, so we mock the `GET /versions` response.
3836
+ let server = MatrixMockServer :: new ( ) . await ;
3837
+ let client = server. client_builder ( ) . no_server_versions ( ) . build ( ) . await ;
3838
+
3839
+ server
3840
+ . mock_versions ( )
3841
+ . ok_custom (
3842
+ & [ "v1.7" , "v1.8" , "v1.9" , "v1.10" ] ,
3843
+ & [ ( "org.matrix.msc3916.stable" , true ) ] . into ( ) ,
3844
+ )
3845
+ . named ( "versions" )
3846
+ . expect ( 1 )
3847
+ . mount ( )
3848
+ . await ;
3849
+
3850
+ assert ! ( !client. inner. server_max_upload_size. lock( ) . await . initialized( ) ) ;
3851
+
3852
+ server. mock_authenticated_media_config ( ) . ok ( uint ! ( 2 ) ) . mock_once ( ) . mount ( ) . await ;
3853
+ client. load_or_fetch_max_upload_size ( ) . await . unwrap ( ) ;
3854
+
3855
+ assert_eq ! ( * client. inner. server_max_upload_size. lock( ) . await . get( ) . unwrap( ) , uint!( 2 ) ) ;
3856
+ }
3857
+
3858
+ #[ async_test]
3859
+ async fn test_load_or_fetch_max_upload_size_no_auth ( ) {
3860
+ // The server must not support Matrix 1.11 or higher for unauthenticated
3861
+ // media requests, so we mock the `GET /versions` response.
3862
+ let server = MatrixMockServer :: new ( ) . await ;
3863
+ let client = server. client_builder ( ) . no_server_versions ( ) . build ( ) . await ;
3864
+
3865
+ server
3866
+ . mock_versions ( )
3867
+ . ok_custom ( & [ "v1.1" ] , & Default :: default ( ) )
3868
+ . named ( "versions" )
3869
+ . expect ( 1 )
3870
+ . mount ( )
3871
+ . await ;
3872
+
3873
+ assert ! ( !client. inner. server_max_upload_size. lock( ) . await . initialized( ) ) ;
3874
+
3875
+ server. mock_media_config ( ) . ok ( uint ! ( 2 ) ) . mock_once ( ) . mount ( ) . await ;
3876
+ client. load_or_fetch_max_upload_size ( ) . await . unwrap ( ) ;
3877
+
3878
+ assert_eq ! ( * client. inner. server_max_upload_size. lock( ) . await . get( ) . unwrap( ) , uint!( 2 ) ) ;
3879
+ }
3880
+
3818
3881
#[ async_test]
3819
3882
async fn test_uploading_a_too_large_media_file ( ) {
3820
3883
let server = MatrixMockServer :: new ( ) . await ;
0 commit comments