Skip to content

Commit 4ea0b7d

Browse files
zecakehpoljar
authored andcommitted
refactor(sdk): Prefer DELETE HTTP method for profile fields
When it is supported by the homeserver. Signed-off-by: Kévin Commaille <[email protected]>
1 parent d2faa1b commit 4ea0b7d

File tree

2 files changed

+102
-39
lines changed

2 files changed

+102
-39
lines changed

crates/matrix-sdk/src/account.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,22 @@ use mime::Mime;
3232
use ruma::api::client::config::set_global_account_data::v3::Request as UpdateGlobalAccountDataRequest;
3333
use 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

crates/matrix-sdk/tests/integration/account.rs

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -157,34 +157,66 @@ async fn test_set_profile_field() {
157157
#[async_test]
158158
async fn test_delete_profile_field() {
159159
let server = MatrixMockServer::new().await;
160-
let client = server.client_builder().server_versions(vec![MatrixVersion::V1_16]).build().await;
161-
let user_id = client.user_id().unwrap();
162160

163-
server
164-
.mock_delete_profile_field(user_id, ProfileFieldName::TimeZone)
165-
.ok()
166-
.mock_once()
167-
.named("delete m.tz profile field")
168-
.mount()
169-
.await;
170-
server
171-
.mock_set_profile_field(user_id, ProfileFieldName::DisplayName)
172-
.ok()
173-
.mock_once()
174-
.named("set displayname profile field")
175-
.mount()
176-
.await;
177-
server
178-
.mock_set_profile_field(user_id, ProfileFieldName::AvatarUrl)
179-
.ok()
180-
.mock_once()
181-
.named("set avatar_url profile field")
182-
.mount()
183-
.await;
161+
// Test with server that does NOT support deleting custom fields.
162+
{
163+
let client =
164+
server.client_builder().server_versions(vec![MatrixVersion::V1_15]).build().await;
165+
let user_id = client.user_id().unwrap();
166+
167+
let _guard = server
168+
.mock_set_profile_field(user_id, ProfileFieldName::DisplayName)
169+
.ok()
170+
.mock_once()
171+
.named("set displayname profile field")
172+
.mount_as_scoped()
173+
.await;
174+
let _guard = server
175+
.mock_set_profile_field(user_id, ProfileFieldName::AvatarUrl)
176+
.ok()
177+
.mock_once()
178+
.named("set avatar_url profile field")
179+
.mount_as_scoped()
180+
.await;
184181

185-
let account = client.account();
182+
let account = client.account();
186183

187-
account.set_avatar_url(None).await.unwrap();
188-
account.set_display_name(None).await.unwrap();
189-
account.delete_profile_field(ProfileFieldName::TimeZone).await.unwrap();
184+
account.set_avatar_url(None).await.unwrap();
185+
account.set_display_name(None).await.unwrap();
186+
}
187+
188+
// Test with server that supports deleting custom fields.
189+
{
190+
let client =
191+
server.client_builder().server_versions(vec![MatrixVersion::V1_16]).build().await;
192+
let user_id = client.user_id().unwrap();
193+
194+
let _guard = server
195+
.mock_delete_profile_field(user_id, ProfileFieldName::AvatarUrl)
196+
.ok()
197+
.mock_once()
198+
.named("delete m.tz profile field")
199+
.mount_as_scoped()
200+
.await;
201+
let _guard = server
202+
.mock_delete_profile_field(user_id, ProfileFieldName::DisplayName)
203+
.ok()
204+
.mock_once()
205+
.named("delete m.tz profile field")
206+
.mount_as_scoped()
207+
.await;
208+
let _guard = server
209+
.mock_delete_profile_field(user_id, ProfileFieldName::TimeZone)
210+
.ok()
211+
.mock_once()
212+
.named("delete m.tz profile field")
213+
.mount_as_scoped()
214+
.await;
215+
216+
let account = client.account();
217+
218+
account.set_avatar_url(None).await.unwrap();
219+
account.set_display_name(None).await.unwrap();
220+
account.delete_profile_field(ProfileFieldName::TimeZone).await.unwrap();
221+
}
190222
}

0 commit comments

Comments
 (0)