fix: account data

This commit is contained in:
timokoesters 2020-07-28 14:02:29 +02:00
parent 21eb8d4fe3
commit 05f9d927b8
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
2 changed files with 15 additions and 11 deletions

View file

@ -622,17 +622,14 @@ pub fn get_global_account_data_route(
let data = db let data = db
.account_data .account_data
.get::<ruma::events::AnyBasicEvent>( .get::<Raw<ruma::events::AnyBasicEvent>>(
None, None,
sender_id, sender_id,
EventType::try_from(&body.event_type).expect("EventType::try_from can never fail"), EventType::try_from(&body.event_type).expect("EventType::try_from can never fail"),
)? )?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
Ok(get_global_account_data::Response { Ok(get_global_account_data::Response { account_data: data }.into())
account_data: Raw::from(data),
}
.into())
} }
#[cfg_attr( #[cfg_attr(

View file

@ -1,5 +1,6 @@
use crate::{utils, Error, Result}; use crate::{utils, Error, Result};
use ruma::{ use ruma::{
api::client::error::ErrorKind,
events::{AnyEvent as EduEvent, EventType}, events::{AnyEvent as EduEvent, EventType},
Raw, RoomId, UserId, Raw, RoomId, UserId,
}; };
@ -19,7 +20,7 @@ impl AccountData {
room_id: Option<&RoomId>, room_id: Option<&RoomId>,
user_id: &UserId, user_id: &UserId,
event_type: EventType, event_type: EventType,
event: &T, data: &T,
globals: &super::globals::Globals, globals: &super::globals::Globals,
) -> Result<()> { ) -> Result<()> {
let mut prefix = room_id let mut prefix = room_id
@ -42,10 +43,16 @@ impl AccountData {
key.push(0xff); key.push(0xff);
key.extend_from_slice(event_type.to_string().as_bytes()); key.extend_from_slice(event_type.to_string().as_bytes());
self.roomuserdataid_accountdata.insert( let json = serde_json::to_value(data).expect("all types here can be serialized"); // TODO: maybe add error handling
key, if json.get("type").is_none() || json.get("content").is_none() {
&*serde_json::to_string(&event).expect("Map::to_string always works"), return Err(Error::BadRequest(
)?; ErrorKind::InvalidParam,
"Account data doesn't have all required fields.",
));
}
self.roomuserdataid_accountdata
.insert(key, &*json.to_string())?;
Ok(()) Ok(())
} }
@ -60,7 +67,7 @@ impl AccountData {
self.find_event(room_id, user_id, &kind) self.find_event(room_id, user_id, &kind)
.map(|r| { .map(|r| {
let (_, v) = r?; let (_, v) = r?;
serde_json::from_slice(&v).map_err(|_| Error::BadDatabase("could not deserialize")) serde_json::from_slice(&v).map_err(|_| Error::bad_database("could not deserialize"))
}) })
.transpose() .transpose()
} }