2021-07-13 15:44:25 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
use crate::{database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Ruma};
|
2020-07-30 18:14:47 +02:00
|
|
|
use ruma::{
|
|
|
|
api::client::r0::redact::redact_event,
|
2021-10-13 10:16:45 +02:00
|
|
|
events::{room::redaction::RoomRedactionEventContent, EventType},
|
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
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 20:23:19 +02:00
|
|
|
pub async fn redact_event_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 17:32:03 +02:00
|
|
|
body: Ruma<redact_event::Request<'_>>,
|
2020-07-30 18:14:47 +02:00
|
|
|
) -> ConduitResult<redact_event::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(
|
2021-07-13 15:44:25 +02:00
|
|
|
db.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
|
|
|
|
2020-10-05 22:19:22 +02:00
|
|
|
let event_id = db.rooms.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::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-01-15 11:05:57 -05:00
|
|
|
&db,
|
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-08-02 10:13:34 +02:00
|
|
|
db.flush()?;
|
2020-10-21 21:28:02 +02:00
|
|
|
|
2021-11-27 17:44:52 +01:00
|
|
|
let event_id = (*event_id).to_owned();
|
2020-07-30 18:14:47 +02:00
|
|
|
Ok(redact_event::Response { event_id }.into())
|
|
|
|
}
|