Skip to content

Commit 33df042

Browse files
zecakehHywan
authored andcommitted
Upgrade Ruma: profile response
Handle the changes to the Response of the get_profile endpoint. The content of the response is private and fields must be accessed with methods. Signed-off-by: Kévin Commaille <[email protected]>
1 parent a3a239f commit 33df042

File tree

9 files changed

+66
-39
lines changed

9 files changed

+66
-39
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ proptest = { version = "1.6.0", default-features = false, features = ["std"] }
5959
rand = "0.8.5"
6060
reqwest = { version = "0.12.12", default-features = false }
6161
rmp-serde = "1.3.0"
62-
ruma = { git = "https://github.com/ruma/ruma", rev = "d879f7df16ba9928a73649f8149dabeee939691e", features = [
62+
ruma = { git = "https://github.com/ruma/ruma", rev = "0ecd23c32c58be9328fa5f3e33fe0458cc139213", features = [
6363
"client-api-c",
6464
"compat-upload-signatures",
6565
"compat-arbitrary-length-ids",
@@ -79,7 +79,7 @@ ruma = { git = "https://github.com/ruma/ruma", rev = "d879f7df16ba9928a73649f814
7979
"unstable-msc4286",
8080
"unstable-msc4306"
8181
] }
82-
ruma-common = { git = "https://github.com/ruma/ruma", rev = "d879f7df16ba9928a73649f8149dabeee939691e" }
82+
ruma-common = { git = "https://github.com/ruma/ruma", rev = "0ecd23c32c58be9328fa5f3e33fe0458cc139213" }
8383
sentry = "0.36.0"
8484
sentry-tracing = "0.36.0"
8585
serde = { version = "1.0.217", features = ["rc"] }

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use matrix_sdk::{
3939
},
4040
sliding_sync::Version as SdkSlidingSyncVersion,
4141
store::RoomLoadSettings as SdkRoomLoadSettings,
42-
AuthApi, AuthSession, Client as MatrixClient, SessionChange, SessionTokens,
42+
Account, AuthApi, AuthSession, Client as MatrixClient, SessionChange, SessionTokens,
4343
STATE_STORE_DATABASE_NAME,
4444
};
4545
use matrix_sdk_common::{stream::StreamExt, SendOutsideWasm, SyncOutsideWasm};
@@ -53,7 +53,12 @@ use matrix_sdk_ui::{
5353
use mime::Mime;
5454
use oauth2::Scope;
5555
use ruma::{
56-
api::client::{alias::get_alias, error::ErrorKind, uiaa::UserIdentifier},
56+
api::client::{
57+
alias::get_alias,
58+
error::ErrorKind,
59+
profile::{AvatarUrl, DisplayName},
60+
uiaa::UserIdentifier,
61+
},
5762
events::{
5863
direct::DirectEventContent,
5964
fully_read::FullyReadEventContent,
@@ -1233,15 +1238,8 @@ impl Client {
12331238
}
12341239

12351240
pub async fn get_profile(&self, user_id: String) -> Result<UserProfile, ClientError> {
1236-
let owned_user_id = UserId::parse(user_id.clone())?;
1237-
1238-
let response = self.inner.account().fetch_user_profile_of(&owned_user_id).await?;
1239-
1240-
Ok(UserProfile {
1241-
user_id,
1242-
display_name: response.displayname.clone(),
1243-
avatar_url: response.avatar_url.as_ref().map(|url| url.to_string()),
1244-
})
1241+
let user_id = <&UserId>::try_from(user_id.as_str())?;
1242+
UserProfile::fetch(&self.inner.account(), user_id).await
12451243
}
12461244

12471245
pub async fn notification_client(
@@ -1812,6 +1810,18 @@ pub struct UserProfile {
18121810
pub avatar_url: Option<String>,
18131811
}
18141812

1813+
impl UserProfile {
1814+
/// Fetch the profile for the given user ID, using the given [`Account`]
1815+
/// API.
1816+
pub(crate) async fn fetch(account: &Account, user_id: &UserId) -> Result<Self, ClientError> {
1817+
let response = account.fetch_user_profile_of(user_id).await?;
1818+
let display_name = response.get_static::<DisplayName>()?;
1819+
let avatar_url = response.get_static::<AvatarUrl>()?.map(|url| url.to_string());
1820+
1821+
Ok(UserProfile { user_id: user_id.to_string(), display_name, avatar_url })
1822+
}
1823+
}
1824+
18151825
impl From<&search_users::v3::User> for UserProfile {
18161826
fn from(value: &search_users::v3::User) -> Self {
18171827
UserProfile {

bindings/matrix-sdk-ffi/src/session_verification.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,14 @@ impl SessionVerificationController {
254254
return;
255255
};
256256

257-
let Ok(sender_profile) = self.account.fetch_user_profile_of(sender).await else {
257+
let Ok(sender_profile) = UserProfile::fetch(&self.account, sender).await else {
258258
error!("Failed fetching user profile for verification request");
259259
return;
260260
};
261261

262262
if let Some(delegate) = &*self.delegate.read().unwrap() {
263263
delegate.did_receive_verification_request(SessionVerificationRequestDetails {
264-
sender_profile: UserProfile {
265-
user_id: request.other_user_id().to_string(),
266-
display_name: sender_profile.displayname,
267-
avatar_url: sender_profile.avatar_url.as_ref().map(|url| url.to_string()),
268-
},
264+
sender_profile,
269265
flow_id: request.flow_id().into(),
270266
device_id: other_device_data.device_id().into(),
271267
device_display_name: other_device_data.display_name().map(str::to_string),

crates/matrix-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ ruma = { workspace = true, features = [
109109
"unstable-msc4230",
110110
"unstable-msc2967",
111111
"unstable-msc4108",
112+
"unstable-msc4133",
112113
"unstable-msc4278",
113114
] }
114115
serde.workspace = true

crates/matrix-sdk/src/account.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,25 @@ impl Account {
269269
Ok(upload_response.content_uri)
270270
}
271271

272-
/// Get the profile of the account.
272+
/// Get the profile of this account.
273273
///
274-
/// Allows to get both the display name and avatar URL in a single call.
274+
/// Allows to get all the profile data in a single call.
275275
///
276276
/// # Examples
277277
///
278278
/// ```no_run
279279
/// # use matrix_sdk::Client;
280+
/// use ruma::api::client::profile::{AvatarUrl, DisplayName};
280281
/// # use url::Url;
281282
/// # async {
282283
/// # let homeserver = Url::parse("http://localhost:8080")?;
283284
/// # let client = Client::new(homeserver).await?;
285+
///
284286
/// let profile = client.account().fetch_user_profile().await?;
285-
/// println!(
286-
/// "You are '{:?}' with avatar '{:?}'",
287-
/// profile.displayname, profile.avatar_url
288-
/// );
287+
/// let display_name = profile.get_static::<DisplayName>()?;
288+
/// let avatar_url = profile.get_static::<AvatarUrl>()?;
289+
///
290+
/// println!("You are '{display_name:?}' with avatar '{avatar_url:?}'");
289291
/// # anyhow::Ok(()) };
290292
/// ```
291293
pub async fn fetch_user_profile(&self) -> Result<get_profile::v3::Response> {

crates/matrix-sdk/src/widget/settings/element_call.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,13 @@ mod tests {
441441
}
442442

443443
fn build_url_from_widget_settings(settings: WidgetSettings) -> String {
444+
let mut profile = get_profile::v3::Response::new();
445+
profile.set("avatar_url", "some-url".into());
446+
profile.set("displayname", "hello".into());
447+
444448
settings
445449
._generate_webview_url(
446-
get_profile::v3::Response::new(Some("some-url".into()), Some("hello".into())),
450+
profile,
447451
"@test:user.org".try_into().unwrap(),
448452
"!room_id:room.org".try_into().unwrap(),
449453
"ABCDEFG".into(),

crates/matrix-sdk/src/widget/settings/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
// limitations under the License.
1414

1515
use language_tags::LanguageTag;
16-
use ruma::{api::client::profile::get_profile, DeviceId, RoomId, UserId};
16+
use ruma::{
17+
api::client::profile::{get_profile, AvatarUrl, DisplayName},
18+
DeviceId, RoomId, UserId,
19+
};
1720
use url::Url;
1821

1922
use crate::Room;
@@ -110,12 +113,17 @@ impl WidgetSettings {
110113
homeserver_url: Url,
111114
client_props: ClientProperties,
112115
) -> Result<Url, url::ParseError> {
113-
let avatar_url = profile.avatar_url.map(|url| url.to_string()).unwrap_or_default();
116+
let avatar_url = profile
117+
.get_static::<AvatarUrl>()
118+
.ok()
119+
.flatten()
120+
.map(|url| url.to_string())
121+
.unwrap_or_default();
114122

115123
let query_props = url_params::QueryProperties {
116124
widget_id: self.widget_id.clone(),
117125
avatar_url,
118-
display_name: profile.displayname.unwrap_or_default(),
126+
display_name: profile.get_static::<DisplayName>().ok().flatten().unwrap_or_default(),
119127
user_id: user_id.into(),
120128
room_id: room_id.into(),
121129
language: client_props.language.to_string(),

examples/get_profiles/src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use std::{env, process::exit};
22

33
use matrix_sdk::{
44
Client, Result as MatrixResult,
5-
ruma::{OwnedMxcUri, UserId, api::client::profile},
5+
ruma::{
6+
OwnedMxcUri, UserId,
7+
api::client::profile::{self, AvatarUrl, DisplayName},
8+
},
69
};
710
use url::Url;
811

@@ -27,7 +30,10 @@ async fn get_profile(client: Client, mxid: &UserId) -> MatrixResult<UserProfile>
2730
// Use the response and construct a UserProfile struct.
2831
// See https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/r0/profile/get_profile/struct.Response.html
2932
// for details on the Response for this Request
30-
let user_profile = UserProfile { avatar_url: resp.avatar_url, displayname: resp.displayname };
33+
let user_profile = UserProfile {
34+
avatar_url: resp.get_static::<AvatarUrl>()?,
35+
displayname: resp.get_static::<DisplayName>()?,
36+
};
3137
Ok(user_profile)
3238
}
3339

0 commit comments

Comments
 (0)