From 6215218c3c7488d5e55a6c0a83f52230dcd60aaa Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sun, 17 May 2020 21:28:36 +0200 Subject: [PATCH] fix: account data json --- src/client_server.rs | 20 ++++++++++++++++---- src/database/account_data.rs | 23 +++++++++++------------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/client_server.rs b/src/client_server.rs index 8627dcb8..d7e129c2 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -204,6 +204,8 @@ pub fn register_route( }, }, }) + .unwrap() + .as_object_mut() .unwrap(), &db.globals, ) @@ -383,6 +385,8 @@ pub fn set_pushrule_route( }, }, }) + .unwrap() + .as_object_mut() .unwrap(), &db.globals, ) @@ -443,7 +447,9 @@ pub fn set_global_account_data_route( None, user_id, &EventType::try_from(&body.event_type).unwrap(), - serde_json::from_str(body.data.get()).unwrap(), + json!({"content": serde_json::from_str::(body.data.get()).unwrap()}) + .as_object_mut() + .unwrap(), &db.globals, ) .unwrap(); @@ -465,7 +471,11 @@ pub fn get_global_account_data_route( if let Some(data) = db .account_data - .get(None, user_id, &EventType::try_from(&body.event_type).unwrap()) + .get( + None, + user_id, + &EventType::try_from(&body.event_type).unwrap(), + ) .unwrap() { MatrixResult(Ok(get_global_account_data::Response { account_data: data })) @@ -792,6 +802,8 @@ pub fn set_read_marker_route( }, room_id: Some(body.room_id.clone()), }) + .unwrap() + .as_object_mut() .unwrap(), &db.globals, ) @@ -1317,7 +1329,7 @@ pub fn sync_route( db: State<'_, Database>, body: Ruma, ) -> MatrixResult { - std::thread::sleep(Duration::from_millis(1500)); + std::thread::sleep(Duration::from_millis(1000)); let user_id = body.user_id.as_ref().expect("user is authenticated"); let device_id = body.device_id.as_ref().expect("user is authenticated"); @@ -1526,7 +1538,7 @@ pub fn sync_route( let timestamp = edu.content.last_active_ago.unwrap(); edu.content.last_active_ago = Some( js_int::UInt::try_from(utils::millis_since_unix_epoch()).unwrap() - - timestamp + - timestamp, ); Some(edu.into()) } else { diff --git a/src/database/account_data.rs b/src/database/account_data.rs index 659d3e55..21982dec 100644 --- a/src/database/account_data.rs +++ b/src/database/account_data.rs @@ -14,9 +14,14 @@ impl AccountData { room_id: Option<&RoomId>, user_id: &UserId, kind: &EventType, - data: serde_json::Value, + json: &mut serde_json::Map, globals: &super::globals::Globals, ) -> Result<()> { + if json.get("content").is_none() { + return Err(Error::BadRequest("json needs to have a content field")); + } + json.insert("type".to_owned(), kind.to_string().into()); + let mut prefix = room_id .map(|r| r.to_string()) .unwrap_or_default() @@ -43,7 +48,6 @@ impl AccountData { { // This is the old room_latest self.roomuserdataid_accountdata.remove(old)?; - println!("removed old account data"); } let mut key = prefix; @@ -52,7 +56,7 @@ impl AccountData { key.extend_from_slice(kind.to_string().as_bytes()); self.roomuserdataid_accountdata - .insert(key, &*data.to_string()) + .insert(key, &*serde_json::to_string(&json)?) .unwrap(); Ok(()) @@ -104,18 +108,13 @@ impl AccountData { .ok_or(Error::BadDatabase("roomuserdataid is invalid"))?, )?) .map_err(|_| Error::BadDatabase("roomuserdataid is invalid"))?, - serde_json::from_slice::(&v).unwrap(), + serde_json::from_slice::>(&v).unwrap(), )) }) { - let (kind, content) = r.unwrap(); - let mut json = serde_json::Map::new(); - json.insert("content".to_owned(), content); - json.insert("type".to_owned(), kind.to_string().into()); - userdata.insert( - kind, - serde_json::from_value::>(json.into())?, - ); + let (kind, data) = r.unwrap(); + &data.deserialize(); + userdata.insert(kind, data); } Ok(userdata)