@@ -23,7 +23,7 @@ use js_int::uint;
2323#[ cfg( feature = "experimental-element-recent-emojis" ) ]
2424use matrix_sdk_base:: recent_emojis:: RecentEmojisContent ;
2525use matrix_sdk_base:: {
26- StateStoreDataKey , StateStoreDataValue ,
26+ SendOutsideWasm , StateStoreDataKey , StateStoreDataValue , SyncOutsideWasm ,
2727 media:: { MediaFormat , MediaRequestParameters } ,
2828 store:: StateStoreExt ,
2929} ;
@@ -40,7 +40,8 @@ use ruma::{
4040 config:: { get_global_account_data, set_global_account_data} ,
4141 error:: ErrorKind ,
4242 profile:: {
43- get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name,
43+ AvatarUrl , DisplayName , ProfileFieldName , ProfileFieldValue , StaticProfileField ,
44+ get_profile, get_profile_field, set_avatar_url, set_display_name,
4445 } ,
4546 uiaa:: AuthData ,
4647 } ,
@@ -107,11 +108,7 @@ impl Account {
107108 /// ```
108109 pub async fn get_display_name ( & self ) -> Result < Option < String > > {
109110 let user_id = self . client . user_id ( ) . ok_or ( Error :: AuthenticationRequired ) ?;
110- #[ allow( deprecated) ]
111- let request = get_display_name:: v3:: Request :: new ( user_id. to_owned ( ) ) ;
112- let request_config = self . client . request_config ( ) . force_auth ( ) ;
113- let response = self . client . send ( request) . with_request_config ( request_config) . await ?;
114- Ok ( response. displayname )
111+ self . fetch_profile_field_of_static :: < DisplayName > ( user_id. to_owned ( ) ) . await
115112 }
116113
117114 /// Set the display name of the account.
@@ -163,13 +160,10 @@ impl Account {
163160 /// ```
164161 pub async fn get_avatar_url ( & self ) -> Result < Option < OwnedMxcUri > > {
165162 let user_id = self . client . user_id ( ) . ok_or ( Error :: AuthenticationRequired ) ?;
166- #[ allow( deprecated) ]
167- let request = get_avatar_url:: v3:: Request :: new ( user_id. to_owned ( ) ) ;
168-
169- let config = Some ( RequestConfig :: new ( ) . force_auth ( ) ) ;
163+ let avatar_url =
164+ self . fetch_profile_field_of_static :: < AvatarUrl > ( user_id. to_owned ( ) ) . await ?;
170165
171- let response = self . client . send ( request) . with_request_config ( config) . await ?;
172- if let Some ( url) = response. avatar_url . clone ( ) {
166+ if let Some ( url) = avatar_url. clone ( ) {
173167 // If an avatar is found cache it.
174168 let _ = self
175169 . client
@@ -187,7 +181,7 @@ impl Account {
187181 . remove_kv_data ( StateStoreDataKey :: UserAvatarUrl ( user_id) )
188182 . await ;
189183 }
190- Ok ( response . avatar_url )
184+ Ok ( avatar_url)
191185 }
192186
193187 /// Get the URL of the account's avatar, if is stored in cache.
@@ -328,6 +322,80 @@ impl Account {
328322 . await ?)
329323 }
330324
325+ /// Get the given field from the given user's profile.
326+ ///
327+ /// # Arguments
328+ ///
329+ /// * `user_id` - The ID of the user to get the profile field of.
330+ ///
331+ /// * `field` - The name of the profile field to get.
332+ ///
333+ /// # Returns
334+ ///
335+ /// Returns an error if the request fails or if deserialization of the
336+ /// response fails.
337+ ///
338+ /// If the field is not set, the server should respond with an error with an
339+ /// [`ErrorCode::NotFound`], but it might also respond with an empty
340+ /// response, which would result in `Ok(None)`. Note that this error code
341+ /// might also mean that the given user ID doesn't exist.
342+ ///
343+ /// [`ErrorCode::NotFound`]: ruma::api::client::error::ErrorCode::NotFound
344+ pub async fn fetch_profile_field_of (
345+ & self ,
346+ user_id : OwnedUserId ,
347+ field : ProfileFieldName ,
348+ ) -> Result < Option < ProfileFieldValue > > {
349+ let request = get_profile_field:: v3:: Request :: new ( user_id, field) ;
350+ let response = self
351+ . client
352+ . send ( request)
353+ . with_request_config ( RequestConfig :: short_retry ( ) . force_auth ( ) )
354+ . await ?;
355+
356+ Ok ( response. value )
357+ }
358+
359+ /// Get the given statically-known field from the given user's profile.
360+ ///
361+ /// # Arguments
362+ ///
363+ /// * `user_id` - The ID of the user to get the profile field of.
364+ ///
365+ /// # Returns
366+ ///
367+ /// Returns an error if the request fails or if deserialization of the
368+ /// response fails.
369+ ///
370+ /// If the field is not set, the server should respond with an error with an
371+ /// [`ErrorCode::NotFound`], but it might also respond with an empty
372+ /// response, which would result in `Ok(None)`. Note that this error code
373+ /// might also mean that the given user ID doesn't exist.
374+ ///
375+ /// [`ErrorCode::NotFound`]: ruma::api::client::error::ErrorCode::NotFound
376+ pub async fn fetch_profile_field_of_static < F > (
377+ & self ,
378+ user_id : OwnedUserId ,
379+ ) -> Result < Option < F :: Value > >
380+ where
381+ F : StaticProfileField
382+ + std:: fmt:: Debug
383+ + Clone
384+ + SendOutsideWasm
385+ + SyncOutsideWasm
386+ + ' static ,
387+ F :: Value : SendOutsideWasm + SyncOutsideWasm ,
388+ {
389+ let request = get_profile_field:: v3:: Request :: new_static :: < F > ( user_id) ;
390+ let response = self
391+ . client
392+ . send ( request)
393+ . with_request_config ( RequestConfig :: short_retry ( ) . force_auth ( ) )
394+ . await ?;
395+
396+ Ok ( response. value )
397+ }
398+
331399 /// Change the password of the account.
332400 ///
333401 /// # Arguments
0 commit comments