Skip to main content

tuwunel_api/client/admin/rooms/
block.rs

1use axum::extract::State;
2use synapse_admin_api::rooms::block::{
3	get::{Request as GetRequest, Response as GetResponse},
4	set::{Request as SetRequest, Response as SetResponse},
5};
6use tuwunel_core::{Result, utils::BoolExt};
7
8use crate::{Ruma, client::admin::require_admin};
9
10/// # `GET /_synapse/admin/v1/rooms/{room_id}/block`
11///
12/// Reports whether a room is blocked and, when it is, the admin that blocked
13/// it. The blocker is omitted for rooms blocked before the mxid was recorded.
14pub(crate) async fn admin_get_room_block_route(
15	State(services): State<crate::State>,
16	body: Ruma<GetRequest>,
17) -> Result<GetResponse> {
18	require_admin(&services, body.sender_user()).await?;
19
20	let block = services.metadata.is_banned(&body.room_id).await;
21
22	let user_id = block
23		.then_async(|| {
24			services
25				.metadata
26				.banned_room_blocker(&body.room_id)
27		})
28		.await
29		.flatten();
30
31	Ok(GetResponse { block, user_id })
32}
33
34/// # `PUT /_synapse/admin/v1/rooms/{room_id}/block`
35///
36/// Blocks or unblocks a room, recording the requesting admin as the blocker.
37/// Pre-emptively blocking a room the server does not know is allowed.
38pub(crate) async fn admin_set_room_block_route(
39	State(services): State<crate::State>,
40	body: Ruma<SetRequest>,
41) -> Result<SetResponse> {
42	let sender_user = body.sender_user();
43
44	require_admin(&services, sender_user).await?;
45
46	match body.block {
47		| true => services
48			.metadata
49			.block_room(&body.room_id, sender_user),
50		| false => services.metadata.unban_room(&body.room_id),
51	}
52
53	Ok(SetResponse { block: body.block })
54}