combine service/users data w/ mod unit split sliding sync related out of service/users instrument database entry points remove increment crap from database interface de-wrap all database get() calls de-wrap all database insert() calls de-wrap all database remove() calls refactor database interface for async streaming add query key serializer for database implement Debug for result handle add query deserializer for database add deserialization trait for option handle start a stream utils suite de-wrap/asyncify/type-query count_one_time_keys() de-wrap/asyncify users count add admin query users command suite de-wrap/asyncify users exists de-wrap/partially asyncify user filter related asyncify/de-wrap users device/keys related asyncify/de-wrap user auth/misc related asyncify/de-wrap users blurhash asyncify/de-wrap account_data get; merge Data into Service partial asyncify/de-wrap uiaa; merge Data into Service partially asyncify/de-wrap transaction_ids get; merge Data into Service partially asyncify/de-wrap key_backups; merge Data into Service asyncify/de-wrap pusher service getters; merge Data into Service asyncify/de-wrap rooms alias getters/some iterators asyncify/de-wrap rooms directory getters/iterator partially asyncify/de-wrap rooms lazy-loading partially asyncify/de-wrap rooms metadata asyncify/dewrap rooms outlier asyncify/dewrap rooms pdu_metadata dewrap/partially asyncify rooms read receipt de-wrap rooms search service de-wrap/partially asyncify rooms user service partial de-wrap rooms state_compressor de-wrap rooms state_cache de-wrap room state et al de-wrap rooms timeline service additional users device/keys related de-wrap/asyncify sender asyncify services refactor database to TryFuture/TryStream refactor services for TryFuture/TryStream asyncify api handlers additional asyncification for admin module abstract stream related; support reverse streams additional stream conversions asyncify state-res related Signed-off-by: Jason Volk <jason@zemos.net>
132 lines
3.8 KiB
Rust
132 lines
3.8 KiB
Rust
use axum::extract::State;
|
|
use conduit::err;
|
|
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,
|
|
OwnedUserId, RoomId,
|
|
};
|
|
use serde::Deserialize;
|
|
use serde_json::{json, value::RawValue as RawJsonValue};
|
|
|
|
use crate::{service::Services, Error, Result, Ruma};
|
|
|
|
/// # `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(
|
|
State(services): State<crate::State>, body: Ruma<set_global_account_data::v3::Request>,
|
|
) -> Result<set_global_account_data::v3::Response> {
|
|
set_account_data(
|
|
&services,
|
|
None,
|
|
&body.sender_user,
|
|
&body.event_type.to_string(),
|
|
body.data.json(),
|
|
)
|
|
.await?;
|
|
|
|
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(crate) async fn set_room_account_data_route(
|
|
State(services): State<crate::State>, body: Ruma<set_room_account_data::v3::Request>,
|
|
) -> Result<set_room_account_data::v3::Response> {
|
|
set_account_data(
|
|
&services,
|
|
Some(&body.room_id),
|
|
&body.sender_user,
|
|
&body.event_type.to_string(),
|
|
body.data.json(),
|
|
)
|
|
.await?;
|
|
|
|
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(crate) async fn get_global_account_data_route(
|
|
State(services): State<crate::State>, 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.to_string().into())
|
|
.await
|
|
.map_err(|_| err!(Request(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(crate) async fn get_room_account_data_route(
|
|
State(services): State<crate::State>, 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())
|
|
.await
|
|
.map_err(|_| err!(Request(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,
|
|
})
|
|
}
|
|
|
|
async fn set_account_data(
|
|
services: &Services, room_id: Option<&RoomId>, sender_user: &Option<OwnedUserId>, event_type: &str,
|
|
data: &RawJsonValue,
|
|
) -> Result<()> {
|
|
let sender_user = sender_user.as_ref().expect("user is authenticated");
|
|
|
|
let data: serde_json::Value =
|
|
serde_json::from_str(data.get()).map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
|
|
|
|
services
|
|
.account_data
|
|
.update(
|
|
room_id,
|
|
sender_user,
|
|
event_type.into(),
|
|
&json!({
|
|
"type": event_type,
|
|
"content": data,
|
|
}),
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct ExtractRoomEventContent {
|
|
content: Raw<AnyRoomAccountDataEventContent>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct ExtractGlobalEventContent {
|
|
content: Raw<AnyGlobalAccountDataEventContent>,
|
|
}
|