2021-07-13 15:44:25 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-10-05 20:34:31 +02:00
|
|
|
use crate::{service::pdu::PduBuilder, services, Result, Ruma};
|
2020-07-30 18:14:47 +02:00
|
|
|
use ruma::{
|
2022-02-18 15:33:14 +01:00
|
|
|
api::client::redact::redact_event,
|
2023-02-26 16:29:06 +01:00
|
|
|
events::{room::redaction::RoomRedactionEventContent, TimelineEventType},
|
2020-07-30 18:14:47 +02:00
|
|
|
};
|
|
|
|
|
2021-10-13 10:16:45 +02:00
|
|
|
use serde_json::value::to_raw_value;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}`
|
|
|
|
///
|
|
|
|
/// Tries to send a redaction event into the room.
|
|
|
|
///
|
|
|
|
/// - TODO: Handle txn id
|
2020-09-14 20:23:19 +02:00
|
|
|
pub async fn redact_event_route(
|
2022-12-14 13:09:10 +01:00
|
|
|
body: Ruma<redact_event::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<redact_event::v3::Response> {
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2021-11-27 17:44:52 +01:00
|
|
|
let body = body.body;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-08-03 11:10:58 +02:00
|
|
|
let mutex_state = Arc::clone(
|
2022-10-05 20:34:31 +02:00
|
|
|
services()
|
|
|
|
.globals
|
2021-08-03 11:10:58 +02:00
|
|
|
.roomid_mutex_state
|
2021-07-13 15:44:25 +02:00
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(body.room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
2021-08-03 11:10:58 +02:00
|
|
|
let state_lock = mutex_state.lock().await;
|
2021-07-13 15:44:25 +02:00
|
|
|
|
2022-10-05 09:34:25 +02:00
|
|
|
let event_id = services().rooms.timeline.build_and_append_pdu(
|
2020-10-05 22:19:22 +02:00
|
|
|
PduBuilder {
|
2023-02-26 16:29:06 +01:00
|
|
|
event_type: TimelineEventType::RoomRedaction,
|
2021-10-13 10:16:45 +02:00
|
|
|
content: to_raw_value(&RoomRedactionEventContent {
|
2020-10-05 22:19:22 +02:00
|
|
|
reason: body.reason.clone(),
|
|
|
|
})
|
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: None,
|
2021-11-27 17:44:52 +01:00
|
|
|
redacts: Some(body.event_id.into()),
|
2020-10-05 22:19:22 +02:00
|
|
|
},
|
2021-09-13 19:45:56 +02:00
|
|
|
sender_user,
|
2020-10-05 22:19:22 +02:00
|
|
|
&body.room_id,
|
2021-08-03 11:10:58 +02:00
|
|
|
&state_lock,
|
2020-10-05 22:19:22 +02:00
|
|
|
)?;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-08-03 11:10:58 +02:00
|
|
|
drop(state_lock);
|
2021-07-13 15:44:25 +02:00
|
|
|
|
2021-11-27 17:44:52 +01:00
|
|
|
let event_id = (*event_id).to_owned();
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(redact_event::v3::Response { event_id })
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|