search: Handle nostr URLs and bech32 encodings and find those things

This commit is contained in:
Mike Dilger 2023-08-06 10:04:04 +12:00
parent c68c54d696
commit 28ad46a83a
2 changed files with 69 additions and 14 deletions

View File

@ -5,6 +5,7 @@ use crate::comms::{
}; };
use crate::error::{Error, ErrorKind}; use crate::error::{Error, ErrorKind};
use crate::globals::{ZapState, GLOBALS}; use crate::globals::{ZapState, GLOBALS};
use crate::people::Person;
use crate::person_relay::PersonRelay; use crate::person_relay::PersonRelay;
use crate::relay::Relay; use crate::relay::Relay;
use crate::tags::{ use crate::tags::{
@ -15,8 +16,9 @@ use gossip_relay_picker::{Direction, RelayAssignment};
use http::StatusCode; use http::StatusCode;
use minion::Minion; use minion::Minion;
use nostr_types::{ use nostr_types::{
EncryptedPrivateKey, EventKind, Id, IdHex, Metadata, MilliSatoshi, NostrBech32, PayRequestData, EncryptedPrivateKey, Event, EventKind, Id, IdHex, Metadata, MilliSatoshi, NostrBech32,
PreEvent, PrivateKey, Profile, PublicKey, RelayUrl, Tag, UncheckedUrl, Unixtime, PayRequestData, PreEvent, PrivateKey, Profile, PublicKey, RelayUrl, Tag, UncheckedUrl,
Unixtime,
}; };
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
@ -1856,21 +1858,67 @@ impl Overlord {
.write("You must enter at least 2 characters to search.".to_string()); .write("You must enter at least 2 characters to search.".to_string());
return Ok(()); return Ok(());
} }
text = text.to_lowercase(); text = text.to_lowercase();
// If npub, convert to hex so we can find it in the database let mut people_search_results: Vec<Person> = Vec::new();
// (This will only work with full npubs) let mut note_search_results: Vec<Event> = Vec::new();
let mut pubkeytext = text.clone();
if let Ok(pk) = PublicKey::try_from_bech32_string(&text, true) { // If a nostr: url, strip the 'nostr:' part
pubkeytext = pk.as_hex_string(); if text.len() >= 6 && &text[0..6] == "nostr:" {
text = text.split_off(6);
} }
*GLOBALS.people_search_results.write() = GLOBALS.storage.filter_people(|p| { if let Some(nb32) = NostrBech32::try_from_string(&text) {
if p.pubkey.as_hex_string().contains(&pubkeytext) { match nb32 {
return true; NostrBech32::EventAddr(ea) => {
if let Some(event) = GLOBALS
.storage
.find_events(
&[ea.kind],
&[ea.author],
None,
|event| {
event.tags.iter().any(|tag| {
if let Tag::Identifier { d, .. } = tag {
if *d == ea.d {
return true;
}
}
false
})
},
true,
)?
.get(1)
{
note_search_results.push(event.clone());
}
}
NostrBech32::EventPointer(ep) => {
if let Some(event) = GLOBALS.storage.read_event(ep.id)? {
note_search_results.push(event);
}
}
NostrBech32::Id(id) => {
if let Some(event) = GLOBALS.storage.read_event(id)? {
note_search_results.push(event);
}
}
NostrBech32::Profile(prof) => {
if let Some(person) = GLOBALS.storage.read_person(&prof.pubkey)? {
people_search_results.push(person);
}
}
NostrBech32::Pubkey(pk) => {
if let Some(person) = GLOBALS.storage.read_person(&pk)? {
people_search_results.push(person);
}
}
NostrBech32::Relay(_relay) => (),
} }
}
people_search_results.extend(GLOBALS.storage.filter_people(|p| {
if let Some(metadata) = &p.metadata { if let Some(metadata) = &p.metadata {
if let Ok(s) = serde_json::to_string(&metadata) { if let Ok(s) = serde_json::to_string(&metadata) {
if s.to_lowercase().contains(&text) { if s.to_lowercase().contains(&text) {
@ -1886,9 +1934,11 @@ impl Overlord {
} }
false false
})?; })?);
let note_search_results = GLOBALS.storage.search_events(&text)?; note_search_results.extend(GLOBALS.storage.search_events(&text)?);
*GLOBALS.people_search_results.write() = people_search_results;
*GLOBALS.note_search_results.write() = note_search_results; *GLOBALS.note_search_results.write() = note_search_results;
Ok(()) Ok(())

View File

@ -108,12 +108,17 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut Frame, ui:
} }
}); });
let summary = event let mut summary = event
.content .content
.get(0..event.content.len().min(100)) .get(0..event.content.len().min(100))
.unwrap_or("...") .unwrap_or("...")
.replace('\n', " "); .replace('\n', " ");
if summary.is_empty() {
// Show something they can click on anyways
summary = "[no event summary]".to_owned();
}
if ui.add(Label::new(summary).sense(Sense::click())).clicked() { if ui.add(Label::new(summary).sense(Sense::click())).clicked() {
app.set_page(Page::Feed(FeedKind::Thread { app.set_page(Page::Feed(FeedKind::Thread {
id: event.id, id: event.id,