|
|
|
@ -6,7 +6,7 @@ use ruma::{ |
|
|
|
keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey}, |
|
|
|
keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
events::{to_device::AnyToDeviceEvent, EventJson, EventType}, |
|
|
|
events::{to_device::AnyToDeviceEvent, EventJson, EventType}, |
|
|
|
identifiers::{DeviceId, UserId}, |
|
|
|
identifiers::UserId, |
|
|
|
}; |
|
|
|
}; |
|
|
|
use std::{collections::BTreeMap, convert::TryFrom, time::SystemTime}; |
|
|
|
use std::{collections::BTreeMap, convert::TryFrom, time::SystemTime}; |
|
|
|
|
|
|
|
|
|
|
|
@ -113,7 +113,7 @@ impl Users { |
|
|
|
pub fn create_device( |
|
|
|
pub fn create_device( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
user_id: &UserId, |
|
|
|
user_id: &UserId, |
|
|
|
device_id: &DeviceId, |
|
|
|
device_id: &str, |
|
|
|
token: &str, |
|
|
|
token: &str, |
|
|
|
initial_device_display_name: Option<String>, |
|
|
|
initial_device_display_name: Option<String>, |
|
|
|
) -> Result<()> { |
|
|
|
) -> Result<()> { |
|
|
|
@ -130,7 +130,7 @@ impl Users { |
|
|
|
self.userdeviceid_metadata.insert( |
|
|
|
self.userdeviceid_metadata.insert( |
|
|
|
userdeviceid, |
|
|
|
userdeviceid, |
|
|
|
serde_json::to_string(&Device { |
|
|
|
serde_json::to_string(&Device { |
|
|
|
device_id: device_id.clone(), |
|
|
|
device_id: device_id.to_owned(), |
|
|
|
display_name: initial_device_display_name, |
|
|
|
display_name: initial_device_display_name, |
|
|
|
last_seen_ip: None, // TODO
|
|
|
|
last_seen_ip: None, // TODO
|
|
|
|
last_seen_ts: Some(SystemTime::now()), |
|
|
|
last_seen_ts: Some(SystemTime::now()), |
|
|
|
@ -144,7 +144,7 @@ impl Users { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Removes a device from a user.
|
|
|
|
/// Removes a device from a user.
|
|
|
|
pub fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> { |
|
|
|
pub fn remove_device(&self, user_id: &UserId, device_id: &str) -> Result<()> { |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
userdeviceid.push(0xff); |
|
|
|
userdeviceid.push(0xff); |
|
|
|
userdeviceid.extend_from_slice(device_id.as_bytes()); |
|
|
|
userdeviceid.extend_from_slice(device_id.as_bytes()); |
|
|
|
@ -173,7 +173,7 @@ impl Users { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over all device ids of this user.
|
|
|
|
/// Returns an iterator over all device ids of this user.
|
|
|
|
pub fn all_device_ids(&self, user_id: &UserId) -> impl Iterator<Item = Result<DeviceId>> { |
|
|
|
pub fn all_device_ids(&self, user_id: &UserId) -> impl Iterator<Item = Result<String>> { |
|
|
|
let mut prefix = user_id.to_string().as_bytes().to_vec(); |
|
|
|
let mut prefix = user_id.to_string().as_bytes().to_vec(); |
|
|
|
prefix.push(0xff); |
|
|
|
prefix.push(0xff); |
|
|
|
// All devices have metadata
|
|
|
|
// All devices have metadata
|
|
|
|
@ -191,7 +191,7 @@ impl Users { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Replaces the access token of one device.
|
|
|
|
/// Replaces the access token of one device.
|
|
|
|
pub fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> { |
|
|
|
pub fn set_token(&self, user_id: &UserId, device_id: &str, token: &str) -> Result<()> { |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
userdeviceid.push(0xff); |
|
|
|
userdeviceid.push(0xff); |
|
|
|
userdeviceid.extend_from_slice(device_id.as_bytes()); |
|
|
|
userdeviceid.extend_from_slice(device_id.as_bytes()); |
|
|
|
@ -219,7 +219,7 @@ impl Users { |
|
|
|
pub fn add_one_time_key( |
|
|
|
pub fn add_one_time_key( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
user_id: &UserId, |
|
|
|
user_id: &UserId, |
|
|
|
device_id: &DeviceId, |
|
|
|
device_id: &str, |
|
|
|
one_time_key_key: &AlgorithmAndDeviceId, |
|
|
|
one_time_key_key: &AlgorithmAndDeviceId, |
|
|
|
one_time_key_value: &OneTimeKey, |
|
|
|
one_time_key_value: &OneTimeKey, |
|
|
|
) -> Result<()> { |
|
|
|
) -> Result<()> { |
|
|
|
@ -248,7 +248,7 @@ impl Users { |
|
|
|
pub fn take_one_time_key( |
|
|
|
pub fn take_one_time_key( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
user_id: &UserId, |
|
|
|
user_id: &UserId, |
|
|
|
device_id: &DeviceId, |
|
|
|
device_id: &str, |
|
|
|
key_algorithm: &KeyAlgorithm, |
|
|
|
key_algorithm: &KeyAlgorithm, |
|
|
|
) -> Result<Option<(AlgorithmAndDeviceId, OneTimeKey)>> { |
|
|
|
) -> Result<Option<(AlgorithmAndDeviceId, OneTimeKey)>> { |
|
|
|
let mut prefix = user_id.to_string().as_bytes().to_vec(); |
|
|
|
let mut prefix = user_id.to_string().as_bytes().to_vec(); |
|
|
|
@ -282,7 +282,7 @@ impl Users { |
|
|
|
pub fn count_one_time_keys( |
|
|
|
pub fn count_one_time_keys( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
user_id: &UserId, |
|
|
|
user_id: &UserId, |
|
|
|
device_id: &DeviceId, |
|
|
|
device_id: &str, |
|
|
|
) -> Result<BTreeMap<KeyAlgorithm, UInt>> { |
|
|
|
) -> Result<BTreeMap<KeyAlgorithm, UInt>> { |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
userdeviceid.push(0xff); |
|
|
|
userdeviceid.push(0xff); |
|
|
|
@ -315,7 +315,7 @@ impl Users { |
|
|
|
pub fn add_device_keys( |
|
|
|
pub fn add_device_keys( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
user_id: &UserId, |
|
|
|
user_id: &UserId, |
|
|
|
device_id: &DeviceId, |
|
|
|
device_id: &str, |
|
|
|
device_keys: &DeviceKeys, |
|
|
|
device_keys: &DeviceKeys, |
|
|
|
globals: &super::globals::Globals, |
|
|
|
globals: &super::globals::Globals, |
|
|
|
) -> Result<()> { |
|
|
|
) -> Result<()> { |
|
|
|
@ -335,7 +335,7 @@ impl Users { |
|
|
|
pub fn get_device_keys( |
|
|
|
pub fn get_device_keys( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
user_id: &UserId, |
|
|
|
user_id: &UserId, |
|
|
|
device_id: &DeviceId, |
|
|
|
device_id: &str, |
|
|
|
) -> impl Iterator<Item = Result<DeviceKeys>> { |
|
|
|
) -> impl Iterator<Item = Result<DeviceKeys>> { |
|
|
|
let mut key = user_id.to_string().as_bytes().to_vec(); |
|
|
|
let mut key = user_id.to_string().as_bytes().to_vec(); |
|
|
|
key.push(0xff); |
|
|
|
key.push(0xff); |
|
|
|
@ -376,7 +376,7 @@ impl Users { |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
sender: &UserId, |
|
|
|
sender: &UserId, |
|
|
|
target_user_id: &UserId, |
|
|
|
target_user_id: &UserId, |
|
|
|
target_device_id: &DeviceId, |
|
|
|
target_device_id: &str, |
|
|
|
event_type: &EventType, |
|
|
|
event_type: &EventType, |
|
|
|
content: serde_json::Value, |
|
|
|
content: serde_json::Value, |
|
|
|
globals: &super::globals::Globals, |
|
|
|
globals: &super::globals::Globals, |
|
|
|
@ -401,7 +401,7 @@ impl Users { |
|
|
|
pub fn take_to_device_events( |
|
|
|
pub fn take_to_device_events( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
user_id: &UserId, |
|
|
|
user_id: &UserId, |
|
|
|
device_id: &DeviceId, |
|
|
|
device_id: &str, |
|
|
|
max: usize, |
|
|
|
max: usize, |
|
|
|
) -> Result<Vec<EventJson<AnyToDeviceEvent>>> { |
|
|
|
) -> Result<Vec<EventJson<AnyToDeviceEvent>>> { |
|
|
|
let mut events = Vec::new(); |
|
|
|
let mut events = Vec::new(); |
|
|
|
@ -423,7 +423,7 @@ impl Users { |
|
|
|
pub fn update_device_metadata( |
|
|
|
pub fn update_device_metadata( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
user_id: &UserId, |
|
|
|
user_id: &UserId, |
|
|
|
device_id: &DeviceId, |
|
|
|
device_id: &str, |
|
|
|
device: &Device, |
|
|
|
device: &Device, |
|
|
|
) -> Result<()> { |
|
|
|
) -> Result<()> { |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
@ -441,11 +441,7 @@ impl Users { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Get device metadata.
|
|
|
|
/// Get device metadata.
|
|
|
|
pub fn get_device_metadata( |
|
|
|
pub fn get_device_metadata(&self, user_id: &UserId, device_id: &str) -> Result<Option<Device>> { |
|
|
|
&self, |
|
|
|
|
|
|
|
user_id: &UserId, |
|
|
|
|
|
|
|
device_id: &DeviceId, |
|
|
|
|
|
|
|
) -> Result<Option<Device>> { |
|
|
|
|
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); |
|
|
|
userdeviceid.push(0xff); |
|
|
|
userdeviceid.push(0xff); |
|
|
|
userdeviceid.extend_from_slice(device_id.as_bytes()); |
|
|
|
userdeviceid.extend_from_slice(device_id.as_bytes()); |
|
|
|
|