feat: end to end encryption
This commit is contained in:
parent
4fb79ebb4c
commit
7fc71b3968
7 changed files with 461 additions and 65 deletions
|
@ -1,7 +1,7 @@
|
|||
use crate::Result;
|
||||
use ruma_events::{collections::only::Event as EduEvent, EventJson};
|
||||
use crate::{utils, Error, Result};
|
||||
use ruma_events::{collections::only::Event as EduEvent, EventJson, EventType};
|
||||
use ruma_identifiers::{RoomId, UserId};
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, convert::TryFrom};
|
||||
|
||||
pub struct AccountData {
|
||||
pub(super) roomuserdataid_accountdata: sled::Tree, // RoomUserDataId = Room + User + Count + Type
|
||||
|
@ -13,7 +13,8 @@ impl AccountData {
|
|||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
user_id: &UserId,
|
||||
event: EduEvent,
|
||||
kind: &EventType,
|
||||
data: serde_json::Value,
|
||||
globals: &super::globals::Globals,
|
||||
) -> Result<()> {
|
||||
let mut prefix = room_id
|
||||
|
@ -48,11 +49,10 @@ impl AccountData {
|
|||
let mut key = prefix;
|
||||
key.extend_from_slice(&globals.next_count()?.to_be_bytes());
|
||||
key.push(0xff);
|
||||
let json = serde_json::to_value(&event)?;
|
||||
key.extend_from_slice(json["type"].as_str().unwrap().as_bytes());
|
||||
key.extend_from_slice(kind.to_string().as_bytes());
|
||||
|
||||
self.roomuserdataid_accountdata
|
||||
.insert(key, &*json.to_string())
|
||||
.insert(key, &*data.to_string())
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
|
@ -64,7 +64,7 @@ impl AccountData {
|
|||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
user_id: &UserId,
|
||||
kind: &str,
|
||||
kind: &EventType,
|
||||
) -> Result<Option<EventJson<EduEvent>>> {
|
||||
Ok(self.all(room_id, user_id)?.remove(kind))
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ impl AccountData {
|
|||
room_id: Option<&RoomId>,
|
||||
user_id: &UserId,
|
||||
since: u64,
|
||||
) -> Result<HashMap<String, EventJson<EduEvent>>> {
|
||||
) -> Result<HashMap<EventType, EventJson<EduEvent>>> {
|
||||
let mut userdata = HashMap::new();
|
||||
|
||||
let mut prefix = room_id
|
||||
|
@ -91,17 +91,30 @@ impl AccountData {
|
|||
let mut first_possible = prefix.clone();
|
||||
first_possible.extend_from_slice(&(since + 1).to_be_bytes());
|
||||
|
||||
for json in self
|
||||
for r in self
|
||||
.roomuserdataid_accountdata
|
||||
.range(&*first_possible..)
|
||||
.filter_map(|r| r.ok())
|
||||
.take_while(move |(k, _)| k.starts_with(&prefix))
|
||||
.map(|(_, v)| serde_json::from_slice::<serde_json::Value>(&v).unwrap())
|
||||
.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(),
|
||||
))
|
||||
})
|
||||
{
|
||||
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(
|
||||
json["type"].as_str().unwrap().to_owned(),
|
||||
serde_json::from_value::<EventJson<EduEvent>>(json)
|
||||
.expect("userdata in db is valid"),
|
||||
kind,
|
||||
serde_json::from_value::<EventJson<EduEvent>>(json.into())?,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -113,7 +126,7 @@ impl AccountData {
|
|||
&self,
|
||||
room_id: Option<&RoomId>,
|
||||
user_id: &UserId,
|
||||
) -> Result<HashMap<String, EventJson<EduEvent>>> {
|
||||
) -> Result<HashMap<EventType, EventJson<EduEvent>>> {
|
||||
self.changes_since(room_id, user_id, 0)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue