refactor: Fix errors in api/client/well_known.rs

This commit is contained in:
Ginger
2026-04-12 11:46:13 -04:00
parent 2bfd6dd7fb
commit f327afffcc
4 changed files with 50 additions and 64 deletions

View File

@@ -375,6 +375,7 @@ features = [
"unstable-msc4121",
"unstable-msc4125",
"unstable-msc4186",
"unstable-msc4195",
"unstable-msc4203", # sending to-device events to appservices
"unstable-msc4310",
"unstable-msc4380",

View File

@@ -1,11 +1,11 @@
use axum::{Json, extract::State, response::IntoResponse};
use conduwuit::{Error, Result};
use ruma::api::client::{
discovery::{
discover_homeserver::{self, HomeserverInfo},
discover_support::{self, Contact},
use axum::extract::State;
use conduwuit::{Err, Result};
use ruma::{
api::client::discovery::{
discover_homeserver::{self, HomeserverInfo, RtcFocusInfo},
discover_support::{self, Contact, ContactRole},
},
error::ErrorKind,
assign,
};
use crate::Ruma;
@@ -19,20 +19,23 @@ pub(crate) async fn well_known_client(
) -> Result<discover_homeserver::Response> {
let client_url = match services.config.well_known.client.as_ref() {
| Some(url) => url.to_string(),
| None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
| None =>
return Err!(Request(NotFound(
"This server is not configured to serve well-known client information."
))),
};
Ok(discover_homeserver::Response {
homeserver: HomeserverInfo { base_url: client_url },
Ok(assign!(discover_homeserver::Response::new(HomeserverInfo::new(client_url)), {
identity_server: None,
sliding_sync_proxy: None,
tile_server: None,
rtc_foci: services
.config
.matrix_rtc
.effective_foci(&services.config.well_known.rtc_focus_server_urls)
.to_vec(),
})
.into_iter()
.map(|focus| RtcFocusInfo::new(focus.transport_type(), focus.data().into_owned()).unwrap())
.collect()
}))
}
/// # `GET /_matrix/client/v1/rtc/transports`
@@ -42,14 +45,13 @@ pub(crate) async fn well_known_client(
/// homeserver, implementing MSC4143.
pub(crate) async fn get_rtc_transports(
State(services): State<crate::State>,
_body: Ruma<ruma::api::client::discovery::get_rtc_transports::Request>,
) -> Result<ruma::api::client::discovery::get_rtc_transports::Response> {
Ok(ruma::api::client::discovery::get_rtc_transports::Response::new(
_body: Ruma<ruma::api::client::rtc::transports::v1::Request>,
) -> Result<ruma::api::client::rtc::transports::v1::Response> {
Ok(ruma::api::client::rtc::transports::v1::Response::new(
services
.config
.matrix_rtc
.effective_foci(&services.config.well_known.rtc_focus_server_urls)
.to_vec(),
.effective_foci(&services.config.well_known.rtc_focus_server_urls),
))
}
@@ -76,21 +78,25 @@ pub(crate) async fn well_known_support(
// TODO: support defining multiple contacts in the config
let mut contacts: Vec<Contact> = vec![];
let role_value = services
let role = services
.config
.well_known
.support_role
.clone()
.unwrap_or_else(|| "m.role.admin".to_owned().into());
.unwrap_or(ContactRole::Admin);
// Add configured contact if at least one contact method is specified
if email_address.is_some() || matrix_id.is_some() {
contacts.push(Contact {
role: role_value.clone(),
email_address: email_address.clone(),
matrix_id: matrix_id.clone(),
pgp_key: pgp_key.clone(),
});
let configured_contact = match (matrix_id, email_address) {
| (Some(matrix_id), email_address) =>
Some(assign!(Contact::with_matrix_id(role, matrix_id), { email_address })),
| (None, Some(email_address)) => Some(Contact::with_email_address(role, email_address)),
| (None, None) => None,
};
if let Some(mut configured_contact) = configured_contact {
configured_contact.pgp_key = pgp_key;
contacts.push(configured_contact);
}
// Try to add admin users as contacts if no contacts are configured
@@ -102,40 +108,14 @@ pub(crate) async fn well_known_support(
continue;
}
contacts.push(Contact {
role: role_value.clone(),
email_address: None,
matrix_id: Some(user_id.to_owned()),
pgp_key: None,
});
contacts.push(Contact::with_matrix_id(ContactRole::Admin, user_id.to_owned()));
}
}
if contacts.is_empty() && support_page.is_none() {
// No admin room, no configured contacts, and no support page
return Err(Error::BadRequest(ErrorKind::NotFound, "Not found."));
return Err!(Request(NotFound("No support information is available.")));
}
Ok(discover_support::Response { contacts, support_page })
}
/// # `GET /client/server.json`
///
/// Endpoint provided by sliding sync proxy used by some clients such as Element
/// Web as a non-standard health check.
pub(crate) async fn syncv3_client_server_json(
State(services): State<crate::State>,
) -> Result<impl IntoResponse> {
let server_url = match services.config.well_known.client.as_ref() {
| Some(url) => url.to_string(),
| None => match services.config.well_known.server.as_ref() {
| Some(url) => url.to_string(),
| None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
},
};
Ok(Json(serde_json::json!({
"server": server_url,
"version": conduwuit::version(),
})))
Ok(assign!(discover_support::Response::with_contacts(contacts), { support_page }))
}

View File

@@ -188,7 +188,6 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::room_initial_sync_route)
.route("/_conduwuit/server_version", get(client::conduwuit_server_version))
.route("/_continuwuity/server_version", get(client::conduwuit_server_version))
.route("/client/server.json", get(client::syncv3_client_server_json))
.ruma_route(&admin::rooms::ban::ban_room)
.ruma_route(&admin::rooms::list::list_rooms);

View File

@@ -20,7 +20,10 @@
use regex::RegexSet;
use ruma::{
OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomVersionId,
api::client::discovery::{discover_homeserver::RtcFocusInfo, discover_support::ContactRole},
api::client::{
discovery::{discover_homeserver::RtcFocusInfo, discover_support::ContactRole},
rtc::transports::v1::RtcTransport,
},
};
use serde::{Deserialize, Serialize, de::IgnoredAny};
use url::Url;
@@ -2281,21 +2284,24 @@ pub struct MatrixRtcConfig {
///
/// default: []
#[serde(default)]
pub foci: Vec<RtcFocusInfo>,
pub foci: Vec<RtcTransport>,
}
impl MatrixRtcConfig {
/// Returns the effective foci, falling back to the deprecated
/// `rtc_focus_server_urls` if the new config is empty.
#[must_use]
pub fn effective_foci<'a>(
&'a self,
deprecated_foci: &'a [RtcFocusInfo],
) -> &'a [RtcFocusInfo] {
pub fn effective_foci(&self, deprecated_foci: &[RtcFocusInfo]) -> Vec<RtcTransport> {
if !self.foci.is_empty() {
&self.foci
self.foci.clone()
} else {
deprecated_foci
.iter()
.map(|focus| {
RtcTransport::new(focus.focus_type().to_owned(), focus.data().into_owned())
.unwrap()
})
.collect()
}
}
}