Skip to main content

tuwunel_api/client/backup/
version_id.rs

1use axum::extract::State;
2use ruma::api::client::backup::{delete_backup_version, get_backup_info, update_backup_version};
3use tuwunel_core::{Result, err};
4
5use super::{get_count_etag, validate_algorithm_shape};
6use crate::Ruma;
7
8/// # `PUT /_matrix/client/r0/room_keys/version/{version}`
9///
10/// Update information about an existing backup. Only `auth_data` can be
11/// modified.
12pub(crate) async fn update_backup_version_route(
13	State(services): State<crate::State>,
14	body: Ruma<update_backup_version::v3::Request>,
15) -> Result<update_backup_version::v3::Response> {
16	validate_algorithm_shape(&body.algorithm)
17		.map_err(|e| err!(Request(BadJson("Invalid backup metadata: {e}"))))?;
18
19	services
20		.key_backups
21		.update_backup(body.sender_user(), &body.version, &body.algorithm)
22		.await?;
23
24	Ok(update_backup_version::v3::Response {})
25}
26
27/// # `GET /_matrix/client/v3/room_keys/version/{version}`
28///
29/// Get information about an existing backup.
30pub(crate) async fn get_backup_info_route(
31	State(services): State<crate::State>,
32	body: Ruma<get_backup_info::v3::Request>,
33) -> Result<get_backup_info::v3::Response> {
34	let algorithm = services
35		.key_backups
36		.get_backup(body.sender_user(), &body.version)
37		.await
38		.map_err(|_| {
39			err!(Request(NotFound("Key backup does not exist at version {:?}", body.version)))
40		})?;
41
42	validate_algorithm_shape(&algorithm).map_err(|e| {
43		err!(Request(NotFound(
44			"Key backup does not exist at version {:?}: {e}",
45			body.version
46		)))
47	})?;
48
49	let (count, etag) = get_count_etag(&services, body.sender_user(), &body.version).await?;
50
51	Ok(get_backup_info::v3::Response {
52		algorithm,
53		count,
54		etag,
55		version: body.version.clone(),
56	})
57}
58
59/// # `DELETE /_matrix/client/r0/room_keys/version/{version}`
60///
61/// Delete an existing key backup.
62///
63/// - Deletes both information about the backup, as well as all key data related
64///   to the backup
65pub(crate) async fn delete_backup_version_route(
66	State(services): State<crate::State>,
67	body: Ruma<delete_backup_version::v3::Request>,
68) -> Result<delete_backup_version::v3::Response> {
69	services
70		.key_backups
71		.delete_backup(body.sender_user(), &body.version)
72		.await;
73
74	Ok(delete_backup_version::v3::Response {})
75}