mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 16:21:24 +01:00
use event content in account_data service api instead of full events
This eliminates the possibility of passing an event that has a mismatching type, reducing the space of possible invalid states.
This commit is contained in:
parent
66210bc32d
commit
b82458a460
15 changed files with 277 additions and 240 deletions
|
|
@ -1,19 +1,10 @@
|
|||
use ruma::{
|
||||
api::client::{
|
||||
config::{
|
||||
get_global_account_data, get_room_account_data,
|
||||
set_global_account_data, set_room_account_data,
|
||||
},
|
||||
error::ErrorKind,
|
||||
use ruma::api::client::{
|
||||
config::{
|
||||
get_global_account_data, get_room_account_data,
|
||||
set_global_account_data, set_room_account_data,
|
||||
},
|
||||
events::{
|
||||
AnyGlobalAccountDataEvent, AnyGlobalAccountDataEventContent,
|
||||
AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent,
|
||||
},
|
||||
serde::Raw,
|
||||
error::ErrorKind,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{services, Ar, Error, Ra, Result};
|
||||
|
||||
|
|
@ -25,17 +16,10 @@ pub(crate) async fn set_global_account_data_route(
|
|||
) -> Result<Ra<set_global_account_data::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = Raw::new(&json!({
|
||||
"type": &body.event_type,
|
||||
"content": &body.data,
|
||||
}))
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?
|
||||
.cast::<AnyGlobalAccountDataEvent>();
|
||||
|
||||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&body.event_type,
|
||||
&event,
|
||||
&body.data,
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_global_account_data::v3::Response {}))
|
||||
|
|
@ -49,18 +33,11 @@ pub(crate) async fn set_room_account_data_route(
|
|||
) -> Result<Ra<set_room_account_data::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = Raw::new(&json!({
|
||||
"type": &body.event_type,
|
||||
"content": &body.data,
|
||||
}))
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?
|
||||
.cast::<AnyRoomAccountDataEvent>();
|
||||
|
||||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&body.event_type,
|
||||
&event,
|
||||
&body.data,
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_room_account_data::v3::Response {}))
|
||||
|
|
@ -74,16 +51,11 @@ pub(crate) async fn get_global_account_data_route(
|
|||
) -> Result<Ra<get_global_account_data::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = services()
|
||||
let account_data = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &body.event_type)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
|
||||
let account_data = event
|
||||
.deserialize_as::<ExtractGlobalEventContent>()
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
.content;
|
||||
|
||||
Ok(Ra(get_global_account_data::v3::Response {
|
||||
account_data,
|
||||
}))
|
||||
|
|
@ -97,27 +69,12 @@ pub(crate) async fn get_room_account_data_route(
|
|||
) -> Result<Ra<get_room_account_data::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = services()
|
||||
let account_data = services()
|
||||
.account_data
|
||||
.get_room(&body.room_id, sender_user, &body.event_type)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
|
||||
let account_data = event
|
||||
.deserialize_as::<ExtractRoomEventContent>()
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
.content;
|
||||
|
||||
Ok(Ra(get_room_account_data::v3::Response {
|
||||
account_data,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ExtractRoomEventContent {
|
||||
content: Raw<AnyRoomAccountDataEventContent>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ExtractGlobalEventContent {
|
||||
content: Raw<AnyGlobalAccountDataEventContent>,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue