2020-05-17 17:56:40 +00:00
|
|
|
use crate::{utils, Error, Result};
|
2020-06-05 16:19:26 +00:00
|
|
|
use ruma::{
|
2020-07-28 12:02:29 +00:00
|
|
|
api::client::error::ErrorKind,
|
2021-05-20 21:46:52 +00:00
|
|
|
events::{AnyEphemeralRoomEvent, EventType},
|
2020-12-04 23:16:17 +00:00
|
|
|
serde::Raw,
|
|
|
|
RoomId, UserId,
|
2020-06-05 16:19:26 +00:00
|
|
|
};
|
2020-07-29 18:28:40 +00:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2021-06-08 16:10:00 +00:00
|
|
|
use std::{collections::HashMap, convert::TryFrom, sync::Arc};
|
|
|
|
|
|
|
|
use super::abstraction::Tree;
|
2020-05-03 15:25:31 +00:00
|
|
|
|
|
|
|
pub struct AccountData {
|
2021-06-08 16:10:00 +00:00
|
|
|
pub(super) roomuserdataid_accountdata: Arc<dyn Tree>, // RoomUserDataId = Room + User + Count + Type
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountData {
|
|
|
|
/// Places one event in the account data of the user and removes the previous entry.
|
2020-07-26 20:33:20 +00:00
|
|
|
pub fn update<T: Serialize>(
|
2020-05-03 15:25:31 +00:00
|
|
|
&self,
|
|
|
|
room_id: Option<&RoomId>,
|
|
|
|
user_id: &UserId,
|
2020-07-26 20:33:20 +00:00
|
|
|
event_type: EventType,
|
2020-07-28 12:02:29 +00:00
|
|
|
data: &T,
|
2020-05-03 15:25:31 +00:00
|
|
|
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);
|
2021-04-05 19:44:21 +00:00
|
|
|
prefix.extend_from_slice(&user_id.as_bytes());
|
2020-05-03 15:25:31 +00:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
// Remove old entry
|
2021-06-08 16:10:00 +00:00
|
|
|
if let Some((old_key, _)) = self.find_event(room_id, user_id, &event_type)? {
|
|
|
|
self.roomuserdataid_accountdata.remove(&old_key)?;
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut key = prefix;
|
|
|
|
key.extend_from_slice(&globals.next_count()?.to_be_bytes());
|
|
|
|
key.push(0xff);
|
2021-04-05 19:44:21 +00:00
|
|
|
key.extend_from_slice(event_type.as_ref().as_bytes());
|
2020-05-03 15:25:31 +00:00
|
|
|
|
2020-07-28 12:02:29 +00:00
|
|
|
let json = serde_json::to_value(data).expect("all types here can be serialized"); // TODO: maybe add error handling
|
|
|
|
if json.get("type").is_none() || json.get("content").is_none() {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Account data doesn't have all required fields.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
self.roomuserdataid_accountdata.insert(
|
|
|
|
&key,
|
|
|
|
&serde_json::to_vec(&json).expect("to_vec always works on json values"),
|
|
|
|
)?;
|
2020-05-03 15:25:31 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Searches the account data for a specific kind.
|
2020-07-26 20:33:20 +00:00
|
|
|
pub fn get<T: DeserializeOwned>(
|
2020-05-03 15:25:31 +00:00
|
|
|
&self,
|
|
|
|
room_id: Option<&RoomId>,
|
|
|
|
user_id: &UserId,
|
2020-07-26 20:33:20 +00:00
|
|
|
kind: EventType,
|
|
|
|
) -> Result<Option<T>> {
|
2021-06-08 16:10:00 +00:00
|
|
|
self.find_event(room_id, user_id, &kind)?
|
|
|
|
.map(|(_, v)| {
|
2020-07-28 12:02:29 +00:00
|
|
|
serde_json::from_slice(&v).map_err(|_| Error::bad_database("could not deserialize"))
|
2020-07-26 20:33:20 +00:00
|
|
|
})
|
|
|
|
.transpose()
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns all changes to the account data that happened after `since`.
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2020-05-03 15:25:31 +00:00
|
|
|
pub fn changes_since(
|
|
|
|
&self,
|
|
|
|
room_id: Option<&RoomId>,
|
|
|
|
user_id: &UserId,
|
|
|
|
since: u64,
|
2021-05-20 21:46:52 +00:00
|
|
|
) -> Result<HashMap<EventType, Raw<AnyEphemeralRoomEvent>>> {
|
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);
|
2021-04-05 19:44:21 +00:00
|
|
|
prefix.extend_from_slice(&user_id.as_bytes());
|
2020-05-03 15:25:31 +00:00
|
|
|
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
|
2021-06-08 16:10:00 +00:00
|
|
|
.iter_from(&first_possible, false)
|
2020-05-03 15:25:31 +00:00
|
|
|
.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."))?,
|
2021-05-20 21:46:52 +00:00
|
|
|
serde_json::from_slice::<Raw<AnyEphemeralRoomEvent>>(&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)
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:33:20 +00:00
|
|
|
fn find_event(
|
2020-05-03 15:25:31 +00:00
|
|
|
&self,
|
|
|
|
room_id: Option<&RoomId>,
|
|
|
|
user_id: &UserId,
|
2020-07-26 20:33:20 +00:00
|
|
|
kind: &EventType,
|
2021-07-14 07:07:08 +00:00
|
|
|
) -> Result<Option<(Vec<u8>, Vec<u8>)>> {
|
2020-07-26 20:33:20 +00:00
|
|
|
let mut prefix = room_id
|
|
|
|
.map(|r| r.to_string())
|
|
|
|
.unwrap_or_default()
|
|
|
|
.as_bytes()
|
|
|
|
.to_vec();
|
|
|
|
prefix.push(0xff);
|
2021-04-05 19:44:21 +00:00
|
|
|
prefix.extend_from_slice(&user_id.as_bytes());
|
2020-07-26 20:33:20 +00:00
|
|
|
prefix.push(0xff);
|
2021-06-08 16:10:00 +00:00
|
|
|
|
|
|
|
let mut last_possible_key = prefix.clone();
|
|
|
|
last_possible_key.extend_from_slice(&u64::MAX.to_be_bytes());
|
|
|
|
|
2020-07-26 20:33:20 +00:00
|
|
|
let kind = kind.clone();
|
|
|
|
|
2021-06-08 16:10:00 +00:00
|
|
|
Ok(self
|
|
|
|
.roomuserdataid_accountdata
|
|
|
|
.iter_from(&last_possible_key, true)
|
|
|
|
.take_while(move |(k, _)| k.starts_with(&prefix))
|
|
|
|
.find(move |(k, _)| {
|
|
|
|
k.rsplit(|&b| b == 0xff)
|
|
|
|
.next()
|
|
|
|
.map(|current_event_type| current_event_type == kind.as_ref().as_bytes())
|
2020-07-26 20:33:20 +00:00
|
|
|
.unwrap_or(false)
|
2021-06-08 16:10:00 +00:00
|
|
|
}))
|
2020-05-03 15:25:31 +00:00
|
|
|
}
|
|
|
|
}
|