feat: like & repost (#3)

This commit is contained in:
Cody Tseng 2024-11-06 22:51:52 +08:00 committed by GitHub
parent 751ad16690
commit bd3078bcd0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 688 additions and 130 deletions

View file

@ -1,6 +1,6 @@
import { TRelayGroup } from '@common/types'
import { formatPubkey } from '@renderer/lib/pubkey'
import { TEventStats, TProfile } from '@renderer/types'
import { TProfile } from '@renderer/types'
import DataLoader from 'dataloader'
import { LRUCache } from 'lru-cache'
import { Filter, kinds, Event as NEvent, SimplePool } from 'nostr-tools'
@ -21,12 +21,6 @@ class ClientService {
private relayUrls: string[] = BIG_RELAY_URLS
private initPromise!: Promise<void>
private eventStatsCache = new LRUCache<string, Promise<TEventStats>>({
max: 10000,
ttl: 1000 * 60 * 10, // 10 minutes
fetchMethod: async (id) => this._fetchEventStatsById(id)
})
private eventCache = new LRUCache<string, Promise<NEvent | undefined>>({
max: 10000,
fetchMethod: async (filterStr) => {
@ -114,11 +108,6 @@ class ClientService {
return await this.pool.querySync(relayUrls, filter)
}
async fetchEventStatsById(id: string): Promise<TEventStats> {
const stats = await this.eventStatsCache.fetch(id)
return stats ?? { reactionCount: 0, repostCount: 0 }
}
async fetchEventByFilter(filter: Filter) {
return this.eventCache.fetch(JSON.stringify({ ...filter, limit: 1 }))
}
@ -131,15 +120,6 @@ class ClientService {
return this.profileDataloader.load(pubkey)
}
private async _fetchEventStatsById(id: string) {
const [reactionEvents, repostEvents] = await Promise.all([
this.fetchEvents({ '#e': [id], kinds: [kinds.Reaction] }),
this.fetchEvents({ '#e': [id], kinds: [kinds.Repost] })
])
return { reactionCount: reactionEvents.length, repostCount: repostEvents.length }
}
private async eventBatchLoadFn(ids: readonly string[]) {
const events = await this.fetchEvents({
ids: ids as string[],