summaryrefslogtreecommitdiff
path: root/src/api/client_server/redact.rs
blob: 20f7e91e7bef9a5fe342a546493f8c3126b4fd5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::sync::Arc;

use crate::{service::pdu::PduBuilder, services, Result, Ruma};
use ruma::{
    api::client::redact::redact_event,
    events::{room::redaction::RoomRedactionEventContent, TimelineEventType},
};

use serde_json::value::to_raw_value;

/// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}`
///
/// Tries to send a redaction event into the room.
///
/// - TODO: Handle txn id
pub async fn redact_event_route(
    body: Ruma<redact_event::v3::Request>,
) -> Result<redact_event::v3::Response> {
    let sender_user = body.sender_user.as_ref().expect("user is authenticated");
    let body = body.body;

    let mutex_state = Arc::clone(
        services()
            .globals
            .roomid_mutex_state
            .write()
            .unwrap()
            .entry(body.room_id.clone())
            .or_default(),
    );
    let state_lock = mutex_state.lock().await;

    let event_id = services().rooms.timeline.build_and_append_pdu(
        PduBuilder {
            event_type: TimelineEventType::RoomRedaction,
            content: to_raw_value(&RoomRedactionEventContent {
                reason: body.reason.clone(),
            })
            .expect("event is valid, we just created it"),
            unsigned: None,
            state_key: None,
            redacts: Some(body.event_id.into()),
        },
        sender_user,
        &body.room_id,
        &state_lock,
    )?;

    drop(state_lock);

    let event_id = (*event_id).to_owned();
    Ok(redact_event::v3::Response { event_id })
}