Skip to main content

tuwunel_api/client/admin/media/
list_user_media.rs

1use axum::extract::State;
2use ruma::UInt;
3use synapse_admin_api::media::list_user_media::v1::{self as list_user_media, UserMedia};
4use tuwunel_core::{Err, Result};
5use tuwunel_service::media::UserMediaEntry;
6
7use super::{select_page, usize_from};
8use crate::{Ruma, client::admin::require_admin};
9
10/// # `GET /_synapse/admin/v1/users/{user_id}/media`
11pub(crate) async fn admin_list_user_media_route(
12	State(services): State<crate::State>,
13	body: Ruma<list_user_media::Request>,
14) -> Result<list_user_media::Response> {
15	require_admin(&services, body.sender_user()).await?;
16
17	if !services
18		.globals
19		.server_is_ours(body.user_id.server_name())
20	{
21		return Err!(Request(InvalidParam("Can only look up local users")));
22	}
23
24	if !services.users.exists(&body.user_id).await {
25		return Err!(Request(NotFound("User not found")));
26	}
27
28	let entries = services.media.user_media(&body.user_id).await?;
29
30	let total = UInt::try_from(entries.len()).unwrap_or(UInt::MAX);
31
32	let from = body.from.map(usize_from).unwrap_or(0);
33	let limit = body.limit.map(usize_from).unwrap_or(100);
34
35	let page = select_page(entries, body.order_by.as_ref(), body.dir, from, limit);
36
37	let next_token = (from.saturating_add(page.len()) < usize_from(total))
38		.then(|| UInt::try_from(from.saturating_add(page.len())).unwrap_or(UInt::MAX));
39
40	let media = page.into_iter().map(into_user_media).collect();
41
42	Ok(list_user_media::Response { media, next_token, total })
43}
44
45fn into_user_media(entry: UserMediaEntry) -> UserMedia {
46	UserMedia {
47		media_id: entry
48			.mxc
49			.media_id()
50			.unwrap_or_default()
51			.to_owned(),
52		media_type: entry.media_type.unwrap_or_default(),
53		media_length: entry
54			.media_length
55			.and_then(|len| UInt::try_from(len).ok()),
56		upload_name: entry.upload_name.unwrap_or_default(),
57		created_ts: UInt::try_from(entry.created_ts).unwrap_or(UInt::MAX),
58		url_cache: None,
59		last_access_ts: UInt::from(0_u32),
60		quarantined_by: None,
61		safe_from_quarantine: false,
62		user_id: entry.user_id,
63		authenticated: None,
64		sha256: None,
65	}
66}