refactor: follow functionality

This commit is contained in:
codytseng 2024-11-12 14:30:29 +08:00
parent 21e4e3badf
commit dcd94fdce3
7 changed files with 149 additions and 77 deletions

View file

@ -51,6 +51,10 @@ class ClientService {
cacheMap: new LRUCache<string, Promise<TRelayList>>({ max: 10000 })
}
)
private followListCache = new LRUCache<string, Promise<NEvent | undefined>>({
max: 10000,
fetchMethod: this._fetchFollowListEvent.bind(this)
})
constructor() {
if (!ClientService.instance) {
@ -125,14 +129,6 @@ class ClientService {
return this.eventCache.fetch(JSON.stringify({ ...filter, limit: 1 }))
}
deleteEventCacheByFilter(filter: Filter) {
try {
this.eventCache.delete(JSON.stringify({ ...filter, limit: 1 }))
} catch {
// ignore
}
}
async fetchEventById(id: string): Promise<NEvent | undefined> {
return this.eventDataloader.load(id)
}
@ -145,6 +141,14 @@ class ClientService {
return this.relayListDataLoader.load(pubkey)
}
async fetchFollowListEvent(pubkey: string) {
return this.followListCache.fetch(pubkey)
}
updateFollowListCache(pubkey: string, event: NEvent) {
this.followListCache.set(pubkey, Promise.resolve(event))
}
private async eventBatchLoadFn(ids: readonly string[]) {
const events = await this.fetchEvents(this.relayUrls, {
ids: ids as string[],
@ -255,6 +259,16 @@ class ClientService {
})
}
private async _fetchFollowListEvent(pubkey: string) {
const relayList = await this.fetchRelayList(pubkey)
const followListEvents = await this.fetchEvents(relayList.write.concat(BIG_RELAY_URLS), {
authors: [pubkey],
kinds: [kinds.Contacts]
})
return followListEvents.sort((a, b) => b.created_at - a.created_at)[0]
}
private parseProfileFromEvent(event: NEvent): TProfile {
try {
const profileObj = JSON.parse(event.content)