diff --git a/src/globals.rs b/src/globals.rs index 67c026cd..b6534d75 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -57,7 +57,6 @@ pub struct Globals { pub desired_events: RwLock>>, /// All nostr people records currently loaded into memory, keyed by pubkey - //pub people: RwLock>, pub people: RwLock, /// All nostr relay records we have diff --git a/src/overlord/mod.rs b/src/overlord/mod.rs index 130bb25d..1e2c1fb8 100644 --- a/src/overlord/mod.rs +++ b/src/overlord/mod.rs @@ -174,7 +174,7 @@ impl Overlord { .get_followed_pubkeys() .await .iter() - .map(|p| PublicKeyHex::from(*p)) + .map(|p| p.to_owned()) .collect(); let (num_relays_per_person, max_relays) = { @@ -593,7 +593,7 @@ impl Overlord { .write() .await .upsert_valid_nip05( - (*pubkey).into(), + &(*pubkey).into(), dns_id.clone(), Unixtime::now().unwrap().0 as u64, ) @@ -604,7 +604,7 @@ impl Overlord { .people .write() .await - .follow((*pubkey).into()) + .follow(&(*pubkey).into()) .await?; info!("Followed {}", &dns_id); @@ -639,7 +639,7 @@ impl Overlord { async fn follow_bech32(bech32: String, relay: String) -> Result<(), Error> { let pk = PublicKey::try_from_bech32_string(&bech32)?; let pkhex: PublicKeyHex = pk.into(); - GLOBALS.people.write().await.follow(pkhex.clone()).await?; + GLOBALS.people.write().await.follow(&pkhex).await?; debug!("Followed {}", &pkhex); @@ -667,7 +667,7 @@ impl Overlord { async fn follow_hexkey(hexkey: String, relay: String) -> Result<(), Error> { let pk = PublicKey::try_from_hex_string(&hexkey)?; let pkhex: PublicKeyHex = pk.into(); - GLOBALS.people.write().await.follow(pkhex.clone()).await?; + GLOBALS.people.write().await.follow(&pkhex).await?; debug!("Followed {}", &pkhex); diff --git a/src/people/mod.rs b/src/people/mod.rs index bf89d521..3b2c278f 100644 --- a/src/people/mod.rs +++ b/src/people/mod.rs @@ -1,14 +1,14 @@ use crate::db::DbPerson; use crate::error::Error; use crate::globals::GLOBALS; -use nostr_types::{Metadata, PublicKey, PublicKeyHex, Unixtime}; +use nostr_types::{Metadata, PublicKeyHex, Unixtime}; use std::cmp::Ordering; use std::collections::{HashMap, HashSet}; use tokio::task; pub struct People { - people: HashMap, - deferred_load: HashSet, + people: HashMap, + deferred_load: HashSet, } impl People { @@ -19,31 +19,29 @@ impl People { } } - pub async fn get_followed_pubkeys(&self) -> Vec { - let mut output: Vec = Vec::new(); + pub async fn get_followed_pubkeys(&self) -> Vec { + let mut output: Vec = Vec::new(); for (_, person) in self.people.iter() { - if let Ok(pubkey) = PublicKey::try_from_hex_string(&person.pubkey.0) { - output.push(pubkey); - } + output.push(person.pubkey.clone()); } output } - pub async fn create_if_missing(&mut self, pubkey: PublicKey) -> Result<(), Error> { - if self.people.contains_key(&pubkey) { + pub async fn create_if_missing(&mut self, pubkeyhex: &PublicKeyHex) -> Result<(), Error> { + if self.people.contains_key(pubkeyhex) { return Ok(()); } // Try loading from the database - let maybe_dbperson = Self::fetch_one(pubkey.into()).await?; + let maybe_dbperson = Self::fetch_one(pubkeyhex).await?; if let Some(dbperson) = maybe_dbperson { // Insert into the map - self.people.insert(pubkey, dbperson); + self.people.insert(pubkeyhex.to_owned(), dbperson); } else { // Create new let dbperson = DbPerson { - pubkey: pubkey.into(), + pubkey: pubkeyhex.to_owned(), name: None, about: None, picture: None, @@ -54,7 +52,7 @@ impl People { followed: 0, }; // Insert into the map - self.people.insert(pubkey, dbperson.clone()); + self.people.insert(pubkeyhex.to_owned(), dbperson.clone()); // Insert into the database Self::insert(dbperson).await?; } @@ -64,15 +62,15 @@ impl People { pub async fn update_metadata( &mut self, - pubkey: PublicKey, + pubkeyhex: &PublicKeyHex, metadata: Metadata, asof: Unixtime, ) -> Result<(), Error> { // Sync in from database first - self.create_if_missing(pubkey).await?; + self.create_if_missing(pubkeyhex).await?; // Update the map - let person = self.people.get_mut(&pubkey).unwrap(); + let person = self.people.get_mut(pubkeyhex).unwrap(); if let Some(metadata_at) = person.metadata_at { if asof.0 <= metadata_at { // Old metadata. Ignore it @@ -91,7 +89,7 @@ impl People { // Update the database let person = person.clone(); - let pubkeyhex: PublicKeyHex = person.pubkey.clone(); + let pubkeyhex2 = pubkeyhex.to_owned(); task::spawn_blocking(move || { let maybe_db = GLOBALS.db.blocking_lock(); let db = maybe_db.as_ref().unwrap(); @@ -105,7 +103,7 @@ impl People { &person.picture, &person.dns_id, &person.metadata_at, - &pubkeyhex.0, + &pubkeyhex2.0, ))?; Ok::<(), Error>(()) }) @@ -154,21 +152,19 @@ impl People { .await?; for person in output? { - if let Ok(pubkey) = PublicKey::try_from_hex_string(&person.pubkey.0) { - self.people.insert(pubkey, person); - } + self.people.insert(person.pubkey.clone(), person); } Ok(()) } - pub fn get(&mut self, pubkey: PublicKey) -> Option { - if self.people.contains_key(&pubkey) { - self.people.get(&pubkey).cloned() + pub fn get(&mut self, pubkeyhex: &PublicKeyHex) -> Option { + if self.people.contains_key(pubkeyhex) { + self.people.get(pubkeyhex).cloned() } else { // Not there. Maybe it's in the database. Defer and let syncer // try to load - self.deferred_load.insert(pubkey); + self.deferred_load.insert(pubkeyhex.to_owned()); let _ = GLOBALS.to_syncer.send("sync_people".to_owned()); None } @@ -188,10 +184,10 @@ impl People { } pub async fn sync(&mut self) -> Result<(), Error> { - for pubkey in self.deferred_load.iter() { - if !self.people.contains_key(pubkey) { - if let Some(person) = Self::fetch_one((*pubkey).into()).await? { - let _ = self.people.insert(*pubkey, person); + for pubkeyhex in self.deferred_load.iter() { + if !self.people.contains_key(pubkeyhex) { + if let Some(person) = Self::fetch_one(pubkeyhex).await? { + let _ = self.people.insert(pubkeyhex.to_owned(), person); } } } @@ -214,11 +210,11 @@ impl People { Ok(()) } - pub async fn follow(&mut self, pubkeyhex: PublicKeyHex) -> Result<(), Error> { + pub async fn follow(&mut self, pubkeyhex: &PublicKeyHex) -> Result<(), Error> { // Follow in database let sql = "INSERT INTO PERSON (pubkey, followed) values (?, 1) \ ON CONFLICT(pubkey) DO UPDATE SET followed=1"; - let pubkeyhex2 = pubkeyhex.clone(); + let pubkeyhex2 = pubkeyhex.to_owned(); task::spawn_blocking(move || { let maybe_db = GLOBALS.db.blocking_lock(); let db = maybe_db.as_ref().unwrap(); @@ -229,13 +225,12 @@ impl People { .await??; // Make sure memory matches - let pubkey: PublicKey = PublicKey::try_from_hex_string(&pubkeyhex.0)?; - if let Some(dbperson) = self.people.get_mut(&pubkey) { + if let Some(dbperson) = self.people.get_mut(pubkeyhex) { dbperson.followed = 1; } else { // load if let Some(person) = Self::fetch_one(pubkeyhex).await? { - self.people.insert(pubkey, person); + self.people.insert(pubkeyhex.to_owned(), person); } } @@ -244,13 +239,12 @@ impl People { pub async fn upsert_valid_nip05( &mut self, - pubkeyhex: PublicKeyHex, + pubkeyhex: &PublicKeyHex, dns_id: String, dns_id_last_checked: u64, ) -> Result<(), Error> { // Update memory - let pubkey: PublicKey = PublicKey::try_from_hex_string(&pubkeyhex.0)?; - if let Some(dbperson) = self.people.get_mut(&pubkey) { + if let Some(dbperson) = self.people.get_mut(pubkeyhex) { dbperson.dns_id = Some(dns_id.clone()); dbperson.dns_id_last_checked = Some(dns_id_last_checked); } @@ -260,13 +254,14 @@ impl People { values (?, ?, 1, ?) \ ON CONFLICT(pubkey) DO UPDATE SET dns_id=?, dns_id_valid=1, dns_id_last_checked=?"; + let pubkeyhex2 = pubkeyhex.to_owned(); task::spawn_blocking(move || { let maybe_db = GLOBALS.db.blocking_lock(); let db = maybe_db.as_ref().unwrap(); let mut stmt = db.prepare(sql)?; stmt.execute(( - &pubkeyhex.0, + &pubkeyhex2.0, &dns_id, &dns_id_last_checked, &dns_id, @@ -317,8 +312,8 @@ impl People { output } - async fn fetch_one(pubkey: PublicKeyHex) -> Result, Error> { - let people = Self::fetch(Some(&format!("pubkey='{}'", pubkey))).await?; + async fn fetch_one(pubkeyhex: &PublicKeyHex) -> Result, Error> { + let people = Self::fetch(Some(&format!("pubkey='{}'", pubkeyhex))).await?; if people.is_empty() { Ok(None) diff --git a/src/process.rs b/src/process.rs index d289937b..fbdd3b7f 100644 --- a/src/process.rs +++ b/src/process.rs @@ -47,7 +47,7 @@ pub async fn process_new_event( .people .write() .await - .create_if_missing(event.pubkey) + .create_if_missing(&event.pubkey.into()) .await?; // Update person_relay.last_fetched @@ -227,7 +227,7 @@ pub async fn process_new_event( .people .write() .await - .update_metadata(event.pubkey, metadata, event.created_at) + .update_metadata(&event.pubkey.into(), metadata, event.created_at) .await?; } diff --git a/src/ui/feed.rs b/src/ui/feed.rs index 0f1a2e5c..cc884fc5 100644 --- a/src/ui/feed.rs +++ b/src/ui/feed.rs @@ -7,7 +7,7 @@ use egui::{ Align, Color32, Context, Frame, Image, Label, Layout, RichText, ScrollArea, Sense, TextEdit, TextStyle, Ui, Vec2, }; -use nostr_types::{EventKind, Id, PublicKey}; +use nostr_types::{EventKind, Id, PublicKeyHex}; pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Frame, ui: &mut Ui) { let feed = GLOBALS.feed.blocking_lock().get(); @@ -166,7 +166,7 @@ fn render_post( return; } - let maybe_person = GLOBALS.people.blocking_write().get(event.pubkey); + let maybe_person = GLOBALS.people.blocking_write().get(&event.pubkey.into()); let reactions = Globals::get_reactions_sync(event.id); let replies = Globals::get_replies_sync(event.id); @@ -251,7 +251,7 @@ fn render_post( ) .clicked() { - set_person_view(app, event.pubkey); + set_person_view(app, &event.pubkey.into()); }; // Everything else next @@ -338,15 +338,15 @@ fn render_post( } } -fn set_person_view(app: &mut GossipUi, pubkey: PublicKey) { - if let Some(dbperson) = GLOBALS.people.blocking_write().get(pubkey) { +fn set_person_view(app: &mut GossipUi, pubkeyhex: &PublicKeyHex) { + if let Some(dbperson) = GLOBALS.people.blocking_write().get(pubkeyhex) { app.person_view_name = if let Some(name) = &dbperson.name { Some(name.to_string()) } else { - Some(GossipUi::pubkey_short(&pubkey)) + Some(GossipUi::hex_pubkey_short(pubkeyhex)) }; app.person_view_person = Some(dbperson); - app.person_view_pubkey = Some(pubkey); + app.person_view_pubkey = Some(pubkeyhex.to_owned()); app.page = Page::Person; } } diff --git a/src/ui/mod.rs b/src/ui/mod.rs index c7ae0538..ce4e9bb5 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -79,7 +79,7 @@ struct GossipUi { import_hex: String, replying_to: Option, hides: Vec, - person_view_pubkey: Option, + person_view_pubkey: Option, person_view_person: Option, person_view_name: Option, } @@ -221,11 +221,6 @@ impl GossipUi { ) } - pub fn pubkey_short(pubkey: &PublicKey) -> String { - let hex: PublicKeyHex = (*pubkey).into(); - Self::hex_pubkey_short(&hex) - } - pub fn pubkey_long(pubkey: &PublicKey) -> String { let hex: PublicKeyHex = (*pubkey).into(); hex.0 diff --git a/src/ui/people.rs b/src/ui/people.rs index 39554410..0f365c73 100644 --- a/src/ui/people.rs +++ b/src/ui/people.rs @@ -4,7 +4,6 @@ use crate::db::DbPerson; use crate::globals::GLOBALS; use eframe::egui; use egui::{Context, Image, RichText, ScrollArea, Sense, TextEdit, TopBottomPanel, Ui, Vec2}; -use nostr_types::PublicKey; pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui) { TopBottomPanel::top("people_menu").show(ctx, |ui| { @@ -188,14 +187,12 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fra } fn set_person_view(app: &mut GossipUi, person: &DbPerson) { - if let Ok(pk) = PublicKey::try_from_hex_string(&person.pubkey) { - app.person_view_pubkey = Some(pk); - app.person_view_person = Some(person.clone()); - app.person_view_name = if let Some(name) = &person.name { - Some(name.to_string()) - } else { - Some(GossipUi::pubkey_short(&pk)) - }; - app.page = Page::Person; - } + app.person_view_pubkey = Some(person.pubkey.clone()); + app.person_view_person = Some(person.clone()); + app.person_view_name = if let Some(name) = &person.name { + Some(name.to_string()) + } else { + Some(GossipUi::hex_pubkey_short(&person.pubkey)) + }; + app.page = Page::Person; }