2026-05-19 11:54:54 -07:00
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
fmt::Debug,
|
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-04 10:09:29 +08:00
|
|
|
use libchat::{IdentityProvider, RegistrationService};
|
|
|
|
|
|
|
|
|
|
pub mod http;
|
2026-05-19 11:54:54 -07:00
|
|
|
|
|
|
|
|
/// A Contact Registry used for Tests.
|
|
|
|
|
/// This implementation stores bundle bytes and then returns them when
|
|
|
|
|
/// retrieved
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct EphemeralRegistry {
|
|
|
|
|
registry: Arc<Mutex<HashMap<String, Vec<u8>>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl EphemeralRegistry {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
registry: Arc::new(Mutex::new(HashMap::new())),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for EphemeralRegistry {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Debug for EphemeralRegistry {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
let registry = self.registry.lock().unwrap();
|
|
|
|
|
let truncated: Vec<(&String, String)> = registry
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(k, v)| {
|
|
|
|
|
let hex = if v.len() <= 8 {
|
|
|
|
|
hex::encode(v)
|
|
|
|
|
} else {
|
|
|
|
|
format!(
|
|
|
|
|
"{}..{}",
|
|
|
|
|
hex::encode(&v[..4]),
|
|
|
|
|
hex::encode(&v[v.len() - 4..])
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
(k, hex)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
f.debug_struct("EphemeralRegistry")
|
|
|
|
|
.field("registry", &truncated)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RegistrationService for EphemeralRegistry {
|
|
|
|
|
type Error = String;
|
|
|
|
|
|
2026-06-04 10:09:29 +08:00
|
|
|
fn register(
|
|
|
|
|
&mut self,
|
|
|
|
|
identity: &dyn IdentityProvider,
|
|
|
|
|
key_bundle: Vec<u8>,
|
|
|
|
|
) -> Result<(), Self::Error> {
|
2026-05-19 11:54:54 -07:00
|
|
|
self.registry
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
2026-06-04 10:09:29 +08:00
|
|
|
.insert(identity.account_id().to_string(), key_bundle);
|
2026-05-19 11:54:54 -07:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 10:09:29 +08:00
|
|
|
fn retrieve(&self, device_id: &str) -> Result<Option<Vec<u8>>, Self::Error> {
|
|
|
|
|
Ok(self.registry.lock().unwrap().get(device_id).cloned())
|
2026-05-19 11:54:54 -07:00
|
|
|
}
|
|
|
|
|
}
|