fix(nostr_users): only keep latest user metadata

This commit is contained in:
xeruf 2025-01-23 15:25:56 +01:00
parent 3ed60c3457
commit 0de4e2e55d

View file

@ -2,6 +2,7 @@ use itertools::Itertools;
use nostr_sdk::{Keys, Metadata, PublicKey, Tag, Timestamp}; use nostr_sdk::{Keys, Metadata, PublicKey, Tag, Timestamp};
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
use log::debug;
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NostrUsers { pub struct NostrUsers {
@ -26,10 +27,7 @@ impl NostrUsers {
return self.users.get_key_value(&key); return self.users.get_key_value(&key);
} }
self.users.iter() self.users.iter()
.sorted_unstable_by_key(|(k, v)| match self.user_times.get(k) { .sorted_unstable_by_key(|(k, v)| self.get_user_time(k))
Some(t) => t.as_u64(),
None => Timestamp::zero().as_u64(),
})
.rev() .rev()
.find(|(k, v)| .find(|(k, v)|
// TODO regex word boundary // TODO regex word boundary
@ -50,9 +48,21 @@ impl NostrUsers {
.unwrap_or_else(|| format!("{:.6}", pubkey.to_string())) .unwrap_or_else(|| format!("{:.6}", pubkey.to_string()))
} }
fn get_user_time(&self, pubkey: &PublicKey) -> u64 {
match self.user_times.get(pubkey) {
Some(t) => t.as_u64(),
None => Timestamp::zero().as_u64(),
}
}
pub(super) fn insert(&mut self, pubkey: PublicKey, metadata: Metadata, timestamp: Timestamp) { pub(super) fn insert(&mut self, pubkey: PublicKey, metadata: Metadata, timestamp: Timestamp) {
if self.get_user_time(&pubkey) < timestamp.as_u64() {
debug!("Inserting user metadata for {}", pubkey);
self.users.insert(pubkey, metadata); self.users.insert(pubkey, metadata);
self.user_times.insert(pubkey, timestamp); self.user_times.insert(pubkey, timestamp);
} else {
debug!("Skipping older user metadata for {}", pubkey);
}
} }
pub(super) fn create(&mut self, pubkey: PublicKey) { pub(super) fn create(&mut self, pubkey: PublicKey) {