Update for nostr-types update

This commit is contained in:
Mike Dilger 2023-02-03 14:50:47 +13:00
parent 26bb213055
commit de47722315
11 changed files with 95 additions and 75 deletions

View File

@ -28,21 +28,20 @@ impl DbEvent {
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare(&sql)?;
let rows = stmt.query_map([], |row| {
Ok(DbEvent {
id: IdHex(row.get(0)?),
let mut rows = stmt.query([])?;
let mut output: Vec<DbEvent> = Vec::new();
while let Some(row) = rows.next()? {
let id: String = row.get(0)?;
let pk: String = row.get(2)?;
output.push(DbEvent {
id: IdHex::try_from_string(id)?,
raw: row.get(1)?,
pubkey: PublicKeyHex(row.get(2)?),
pubkey: PublicKeyHex::try_from_string(pk)?,
created_at: row.get(3)?,
kind: row.get(4)?,
content: row.get(5)?,
ots: row.get(6)?,
})
})?;
let mut output: Vec<DbEvent> = Vec::new();
for row in rows {
output.push(row?);
}
Ok(output)
})
@ -61,7 +60,7 @@ impl DbEvent {
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare(sql)?;
stmt.raw_bind_parameter(1, &pubkeyhex.0)?;
stmt.raw_bind_parameter(1, pubkeyhex.as_str())?;
let mut rows = stmt.raw_query();
let mut events: Vec<Event> = Vec::new();
while let Some(row) = rows.next()? {
@ -103,15 +102,17 @@ impl DbEvent {
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare(&sql)?;
stmt.raw_bind_parameter(1, public_key.0)?;
stmt.raw_bind_parameter(1, public_key.as_str())?;
stmt.raw_bind_parameter(2, since)?;
let mut rows = stmt.raw_query();
let mut events: Vec<DbEvent> = Vec::new();
while let Some(row) = rows.next()? {
let id: String = row.get(0)?;
let pk: String = row.get(2)?;
let event = DbEvent {
id: IdHex(row.get(0)?),
id: IdHex::try_from_str(&id)?,
raw: row.get(1)?,
pubkey: PublicKeyHex(row.get(2)?),
pubkey: PublicKeyHex::try_from_str(&pk)?,
created_at: row.get(3)?,
kind: row.get(4)?,
content: row.get(5)?,
@ -177,21 +178,20 @@ impl DbEvent {
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare(&sql)?;
let rows = stmt.query_map([], |row| {
Ok(DbEvent {
id: IdHex(row.get(0)?),
let mut rows = stmt.query([])?;
let mut output: Vec<DbEvent> = Vec::new();
while let Some(row) = rows.next()? {
let id: String = row.get(0)?;
let pk: String = row.get(2)?;
output.push(DbEvent {
id: IdHex::try_from_string(id)?,
raw: row.get(1)?,
pubkey: PublicKeyHex(row.get(2)?),
pubkey: PublicKeyHex::try_from_string(pk)?,
created_at: row.get(3)?,
kind: row.get(4)?,
content: row.get(5)?,
ots: row.get(6)?,
})
})?;
let mut output: Vec<DbEvent> = Vec::new();
for row in rows {
output.push(row?);
});
}
Ok(output)
})
@ -207,12 +207,11 @@ impl DbEvent {
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((
&event.id.0,
event.id.as_str(),
&event.raw,
&event.pubkey.0,
event.pubkey.as_str(),
&event.created_at,
&event.kind,
&event.content,

View File

@ -35,7 +35,7 @@ impl DbPersonRelay {
repeat_vars(pubkeys.len())
);
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.0.clone()).collect();
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.to_string()).collect();
let output: Result<Vec<DbPersonRelay>, Error> = spawn_blocking(move || {
let maybe_db = GLOBALS.db.blocking_lock();
@ -186,7 +186,7 @@ impl DbPersonRelay {
let mut stmt = db.prepare(sql)?;
stmt.execute((
&person.0,
person.as_str(),
&relay.0,
&last_suggested_nip05,
&last_suggested_nip05,
@ -213,7 +213,7 @@ impl DbPersonRelay {
let mut stmt = db.prepare(sql)?;
stmt.execute((
&person.0,
person.as_str(),
&relay.0,
&last_suggested_nip23,
&last_suggested_nip23,
@ -236,7 +236,7 @@ impl DbPersonRelay {
let maybe_db = GLOBALS.db.blocking_lock();
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare(sql)?;
stmt.raw_bind_parameter(1, &pubkey.0)?;
stmt.raw_bind_parameter(1, pubkey.as_str())?;
let mut rows = stmt.raw_query();
let mut dbprs: Vec<DbPersonRelay> = Vec::new();

View File

@ -111,7 +111,7 @@ impl Feed {
.iter()
.map(|r| r.value().to_owned())
.filter(|e| e.kind == EventKind::TextNote)
.filter(|e| e.pubkey.as_hex_string() == person.0)
.filter(|e| e.pubkey.as_hex_string() == person.as_str())
.filter(|e| !GLOBALS.dismissed.blocking_read().contains(&e.id))
.collect();

View File

@ -10,8 +10,8 @@ use futures::{SinkExt, StreamExt};
use futures_util::stream::{SplitSink, SplitStream};
use http::Uri;
use nostr_types::{
ClientMessage, EventKind, Filter, IdHex, PublicKeyHex, RelayInformationDocument, RelayUrl,
Unixtime,
ClientMessage, EventKind, Filter, IdHex, IdHexPrefix, PublicKeyHex, PublicKeyHexPrefix,
RelayInformationDocument, RelayUrl, Unixtime,
};
use std::time::Duration;
use subscription::Subscriptions;
@ -364,8 +364,9 @@ impl Minion {
}
// feed related by me
let pkh: PublicKeyHex = pubkey.into();
filters.push(Filter {
authors: vec![pubkey.into()],
authors: vec![pkh.clone().into()],
kinds,
since: Some(feed_since),
..Default::default()
@ -378,7 +379,7 @@ impl Minion {
kinds.push(EventKind::Reaction);
}
filters.push(Filter {
p: vec![pubkey.into()],
p: vec![pkh.clone().into()],
kinds: vec![EventKind::TextNote],
since: Some(replies_since),
..Default::default()
@ -386,7 +387,7 @@ impl Minion {
// Listen for my metadata and similar kinds of posts
filters.push(Filter {
authors: vec![pubkey.into()],
authors: vec![pkh.clone().into()],
kinds: vec![
EventKind::Metadata,
EventKind::RecommendRelay,
@ -403,9 +404,15 @@ impl Minion {
if enable_reactions {
kinds.push(EventKind::Reaction);
}
let pkp: Vec<PublicKeyHexPrefix> = followed_pubkeys
.iter()
.map(|pk| pk.to_owned().into())
.collect();
// feed related by people followed
filters.push(Filter {
authors: followed_pubkeys.clone(),
authors: pkp,
kinds,
since: Some(feed_since),
..Default::default()
@ -416,9 +423,12 @@ impl Minion {
//
// BUT ONLY for people whose contact list has not been received in the last
// 24 hours.
let contact_list_keys = GLOBALS
let contact_list_keys: Vec<PublicKeyHexPrefix> = GLOBALS
.people
.get_followed_pubkeys_needing_contact_lists(&followed_pubkeys);
.get_followed_pubkeys_needing_contact_lists(&followed_pubkeys)
.drain(..)
.map(|pk| pk.into())
.collect();
if !contact_list_keys.is_empty() {
tracing::debug!(
@ -470,7 +480,7 @@ impl Minion {
// NOTE we do not unsubscribe to the general feed
let filters: Vec<Filter> = vec![Filter {
authors: vec![pubkey.clone()],
authors: vec![pubkey.clone().into()],
kinds: vec![EventKind::TextNote, EventKind::EventDeletion],
// No since, just a limit on quantity of posts
limit: Some(25),
@ -529,9 +539,11 @@ impl Minion {
let enable_reactions = GLOBALS.settings.read().await.reactions;
if !vec_ids.is_empty() {
let idhp: Vec<IdHexPrefix> = vec_ids.iter().map(|id| id.to_owned().into()).collect();
// Get ancestors we know of so far
filters.push(Filter {
ids: vec_ids.clone(),
ids: idhp,
..Default::default()
});
@ -689,7 +701,7 @@ impl Minion {
// create the filter
let mut filter = Filter::new();
filter.ids = ids;
filter.ids = ids.iter().map(|id| id.to_owned().into()).collect();
tracing::trace!("{}: Event Filter: {} events", &self.url, filter.ids.len());
@ -715,11 +727,13 @@ impl Minion {
async fn temp_subscribe_metadata(
&mut self,
pubkeyhexs: Vec<PublicKeyHex>,
mut pubkeyhexs: Vec<PublicKeyHex>,
) -> Result<(), Error> {
let pkhp: Vec<PublicKeyHexPrefix> = pubkeyhexs.drain(..).map(|pk| pk.into()).collect();
let handle = "temp_subscribe_metadata".to_string();
let filter = Filter {
authors: pubkeyhexs,
authors: pkhp,
kinds: vec![EventKind::Metadata],
// FIXME: we could probably get a since-last-fetched-their-metadata here.
// but relays should just return the lastest of these.
@ -730,8 +744,9 @@ impl Minion {
async fn pull_following(&mut self) -> Result<(), Error> {
if let Some(pubkey) = GLOBALS.signer.read().await.public_key() {
let pkh: PublicKeyHex = pubkey.into();
let filter = Filter {
authors: vec![pubkey.into()],
authors: vec![pkh.into()],
kinds: vec![EventKind::ContactList],
..Default::default()
};

View File

@ -634,7 +634,7 @@ impl Overlord {
// Save person_relay
DbPersonRelay::insert(DbPersonRelay {
person: pkhex.0.clone(),
person: pkhex.to_string(),
relay,
last_fetched: None,
last_suggested_kind2: None,
@ -761,7 +761,7 @@ impl Overlord {
// FIXME: Should we avoid taging people who are muted?
for tag in &event.tags {
if let Tag::Pubkey { pubkey, .. } = tag {
if pubkey.0 != public_key.as_hex_string() {
if pubkey.as_str() != &public_key.as_hex_string() {
add_pubkey_hex_to_tags(&mut tags, pubkey).await;
}
}

View File

@ -155,7 +155,7 @@ impl People {
sql.push_str(&"(?),".repeat(pubkeys.len()));
sql.pop(); // remove trailing comma
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.0.clone()).collect();
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.to_string()).collect();
task::spawn_blocking(move || {
let maybe_db = GLOBALS.db.blocking_lock();
@ -243,7 +243,7 @@ impl People {
&person.metadata_at,
&person.nip05_valid,
&person.nip05_last_checked,
&pubkeyhex2.0,
pubkeyhex2.as_str(),
))?;
Ok::<(), Error>(())
})
@ -323,8 +323,9 @@ impl People {
Some(s) => serde_json::from_str(&s)?,
None => None,
};
let pk: String = row.get(0)?;
output.push(DbPerson {
pubkey: PublicKeyHex(row.get(0)?),
pubkey: PublicKeyHex::try_from_string(pk)?,
metadata,
metadata_at: row.get(2)?,
nip05_valid: row.get(3)?,
@ -635,7 +636,7 @@ impl People {
let maybe_db = GLOBALS.db.blocking_lock();
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare(sql)?;
stmt.execute((&pubkeyhex2.0, &follow, &follow))?;
stmt.execute((pubkeyhex2.as_str(), &follow, &follow))?;
Ok::<(), Error>(())
})
.await??;
@ -702,7 +703,7 @@ impl People {
repeat_vars(pubkeys.len())
);
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.0.clone()).collect();
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.to_string()).collect();
task::spawn_blocking(move || {
let maybe_db = GLOBALS.db.blocking_lock();
@ -728,7 +729,7 @@ impl People {
repeat_vars(pubkeys.len())
);
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.0.clone()).collect();
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.to_string()).collect();
task::spawn_blocking(move || {
let maybe_db = GLOBALS.db.blocking_lock();
@ -795,7 +796,7 @@ impl People {
let maybe_db = GLOBALS.db.blocking_lock();
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare(sql)?;
stmt.execute((&pubkeyhex2.0, &mute, &mute))?;
stmt.execute((pubkeyhex2.as_str(), &mute, &mute))?;
Ok::<(), Error>(())
})
.await??;
@ -828,7 +829,7 @@ impl People {
let db = maybe_db.as_ref().unwrap();
let mut stmt =
db.prepare("UPDATE person SET contact_list_last_received=? WHERE pubkey=?")?;
stmt.execute((&now, &pubkeyhex.0))?;
stmt.execute((&now, pubkeyhex.as_str()))?;
Ok(())
})
.await?
@ -845,7 +846,7 @@ impl People {
let maybe_db = GLOBALS.db.blocking_lock();
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare("UPDATE person SET nip05_last_checked=? WHERE pubkey=?")?;
stmt.execute((&now, &pubkeyhex.0))?;
stmt.execute((&now, pubkeyhex.as_str()))?;
Ok(())
})
.await?
@ -890,7 +891,7 @@ impl People {
let mut stmt = db.prepare(sql)?;
stmt.execute((
&pubkeyhex2.0,
pubkeyhex2.as_str(),
&metadata_json,
&nip05_valid,
&nip05_last_checked,
@ -929,8 +930,9 @@ impl People {
Some(s) => serde_json::from_str(&s)?,
None => None,
};
let pk: String = row.get(0)?;
output.push(DbPerson {
pubkey: PublicKeyHex(row.get(0)?),
pubkey: PublicKeyHex::try_from_string(pk)?,
metadata,
metadata_at: row.get(2)?,
nip05_valid: row.get(3)?,
@ -966,7 +968,7 @@ impl People {
repeat_vars(pubkeys.len())
);
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.0.clone()).collect();
let pubkey_strings: Vec<String> = pubkeys.iter().map(|p| p.to_string()).collect();
let output: Result<Vec<DbPerson>, Error> = task::spawn_blocking(move || {
let maybe_db = GLOBALS.db.blocking_lock();
@ -988,8 +990,9 @@ impl People {
Some(s) => serde_json::from_str(&s)?,
None => None,
};
let pk: String = row.get(0)?;
people.push(DbPerson {
pubkey: PublicKeyHex(row.get(0)?),
pubkey: PublicKeyHex::try_from_string(pk)?,
metadata,
metadata_at: row.get(2)?,
nip05_valid: row.get(3)?,

View File

@ -105,7 +105,7 @@ pub async fn process_new_event(
// upsert person_relay.last_suggested_bytag
let now = Unixtime::now()?.0 as u64;
DbPersonRelay::upsert_last_suggested_bytag(
pubkey.0.to_owned(),
pubkey.to_string(),
url.clone(),
now,
)
@ -306,7 +306,7 @@ async fn process_your_contact_list(event: &Event) -> Result<(), Error> {
.and_then(|rru| RelayUrl::try_from_unchecked_url(rru).ok())
{
DbPersonRelay::upsert_last_suggested_kind3(
pubkey.0.to_owned(),
pubkey.to_string(),
url,
now.0 as u64,
)

View File

@ -49,7 +49,7 @@ pub async fn add_pubkey_hex_to_tags(existing_tags: &mut Vec<Tag>, hex: &PublicKe
match existing_tags.iter().position(|existing_tag| {
matches!(
existing_tag,
Tag::Pubkey { pubkey: existing_p, .. } if existing_p.0 == hex.0
Tag::Pubkey { pubkey: existing_p, .. } if existing_p == hex
)
}) {
None => {

View File

@ -105,7 +105,7 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, frame: &mut eframe::Fram
}
FeedKind::Person(pubkeyhex) => {
let feed = GLOBALS.feed.get_person_feed(pubkeyhex.clone());
render_a_feed(app, ctx, frame, ui, feed, false, &pubkeyhex.0);
render_a_feed(app, ctx, frame, ui, feed, false, &pubkeyhex.as_str());
}
}
}

View File

@ -429,10 +429,10 @@ impl GossipUi {
pub fn hex_pubkey_short(pubkeyhex: &PublicKeyHex) -> String {
format!(
"{}_{}...{}_{}",
&pubkeyhex.0.get(0..4).unwrap_or("????"),
&pubkeyhex.0.get(4..8).unwrap_or("????"),
&pubkeyhex.0.get(56..60).unwrap_or("????"),
&pubkeyhex.0.get(60..64).unwrap_or("????"),
&pubkeyhex.as_str()[0..4],
&pubkeyhex.as_str()[4..8],
&pubkeyhex.as_str()[56..60],
&pubkeyhex.as_str()[60..64],
)
}
@ -447,7 +447,7 @@ impl GossipUi {
}
pub fn hex_id_short(idhex: &IdHex) -> String {
idhex.0.get(0..8).unwrap_or("????????").to_string()
idhex.as_str()[0..8].to_string()
}
pub fn render_person_name_line(app: &mut GossipUi, ui: &mut Ui, person: &DbPerson) {
@ -527,8 +527,11 @@ impl GossipUi {
None
}
Ok(Some(color_image)) => {
let texture_handle =
ctx.load_texture(pubkeyhex.0.clone(), color_image, TextureOptions::default());
let texture_handle = ctx.load_texture(
pubkeyhex.to_string(),
color_image,
TextureOptions::default(),
);
self.avatars
.insert(pubkeyhex.to_owned(), texture_handle.clone());
Some(texture_handle)

View File

@ -125,9 +125,9 @@ fn show_pub_key_detail(app: &mut GossipUi, ctx: &Context, ui: &mut Ui) {
let pkhex: PublicKeyHex = public_key.into();
ui.horizontal_wrapped(|ui| {
ui.label(&format!("Public Key (Hex): {}", pkhex.0));
ui.label(&format!("Public Key (Hex): {}", pkhex.as_str()));
if ui.add(CopyButton {}).clicked() {
ui.output().copied_text = pkhex.0;
ui.output().copied_text = pkhex.into_string();
}
});
@ -298,9 +298,9 @@ fn offer_import_pub_key(app: &mut GossipUi, ui: &mut Ui) {
if let Some(pk) = GLOBALS.signer.blocking_read().public_key() {
let pkhex: PublicKeyHex = pk.into();
ui.horizontal(|ui| {
ui.label(&format!("Public Key (Hex): {}", pkhex.0));
ui.label(&format!("Public Key (Hex): {}", pkhex.as_str()));
if ui.add(CopyButton {}).clicked() {
ui.output().copied_text = pkhex.0;
ui.output().copied_text = pkhex.into_string();
}
});