Skip to content

Commit ea28234

Browse files
pixlwavepoljar
authored andcommitted
sdk: Cache the client well-known file and add Client::rtc_foci which uses it.
1 parent c74295c commit ea28234

File tree

12 files changed

+226
-49
lines changed

12 files changed

+226
-49
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ reqwest = { version = "0.12.12", default-features = false }
6060
rmp-serde = "1.3.0"
6161
# Be careful to use commits from the https://github.com/ruma/ruma/tree/ruma-0.12
6262
# branch until a proper release with breaking changes happens.
63-
ruma = { version = "0.12.3", features = [
63+
ruma = { git = "https://github.com/ruma/ruma", rev = "d1d53e2b7aaf9190f11a5465b9edf6a19fc5b59a", features = [
6464
"client-api-c",
6565
"compat-upload-signatures",
6666
"compat-user-id",
@@ -73,11 +73,12 @@ ruma = { version = "0.12.3", features = [
7373
"unstable-msc3489",
7474
"unstable-msc4075",
7575
"unstable-msc4140",
76+
"unstable-msc4143",
7677
"unstable-msc4171",
7778
"unstable-msc4278",
7879
"unstable-msc4286",
79-
] }
80-
ruma-common = "0.15.2"
80+
] }
81+
ruma-common = { git = "https://github.com/ruma/ruma", rev = "d1d53e2b7aaf9190f11a5465b9edf6a19fc5b59a" }
8182
sentry = "0.36.0"
8283
sentry-tracing = "0.36.0"
8384
serde = { version = "1.0.217", features = ["rc"] }

crates/matrix-sdk-base/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ No notable changes in this release.
5252
([#4897](https://github.com/matrix-org/matrix-rust-sdk/pull/4897))
5353
- [**breaking**] `RoomInfo::prev_state` has been removed due to being useless.
5454
([#5054](https://github.com/matrix-org/matrix-rust-sdk/pull/5054))
55+
- The cached `ServerCapabilities` has been renamed to `ServerInfo` and additionally contains
56+
the well-known response alongside the existing server versions. Despite the old name, it
57+
does not contain the server capabilities (yet?!).
5558

5659
## [0.10.0] - 2025-02-04
5760

crates/matrix-sdk-base/src/store/integration_tests.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use assert_matches2::assert_let;
77
use growable_bloom_filter::GrowableBloomBuilder;
88
use matrix_sdk_test::{event_factory::EventFactory, test_json};
99
use ruma::{
10-
api::MatrixVersion,
10+
api::{
11+
client::discovery::discover_homeserver::{HomeserverInfo, RtcFocusInfo},
12+
MatrixVersion,
13+
},
1114
event_id,
1215
events::{
1316
presence::PresenceEvent,
@@ -34,7 +37,7 @@ use serde_json::{json, value::Value as JsonValue};
3437

3538
use super::{
3639
send_queue::SentRequestKey, DependentQueuedRequestKind, DisplayName, DynStateStore,
37-
RoomLoadSettings, ServerInfo,
40+
RoomLoadSettings, ServerInfo, WellKnownResponse,
3841
};
3942
use crate::{
4043
deserialized_responses::MemberEvent,
@@ -477,6 +480,12 @@ impl StateStoreIntegrationTests for DynStateStore {
477480
let server_info = ServerInfo::new(
478481
versions.iter().map(|version| version.to_string()).collect(),
479482
[("org.matrix.experimental".to_owned(), true)].into(),
483+
Some(WellKnownResponse {
484+
homeserver: HomeserverInfo::new("matrix.example.com".to_owned()),
485+
identity_server: None,
486+
tile_server: None,
487+
rtc_foci: vec![RtcFocusInfo::livekit("livekit.example.com".to_owned())],
488+
}),
480489
);
481490

482491
self.set_kv_data(

crates/matrix-sdk-base/src/store/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub use self::{
8282
},
8383
traits::{
8484
ComposerDraft, ComposerDraftType, DynStateStore, IntoStateStore, ServerInfo, StateStore,
85-
StateStoreDataKey, StateStoreDataValue, StateStoreExt,
85+
StateStoreDataKey, StateStoreDataValue, StateStoreExt, WellKnownResponse,
8686
},
8787
};
8888

crates/matrix-sdk-base/src/store/traits.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ use async_trait::async_trait;
2424
use growable_bloom_filter::GrowableBloom;
2525
use matrix_sdk_common::AsyncTraitDeps;
2626
use ruma::{
27-
api::{client::discovery::get_supported_versions, MatrixVersion},
27+
api::{
28+
client::discovery::{
29+
discover_homeserver,
30+
discover_homeserver::{
31+
HomeserverInfo, IdentityServerInfo, RtcFocusInfo, TileServerInfo,
32+
},
33+
get_supported_versions,
34+
},
35+
MatrixVersion,
36+
},
2837
events::{
2938
presence::PresenceEvent,
3039
receipt::{Receipt, ReceiptThread, ReceiptType},
@@ -960,6 +969,10 @@ pub struct ServerInfo {
960969
/// List of unstable features and their enablement status.
961970
pub unstable_features: BTreeMap<String, bool>,
962971

972+
/// Information about the server found in the client well-known file.
973+
#[serde(skip_serializing_if = "Option::is_none")]
974+
pub well_known: Option<WellKnownResponse>,
975+
963976
/// Last time we fetched this data from the server, in milliseconds since
964977
/// epoch.
965978
last_fetch_ts: f64,
@@ -970,8 +983,12 @@ impl ServerInfo {
970983
pub const STALE_THRESHOLD: f64 = (1000 * 60 * 60 * 24 * 7) as _; // seven days
971984

972985
/// Encode server info into this serializable struct.
973-
pub fn new(versions: Vec<String>, unstable_features: BTreeMap<String, bool>) -> Self {
974-
Self { versions, unstable_features, last_fetch_ts: now_timestamp_ms() }
986+
pub fn new(
987+
versions: Vec<String>,
988+
unstable_features: BTreeMap<String, bool>,
989+
well_known: Option<WellKnownResponse>,
990+
) -> Self {
991+
Self { versions, unstable_features, well_known, last_fetch_ts: now_timestamp_ms() }
975992
}
976993

977994
/// Decode server info from this serializable struct.
@@ -998,6 +1015,33 @@ impl ServerInfo {
9981015
}
9991016
}
10001017

1018+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1019+
/// A serialisable representation of discover_homeserver::Response.
1020+
pub struct WellKnownResponse {
1021+
/// Information about the homeserver to connect to.
1022+
pub homeserver: HomeserverInfo,
1023+
1024+
/// Information about the identity server to connect to.
1025+
pub identity_server: Option<IdentityServerInfo>,
1026+
1027+
/// Information about the tile server to use to display location data.
1028+
pub tile_server: Option<TileServerInfo>,
1029+
1030+
/// A list of the available MatrixRTC foci, ordered by priority.
1031+
pub rtc_foci: Vec<RtcFocusInfo>,
1032+
}
1033+
1034+
impl From<discover_homeserver::Response> for WellKnownResponse {
1035+
fn from(response: discover_homeserver::Response) -> Self {
1036+
Self {
1037+
homeserver: response.homeserver,
1038+
identity_server: response.identity_server,
1039+
tile_server: response.tile_server,
1040+
rtc_foci: response.rtc_foci,
1041+
}
1042+
}
1043+
}
1044+
10011045
/// Get the current timestamp as the number of milliseconds since Unix Epoch.
10021046
fn now_timestamp_ms() -> f64 {
10031047
SystemTime::now()
@@ -1180,6 +1224,7 @@ mod tests {
11801224
let mut server_info = ServerInfo {
11811225
versions: Default::default(),
11821226
unstable_features: Default::default(),
1227+
well_known: Default::default(),
11831228
last_fetch_ts: now_timestamp_ms() - ServerInfo::STALE_THRESHOLD - 1.0,
11841229
};
11851230

crates/matrix-sdk/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ All notable changes to this project will be documented in this file.
7878
([#5047](https://github.com/matrix-org/matrix-rust-sdk/pull/5047))
7979
- `Room::set_unread_flag()` is now a no-op if the unread flag already has the wanted value.
8080
([#5055](https://github.com/matrix-org/matrix-rust-sdk/pull/5055))
81+
- `ClientServerCapabilities` has been renamed to `ClientServerInfo`. Alongside this,
82+
`Client::reset_server_info` is now `Client::reset_server_info` and `Client::fetch_server_capabilities`
83+
is now `Client::fetch_server_versions`, returning the server versions response directly.
8184

8285
## [0.11.0] - 2025-04-11
8386

crates/matrix-sdk/src/authentication/oauth/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ async fn test_insecure_clients() -> anyhow::Result<()> {
516516
let server = MatrixMockServer::new().await;
517517
let server_url = server.server().uri();
518518

519-
server.mock_well_known().ok().expect(1).named("well_known").mount().await;
519+
server.mock_well_known().ok().expect(1..).named("well_known").mount().await;
520520
server.mock_versions().ok().expect(1..).named("versions").mount().await;
521521

522522
let oauth_server = server.oauth();

crates/matrix-sdk/src/client/builder/homeserver_config.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub(super) struct HomeserverDiscoveryResult {
5353
pub server: Option<Url>,
5454
pub homeserver: Url,
5555
pub supported_versions: Option<get_supported_versions::Response>,
56+
pub well_known: Option<discover_homeserver::Response>,
5657
}
5758

5859
impl HomeserverConfig {
@@ -68,6 +69,7 @@ impl HomeserverConfig {
6869
server: None, // We can't know the `server` if we only have a `homeserver`.
6970
homeserver,
7071
supported_versions: None,
72+
well_known: None,
7173
}
7274
}
7375

@@ -79,18 +81,19 @@ impl HomeserverConfig {
7981
server: Some(server),
8082
homeserver: Url::parse(&well_known.homeserver.base_url)?,
8183
supported_versions: None,
84+
well_known: Some(well_known),
8285
}
8386
}
8487

8588
Self::ServerNameOrHomeserverUrl(server_name_or_url) => {
86-
let (server, homeserver, supported_versions) =
89+
let (server, homeserver, supported_versions, well_known) =
8790
discover_homeserver_from_server_name_or_url(
8891
server_name_or_url.to_owned(),
8992
http_client,
9093
)
9194
.await?;
9295

93-
HomeserverDiscoveryResult { server, homeserver, supported_versions }
96+
HomeserverDiscoveryResult { server, homeserver, supported_versions, well_known }
9497
}
9598
})
9699
}
@@ -102,7 +105,15 @@ impl HomeserverConfig {
102105
async fn discover_homeserver_from_server_name_or_url(
103106
mut server_name_or_url: String,
104107
http_client: &HttpClient,
105-
) -> Result<(Option<Url>, Url, Option<get_supported_versions::Response>), ClientBuildError> {
108+
) -> Result<
109+
(
110+
Option<Url>,
111+
Url,
112+
Option<get_supported_versions::Response>,
113+
Option<discover_homeserver::Response>,
114+
),
115+
ClientBuildError,
116+
> {
106117
let mut discovery_error: Option<ClientBuildError> = None;
107118

108119
// Attempt discovery as a server name first.
@@ -117,7 +128,12 @@ async fn discover_homeserver_from_server_name_or_url(
117128

118129
match discover_homeserver(server_name, &protocol, http_client).await {
119130
Ok((server, well_known)) => {
120-
return Ok((Some(server), Url::parse(&well_known.homeserver.base_url)?, None));
131+
return Ok((
132+
Some(server),
133+
Url::parse(&well_known.homeserver.base_url)?,
134+
None,
135+
Some(well_known),
136+
));
121137
}
122138
Err(e) => {
123139
debug!(error = %e, "Well-known discovery failed.");
@@ -138,7 +154,7 @@ async fn discover_homeserver_from_server_name_or_url(
138154
// Make sure the URL is definitely for a homeserver.
139155
match get_supported_versions(&homeserver_url, http_client).await {
140156
Ok(response) => {
141-
return Ok((None, homeserver_url, Some(response)));
157+
return Ok((None, homeserver_url, Some(response), None));
142158
}
143159
Err(e) => {
144160
debug!(error = %e, "Checking supported versions failed.");

crates/matrix-sdk/src/client/builder/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl ClientBuilder {
525525
let http_client = HttpClient::new(inner_http_client.clone(), self.request_config);
526526

527527
#[allow(unused_variables)]
528-
let HomeserverDiscoveryResult { server, homeserver, supported_versions } =
528+
let HomeserverDiscoveryResult { server, homeserver, supported_versions, well_known } =
529529
homeserver_cfg.discover(&http_client).await?;
530530

531531
let sliding_sync_version = {
@@ -560,8 +560,11 @@ impl ClientBuilder {
560560
// Enable the send queue by default.
561561
let send_queue = Arc::new(SendQueueData::new(true));
562562

563-
let server_info =
564-
ClientServerInfo { server_versions: self.server_versions, unstable_features: None };
563+
let server_info = ClientServerInfo {
564+
server_versions: self.server_versions,
565+
unstable_features: None,
566+
well_known: well_known.map(Into::into),
567+
};
565568

566569
let event_cache = OnceCell::new();
567570
let inner = ClientInner::new(

0 commit comments

Comments
 (0)