From 6d4a9c8d0dcde0d6d1c0c17cc71ea7b16acf53dc Mon Sep 17 00:00:00 2001 From: Jonathan Staab Date: Wed, 12 Apr 2023 17:29:48 -0500 Subject: [PATCH] Add topic search, tweak topic autocomplete --- ROADMAP.md | 8 +++-- src/agent/db.ts | 7 ++++ src/agent/sync.ts | 2 +- src/app/EnsureData.svelte | 2 +- src/app/shared/NoteContent.svelte | 2 +- src/app/shared/PersonSearch.svelte | 22 +++--------- src/app/views/Search.svelte | 56 +++++++++++++++++++++++++++--- src/app/views/TopicFeed.svelte | 4 +-- src/partials/Compose.svelte | 12 +++++-- src/util/nostr.ts | 2 +- 10 files changed, 83 insertions(+), 34 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index fffef987..7318e73b 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,8 +1,10 @@ # Current +- [ ] Buttons on profile detail is broken - [ ] Topics - - [ ] Improve topic suggestions and rendering - - [ ] Add topic search, keep cache of topics + - [x] Improve topic suggestions and rendering + - [x] Add topic search, keep cache of topics + - [ ] Add ability to follow topics - [ ] Relays bounty - [ ] Ability to create custom feeds - [ ] Add global/following/network tabs to relay detail @@ -69,7 +71,7 @@ - [ ] Review QR codes, search, basic affordances for link navigation - [ ] Add delete button to notes - [ ] Log in as user button -- [ ] Separate notifications out by type, mute certain kinds +- [ ] Separate notifications out by type, mute certain kinds. Likes are extraneous - [ ] Relay recommendations based on follows/followers - [ ] Make the note relays button modal make sense, one relay with no explanation is not good - [ ] Linkify invoices diff --git a/src/agent/db.ts b/src/agent/db.ts index b4dc19e4..71eb485f 100644 --- a/src/agent/db.ts +++ b/src/agent/db.ts @@ -6,6 +6,7 @@ import {throttle} from "throttle-debounce" import {writable} from "svelte/store" import {ensurePlural, noop, createMap} from "hurdak/lib/hurdak" import {Tags} from "src/util/nostr" +import {fuzzy} from "src/util/misc" import user from "src/agent/user" const Adapter = window.indexedDB ? IncrementalIndexedDBAdapter : Loki.LokiMemoryAdapter @@ -224,3 +225,9 @@ export const onReady = cb => { } }) } + +export const searchPeople = watch("people", t => + fuzzy(t.all({"kind0.name": {$type: "string"}}), { + keys: ["kind0.name", "kind0.about", "pubkey"], + }) +) diff --git a/src/agent/sync.ts b/src/agent/sync.ts index 113256b1..358975e0 100644 --- a/src/agent/sync.ts +++ b/src/agent/sync.ts @@ -344,7 +344,7 @@ addHandler(10002, e => { // Topics const processTopics = e => { - const matches = Array.from(e.content.matchAll(/#(\w{2,100})/g)) + const matches = Array.from(e.content.toLowerCase().matchAll(/#(\w{2,100})/g)) if (matches.length > 0) { topics.patch(matches.map(nth(1)).map(objOf("name"))) diff --git a/src/app/EnsureData.svelte b/src/app/EnsureData.svelte index 5009d339..8dcc7a32 100644 --- a/src/app/EnsureData.svelte +++ b/src/app/EnsureData.svelte @@ -72,7 +72,7 @@
Search below to find some interesting people.
{/each} - + {:else if needsPeople()} diff --git a/src/app/shared/NoteContent.svelte b/src/app/shared/NoteContent.svelte index e63fbda1..93bd645e 100644 --- a/src/app/shared/NoteContent.svelte +++ b/src/app/shared/NoteContent.svelte @@ -122,7 +122,7 @@
{/each} {:else if type === "topic"} - openTopic(value)}>{value} + openTopic(value)}>#{value} {:else if type === "link"} {value.replace(/https?:\/\/(www\.)?/, "")} diff --git a/src/app/shared/PersonSearch.svelte b/src/app/shared/PersonSearch.svelte index a8e0fee7..4c30af02 100644 --- a/src/app/shared/PersonSearch.svelte +++ b/src/app/shared/PersonSearch.svelte @@ -1,42 +1,28 @@ {#each results as person (person.pubkey)} - {#if person.pubkey !== user.getPubkey() && !(hideFollowing && $petnamePubkeys.includes(person.pubkey))} + {#if person.pubkey !== user.getPubkey()} {/if} {:else} diff --git a/src/app/views/Search.svelte b/src/app/views/Search.svelte index d8006cda..fbce62ac 100644 --- a/src/app/views/Search.svelte +++ b/src/app/views/Search.svelte @@ -1,16 +1,62 @@
- Profile Search + Search

- Search for people on Nostr. For now, only profiles that have already been loaded will appear - in search results. + Search for people and topics on Nostr. For now, only results that have already been loaded + will appear in search results.

- + + + + {#each $search(q).slice(0, 50) as result (result.type + result.id)} + {#if result.type === "topic"} + openTopic(result.topic.name)}> + #{result.topic.name} + + {:else if result.type === "person"} + + {/if} + {/each}
diff --git a/src/app/views/TopicFeed.svelte b/src/app/views/TopicFeed.svelte index 2649d7ab..14ac091c 100644 --- a/src/app/views/TopicFeed.svelte +++ b/src/app/views/TopicFeed.svelte @@ -6,10 +6,10 @@ export let topic const relays = sampleRelays(getUserReadRelays()) - const filter = [{kinds: [1], "#t": [topic.replace("#", "")]}] + const filter = [{kinds: [1], "#t": [topic]}] - {topic} + #{topic} diff --git a/src/partials/Compose.svelte b/src/partials/Compose.svelte index 7c2cc871..cef564dd 100644 --- a/src/partials/Compose.svelte +++ b/src/partials/Compose.svelte @@ -44,6 +44,8 @@ } const autocomplete = ({person = null, force = false} = {}) => { + let completed = false + const {selection, node, offset, word} = getInfo() const annotate = (prefix, text, value) => { @@ -67,10 +69,12 @@ selection.collapse(span.nextSibling, 0) selection.getRangeAt(0).insertNode(space) selection.collapse(space, 2) + + completed = true } // Mentions - if ((force || word.length > 1) && word.startsWith("@") && person) { + if ((force || word.length > 1) && word.startsWith("@")) { annotate("@", displayPerson(person).trim(), pubkeyEncoder.encode(person.pubkey)) } @@ -80,6 +84,8 @@ } suggestions.setData([]) + + return completed } const onKeyDown = e => { @@ -105,7 +111,9 @@ // Only autocomplete topics on space if (["Space"].includes(e.code)) { - autocomplete() + if (autocomplete()) { + e.preventDefault() + } } } diff --git a/src/util/nostr.ts b/src/util/nostr.ts index dbd82cca..f2d54e6a 100644 --- a/src/util/nostr.ts +++ b/src/util/nostr.ts @@ -193,7 +193,7 @@ export const parseContent = ({content, tags = []}) => { const topic = first(text.match(/^#\w+/i)) if (topic) { - return ["topic", topic, topic] + return ["topic", topic, topic.slice(1)] } }