2020-05-17 17:56:40 +00:00
|
|
|
use crate::{utils, Error, Result};
|
2020-06-05 16:19:26 +00:00
|
|
|
use ruma::{
|
2020-06-09 13:13:17 +00:00
|
|
|
api::client::error::ErrorKind,
|
2020-07-26 13:41:28 +00:00
|
|
|
events::{AnyEvent as EduEvent, EventType},
|
|
|
|
Raw, RoomId, UserId,
|
2020-06-05 16:19:26 +00:00
|
|
|
};
|
2020-05-17 17:56:40 +00:00
|
|
|
use std::{collections::HashMap, convert::TryFrom};
|
2020-05-03 15:25:31 +00:00
|
|
|
|
|
|
|
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,
|
2020-05-17 19:28:36 +00:00
|
|
|
json: &mut serde_json::Map<String, serde_json::Value>,
|
2020-05-03 15:25:31 +00:00
|
|
|
globals: &super::globals::Globals,
|
|
|
|
) -> Result<()> {
|
2020-05-17 19:28:36 +00:00
|
|
|
if json.get("content").is_none() {
|
2020-06-09 13:13:17 +00:00
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::BadJson,
|
|
|
|
"Json needs to have a content field.",
|
|
|
|
));
|
2020-05-17 19:28:36 +00:00
|
|
|
}
|
|
|
|
json.insert("type".to_owned(), kind.to_string().into());
|
|
|
|
|
2020-05-19 20:56:28 +00:00
|
|
|
let user_id_string = user_id.to_string();
|
|
|
|
let kind_string = kind.to_string();
|
|
|
|
|
2020-05-03 15:25:31 +00:00
|
|
|
let mut prefix = room_id
|
|
|
|
.map(|r| r.to_string())
|
|
|
|
.unwrap_or_default()
|
|
|
|
.as_bytes()
|
|
|
|
.to_vec();
|
|
|
|
prefix.push(0xff);
|
2020-05-19 20:56:28 +00:00
|
|
|
prefix.extend_from_slice(&user_id_string.as_bytes());
|
2020-05-03 15:25:31 +00:00
|
|
|
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| {
|
2020-05-19 20:56:28 +00:00
|
|
|
let user = key.split(|&b| b == 0xff).nth(1);
|
|
|
|
let k = key.rsplit(|&b| b == 0xff).next();
|
|
|
|
|
|
|
|
user.filter(|&user| user == user_id_string.as_bytes())
|
2020-05-03 15:25:31 +00:00
|
|
|
.is_some()
|
2020-05-19 20:56:28 +00:00
|
|
|
&& k.filter(|&k| k == kind_string.as_bytes()).is_some()
|
2020-05-03 15:25:31 +00:00
|
|
|
})
|
|
|
|
{
|
|
|
|
// This is the old room_latest
|
|
|
|
self.roomuserdataid_accountdata.remove(old)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2020-05-03 15:25:31 +00:00
|
|
|
|
2020-06-09 13:13:17 +00:00
|
|
|
self.roomuserdataid_accountdata.insert(
|
|
|
|
key,
|
|
|
|
&*serde_json::to_string(&json).expect("Map::to_string always works"),
|
|
|
|
)?;
|
2020-05-03 15:25:31 +00:00
|
|
|
|
|
|
|
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,
|
2020-07-26 13:41:28 +00:00
|
|
|
) -> Result<Option<Raw<EduEvent>>> {
|
2020-05-03 15:25:31 +00:00
|
|
|
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-07-26 13:41:28 +00:00
|
|
|
) -> Result<HashMap<EventType, Raw<EduEvent>>> {
|
2020-05-03 15:25:31 +00:00
|
|
|
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
|
2020-05-03 15:25:31 +00:00
|
|
|
.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>((
|
2020-06-09 13:13:17 +00:00
|
|
|
EventType::try_from(
|
2020-06-11 08:03:08 +00:00
|
|
|
utils::string_from_bytes(k.rsplit(|&b| b == 0xff).next().ok_or_else(
|
|
|
|
|| Error::bad_database("RoomUserData ID in db is invalid."),
|
|
|
|
)?)
|
|
|
|
.map_err(|_| Error::bad_database("RoomUserData ID in db is invalid."))?,
|
2020-06-09 13:13:17 +00:00
|
|
|
)
|
2020-06-11 08:03:08 +00:00
|
|
|
.map_err(|_| Error::bad_database("RoomUserData ID in db is invalid."))?,
|
2020-07-26 13:41:28 +00:00
|
|
|
serde_json::from_slice::<Raw<EduEvent>>(&v).map_err(|_| {
|
2020-06-11 08:03:08 +00:00
|
|
|
Error::bad_database("Database contains invalid account data.")
|
2020-06-09 13:13:17 +00:00
|
|
|
})?,
|
2020-05-17 17:56:40 +00:00
|
|
|
))
|
|
|
|
})
|
2020-05-03 15:25:31 +00:00
|
|
|
{
|
2020-06-09 13:13:17 +00:00
|
|
|
let (kind, data) = r?;
|
2020-05-17 19:28:36 +00:00
|
|
|
userdata.insert(kind, data);
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(userdata)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns all account data.
|
|
|
|
pub fn all(
|
|
|
|
&self,
|
|
|
|
room_id: Option<&RoomId>,
|
|
|
|
user_id: &UserId,
|
2020-07-26 13:41:28 +00:00
|
|
|
) -> Result<HashMap<EventType, Raw<EduEvent>>> {
|
2020-05-03 15:25:31 +00:00
|
|
|
self.changes_since(room_id, user_id, 0)
|
|
|
|
}
|
|
|
|
}
|