Skip to main content

tuwunel_api/client/report/
report_user.rs

1use axum::extract::State;
2use ruma::api::client::reporting::report_user;
3use tuwunel_core::{Err, Result, info};
4
5use super::REASON_MAX_LEN;
6use crate::{ClientIp, Ruma};
7
8/// # `POST /_matrix/client/v3/users/{userId}/report`
9///
10/// Reports an inappropriate user to homeserver admins (MSC4260).
11#[tracing::instrument(skip_all, fields(%client), name = "report_user")]
12pub(crate) async fn report_user_route(
13	State(services): State<crate::State>,
14	ClientIp(client): ClientIp,
15	body: Ruma<report_user::v3::Request>,
16) -> Result<report_user::v3::Response> {
17	let sender_user = body.sender_user();
18	let target_user = &body.user_id;
19	let reason = &body.reason;
20
21	if reason.len() > REASON_MAX_LEN {
22		return Err!(Request(InvalidParam(
23			"Reason too long, should be {REASON_MAX_LEN} characters or fewer"
24		)));
25	}
26
27	info!("Received user report by {sender_user} for {target_user} with reason: \"{reason}\"");
28
29	// Succeed regardless of user existence to deter enumeration (MSC4277).
30	if services.users.is_active_local(target_user).await {
31		services
32			.admin
33			.send_report(&format!(
34				"@room User report received from {sender_user}\nReport Reason: \
35				 {reason}\n\nReported User ID: {target_user}",
36			))
37			.await;
38	}
39
40	Ok(report_user::v3::Response {})
41}