2020-03-24 16:25:01 +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-03-26 10:22:40 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-04-02 12:52:15 +00:00
|
|
|
use dashmap::{DashMap, ReadOnlyView};
|
2020-03-26 10:22:40 +00:00
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
2020-04-02 12:52:15 +00:00
|
|
|
use super::device::Device;
|
2020-03-26 10:22:40 +00:00
|
|
|
use super::olm::{InboundGroupSession, Session};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SessionStore {
|
2020-03-30 15:07:36 +00:00
|
|
|
entries: HashMap<String, Arc<Mutex<Vec<Arc<Mutex<Session>>>>>>,
|
2020-03-26 10:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SessionStore {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
SessionStore {
|
2020-03-27 11:09:54 +00:00
|
|
|
entries: HashMap::new(),
|
2020-03-26 10:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 14:19:08 +00:00
|
|
|
pub async fn add(&mut self, session: Session) -> Arc<Mutex<Session>> {
|
2020-03-27 11:09:54 +00:00
|
|
|
if !self.entries.contains_key(&session.sender_key) {
|
2020-03-30 15:07:36 +00:00
|
|
|
self.entries.insert(
|
|
|
|
session.sender_key.to_owned(),
|
|
|
|
Arc::new(Mutex::new(Vec::new())),
|
|
|
|
);
|
2020-03-26 10:22:40 +00:00
|
|
|
}
|
2020-03-27 11:09:54 +00:00
|
|
|
let mut sessions = self.entries.get_mut(&session.sender_key).unwrap();
|
2020-03-31 14:19:08 +00:00
|
|
|
let session = Arc::new(Mutex::new(session));
|
|
|
|
sessions.lock().await.push(session.clone());
|
|
|
|
|
|
|
|
session
|
2020-03-27 11:09:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 15:07:36 +00:00
|
|
|
pub fn get(&self, sender_key: &str) -> Option<Arc<Mutex<Vec<Arc<Mutex<Session>>>>>> {
|
|
|
|
self.entries.get(sender_key).cloned()
|
2020-03-26 10:22:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:09:54 +00:00
|
|
|
pub fn set_for_sender(&mut self, sender_key: &str, sessions: Vec<Arc<Mutex<Session>>>) {
|
2020-03-30 15:07:36 +00:00
|
|
|
self.entries
|
|
|
|
.insert(sender_key.to_owned(), Arc::new(Mutex::new(sessions)));
|
2020-03-26 10:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-24 16:25:01 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct GroupSessionStore {
|
2020-03-26 10:22:40 +00:00
|
|
|
entries: HashMap<String, HashMap<String, HashMap<String, Arc<Mutex<InboundGroupSession>>>>>,
|
2020-03-24 16:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GroupSessionStore {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
GroupSessionStore {
|
|
|
|
entries: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:32:40 +00:00
|
|
|
pub fn add(&mut self, session: InboundGroupSession) -> bool {
|
|
|
|
if !self.entries.contains_key(&session.room_id) {
|
|
|
|
self.entries
|
|
|
|
.insert(session.room_id.to_owned(), HashMap::new());
|
2020-03-24 16:25:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 10:32:40 +00:00
|
|
|
let mut room_map = self.entries.get_mut(&session.room_id).unwrap();
|
2020-03-24 16:25:01 +00:00
|
|
|
|
2020-03-25 10:32:40 +00:00
|
|
|
if !room_map.contains_key(&session.sender_key) {
|
|
|
|
room_map.insert(session.sender_key.to_owned(), HashMap::new());
|
2020-03-24 16:25:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 10:32:40 +00:00
|
|
|
let mut sender_map = room_map.get_mut(&session.sender_key).unwrap();
|
2020-03-26 10:22:40 +00:00
|
|
|
let ret = sender_map.insert(session.session_id(), Arc::new(Mutex::new(session)));
|
2020-03-24 16:25:01 +00:00
|
|
|
|
|
|
|
ret.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(
|
|
|
|
&self,
|
|
|
|
room_id: &str,
|
|
|
|
sender_key: &str,
|
|
|
|
session_id: &str,
|
2020-03-26 10:22:40 +00:00
|
|
|
) -> Option<Arc<Mutex<InboundGroupSession>>> {
|
2020-03-24 16:25:01 +00:00
|
|
|
self.entries
|
|
|
|
.get(room_id)
|
2020-03-26 10:22:40 +00:00
|
|
|
.and_then(|m| m.get(sender_key).and_then(|m| m.get(session_id).cloned()))
|
2020-03-24 16:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-02 12:52:15 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DeviceStore {
|
|
|
|
entries: DashMap<String, DashMap<String, Device>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UserDevices {
|
|
|
|
entries: ReadOnlyView<String, Device>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserDevices {
|
|
|
|
pub fn get(&self, device_id: &str) -> Option<Device> {
|
|
|
|
self.entries.get(device_id).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn keys(&self) -> impl Iterator<Item = &String> {
|
|
|
|
self.entries.keys()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeviceStore {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
DeviceStore {
|
|
|
|
entries: DashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add(&self, device: Device) -> bool {
|
|
|
|
if !self.entries.contains_key(device.user_id()) {
|
|
|
|
self.entries
|
|
|
|
.insert(device.user_id().to_owned(), DashMap::new());
|
|
|
|
}
|
|
|
|
let mut device_map = self.entries.get_mut(device.user_id()).unwrap();
|
|
|
|
|
|
|
|
device_map
|
|
|
|
.insert(device.device_id().to_owned(), device)
|
|
|
|
.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, user_id: &str, device_id: &str) -> Option<Device> {
|
|
|
|
self.entries
|
|
|
|
.get(user_id)
|
|
|
|
.and_then(|m| m.get(device_id).map(|d| d.value().clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn user_devices(&self, user_id: &str) -> UserDevices {
|
|
|
|
if !self.entries.contains_key(user_id) {
|
|
|
|
self.entries.insert(user_id.to_owned(), DashMap::new());
|
|
|
|
}
|
|
|
|
UserDevices {
|
|
|
|
entries: self.entries.get(user_id).unwrap().clone().into_read_only(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|