Files
continuwuity/src/api/client/account_data.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

161 lines
4.5 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
use conduwuit::{Err, Result, err};
use conduwuit_service::Services;
2020-07-30 18:14:47 +02:00
use ruma::{
RoomId, UserId,
api::client::config::{
get_global_account_data, get_room_account_data, set_global_account_data,
set_room_account_data,
},
events::{
AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent,
GlobalAccountDataEventType, RoomAccountDataEventType,
2020-07-30 18:14:47 +02:00
},
serde::Raw,
2020-07-30 18:14:47 +02:00
};
use serde::Deserialize;
use serde_json::{json, value::RawValue as RawJsonValue};
2020-07-30 18:14:47 +02:00
use crate::Ruma;
2024-03-05 19:48:54 -05:00
2021-08-31 19:14:37 +02:00
/// # `PUT /_matrix/client/r0/user/{userId}/account_data/{type}`
///
/// Sets some account data for the sender user.
2024-04-22 23:48:57 -04:00
pub(crate) async fn set_global_account_data_route(
State(services): State<crate::State>,
body: Ruma<set_global_account_data::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<set_global_account_data::v3::Response> {
let sender_user = body.sender_user();
if sender_user != body.user_id && body.appservice_info.is_none() {
return Err!(Request(Forbidden("You cannot set account data for other users.")));
}
set_account_data(
&services,
None,
&body.user_id,
&body.event_type.to_string(),
body.data.json(),
)
.await?;
2020-07-30 18:14:47 +02:00
2022-02-18 15:33:14 +01:00
Ok(set_global_account_data::v3::Response {})
2020-07-30 18:14:47 +02:00
}
2021-08-31 19:14:37 +02:00
/// # `PUT /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}`
///
/// Sets some room account data for the sender user.
2024-04-22 23:48:57 -04:00
pub(crate) async fn set_room_account_data_route(
State(services): State<crate::State>,
body: Ruma<set_room_account_data::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<set_room_account_data::v3::Response> {
let sender_user = body.sender_user();
if sender_user != body.user_id && body.appservice_info.is_none() {
return Err!(Request(Forbidden("You cannot set account data for other users.")));
}
set_account_data(
2024-07-27 07:17:07 +00:00
&services,
2021-03-24 08:48:28 +01:00
Some(&body.room_id),
&body.user_id,
&body.event_type.to_string(),
body.data.json(),
2024-08-08 17:18:30 +00:00
)
.await?;
2021-03-24 08:48:28 +01:00
2022-02-18 15:33:14 +01:00
Ok(set_room_account_data::v3::Response {})
2021-03-24 08:48:28 +01:00
}
2021-08-31 19:14:37 +02:00
/// # `GET /_matrix/client/r0/user/{userId}/account_data/{type}`
///
/// Gets some account data for the sender user.
2024-04-22 23:48:57 -04:00
pub(crate) async fn get_global_account_data_route(
State(services): State<crate::State>,
body: Ruma<get_global_account_data::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<get_global_account_data::v3::Response> {
let sender_user = body.sender_user();
if sender_user != body.user_id && body.appservice_info.is_none() {
return Err!(Request(Forbidden("You cannot get account data of other users.")));
}
2020-07-30 18:14:47 +02:00
let account_data: ExtractGlobalEventContent = services
2020-07-30 18:14:47 +02:00
.account_data
.get_global(&body.user_id, body.event_type.clone())
2024-08-08 17:18:30 +00:00
.await
.map_err(|_| err!(Request(NotFound("Data not found."))))?;
Ok(get_global_account_data::v3::Response { account_data: account_data.content })
2020-07-30 18:14:47 +02:00
}
2021-03-24 08:48:28 +01:00
2021-08-31 19:14:37 +02:00
/// # `GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}`
///
/// Gets some room account data for the sender user.
2024-04-22 23:48:57 -04:00
pub(crate) async fn get_room_account_data_route(
State(services): State<crate::State>,
body: Ruma<get_room_account_data::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<get_room_account_data::v3::Response> {
let sender_user = body.sender_user();
if sender_user != body.user_id && body.appservice_info.is_none() {
return Err!(Request(Forbidden("You cannot get account data of other users.")));
}
2021-03-24 08:48:28 +01:00
let account_data: ExtractRoomEventContent = services
2021-03-24 08:48:28 +01:00
.account_data
.get_room(&body.room_id, &body.user_id, body.event_type.clone())
2024-08-08 17:18:30 +00:00
.await
.map_err(|_| err!(Request(NotFound("Data not found."))))?;
2021-03-24 08:48:28 +01:00
Ok(get_room_account_data::v3::Response { account_data: account_data.content })
2021-03-24 08:48:28 +01:00
}
2024-08-08 17:18:30 +00:00
async fn set_account_data(
services: &Services,
room_id: Option<&RoomId>,
sender_user: &UserId,
event_type_s: &str,
data: &RawJsonValue,
) -> Result {
if event_type_s == RoomAccountDataEventType::FullyRead.to_cow_str() {
return Err!(Request(BadJson(
"This endpoint cannot be used for marking a room as fully read (setting \
m.fully_read)"
)));
}
if event_type_s == GlobalAccountDataEventType::PushRules.to_cow_str() {
return Err!(Request(BadJson(
"This endpoint cannot be used for setting/configuring push rules."
)));
}
let data: serde_json::Value = serde_json::from_str(data.get())
.map_err(|e| err!(Request(BadJson(warn!("Invalid JSON provided: {e}")))))?;
2024-08-08 17:18:30 +00:00
services
.account_data
.update(
room_id,
sender_user,
event_type_s.into(),
2024-08-08 17:18:30 +00:00
&json!({
"type": event_type_s,
2024-08-08 17:18:30 +00:00
"content": data,
}),
)
.await
}
#[derive(Deserialize)]
struct ExtractRoomEventContent {
content: Raw<AnyRoomAccountDataEventContent>,
}
#[derive(Deserialize)]
struct ExtractGlobalEventContent {
content: Raw<AnyGlobalAccountDataEventContent>,
}