2021-07-14 07:07:08 +00:00
|
|
|
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
2020-07-30 16:14:47 +00:00
|
|
|
use ruma::{
|
|
|
|
api::client::{
|
|
|
|
error::ErrorKind,
|
2021-03-24 10:52:10 +00:00
|
|
|
r0::config::{
|
|
|
|
get_global_account_data, get_room_account_data, set_global_account_data,
|
|
|
|
set_room_account_data,
|
|
|
|
},
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
2021-05-20 21:46:52 +00:00
|
|
|
events::{AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent},
|
2020-12-04 23:16:17 +00:00
|
|
|
serde::Raw,
|
2020-07-30 16:14:47 +00:00
|
|
|
};
|
2021-04-29 19:06:15 +00:00
|
|
|
use serde::Deserialize;
|
2021-05-20 21:46:52 +00:00
|
|
|
use serde_json::{json, value::RawValue as RawJsonValue};
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, put};
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/user/<_>/account_data/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn set_global_account_data_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<set_global_account_data::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<set_global_account_data::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-05-20 21:46:52 +00:00
|
|
|
let data = serde_json::from_str::<serde_json::Value>(body.data.get())
|
2020-07-30 16:14:47 +00:00
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
|
|
|
|
|
|
|
|
let event_type = body.event_type.to_string();
|
|
|
|
|
|
|
|
db.account_data.update(
|
|
|
|
None,
|
2020-10-18 18:33:12 +00:00
|
|
|
sender_user,
|
2020-07-30 16:14:47 +00:00
|
|
|
event_type.clone().into(),
|
2021-05-20 21:46:52 +00:00
|
|
|
&json!({
|
|
|
|
"type": event_type,
|
|
|
|
"content": data,
|
|
|
|
}),
|
2020-07-30 16:14:47 +00:00
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
2021-07-15 21:17:58 +00:00
|
|
|
Ok(set_global_account_data::Response {}.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 07:48:28 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
2021-03-24 10:52:10 +00:00
|
|
|
put(
|
|
|
|
"/_matrix/client/r0/user/<_>/rooms/<_>/account_data/<_>",
|
|
|
|
data = "<body>"
|
|
|
|
)
|
2021-03-24 07:48:28 +00:00
|
|
|
)]
|
|
|
|
#[tracing::instrument(skip(db, body))]
|
|
|
|
pub async fn set_room_account_data_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2021-03-24 07:48:28 +00:00
|
|
|
body: Ruma<set_room_account_data::Request<'_>>,
|
|
|
|
) -> ConduitResult<set_room_account_data::Response> {
|
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
2021-05-29 08:49:13 +00:00
|
|
|
let data = serde_json::from_str::<serde_json::Value>(body.data.get())
|
2021-03-24 07:48:28 +00:00
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
|
|
|
|
|
|
|
|
let event_type = body.event_type.to_string();
|
|
|
|
|
|
|
|
db.account_data.update(
|
|
|
|
Some(&body.room_id),
|
|
|
|
sender_user,
|
|
|
|
event_type.clone().into(),
|
2021-05-20 21:46:52 +00:00
|
|
|
&json!({
|
|
|
|
"type": event_type,
|
|
|
|
"content": data,
|
|
|
|
}),
|
2021-03-24 07:48:28 +00:00
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2021-03-24 07:48:28 +00:00
|
|
|
|
2021-07-15 21:17:58 +00:00
|
|
|
Ok(set_room_account_data::Response {}.into())
|
2021-03-24 07:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/user/<_>/account_data/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_global_account_data_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<get_global_account_data::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_global_account_data::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-04-29 19:06:15 +00:00
|
|
|
let event = db
|
2020-07-30 16:14:47 +00:00
|
|
|
.account_data
|
2021-04-29 19:06:15 +00:00
|
|
|
.get::<Box<RawJsonValue>>(None, sender_user, body.event_type.clone().into())?
|
2020-07-30 16:14:47 +00:00
|
|
|
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
2021-05-20 21:46:52 +00:00
|
|
|
let account_data = serde_json::from_str::<ExtractGlobalEventContent>(event.get())
|
2021-04-29 19:06:15 +00:00
|
|
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
|
|
|
.content;
|
|
|
|
|
2021-04-29 18:56:45 +00:00
|
|
|
Ok(get_global_account_data::Response { account_data }.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
2021-03-24 07:48:28 +00:00
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
2021-03-24 10:52:10 +00:00
|
|
|
get(
|
|
|
|
"/_matrix/client/r0/user/<_>/rooms/<_>/account_data/<_>",
|
|
|
|
data = "<body>"
|
|
|
|
)
|
2021-03-24 07:48:28 +00:00
|
|
|
)]
|
|
|
|
#[tracing::instrument(skip(db, body))]
|
|
|
|
pub async fn get_room_account_data_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2021-03-24 07:48:28 +00:00
|
|
|
body: Ruma<get_room_account_data::Request<'_>>,
|
|
|
|
) -> ConduitResult<get_room_account_data::Response> {
|
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
2021-04-29 19:06:15 +00:00
|
|
|
let event = db
|
2021-03-24 07:48:28 +00:00
|
|
|
.account_data
|
2021-04-29 19:06:15 +00:00
|
|
|
.get::<Box<RawJsonValue>>(
|
2021-03-24 10:52:10 +00:00
|
|
|
Some(&body.room_id),
|
|
|
|
sender_user,
|
|
|
|
body.event_type.clone().into(),
|
|
|
|
)?
|
2021-03-24 07:48:28 +00:00
|
|
|
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2021-03-24 07:48:28 +00:00
|
|
|
|
2021-05-20 21:46:52 +00:00
|
|
|
let account_data = serde_json::from_str::<ExtractRoomEventContent>(event.get())
|
2021-04-29 19:06:15 +00:00
|
|
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
|
|
|
.content;
|
|
|
|
|
2021-04-29 18:56:45 +00:00
|
|
|
Ok(get_room_account_data::Response { account_data }.into())
|
2021-03-24 07:48:28 +00:00
|
|
|
}
|
2021-04-29 19:06:15 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2021-05-20 21:46:52 +00:00
|
|
|
struct ExtractRoomEventContent {
|
|
|
|
content: Raw<AnyRoomAccountDataEventContent>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ExtractGlobalEventContent {
|
|
|
|
content: Raw<AnyGlobalAccountDataEventContent>,
|
2021-04-29 19:06:15 +00:00
|
|
|
}
|