11 changed files with 1347 additions and 292 deletions
@ -0,0 +1,207 @@ |
|||||||
|
use crate::{utils, Error, Result}; |
||||||
|
use ruma::{ |
||||||
|
api::client::{ |
||||||
|
error::ErrorKind, |
||||||
|
r0::backup::{get_backup_keys::Sessions, BackupAlgorithm, KeyData}, |
||||||
|
}, |
||||||
|
identifiers::{RoomId, UserId}, |
||||||
|
}; |
||||||
|
use std::{collections::BTreeMap, convert::TryFrom}; |
||||||
|
|
||||||
|
pub struct KeyBackups { |
||||||
|
pub(super) backupid_algorithm: sled::Tree, // BackupId = UserId + Version(Count)
|
||||||
|
pub(super) backupid_etag: sled::Tree, // BackupId = UserId + Version(Count)
|
||||||
|
pub(super) backupkeyid_backup: sled::Tree, // BackupKeyId = UserId + Version + RoomId + SessionId
|
||||||
|
} |
||||||
|
|
||||||
|
impl KeyBackups { |
||||||
|
pub fn create_backup( |
||||||
|
&self, |
||||||
|
user_id: &UserId, |
||||||
|
backup_metadata: &BackupAlgorithm, |
||||||
|
globals: &super::globals::Globals, |
||||||
|
) -> Result<String> { |
||||||
|
let version = globals.next_count()?.to_string(); |
||||||
|
|
||||||
|
let mut key = user_id.to_string().as_bytes().to_vec(); |
||||||
|
key.push(0xff); |
||||||
|
key.extend_from_slice(&version.as_bytes()); |
||||||
|
|
||||||
|
self.backupid_algorithm.insert( |
||||||
|
&key, |
||||||
|
&*serde_json::to_string(backup_metadata) |
||||||
|
.expect("BackupAlgorithm::to_string always works"), |
||||||
|
)?; |
||||||
|
self.backupid_etag |
||||||
|
.insert(&key, &globals.next_count()?.to_be_bytes())?; |
||||||
|
Ok(version) |
||||||
|
} |
||||||
|
|
||||||
|
pub fn update_backup( |
||||||
|
&self, |
||||||
|
user_id: &UserId, |
||||||
|
version: &str, |
||||||
|
backup_metadata: &BackupAlgorithm, |
||||||
|
globals: &super::globals::Globals, |
||||||
|
) -> Result<String> { |
||||||
|
let mut key = user_id.to_string().as_bytes().to_vec(); |
||||||
|
key.push(0xff); |
||||||
|
key.extend_from_slice(&version.as_bytes()); |
||||||
|
|
||||||
|
if self.backupid_algorithm.get(&key)?.is_none() { |
||||||
|
return Err(Error::BadRequest( |
||||||
|
ErrorKind::NotFound, |
||||||
|
"Tried to update nonexistent backup.", |
||||||
|
)); |
||||||
|
} |
||||||
|
|
||||||
|
self.backupid_algorithm.insert( |
||||||
|
&key, |
||||||
|
&*serde_json::to_string(backup_metadata) |
||||||
|
.expect("BackupAlgorithm::to_string always works"), |
||||||
|
)?; |
||||||
|
self.backupid_etag |
||||||
|
.insert(&key, &globals.next_count()?.to_be_bytes())?; |
||||||
|
Ok(version.to_string()) |
||||||
|
} |
||||||
|
|
||||||
|
pub fn get_latest_backup(&self, user_id: &UserId) -> Result<Option<(String, BackupAlgorithm)>> { |
||||||
|
let mut prefix = user_id.to_string().as_bytes().to_vec(); |
||||||
|
prefix.push(0xff); |
||||||
|
self.backupid_algorithm |
||||||
|
.scan_prefix(&prefix) |
||||||
|
.last() |
||||||
|
.map_or(Ok(None), |r| { |
||||||
|
let (key, value) = r?; |
||||||
|
let version = utils::string_from_bytes( |
||||||
|
key.rsplit(|&b| b == 0xff) |
||||||
|
.next() |
||||||
|
.expect("rsplit always returns an element"), |
||||||
|
) |
||||||
|
.map_err(|_| Error::bad_database("backupid_algorithm key is invalid."))?; |
||||||
|
|
||||||
|
Ok(Some(( |
||||||
|
version, |
||||||
|
serde_json::from_slice(&value).map_err(|_| { |
||||||
|
Error::bad_database("Algorithm in backupid_algorithm is invalid.") |
||||||
|
})?, |
||||||
|
))) |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
pub fn get_backup(&self, user_id: &UserId, version: &str) -> Result<Option<BackupAlgorithm>> { |
||||||
|
let mut key = user_id.to_string().as_bytes().to_vec(); |
||||||
|
key.push(0xff); |
||||||
|
key.extend_from_slice(version.as_bytes()); |
||||||
|
|
||||||
|
self.backupid_algorithm.get(key)?.map_or(Ok(None), |bytes| { |
||||||
|
Ok(serde_json::from_slice(&bytes) |
||||||
|
.map_err(|_| Error::bad_database("Algorithm in backupid_algorithm is invalid."))?) |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
pub fn add_key( |
||||||
|
&self, |
||||||
|
user_id: &UserId, |
||||||
|
version: &str, |
||||||
|
room_id: &RoomId, |
||||||
|
session_id: &str, |
||||||
|
key_data: &KeyData, |
||||||
|
globals: &super::globals::Globals, |
||||||
|
) -> Result<()> { |
||||||
|
let mut key = user_id.to_string().as_bytes().to_vec(); |
||||||
|
key.push(0xff); |
||||||
|
key.extend_from_slice(version.as_bytes()); |
||||||
|
|
||||||
|
if self.backupid_algorithm.get(&key)?.is_none() { |
||||||
|
return Err(Error::BadRequest( |
||||||
|
ErrorKind::NotFound, |
||||||
|
"Tried to update nonexistent backup.", |
||||||
|
)); |
||||||
|
} |
||||||
|
|
||||||
|
self.backupid_etag |
||||||
|
.insert(&key, &globals.next_count()?.to_be_bytes())?; |
||||||
|
|
||||||
|
key.push(0xff); |
||||||
|
key.extend_from_slice(room_id.to_string().as_bytes()); |
||||||
|
key.push(0xff); |
||||||
|
key.extend_from_slice(session_id.as_bytes()); |
||||||
|
|
||||||
|
self.backupkeyid_backup.insert( |
||||||
|
&key, |
||||||
|
&*serde_json::to_string(&key_data).expect("KeyData::to_string always works"), |
||||||
|
)?; |
||||||
|
|
||||||
|
Ok(()) |
||||||
|
} |
||||||
|
|
||||||
|
pub fn count_keys(&self, user_id: &UserId, version: &str) -> Result<usize> { |
||||||
|
let mut prefix = user_id.to_string().as_bytes().to_vec(); |
||||||
|
prefix.push(0xff); |
||||||
|
prefix.extend_from_slice(version.as_bytes()); |
||||||
|
|
||||||
|
Ok(self.backupkeyid_backup.scan_prefix(&prefix).count()) |
||||||
|
} |
||||||
|
|
||||||
|
pub fn get_etag(&self, user_id: &UserId, version: &str) -> Result<String> { |
||||||
|
let mut key = user_id.to_string().as_bytes().to_vec(); |
||||||
|
key.push(0xff); |
||||||
|
key.extend_from_slice(&version.as_bytes()); |
||||||
|
|
||||||
|
Ok(utils::u64_from_bytes( |
||||||
|
&self |
||||||
|
.backupid_etag |
||||||
|
.get(&key)? |
||||||
|
.ok_or_else(|| Error::bad_database("Backup has no etag."))?, |
||||||
|
) |
||||||
|
.map_err(|_| Error::bad_database("etag in backupid_etag invalid."))? |
||||||
|
.to_string()) |
||||||
|
} |
||||||
|
|
||||||
|
pub fn get_all(&self, user_id: &UserId, version: &str) -> Result<BTreeMap<RoomId, Sessions>> { |
||||||
|
let mut prefix = user_id.to_string().as_bytes().to_vec(); |
||||||
|
prefix.push(0xff); |
||||||
|
prefix.extend_from_slice(version.as_bytes()); |
||||||
|
|
||||||
|
let mut rooms = BTreeMap::<RoomId, Sessions>::new(); |
||||||
|
|
||||||
|
for result in self.backupkeyid_backup.scan_prefix(&prefix).map(|r| { |
||||||
|
let (key, value) = r?; |
||||||
|
let mut parts = key.rsplit(|&b| b == 0xff); |
||||||
|
|
||||||
|
let session_id = utils::string_from_bytes( |
||||||
|
&parts |
||||||
|
.next() |
||||||
|
.ok_or_else(|| Error::bad_database("backupkeyid_backup key is invalid."))?, |
||||||
|
) |
||||||
|
.map_err(|_| Error::bad_database("backupkeyid_backup session_id is invalid."))?; |
||||||
|
|
||||||
|
let room_id = RoomId::try_from( |
||||||
|
utils::string_from_bytes( |
||||||
|
&parts |
||||||
|
.next() |
||||||
|
.ok_or_else(|| Error::bad_database("backupkeyid_backup key is invalid."))?, |
||||||
|
) |
||||||
|
.map_err(|_| Error::bad_database("backupkeyid_backup room_id is invalid."))?, |
||||||
|
) |
||||||
|
.map_err(|_| Error::bad_database("backupkeyid_backup room_id is invalid room id."))?; |
||||||
|
|
||||||
|
let key_data = serde_json::from_slice(&value) |
||||||
|
.map_err(|_| Error::bad_database("KeyData in backupkeyid_backup is invalid."))?; |
||||||
|
|
||||||
|
Ok::<_, Error>((room_id, session_id, key_data)) |
||||||
|
}) { |
||||||
|
let (room_id, session_id, key_data) = result?; |
||||||
|
rooms |
||||||
|
.entry(room_id) |
||||||
|
.or_insert_with(|| Sessions { |
||||||
|
sessions: BTreeMap::new(), |
||||||
|
}) |
||||||
|
.sessions |
||||||
|
.insert(session_id, key_data); |
||||||
|
} |
||||||
|
|
||||||
|
Ok(rooms) |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,230 @@ |
|||||||
|
use ruma::{ |
||||||
|
events::push_rules::{ConditionalPushRule, PatternedPushRule, PushCondition, Ruleset}, |
||||||
|
identifiers::UserId, |
||||||
|
push::{Action, Tweak}, |
||||||
|
}; |
||||||
|
|
||||||
|
pub fn default_pushrules(user_id: &UserId) -> Ruleset { |
||||||
|
Ruleset { |
||||||
|
content: vec![contains_user_name_rule(&user_id)], |
||||||
|
override_: vec![ |
||||||
|
master_rule(), |
||||||
|
suppress_notices_rule(), |
||||||
|
invite_for_me_rule(), |
||||||
|
member_event_rule(), |
||||||
|
contains_display_name_rule(), |
||||||
|
tombstone_rule(), |
||||||
|
roomnotif_rule(), |
||||||
|
], |
||||||
|
room: vec![], |
||||||
|
sender: vec![], |
||||||
|
underride: vec![ |
||||||
|
call_rule(), |
||||||
|
encrypted_room_one_to_one_rule(), |
||||||
|
room_one_to_one_rule(), |
||||||
|
message_rule(), |
||||||
|
encrypted_rule(), |
||||||
|
], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn master_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![Action::DontNotify], |
||||||
|
default: true, |
||||||
|
enabled: false, |
||||||
|
rule_id: ".m.rule.master".to_owned(), |
||||||
|
conditions: vec![], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn suppress_notices_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![Action::DontNotify], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.suppress_notices".to_owned(), |
||||||
|
conditions: vec![PushCondition::EventMatch { |
||||||
|
key: "content.msgtype".to_owned(), |
||||||
|
pattern: "m.notice".to_owned(), |
||||||
|
}], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn invite_for_me_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![ |
||||||
|
Action::Notify, |
||||||
|
Action::SetTweak(Tweak::Sound("default".to_owned())), |
||||||
|
Action::SetTweak(Tweak::Highlight(false)), |
||||||
|
], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.invite_for_me".to_owned(), |
||||||
|
conditions: vec![PushCondition::EventMatch { |
||||||
|
key: "content.membership".to_owned(), |
||||||
|
pattern: "m.invite".to_owned(), |
||||||
|
}], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn member_event_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![Action::DontNotify], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.member_event".to_owned(), |
||||||
|
conditions: vec![PushCondition::EventMatch { |
||||||
|
key: "content.membership".to_owned(), |
||||||
|
pattern: "type".to_owned(), |
||||||
|
}], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn contains_display_name_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![ |
||||||
|
Action::Notify, |
||||||
|
Action::SetTweak(Tweak::Sound("default".to_owned())), |
||||||
|
Action::SetTweak(Tweak::Highlight(true)), |
||||||
|
], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.contains_display_name".to_owned(), |
||||||
|
conditions: vec![PushCondition::ContainsDisplayName], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn tombstone_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(true))], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.tombstone".to_owned(), |
||||||
|
conditions: vec![ |
||||||
|
PushCondition::EventMatch { |
||||||
|
key: "type".to_owned(), |
||||||
|
pattern: "m.room.tombstone".to_owned(), |
||||||
|
}, |
||||||
|
PushCondition::EventMatch { |
||||||
|
key: "state_key".to_owned(), |
||||||
|
pattern: "".to_owned(), |
||||||
|
}, |
||||||
|
], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn roomnotif_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(true))], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.roomnotif".to_owned(), |
||||||
|
conditions: vec![ |
||||||
|
PushCondition::EventMatch { |
||||||
|
key: "content.body".to_owned(), |
||||||
|
pattern: "@room".to_owned(), |
||||||
|
}, |
||||||
|
PushCondition::SenderNotificationPermission { |
||||||
|
key: "room".to_owned(), |
||||||
|
}, |
||||||
|
], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn contains_user_name_rule(user_id: &UserId) -> PatternedPushRule { |
||||||
|
PatternedPushRule { |
||||||
|
actions: vec![ |
||||||
|
Action::Notify, |
||||||
|
Action::SetTweak(Tweak::Sound("default".to_owned())), |
||||||
|
Action::SetTweak(Tweak::Highlight(true)), |
||||||
|
], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.contains_user_name".to_owned(), |
||||||
|
pattern: user_id.localpart().to_owned(), |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn call_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![ |
||||||
|
Action::Notify, |
||||||
|
Action::SetTweak(Tweak::Sound("ring".to_owned())), |
||||||
|
Action::SetTweak(Tweak::Highlight(false)), |
||||||
|
], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.call".to_owned(), |
||||||
|
conditions: vec![PushCondition::EventMatch { |
||||||
|
key: "type".to_owned(), |
||||||
|
pattern: "m.call.invite".to_owned(), |
||||||
|
}], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn encrypted_room_one_to_one_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![ |
||||||
|
Action::Notify, |
||||||
|
Action::SetTweak(Tweak::Sound("default".to_owned())), |
||||||
|
Action::SetTweak(Tweak::Highlight(false)), |
||||||
|
], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.encrypted_room_one_to_one".to_owned(), |
||||||
|
conditions: vec![ |
||||||
|
PushCondition::RoomMemberCount { is: "2".to_owned() }, |
||||||
|
PushCondition::EventMatch { |
||||||
|
key: "type".to_owned(), |
||||||
|
pattern: "m.room.encrypted".to_owned(), |
||||||
|
}, |
||||||
|
], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn room_one_to_one_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![ |
||||||
|
Action::Notify, |
||||||
|
Action::SetTweak(Tweak::Sound("default".to_owned())), |
||||||
|
Action::SetTweak(Tweak::Highlight(false)), |
||||||
|
], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.room_one_to_one".to_owned(), |
||||||
|
conditions: vec![ |
||||||
|
PushCondition::RoomMemberCount { is: "2".to_owned() }, |
||||||
|
PushCondition::EventMatch { |
||||||
|
key: "type".to_owned(), |
||||||
|
pattern: "m.room.message".to_owned(), |
||||||
|
}, |
||||||
|
], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn message_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(false))], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.message".to_owned(), |
||||||
|
conditions: vec![PushCondition::EventMatch { |
||||||
|
key: "type".to_owned(), |
||||||
|
pattern: "m.room.message".to_owned(), |
||||||
|
}], |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
pub fn encrypted_rule() -> ConditionalPushRule { |
||||||
|
ConditionalPushRule { |
||||||
|
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(false))], |
||||||
|
default: true, |
||||||
|
enabled: true, |
||||||
|
rule_id: ".m.rule.encrypted".to_owned(), |
||||||
|
conditions: vec![PushCondition::EventMatch { |
||||||
|
key: "type".to_owned(), |
||||||
|
pattern: "m.room.encrypted".to_owned(), |
||||||
|
}], |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue