fix: account data json

This commit is contained in:
timokoesters 2020-05-17 21:28:36 +02:00
parent 7fc71b3968
commit 6215218c3c
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
2 changed files with 27 additions and 16 deletions

View File

@ -204,6 +204,8 @@ pub fn register_route(
}, },
}, },
}) })
.unwrap()
.as_object_mut()
.unwrap(), .unwrap(),
&db.globals, &db.globals,
) )
@ -383,6 +385,8 @@ pub fn set_pushrule_route(
}, },
}, },
}) })
.unwrap()
.as_object_mut()
.unwrap(), .unwrap(),
&db.globals, &db.globals,
) )
@ -443,7 +447,9 @@ pub fn set_global_account_data_route(
None, None,
user_id, user_id,
&EventType::try_from(&body.event_type).unwrap(), &EventType::try_from(&body.event_type).unwrap(),
serde_json::from_str(body.data.get()).unwrap(), json!({"content": serde_json::from_str::<serde_json::Value>(body.data.get()).unwrap()})
.as_object_mut()
.unwrap(),
&db.globals, &db.globals,
) )
.unwrap(); .unwrap();
@ -465,7 +471,11 @@ pub fn get_global_account_data_route(
if let Some(data) = db if let Some(data) = db
.account_data .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() .unwrap()
{ {
MatrixResult(Ok(get_global_account_data::Response { account_data: data })) 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()), room_id: Some(body.room_id.clone()),
}) })
.unwrap()
.as_object_mut()
.unwrap(), .unwrap(),
&db.globals, &db.globals,
) )
@ -1317,7 +1329,7 @@ pub fn sync_route(
db: State<'_, Database>, db: State<'_, Database>,
body: Ruma<sync_events::Request>, body: Ruma<sync_events::Request>,
) -> MatrixResult<sync_events::Response> { ) -> MatrixResult<sync_events::Response> {
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 user_id = body.user_id.as_ref().expect("user is authenticated");
let device_id = body.device_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(); let timestamp = edu.content.last_active_ago.unwrap();
edu.content.last_active_ago = Some( edu.content.last_active_ago = Some(
js_int::UInt::try_from(utils::millis_since_unix_epoch()).unwrap() js_int::UInt::try_from(utils::millis_since_unix_epoch()).unwrap()
- timestamp - timestamp,
); );
Some(edu.into()) Some(edu.into())
} else { } else {

View File

@ -14,9 +14,14 @@ impl AccountData {
room_id: Option<&RoomId>, room_id: Option<&RoomId>,
user_id: &UserId, user_id: &UserId,
kind: &EventType, kind: &EventType,
data: serde_json::Value, json: &mut serde_json::Map<String, serde_json::Value>,
globals: &super::globals::Globals, globals: &super::globals::Globals,
) -> Result<()> { ) -> 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 let mut prefix = room_id
.map(|r| r.to_string()) .map(|r| r.to_string())
.unwrap_or_default() .unwrap_or_default()
@ -43,7 +48,6 @@ impl AccountData {
{ {
// This is the old room_latest // This is the old room_latest
self.roomuserdataid_accountdata.remove(old)?; self.roomuserdataid_accountdata.remove(old)?;
println!("removed old account data");
} }
let mut key = prefix; let mut key = prefix;
@ -52,7 +56,7 @@ impl AccountData {
key.extend_from_slice(kind.to_string().as_bytes()); key.extend_from_slice(kind.to_string().as_bytes());
self.roomuserdataid_accountdata self.roomuserdataid_accountdata
.insert(key, &*data.to_string()) .insert(key, &*serde_json::to_string(&json)?)
.unwrap(); .unwrap();
Ok(()) Ok(())
@ -104,18 +108,13 @@ impl AccountData {
.ok_or(Error::BadDatabase("roomuserdataid is invalid"))?, .ok_or(Error::BadDatabase("roomuserdataid is invalid"))?,
)?) )?)
.map_err(|_| Error::BadDatabase("roomuserdataid is invalid"))?, .map_err(|_| Error::BadDatabase("roomuserdataid is invalid"))?,
serde_json::from_slice::<serde_json::Value>(&v).unwrap(), serde_json::from_slice::<EventJson<EduEvent>>(&v).unwrap(),
)) ))
}) })
{ {
let (kind, content) = r.unwrap(); let (kind, data) = r.unwrap();
let mut json = serde_json::Map::new(); &data.deserialize();
json.insert("content".to_owned(), content); userdata.insert(kind, data);
json.insert("type".to_owned(), kind.to_string().into());
userdata.insert(
kind,
serde_json::from_value::<EventJson<EduEvent>>(json.into())?,
);
} }
Ok(userdata) Ok(userdata)