wip: refactor

This commit is contained in:
2024-12-26 12:55:51 +07:00
parent 5e5f286cfe
commit 7fd9f22b4a
8 changed files with 190 additions and 139 deletions

View File

@@ -1,8 +1,13 @@
use gpui::*;
use nostr_sdk::prelude::*;
use std::{
collections::HashMap,
sync::{Arc, Mutex, RwLock},
};
pub struct MetadataRegistry {
seens: Vec<PublicKey>,
seens: Arc<Mutex<Vec<PublicKey>>>,
profiles: Arc<RwLock<HashMap<PublicKey, Metadata>>>,
}
impl Global for MetadataRegistry {}
@@ -13,16 +18,31 @@ impl MetadataRegistry {
}
pub fn contains(&self, public_key: PublicKey) -> bool {
self.seens.contains(&public_key)
self.seens.lock().unwrap().contains(&public_key)
}
pub fn seen(&mut self, public_key: PublicKey) {
if !self.seens.contains(&public_key) {
self.seens.push(public_key);
pub fn seen(&mut self, public_key: PublicKey, metadata: Option<Metadata>) {
let mut seens = self.seens.lock().unwrap();
if !seens.contains(&public_key) {
seens.push(public_key);
drop(seens);
if let Some(metadata) = metadata {
self.profiles.write().unwrap().insert(public_key, metadata);
}
}
}
pub fn get(&self, public_key: PublicKey) -> Option<Metadata> {
self.profiles.read().unwrap().get(&public_key).cloned()
}
fn new() -> Self {
Self { seens: Vec::new() }
let seens = Arc::new(Mutex::new(Vec::new()));
let profiles = Arc::new(RwLock::new(HashMap::new()));
Self { seens, profiles }
}
}