@@ -32,19 +32,22 @@ use mime::Mime;
3232use ruma:: api:: client:: config:: set_global_account_data:: v3:: Request as UpdateGlobalAccountDataRequest ;
3333use ruma:: {
3434 ClientSecret , MxcUri , OwnedMxcUri , OwnedRoomId , OwnedUserId , RoomId , SessionId , UInt , UserId ,
35- api:: client:: {
36- account:: {
37- add_3pid, change_password, deactivate, delete_3pid, get_3pids,
38- request_3pid_management_token_via_email, request_3pid_management_token_via_msisdn,
35+ api:: {
36+ Metadata ,
37+ client:: {
38+ account:: {
39+ add_3pid, change_password, deactivate, delete_3pid, get_3pids,
40+ request_3pid_management_token_via_email, request_3pid_management_token_via_msisdn,
41+ } ,
42+ config:: { get_global_account_data, set_global_account_data} ,
43+ error:: ErrorKind ,
44+ profile:: {
45+ AvatarUrl , DisplayName , ProfileFieldName , ProfileFieldValue , StaticProfileField ,
46+ delete_profile_field, get_profile, get_profile_field, set_avatar_url,
47+ set_display_name, set_profile_field,
48+ } ,
49+ uiaa:: AuthData ,
3950 } ,
40- config:: { get_global_account_data, set_global_account_data} ,
41- error:: ErrorKind ,
42- profile:: {
43- AvatarUrl , DisplayName , ProfileFieldName , ProfileFieldValue , StaticProfileField ,
44- delete_profile_field, get_profile, get_profile_field, set_avatar_url, set_display_name,
45- set_profile_field,
46- } ,
47- uiaa:: AuthData ,
4851 } ,
4952 assign,
5053 events:: {
@@ -130,10 +133,24 @@ impl Account {
130133 /// ```
131134 pub async fn set_display_name ( & self , name : Option < & str > ) -> Result < ( ) > {
132135 let user_id = self . client . user_id ( ) . ok_or ( Error :: AuthenticationRequired ) ?;
136+
137+ // Prefer the endpoint to delete profile fields, if it is supported.
138+ if name. is_none ( ) {
139+ let versions = self . client . supported_versions ( ) . await ?;
140+
141+ if delete_profile_field:: v3:: Request :: PATH_BUILDER . is_supported ( & versions) {
142+ return self . delete_profile_field ( ProfileFieldName :: DisplayName ) . await ;
143+ }
144+ }
145+
146+ // If name is `Some(_)`, this endpoint is the same as `set_profile_field`, but
147+ // we still need to use it in case it is `None` and the server doesn't support
148+ // the delete endpoint yet.
133149 #[ allow( deprecated) ]
134150 let request =
135151 set_display_name:: v3:: Request :: new ( user_id. to_owned ( ) , name. map ( ToOwned :: to_owned) ) ;
136152 self . client . send ( request) . await ?;
153+
137154 Ok ( ( ) )
138155 }
139156
@@ -201,10 +218,24 @@ impl Account {
201218 /// The avatar is unset if `url` is `None`.
202219 pub async fn set_avatar_url ( & self , url : Option < & MxcUri > ) -> Result < ( ) > {
203220 let user_id = self . client . user_id ( ) . ok_or ( Error :: AuthenticationRequired ) ?;
221+
222+ // Prefer the endpoint to delete profile fields, if it is supported.
223+ if url. is_none ( ) {
224+ let versions = self . client . supported_versions ( ) . await ?;
225+
226+ if delete_profile_field:: v3:: Request :: PATH_BUILDER . is_supported ( & versions) {
227+ return self . delete_profile_field ( ProfileFieldName :: AvatarUrl ) . await ;
228+ }
229+ }
230+
231+ // If url is `Some(_)`, this endpoint is the same as `set_profile_field`, but
232+ // we still need to use it in case it is `None` and the server doesn't support
233+ // the delete endpoint yet.
204234 #[ allow( deprecated) ]
205235 let request =
206236 set_avatar_url:: v3:: Request :: new ( user_id. to_owned ( ) , url. map ( ToOwned :: to_owned) ) ;
207237 self . client . send ( request) . await ?;
238+
208239 Ok ( ( ) )
209240 }
210241
0 commit comments