conduit/src/database/account_data.rs

133 lines
4.2 KiB
Rust
Raw Normal View History

2020-05-17 17:56:40 +00:00
use crate::{utils, Error, Result};
use ruma_events::{collections::only::Event as EduEvent, EventJson, EventType};
use ruma_identifiers::{RoomId, UserId};
2020-05-17 17:56:40 +00:00
use std::{collections::HashMap, convert::TryFrom};
pub struct AccountData {
pub(super) roomuserdataid_accountdata: sled::Tree, // RoomUserDataId = Room + User + Count + Type
}
impl AccountData {
/// Places one event in the account data of the user and removes the previous entry.
pub fn update(
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
2020-05-17 17:56:40 +00:00
kind: &EventType,
data: serde_json::Value,
globals: &super::globals::Globals,
) -> Result<()> {
let mut prefix = room_id
.map(|r| r.to_string())
.unwrap_or_default()
.as_bytes()
.to_vec();
prefix.push(0xff);
prefix.extend_from_slice(&user_id.to_string().as_bytes());
prefix.push(0xff);
// Remove old entry
if let Some(old) = self
.roomuserdataid_accountdata
.scan_prefix(&prefix)
.keys()
.rev()
.filter_map(|r| r.ok())
.take_while(|key| key.starts_with(&prefix))
2020-05-11 10:33:25 +00:00
.find(|key| {
key.split(|&b| b == 0xff)
.nth(1)
.filter(|&user| user == user_id.to_string().as_bytes())
.is_some()
})
{
// This is the old room_latest
self.roomuserdataid_accountdata.remove(old)?;
println!("removed old account data");
}
let mut key = prefix;
key.extend_from_slice(&globals.next_count()?.to_be_bytes());
key.push(0xff);
2020-05-17 17:56:40 +00:00
key.extend_from_slice(kind.to_string().as_bytes());
self.roomuserdataid_accountdata
2020-05-17 17:56:40 +00:00
.insert(key, &*data.to_string())
.unwrap();
Ok(())
}
// TODO: Optimize
/// Searches the account data for a specific kind.
pub fn get(
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
2020-05-17 17:56:40 +00:00
kind: &EventType,
) -> Result<Option<EventJson<EduEvent>>> {
Ok(self.all(room_id, user_id)?.remove(kind))
}
/// Returns all changes to the account data that happened after `since`.
pub fn changes_since(
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
since: u64,
2020-05-17 17:56:40 +00:00
) -> Result<HashMap<EventType, EventJson<EduEvent>>> {
let mut userdata = HashMap::new();
let mut prefix = room_id
.map(|r| r.to_string())
.unwrap_or_default()
.as_bytes()
.to_vec();
prefix.push(0xff);
prefix.extend_from_slice(&user_id.to_string().as_bytes());
prefix.push(0xff);
// Skip the data that's exactly at since, because we sent that last time
let mut first_possible = prefix.clone();
first_possible.extend_from_slice(&(since + 1).to_be_bytes());
2020-05-17 17:56:40 +00:00
for r in self
.roomuserdataid_accountdata
.range(&*first_possible..)
.filter_map(|r| r.ok())
.take_while(move |(k, _)| k.starts_with(&prefix))
2020-05-17 17:56:40 +00:00
.map(|(k, v)| {
Ok::<_, Error>((
EventType::try_from(utils::string_from_bytes(
k.rsplit(|&b| b == 0xff)
.next()
.ok_or(Error::BadDatabase("roomuserdataid is invalid"))?,
)?)
.map_err(|_| Error::BadDatabase("roomuserdataid is invalid"))?,
serde_json::from_slice::<serde_json::Value>(&v).unwrap(),
))
})
{
2020-05-17 17:56:40 +00:00
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(
2020-05-17 17:56:40 +00:00
kind,
serde_json::from_value::<EventJson<EduEvent>>(json.into())?,
);
}
Ok(userdata)
}
/// Returns all account data.
pub fn all(
&self,
room_id: Option<&RoomId>,
user_id: &UserId,
2020-05-17 17:56:40 +00:00
) -> Result<HashMap<EventType, EventJson<EduEvent>>> {
self.changes_since(room_id, user_id, 0)
}
}