Return only event content in account_data endpoints, not the entire event

This commit is contained in:
Jonas Platte 2021-04-29 21:06:15 +02:00
parent 8bfaf09f37
commit e1c4e5c73e
No known key found for this signature in database
GPG key ID: 7D261D771D915378

View file

@ -11,6 +11,8 @@ use ruma::{
events::{custom::CustomEventContent, AnyBasicEventContent, BasicEvent}, events::{custom::CustomEventContent, AnyBasicEventContent, BasicEvent},
serde::Raw, serde::Raw,
}; };
use serde::Deserialize;
use serde_json::value::RawValue as RawJsonValue;
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
use rocket::{get, put}; use rocket::{get, put};
@ -91,13 +93,16 @@ pub async fn get_global_account_data_route(
) -> ConduitResult<get_global_account_data::Response> { ) -> ConduitResult<get_global_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let account_data = db let event = db
.account_data .account_data
.get::<Raw<AnyBasicEventContent>>(None, sender_user, body.event_type.clone().into())? .get::<Box<RawJsonValue>>(None, sender_user, body.event_type.clone().into())?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
db.flush().await?; db.flush().await?;
let account_data = serde_json::from_str::<ExtractEventContent>(event.get())
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
.content;
Ok(get_global_account_data::Response { account_data }.into()) Ok(get_global_account_data::Response { account_data }.into())
} }
@ -115,16 +120,24 @@ pub async fn get_room_account_data_route(
) -> ConduitResult<get_room_account_data::Response> { ) -> ConduitResult<get_room_account_data::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let account_data = db let event = db
.account_data .account_data
.get::<Raw<AnyBasicEventContent>>( .get::<Box<RawJsonValue>>(
Some(&body.room_id), Some(&body.room_id),
sender_user, sender_user,
body.event_type.clone().into(), body.event_type.clone().into(),
)? )?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
db.flush().await?; db.flush().await?;
let account_data = serde_json::from_str::<ExtractEventContent>(event.get())
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
.content;
Ok(get_room_account_data::Response { account_data }.into()) Ok(get_room_account_data::Response { account_data }.into())
} }
#[derive(Deserialize)]
struct ExtractEventContent {
content: Raw<AnyBasicEventContent>,
}