Skip to main content

tuwunel_api/client/admin/devices/
mod.rs

1//! Synapse admin API: device endpoints.
2
3mod create_device;
4mod delete_device;
5mod delete_devices;
6mod get_device;
7mod list_devices;
8mod update_device;
9
10use ruma::{
11	DeviceId, UserId,
12	api::client::device::{Device as ClientDevice, DisplayName, LastSeenIp},
13};
14use synapse_admin_api::devices::Device;
15use tuwunel_core::{Err, Result, err};
16
17pub(crate) use self::{
18	create_device::admin_create_device_route, delete_device::admin_delete_device_route,
19	delete_devices::admin_delete_devices_route, get_device::admin_get_device_route,
20	list_devices::admin_list_devices_route, update_device::admin_update_device_route,
21};
22
23/// Reject a non-local user with `400` and an absent one with `404`, mirroring
24/// Synapse's device servlets.
25pub(super) async fn require_local_user(services: &crate::State, user_id: &UserId) -> Result<()> {
26	if !services.globals.user_is_local(user_id) {
27		return Err!(Request(InvalidParam("Can only lookup local users")));
28	}
29
30	services
31		.users
32		.exists(user_id)
33		.await
34		.then_some(())
35		.ok_or_else(|| err!(Request(NotFound("Unknown user"))))
36}
37
38/// Map a stored device into the admin API wire shape. Tuwunel keeps no
39/// user-agent, so `last_seen_user_agent` is always absent; `dehydrated` is
40/// present on every device only when the owner has a dehydrated one.
41pub(super) fn device_response(
42	device: ClientDevice,
43	user_id: &UserId,
44	dehydrated: Option<&DeviceId>,
45) -> Device {
46	Device {
47		dehydrated: dehydrated.map(|id| device.device_id == id),
48		device_id: device.device_id,
49		display_name: device.display_name.map(DisplayName::into_string),
50		user_id: user_id.to_owned(),
51		last_seen_ip: device.last_seen_ip.map(LastSeenIp::into_string),
52		last_seen_user_agent: None,
53		last_seen_ts: device.last_seen_ts,
54	}
55}