2020-04-02 09:14:23 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2020-04-02 12:52:15 +00:00
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use atomic::Atomic;
|
2020-04-02 09:14:23 +00:00
|
|
|
|
|
|
|
use ruma_client_api::r0::keys::{DeviceKeys, KeyAlgorithm};
|
|
|
|
use ruma_events::Algorithm;
|
2020-04-09 14:22:25 +00:00
|
|
|
use ruma_identifiers::{DeviceId, UserId};
|
2020-04-02 09:14:23 +00:00
|
|
|
|
2020-04-02 12:52:15 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-04-02 09:14:23 +00:00
|
|
|
pub struct Device {
|
2020-04-09 14:22:25 +00:00
|
|
|
user_id: Arc<UserId>,
|
|
|
|
device_id: Arc<DeviceId>,
|
2020-04-02 12:52:15 +00:00
|
|
|
algorithms: Arc<Vec<Algorithm>>,
|
|
|
|
keys: Arc<HashMap<KeyAlgorithm, String>>,
|
|
|
|
display_name: Arc<Option<String>>,
|
|
|
|
deleted: Arc<AtomicBool>,
|
|
|
|
trust_state: Arc<Atomic<TrustState>>,
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 14:07:21 +00:00
|
|
|
impl Device {
|
|
|
|
pub fn keys(&self, algorithm: &KeyAlgorithm) -> Option<&String> {
|
|
|
|
self.keys.get(algorithm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 12:52:15 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2020-04-02 09:14:23 +00:00
|
|
|
pub enum TrustState {
|
|
|
|
Verified,
|
|
|
|
BlackListed,
|
|
|
|
Ignored,
|
|
|
|
Unset,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Device {
|
2020-04-09 14:22:25 +00:00
|
|
|
pub fn device_id(&self) -> &DeviceId {
|
2020-04-02 09:14:23 +00:00
|
|
|
&self.device_id
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:22:25 +00:00
|
|
|
pub fn user_id(&self) -> &UserId {
|
2020-04-02 09:14:23 +00:00
|
|
|
&self.user_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&DeviceKeys> for Device {
|
|
|
|
fn from(device_keys: &DeviceKeys) -> Self {
|
|
|
|
let mut keys = HashMap::new();
|
|
|
|
|
|
|
|
for (key_id, key) in device_keys.keys.iter() {
|
|
|
|
let key_id = key_id.0;
|
|
|
|
keys.insert(key_id, key.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
Device {
|
2020-04-09 14:22:25 +00:00
|
|
|
user_id: Arc::new(device_keys.user_id.clone()),
|
2020-04-02 12:52:15 +00:00
|
|
|
device_id: Arc::new(device_keys.device_id.clone()),
|
|
|
|
algorithms: Arc::new(device_keys.algorithms.clone()),
|
|
|
|
keys: Arc::new(keys),
|
|
|
|
display_name: Arc::new(
|
|
|
|
device_keys
|
|
|
|
.unsigned
|
|
|
|
.as_ref()
|
|
|
|
.map(|d| d.device_display_name.clone()),
|
|
|
|
),
|
|
|
|
deleted: Arc::new(AtomicBool::new(false)),
|
|
|
|
trust_state: Arc::new(Atomic::new(TrustState::Unset)),
|
2020-04-02 09:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|