summaryrefslogtreecommitdiff
path: root/src/api/client_server/config.rs
blob: 12f9aeaaacbf0f153968708169c10b2a6ca5140d (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::{services, Error, Result, Ruma};
use ruma::{
    api::client::{
        config::{
            get_global_account_data, get_room_account_data, set_global_account_data,
            set_room_account_data,
        },
        error::ErrorKind,
    },
    events::{AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent},
    serde::Raw,
};
use serde::Deserialize;
use serde_json::{json, value::RawValue as RawJsonValue};

/// # `PUT /_matrix/client/r0/user/{userId}/account_data/{type}`
///
/// Sets some account data for the sender user.
pub async fn set_global_account_data_route(
    body: Ruma<set_global_account_data::v3::Request>,
) -> Result<set_global_account_data::v3::Response> {
    let sender_user = body.sender_user.as_ref().expect("user is authenticated");

    let data: serde_json::Value = serde_json::from_str(body.data.json().get())
        .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;

    let event_type = body.event_type.to_string();

    services().account_data.update(
        None,
        sender_user,
        event_type.clone().into(),
        &json!({
            "type": event_type,
            "content": data,
        }),
    )?;

    Ok(set_global_account_data::v3::Response {})
}

/// # `PUT /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}`
///
/// Sets some room account data for the sender user.
pub async fn set_room_account_data_route(
    body: Ruma<set_room_account_data::v3::Request>,
) -> Result<set_room_account_data::v3::Response> {
    let sender_user = body.sender_user.as_ref().expect("user is authenticated");

    let data: serde_json::Value = serde_json::from_str(body.data.json().get())
        .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;

    let event_type = body.event_type.to_string();

    services().account_data.update(
        Some(&body.room_id),
        sender_user,
        event_type.clone().into(),
        &json!({
            "type": event_type,
            "content": data,
        }),
    )?;

    Ok(set_room_account_data::v3::Response {})
}

/// # `GET /_matrix/client/r0/user/{userId}/account_data/{type}`
///
/// Gets some account data for the sender user.
pub async fn get_global_account_data_route(
    body: Ruma<get_global_account_data::v3::Request>,
) -> Result<get_global_account_data::v3::Response> {
    let sender_user = body.sender_user.as_ref().expect("user is authenticated");

    let event: Box<RawJsonValue> = services()
        .account_data
        .get(None, sender_user, body.event_type.clone().into())?
        .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;

    let account_data = serde_json::from_str::<ExtractGlobalEventContent>(event.get())
        .map_err(|_| Error::bad_database("Invalid account data event in db."))?
        .content;

    Ok(get_global_account_data::v3::Response { account_data })
}

/// # `GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}`
///
/// Gets some room account data for the sender user.
pub async fn get_room_account_data_route(
    body: Ruma<get_room_account_data::v3::Request>,
) -> Result<get_room_account_data::v3::Response> {
    let sender_user = body.sender_user.as_ref().expect("user is authenticated");

    let event: Box<RawJsonValue> = services()
        .account_data
        .get(
            Some(&body.room_id),
            sender_user,
            body.event_type.clone().into(),
        )?
        .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;

    let account_data = serde_json::from_str::<ExtractRoomEventContent>(event.get())
        .map_err(|_| Error::bad_database("Invalid account data event in db."))?
        .content;

    Ok(get_room_account_data::v3::Response { account_data })
}

#[derive(Deserialize)]
struct ExtractRoomEventContent {
    content: Raw<AnyRoomAccountDataEventContent>,
}

#[derive(Deserialize)]
struct ExtractGlobalEventContent {
    content: Raw<AnyGlobalAccountDataEventContent>,
}