enhance: sort users by newest first for name matching

This commit is contained in:
xeruf 2025-01-23 14:59:26 +01:00
parent 942e2fca75
commit 8c1902c1d3
2 changed files with 22 additions and 12 deletions

View file

@ -158,7 +158,7 @@ impl TasksRelay {
metadata: Option<Metadata>, metadata: Option<Metadata>,
) -> Self { ) -> Self {
let mut new = Self::with_sender(EventSender::from(url, tx, keys)); let mut new = Self::with_sender(EventSender::from(url, tx, keys));
metadata.map(|m| new.users.insert(keys.public_key(), m)); metadata.map(|m| new.users.insert(keys.public_key(), m, Timestamp::now()));
new new
} }
@ -1228,7 +1228,7 @@ impl TasksRelay {
match event.kind { match event.kind {
Kind::GitIssue => self.add_task(event), Kind::GitIssue => self.add_task(event),
Kind::Metadata => match Metadata::from_json(event.content.as_str()) { Kind::Metadata => match Metadata::from_json(event.content.as_str()) {
Ok(metadata) => { self.users.insert(event.pubkey, metadata); } Ok(metadata) => { self.users.insert(event.pubkey, metadata, event.created_at); }
Err(e) => warn!("Cannot parse metadata: {} from {:?}", e, event), Err(e) => warn!("Cannot parse metadata: {} from {:?}", e, event),
}, },
Kind::Bookmarks => { Kind::Bookmarks => {

View file

@ -1,10 +1,12 @@
use nostr_sdk::{Keys, Metadata, PublicKey, Tag}; use itertools::Itertools;
use nostr_sdk::{Keys, Metadata, PublicKey, Tag, Timestamp};
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NostrUsers { pub struct NostrUsers {
users: HashMap<PublicKey, Metadata>, users: HashMap<PublicKey, Metadata>,
user_times: HashMap<PublicKey, Timestamp>,
} }
impl NostrUsers { impl NostrUsers {
@ -23,7 +25,13 @@ impl NostrUsers {
if let Ok(key) = PublicKey::from_str(term) { if let Ok(key) = PublicKey::from_str(term) {
return self.users.get_key_value(&key); return self.users.get_key_value(&key);
} }
self.users.iter().find(|(k, v)| self.users.iter()
.sorted_unstable_by_key(|(k, v)| match self.user_times.get(k) {
Some(t) => t.as_u64(),
None => Timestamp::zero().as_u64(),
})
.rev()
.find(|(k, v)|
// TODO regex word boundary // TODO regex word boundary
v.name.as_ref().is_some_and(|n| n.to_ascii_lowercase().starts_with(term)) || v.name.as_ref().is_some_and(|n| n.to_ascii_lowercase().starts_with(term)) ||
v.display_name.as_ref().is_some_and(|n| n.to_ascii_lowercase().starts_with(term)) || v.display_name.as_ref().is_some_and(|n| n.to_ascii_lowercase().starts_with(term)) ||
@ -38,12 +46,14 @@ impl NostrUsers {
pub(crate) fn get_username(&self, pubkey: &PublicKey) -> String { pub(crate) fn get_username(&self, pubkey: &PublicKey) -> String {
self.users.get(pubkey) self.users.get(pubkey)
.and_then(|m| m.name.clone()) .and_then(|m| m.name.as_ref())
.unwrap_or_else(|| format!("{:.6}", pubkey.to_string())) .map_or_else(|| format!("{:.6}", pubkey.to_string()),
|m| format!("{}@{:.6}", m, pubkey.to_string()))
} }
pub(super) fn insert(&mut self, pubkey: PublicKey, metadata: Metadata) { pub(super) fn insert(&mut self, pubkey: PublicKey, metadata: Metadata, timestamp: Timestamp) {
self.users.insert(pubkey, metadata); self.users.insert(pubkey, metadata);
self.user_times.insert(pubkey, timestamp);
} }
pub(super) fn create(&mut self, pubkey: PublicKey) { pub(super) fn create(&mut self, pubkey: PublicKey) {
@ -57,7 +67,7 @@ impl NostrUsers {
fn test_user_extract() { fn test_user_extract() {
let keys = Keys::generate(); let keys = Keys::generate();
let mut users = NostrUsers::default(); let mut users = NostrUsers::default();
users.insert(keys.public_key, Metadata::new().display_name("Tester Jo")); users.insert(keys.public_key, Metadata::new().display_name("Tester Jo"), Timestamp::now());
assert_eq!(crate::kinds::extract_tags("Hello @test", &users), assert_eq!(crate::kinds::extract_tags("Hello @test", &users),
("Hello".to_string(), vec![Tag::public_key(keys.public_key)])); ("Hello".to_string(), vec![Tag::public_key(keys.public_key)]));
} }