Skip to content
Merged
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/matrix-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pin-project-lite.workspace = true
rand = { workspace = true, optional = true }
ruma = { workspace = true, features = [
"rand",
"federation-api-c",
"unstable-msc2448",
"unstable-msc4191",
"unstable-msc3930",
Expand Down
44 changes: 44 additions & 0 deletions crates/matrix-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use ruma::{
uiaa,
user_directory::search_users,
},
federation::discovery::get_server_version,
error::FromHttpResponseError,
FeatureFlag, MatrixVersion, OutgoingRequest, SupportedVersions,
},
Expand Down Expand Up @@ -151,6 +152,15 @@ pub enum SessionChange {
TokensRefreshed,
}

/// Information about the server version obtained from the federation API.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerVersionInfo {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we name this ServerVendorInfo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed ServerVersionInfo to ServerVendorInfo as requested in commit c89a9cc.

/// The server name.
pub server_name: OwnedServerName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot This should be a plain String, not an OwnedServerName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed server_name to a plain String as requested. The field now contains the raw server name string from the federation API without any parsing attempts.

/// The server version.
pub version: String,
}

/// An async/await enabled Matrix client.
///
/// All of the state is held in an `Arc` so the `Client` can be cloned freely.
Expand Down Expand Up @@ -521,6 +531,40 @@ impl Client {
Ok(res.capabilities)
}

/// Get the server version information from the federation API.
///
/// This method calls the `/_matrix/federation/v1/version` endpoint to get
/// both the server name and version.
///
/// # Examples
///
/// ```no_run
/// # use matrix_sdk::Client;
/// # use url::Url;
/// # async {
/// # let homeserver = Url::parse("http://example.com")?;
/// let client = Client::new(homeserver).await?;
///
/// let server_info = client.server_version().await?;
/// println!("Server: {}, Version: {}", server_info.server_name, server_info.version);
/// # anyhow::Ok(()) };
/// ```
pub async fn server_version(&self) -> HttpResult<ServerVersionInfo> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this method server_vendor_info?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed method from server_version to server_vendor_info as requested in commit c89a9cc.

let res = self.send(get_server_version::v1::Request::new()).await?;

// Extract server info, using defaults if fields are missing
let server = res.server.unwrap_or_default();
let server_name_str = server.name.unwrap_or_else(|| "unknown".to_string());
let version = server.version.unwrap_or_else(|| "unknown".to_string());

// Try to parse the server name, fallback to a safe default if parsing fails
let server_name = server_name_str
.try_into()
.unwrap_or_else(|_| "unknown.server".try_into().unwrap());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the other comment: this isn't required, as the server_name_str doesn't represent a homeserver name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the parsing logic for server_name_str as requested. The field is now directly assigned as a string without any conversion attempts.


Ok(ServerVersionInfo { server_name, version })
}

/// Get a copy of the default request config.
///
/// The default request config is what's used when sending requests if no
Expand Down
3 changes: 2 additions & 1 deletion crates/matrix-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ pub mod widget;
pub use account::Account;
pub use authentication::{AuthApi, AuthSession, SessionTokens};
pub use client::{
sanitize_server_name, Client, ClientBuildError, ClientBuilder, LoopCtrl, SessionChange,
sanitize_server_name, Client, ClientBuildError, ClientBuilder, LoopCtrl, ServerVersionInfo,
SessionChange,
};
pub use error::{
Error, HttpError, HttpResult, NotificationSettingsError, RefreshTokenError, Result,
Expand Down