refactor: search

This commit is contained in:
codytseng
2025-08-31 22:43:47 +08:00
parent 88567c2c13
commit 0153465e29
24 changed files with 785 additions and 345 deletions

View File

@@ -22,7 +22,9 @@ import {
kinds,
Event as NEvent,
nip19,
Relay,
SimplePool,
validateEvent,
VerifiedEvent
} from 'nostr-tools'
import { AbstractRelay } from 'nostr-tools/abstract-relay'
@@ -57,6 +59,7 @@ class ClientService extends EventTarget {
this.fetchEventsFromBigRelays.bind(this),
{ cache: false, batchScheduleFn: (callback) => setTimeout(callback, 50) }
)
private trendingNotesCache: NEvent[] | null = null
private userIndex = new FlexSearch.Index({
tokenize: 'forward'
@@ -709,6 +712,39 @@ class ClientService extends EventTarget {
return this.eventDataLoader.load(id)
}
async fetchTrendingNotes() {
if (this.trendingNotesCache) {
return this.trendingNotesCache
}
try {
const response = await fetch('https://api.nostr.band/v0/trending/notes')
const data = await response.json()
const events: NEvent[] = []
for (const note of data.notes ?? []) {
if (validateEvent(note.event)) {
events.push(note.event)
this.addEventToCache(note.event)
if (note.relays?.length) {
note.relays.map((r: string) => {
try {
const relay = new Relay(r)
this.trackEventSeenOn(note.event.id, relay)
} catch {
return null
}
})
}
}
}
this.trendingNotesCache = events
return this.trendingNotesCache
} catch (error) {
console.error('fetchTrendingNotes error', error)
return []
}
}
addEventToCache(event: NEvent) {
this.eventDataLoader.prime(event.id, Promise.resolve(event))
if (isReplaceableEvent(event.kind)) {