grapevine/src/api/client_server/config.rs

80 lines
2.6 KiB
Rust

use ruma::api::client::{
config::{
get_global_account_data, get_room_account_data,
set_global_account_data, set_room_account_data,
},
error::ErrorKind,
};
use crate::{services, Ar, Error, Ra, Result};
/// # `PUT /_matrix/client/r0/user/{userId}/account_data/{type}`
///
/// Sets some account data for the sender user.
pub(crate) async fn set_global_account_data_route(
body: Ar<set_global_account_data::v3::Request>,
) -> Result<Ra<set_global_account_data::v3::Response>> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().account_data.update_global_any(
sender_user,
&body.event_type,
&body.data,
)?;
Ok(Ra(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(crate) async fn set_room_account_data_route(
body: Ar<set_room_account_data::v3::Request>,
) -> Result<Ra<set_room_account_data::v3::Response>> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().account_data.update_room_any(
&body.room_id,
sender_user,
&body.event_type,
&body.data,
)?;
Ok(Ra(set_room_account_data::v3::Response {}))
}
/// # `GET /_matrix/client/r0/user/{userId}/account_data/{type}`
///
/// Gets some account data for the sender user.
pub(crate) async fn get_global_account_data_route(
body: Ar<get_global_account_data::v3::Request>,
) -> Result<Ra<get_global_account_data::v3::Response>> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let account_data = services()
.account_data
.get_global_any(sender_user, &body.event_type)?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
Ok(Ra(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(crate) async fn get_room_account_data_route(
body: Ar<get_room_account_data::v3::Request>,
) -> Result<Ra<get_room_account_data::v3::Response>> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let account_data = services()
.account_data
.get_room_any(&body.room_id, sender_user, &body.event_type)?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
Ok(Ra(get_room_account_data::v3::Response {
account_data,
}))
}