Skip to main content

tuwunel_api/client/
redact.rs

1use axum::extract::State;
2use ruma::{
3	api::client::redact::redact_event, events::room::redaction::RoomRedactionEventContent,
4};
5use tuwunel_core::{Err, Result, matrix::pdu::PduBuilder, warn};
6
7use crate::{Ruma, client::utils::is_self_redaction};
8
9/// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}`
10///
11/// Tries to send a redaction event into the room.
12///
13/// - TODO: Handle txn id
14pub(crate) async fn redact_event_route(
15	State(services): State<crate::State>,
16	body: Ruma<redact_event::v3::Request>,
17) -> Result<redact_event::v3::Response> {
18	let sender_user = body.sender_user();
19
20	if services.config.disable_local_redactions
21		&& !services.admin.user_is_admin(sender_user).await
22	{
23		warn!(
24			%sender_user,
25			event_id = %body.event_id,
26			"Local redactions are disabled, non-admin user attempted to redact an event"
27		);
28		return Err!(Request(Forbidden("Redactions are disabled on this server.")));
29	}
30
31	if services.users.is_suspended(sender_user).await
32		&& !is_self_redaction(&services, sender_user, &body.event_id).await
33	{
34		return Err!(Request(UserSuspended("Account is suspended.")));
35	}
36
37	let state_lock = services.state.mutex.lock(&body.room_id).await;
38
39	let event_id = services
40		.timeline
41		.build_and_append_pdu(
42			PduBuilder {
43				redacts: Some(body.event_id.clone()),
44				..PduBuilder::timeline(&RoomRedactionEventContent {
45					redacts: Some(body.event_id.clone()),
46					reason: body.reason.clone(),
47				})
48			},
49			sender_user,
50			&body.room_id,
51			&state_lock,
52		)
53		.await?;
54
55	drop(state_lock);
56
57	Ok(redact_event::v3::Response { event_id })
58}