refactor: search
This commit is contained in:
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user