tuwunel_admin/user/
redact_event.rs1use ruma::{OwnedEventId, events::room::redaction::RoomRedactionEventContent};
2use tuwunel_core::{Err, Result};
3use tuwunel_matrix::{Event, PduEvent, pdu::PduBuilder};
4use tuwunel_service::Services;
5
6use crate::admin_command;
7
8#[admin_command]
9pub(super) async fn redact_event(&self, event_id: OwnedEventId) -> Result {
10 let Ok(event) = self
11 .services
12 .timeline
13 .get_non_outlier_pdu(&event_id)
14 .await
15 else {
16 return Err!("Event does not exist in our database.");
17 };
18
19 if event.is_redacted() {
20 return Err!("Event is already redacted.");
21 }
22
23 if !self
24 .services
25 .globals
26 .user_is_local(event.sender())
27 {
28 return Err!("This command only works on local users.");
29 }
30
31 let redaction_event_id = redact(self.services, &event).await?;
32
33 write!(self, "Successfully redacted event. Redaction event ID: {redaction_event_id}").await
34}
35
36pub(super) async fn redact(services: &Services, event: &PduEvent) -> Result<OwnedEventId> {
37 let reason = format!(
38 "The administrator(s) of {} has redacted this user's message.",
39 services.globals.server_name()
40 );
41
42 let state_lock = services.state.mutex.lock(event.room_id()).await;
43 let event_id = || event.event_id().to_owned();
44 let builder = PduBuilder {
45 redacts: Some(event_id()),
46 ..PduBuilder::timeline(&RoomRedactionEventContent {
47 redacts: Some(event_id()),
48 reason: Some(reason),
49 })
50 };
51
52 services
53 .timeline
54 .build_and_append_pdu(builder, event.sender(), event.room_id(), &state_lock)
55 .await
56}