1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-07-27 17:28:36 +00:00
conduit/src/client_server/redact.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

2021-07-13 15:44:25 +02:00
use std::sync::Arc;
use crate::{database::DatabaseGuard, pdu::PduBuilder, Result, Ruma};
2020-07-30 18:14:47 +02:00
use ruma::{
api::client::r0::redact::redact_event,
events::{room::redaction::RoomRedactionEventContent, EventType},
2020-07-30 18:14:47 +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(
2021-07-14 07:07:08 +00:00
db: DatabaseGuard,
2020-09-08 17:32:03 +02:00
body: Ruma<redact_event::Request<'_>>,
) -> Result<redact_event::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
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,
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,
redacts: Some(body.event_id.into()),
2020-10-05 22:19:22 +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
db.flush()?;
let event_id = (*event_id).to_owned();
Ok(redact_event::Response { event_id })
2020-07-30 18:14:47 +02:00
}