Files
continuwuity/src/api/client/unversioned.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
3.2 KiB
Rust
Raw Normal View History

use axum::{Json, extract::State, response::IntoResponse};
2026-05-11 23:13:52 +01:00
use conduwuit::{
2026-09-24 10:44:15 +02:00
Err, Result,
2026-05-11 23:13:52 +01:00
matrix::versions::{unstable_features, versions},
metrics::PROMETHEUS_CONTENT_TYPE,
2026-09-24 10:44:15 +02:00
utils::string::constant_time_eq,
2026-05-11 23:13:52 +01:00
};
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
2026-09-24 10:44:15 +02:00
use http::{HeaderMap, header};
use ruma::{api::client::discovery::get_supported_versions, assign};
2022-04-06 21:31:29 +02:00
use crate::Ruma;
2020-07-30 18:14:47 +02:00
2020-07-31 14:40:28 +02:00
/// # `GET /_matrix/client/versions`
///
/// Get the versions of the specification and unstable features supported by
/// this server.
///
/// - Versions take the form MAJOR.MINOR.PATCH
/// - Only the latest PATCH release will be reported for each MAJOR.MINOR value
2021-08-31 19:14:37 +02:00
/// - Unstable features are namespaced and may include version information in
/// their name
2020-07-31 14:40:28 +02:00
///
/// Note: Unstable features are used while developing new features. Clients
/// should avoid using unstable features in their stable releases
2024-04-22 23:48:57 -04:00
pub(crate) async fn get_supported_versions_route(
2026-08-02 10:14:17 +02:00
State(services): State<crate::State>,
2022-12-14 13:09:10 +01:00
_body: Ruma<get_supported_versions::Request>,
2022-01-22 16:58:32 +01:00
) -> Result<get_supported_versions::Response> {
2026-08-02 10:14:17 +02:00
let mut unstable_features = unstable_features();
if services.config.allow_sticky_events {
// sticky events (https://github.com/matrix-org/matrix-spec-proposals/pull/4354)
unstable_features.insert("org.matrix.msc4354".to_owned(), true);
}
Ok(assign!(get_supported_versions::Response::new(versions()), {
unstable_features
}))
2020-07-30 18:14:47 +02:00
}
2026-07-07 10:21:56 -04:00
/// # `GET /_continuwuity/server_version`
2024-03-27 10:17:11 -04:00
///
/// Conduwuit-specific API to get the server version, results akin to
/// `/_matrix/federation/v1/version`
2026-07-07 10:21:56 -04:00
pub(crate) async fn continuwuity_server_version() -> Result<impl IntoResponse> {
2024-03-27 10:17:11 -04:00
Ok(Json(serde_json::json!({
"name": conduwuit::BRANDING,
"version": conduwuit::version(),
})))
}
2026-07-07 10:21:56 -04:00
/// # `GET /_continuwuity/local_user_count`
///
/// conduwuit-specific API to return the amount of users registered on this
/// homeserver. Endpoint is disabled if federation is disabled for privacy. This
///
/// only includes active users (not deactivated, etc)
2026-07-07 10:21:56 -04:00
pub(crate) async fn continuwuity_local_user_count(
State(services): State<crate::State>,
) -> Result<impl IntoResponse> {
let user_count = services
.users
.stream_local_users()
.filter_map(async |user_id| {
if !services.users.status(&user_id).await.is_active()
|| services.appservice.is_exclusive_user_id(&user_id).await
{
None
} else {
Some(user_id)
}
})
.count()
.await;
Ok(Json(serde_json::json!({
"count": user_count
})))
}
2026-09-24 10:44:15 +02:00
/// # `GET /_continuwuity/metrics`
///
/// Prometheus metrics in the text exposition format. Only routed when
2026-09-24 10:44:15 +02:00
/// `allow_prometheus_metrics` is enabled.
pub(crate) async fn continuwuity_metrics(
State(services): State<crate::State>,
headers: HeaderMap,
) -> Result<impl IntoResponse> {
if let Some(token) = &services.config.prometheus_metrics_token {
let authorized = headers
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.split_once(' '))
.is_some_and(|(scheme, provided)| {
scheme.eq_ignore_ascii_case("bearer") && constant_time_eq(provided, token)
});
if !authorized {
return Err!(Request(Forbidden("Invalid metrics token")));
}
}
let body = services.server.metrics.render()?;
2026-09-24 10:44:15 +02:00
Ok(([(header::CONTENT_TYPE, PROMETHEUS_CONTENT_TYPE)], body))
2026-09-24 10:44:15 +02:00
}