Files
continuwuity/src/api/server/user.rs
T

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

135 lines
3.3 KiB
Rust
Raw Normal View History

use std::time::Duration;
2024-07-16 08:05:25 +00:00
use axum::extract::State;
2024-12-14 21:58:01 -05:00
use conduwuit::{Error, Result};
2024-08-08 17:18:30 +00:00
use futures::{FutureExt, StreamExt, TryFutureExt};
2024-06-05 04:32:58 +00:00
use ruma::api::{
client::error::ErrorKind,
federation::{
device::get_devices::{self, v1::UserDevice},
keys::{claim_keys, get_keys},
},
};
use crate::{
2024-07-22 07:43:51 +00:00
Ruma,
client::{claim_keys_helper, get_keys_helper},
2024-06-05 04:32:58 +00:00
};
/// # `GET /_matrix/federation/v1/user/devices/{userId}`
///
/// Gets information on all devices of the user.
2024-07-16 08:05:25 +00:00
pub(crate) async fn get_devices_route(
State(services): State<crate::State>,
body: Ruma<get_devices::v1::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<get_devices::v1::Response> {
2024-07-22 07:43:51 +00:00
if !services.globals.user_is_local(&body.user_id) {
2024-06-05 04:32:58 +00:00
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Tried to access user from other server.",
));
}
2024-08-08 17:18:30 +00:00
let user_id = &body.user_id;
2024-06-05 04:32:58 +00:00
Ok(get_devices::v1::Response {
2024-08-08 17:18:30 +00:00
user_id: user_id.clone(),
2024-07-16 08:05:25 +00:00
stream_id: services
2024-06-05 04:32:58 +00:00
.users
2024-08-08 17:18:30 +00:00
.get_devicelist_version(user_id)
.await
2024-06-05 04:32:58 +00:00
.unwrap_or(0)
2024-08-08 17:18:30 +00:00
.try_into()?,
2024-07-16 08:05:25 +00:00
devices: services
2024-06-05 04:32:58 +00:00
.users
2024-08-08 17:18:30 +00:00
.all_devices_metadata(user_id)
.filter_map(|metadata| async move {
let device_id = metadata.device_id.clone();
let device_id_clone = device_id.clone();
let device_id_string = device_id.as_str().to_owned();
2024-07-16 08:05:25 +00:00
let device_display_name = if services.globals.allow_device_name_federation() {
2024-08-08 17:18:30 +00:00
metadata.display_name.clone()
2024-06-05 04:32:58 +00:00
} else {
Some(device_id_string)
};
2024-08-08 17:18:30 +00:00
services
.users
.get_device_keys(user_id, &device_id_clone)
.map_ok(|keys| UserDevice { device_id, keys, device_display_name })
2024-08-08 17:18:30 +00:00
.map(Result::ok)
.await
2024-06-05 04:32:58 +00:00
})
2024-08-08 17:18:30 +00:00
.collect()
.await,
2024-07-16 08:05:25 +00:00
master_key: services
2024-06-05 04:32:58 +00:00
.users
.get_master_key(None, &body.user_id, &|u| u.server_name() == body.origin())
2024-08-08 17:18:30 +00:00
.await
.ok(),
2024-07-16 08:05:25 +00:00
self_signing_key: services
2024-06-05 04:32:58 +00:00
.users
.get_self_signing_key(None, &body.user_id, &|u| u.server_name() == body.origin())
2024-08-08 17:18:30 +00:00
.await
.ok(),
2024-06-05 04:32:58 +00:00
})
}
/// # `POST /_matrix/federation/v1/user/keys/query`
///
/// Gets devices and identity keys for the given users.
2024-07-16 08:05:25 +00:00
pub(crate) async fn get_keys_route(
State(services): State<crate::State>,
body: Ruma<get_keys::v1::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<get_keys::v1::Response> {
2024-07-22 07:43:51 +00:00
if body
.device_keys
.iter()
.any(|(u, _)| !services.globals.user_is_local(u))
{
2024-06-05 04:32:58 +00:00
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"User does not belong to this server.",
));
}
let result = get_keys_helper(
2024-07-27 07:17:07 +00:00
&services,
2024-06-05 04:32:58 +00:00
None,
&body.device_keys,
|u| Some(u.server_name()) == body.origin.as_deref(),
2024-07-16 08:05:25 +00:00
services.globals.allow_device_name_federation(),
Duration::from_secs(0),
2024-06-05 04:32:58 +00:00
)
.await?;
Ok(get_keys::v1::Response {
device_keys: result.device_keys,
master_keys: result.master_keys,
self_signing_keys: result.self_signing_keys,
})
}
/// # `POST /_matrix/federation/v1/user/keys/claim`
///
/// Claims one-time keys.
2024-07-16 08:05:25 +00:00
pub(crate) async fn claim_keys_route(
State(services): State<crate::State>,
body: Ruma<claim_keys::v1::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<claim_keys::v1::Response> {
2024-07-22 07:43:51 +00:00
if body
.one_time_keys
.iter()
.any(|(u, _)| !services.globals.user_is_local(u))
{
2024-06-05 04:32:58 +00:00
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Tried to access user from other server.",
));
}
let result =
claim_keys_helper(&services, &body.one_time_keys, Duration::from_secs(0)).await?;
2024-06-05 04:32:58 +00:00
Ok(claim_keys::v1::Response { one_time_keys: result.one_time_keys })
2024-06-05 04:32:58 +00:00
}