Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(sdk): Welcome sliding_sync::Version and sliding_sync::VersionBuilder #3889

Merged
merged 11 commits into from
Aug 27, 2024
45 changes: 24 additions & 21 deletions crates/matrix-sdk/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,13 @@ impl ClientBuilder {
let http_client = HttpClient::new(inner_http_client.clone(), self.request_config);

#[allow(unused_variables)]
let (homeserver, well_known) = match homeserver_cfg {
HomeserverConfig::Url(url) => (url, None),
let (homeserver, well_known, versions) = match homeserver_cfg {
HomeserverConfig::Url(url) => (Url::parse(&url)?, None, None),

HomeserverConfig::ServerName { server: server_name, protocol } => {
let well_known = discover_homeserver(server_name, protocol, &http_client).await?;
(well_known.homeserver.base_url.clone(), Some(well_known))

(Url::parse(&well_known.homeserver.base_url)?, Some(well_known), None)
}

HomeserverConfig::ServerNameOrUrl(server_name_or_url) => {
Expand All @@ -472,9 +473,6 @@ impl ClientBuilder {
}
};

#[cfg(feature = "experimental-oidc")]
let allow_insecure_oidc = homeserver.starts_with("http://");

/*
#[cfg(feature = "experimental-sliding-sync")]
if self.is_simplified_sliding_sync_enabled {
Expand All @@ -491,7 +489,9 @@ impl ClientBuilder {
}
*/

let homeserver = Url::parse(&homeserver)?;

#[cfg(feature = "experimental-oidc")]
let allow_insecure_oidc = homeserver.scheme() == "http";

let auth_ctx = Arc::new(AuthCtx {
handle_refresh_tokens: self.handle_refresh_tokens,
Expand Down Expand Up @@ -541,7 +541,10 @@ impl ClientBuilder {
async fn discover_homeserver_from_server_name_or_url(
mut server_name_or_url: String,
http_client: &HttpClient,
) -> Result<(String, Option<discover_homeserver::Response>), ClientBuildError> {
) -> Result<
(Url, Option<discover_homeserver::Response>, Option<get_supported_versions::Response>),
ClientBuildError,
> {
let mut discovery_error: Option<ClientBuildError> = None;

// Attempt discovery as a server name first.
Expand All @@ -556,7 +559,7 @@ async fn discover_homeserver_from_server_name_or_url(

match discover_homeserver(server_name.clone(), protocol, http_client).await {
Ok(well_known) => {
return Ok((well_known.homeserver.base_url.clone(), Some(well_known)));
return Ok((Url::parse(&well_known.homeserver.base_url)?, Some(well_known), None));
}
Err(e) => {
debug!(error = %e, "Well-known discovery failed.");
Expand All @@ -575,8 +578,13 @@ async fn discover_homeserver_from_server_name_or_url(
// trying a homeserver URL.
if let Ok(homeserver_url) = Url::parse(&server_name_or_url) {
// Make sure the URL is definitely for a homeserver.
if check_is_homeserver(&homeserver_url, http_client).await {
return Ok((homeserver_url.to_string(), None));
match get_supported_versions(&homeserver_url, http_client).await {
Ok(_) => {
return Ok((homeserver_url, None, None));
}
Err(e) => {
debug!(error = %e, "Checking supported versions failed.");
}
}
}

Expand Down Expand Up @@ -626,9 +634,11 @@ async fn discover_homeserver(
Ok(well_known)
}

/// Checks if the given URL represents a valid homeserver.
async fn check_is_homeserver(homeserver_url: &Url, http_client: &HttpClient) -> bool {
match http_client
async fn get_supported_versions(
homeserver_url: &Url,
http_client: &HttpClient,
) -> Result<get_supported_versions::Response, HttpError> {
http_client
.send(
get_supported_versions::Request::new(),
Some(RequestConfig::short_retry()),
Expand All @@ -638,13 +648,6 @@ async fn check_is_homeserver(homeserver_url: &Url, http_client: &HttpClient) ->
Default::default(),
)
.await
{
Ok(_) => true,
Err(e) => {
debug!(error = %e, "Checking supported versions failed.");
false
}
}
}

#[allow(clippy::unused_async)] // False positive when building with !sqlite & !indexeddb
Expand Down