fix: 🐛
This commit is contained in:
parent
d0350c6ad3
commit
c62a82f673
9 changed files with 283 additions and 244 deletions
|
|
@ -1,8 +1,10 @@
|
|||
import { BIG_RELAY_URLS } from '@/constants'
|
||||
import { getFollowingsFromFollowListEvent } from '@/lib/event'
|
||||
import { formatPubkey } from '@/lib/pubkey'
|
||||
import { tagNameEquals } from '@/lib/tag'
|
||||
import { isWebsocketUrl, normalizeUrl } from '@/lib/url'
|
||||
import {
|
||||
getFollowingsFromFollowListEvent,
|
||||
getProfileFromProfileEvent,
|
||||
getRelayListFromRelayListEvent
|
||||
} from '@/lib/event'
|
||||
import { userIdToPubkey } from '@/lib/pubkey'
|
||||
import { TDraftEvent, TProfile, TRelayInfo, TRelayList } from '@/types'
|
||||
import { sha256 } from '@noble/hashes/sha2'
|
||||
import DataLoader from 'dataloader'
|
||||
|
|
@ -43,19 +45,19 @@ class ClientService extends EventTarget {
|
|||
this.eventBatchLoadFn.bind(this),
|
||||
{ cache: false }
|
||||
)
|
||||
private profileCache = new LRUCache<string, Promise<TProfile>>({ max: 10000 })
|
||||
private profileDataloader = new DataLoader<string, TProfile>(
|
||||
(ids) => Promise.all(ids.map((id) => this._fetchProfile(id))),
|
||||
{ cacheMap: this.profileCache }
|
||||
private profileEventCache = new LRUCache<string, Promise<NEvent | undefined>>({ max: 10000 })
|
||||
private profileEventDataloader = new DataLoader<string, NEvent | undefined>(
|
||||
(ids) => Promise.all(ids.map((id) => this._fetchProfileEvent(id))),
|
||||
{ cacheMap: this.profileEventCache }
|
||||
)
|
||||
private fetchProfileFromDefaultRelaysDataloader = new DataLoader<string, TProfile | undefined>(
|
||||
this.profileBatchLoadFn.bind(this),
|
||||
private fetchProfileEventFromDefaultRelaysDataloader = new DataLoader<string, NEvent | undefined>(
|
||||
this.profileEventBatchLoadFn.bind(this),
|
||||
{ cache: false }
|
||||
)
|
||||
private relayListDataLoader = new DataLoader<string, TRelayList>(
|
||||
this.relayListBatchLoadFn.bind(this),
|
||||
private relayListEventDataLoader = new DataLoader<string, NEvent | undefined>(
|
||||
this.relayListEventBatchLoadFn.bind(this),
|
||||
{
|
||||
cacheMap: new LRUCache<string, Promise<TRelayList>>({ max: 10000 })
|
||||
cacheMap: new LRUCache<string, Promise<NEvent | undefined>>({ max: 10000 })
|
||||
}
|
||||
)
|
||||
private relayInfoDataLoader = new DataLoader<string, TRelayInfo | undefined>(async (urls) => {
|
||||
|
|
@ -373,30 +375,28 @@ class ClientService extends EventTarget {
|
|||
this.eventDataLoader.prime(event.id, Promise.resolve(event))
|
||||
}
|
||||
|
||||
async fetchProfile(id: string): Promise<TProfile | undefined> {
|
||||
if (!/^[0-9a-f]{64}$/.test(id)) {
|
||||
let pubkey: string | undefined
|
||||
const { data, type } = nip19.decode(id)
|
||||
switch (type) {
|
||||
case 'npub':
|
||||
pubkey = data
|
||||
break
|
||||
case 'nprofile':
|
||||
pubkey = data.pubkey
|
||||
break
|
||||
}
|
||||
|
||||
if (!pubkey) {
|
||||
throw new Error('Invalid id')
|
||||
}
|
||||
|
||||
const cache = await this.profileCache.get(pubkey)
|
||||
if (cache) {
|
||||
return cache
|
||||
}
|
||||
async fetchProfileEvent(id: string): Promise<NEvent | undefined> {
|
||||
const pubkey = userIdToPubkey(id)
|
||||
const cache = await this.profileEventCache.get(pubkey)
|
||||
if (cache) {
|
||||
return cache
|
||||
}
|
||||
|
||||
return this.profileDataloader.load(id)
|
||||
return await this.profileEventDataloader.load(id)
|
||||
}
|
||||
|
||||
async fetchProfile(id: string): Promise<TProfile | undefined> {
|
||||
const profileEvent = await this.fetchProfileEvent(id)
|
||||
if (profileEvent) {
|
||||
return getProfileFromProfileEvent(profileEvent)
|
||||
}
|
||||
|
||||
try {
|
||||
const pubkey = userIdToPubkey(id)
|
||||
return { pubkey, username: pubkey }
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
async fetchProfiles(relayUrls: string[], filter: Filter): Promise<TProfile[]> {
|
||||
|
|
@ -405,15 +405,21 @@ class ClientService extends EventTarget {
|
|||
kinds: [kinds.Metadata]
|
||||
})
|
||||
|
||||
const profiles = events
|
||||
.sort((a, b) => b.created_at - a.created_at)
|
||||
.map((event) => this.parseProfileFromEvent(event))
|
||||
profiles.forEach((profile) => this.profileDataloader.prime(profile.pubkey, profile))
|
||||
return profiles
|
||||
const profileEvents = events.sort((a, b) => b.created_at - a.created_at)
|
||||
profileEvents.forEach((profile) => this.profileEventDataloader.prime(profile.pubkey, profile))
|
||||
return profileEvents.map((profileEvent) => getProfileFromProfileEvent(profileEvent))
|
||||
}
|
||||
|
||||
async fetchRelayListEvent(pubkey: string) {
|
||||
return this.relayListEventDataLoader.load(pubkey)
|
||||
}
|
||||
|
||||
async fetchRelayList(pubkey: string): Promise<TRelayList> {
|
||||
return this.relayListDataLoader.load(pubkey)
|
||||
const event = await this.relayListEventDataLoader.load(pubkey)
|
||||
if (!event) {
|
||||
return { write: BIG_RELAY_URLS, read: BIG_RELAY_URLS }
|
||||
}
|
||||
return getRelayListFromRelayListEvent(event)
|
||||
}
|
||||
|
||||
async fetchFollowListEvent(pubkey: string) {
|
||||
|
|
@ -488,7 +494,7 @@ class ClientService extends EventTarget {
|
|||
return event
|
||||
}
|
||||
|
||||
private async _fetchProfile(id: string): Promise<TProfile> {
|
||||
private async _fetchProfileEvent(id: string): Promise<NEvent | undefined> {
|
||||
let pubkey: string | undefined
|
||||
let relays: string[] = []
|
||||
if (/^[0-9a-f]{64}$/.test(id)) {
|
||||
|
|
@ -509,8 +515,8 @@ class ClientService extends EventTarget {
|
|||
if (!pubkey) {
|
||||
throw new Error('Invalid id')
|
||||
}
|
||||
|
||||
const profileFromDefaultRelays = await this.fetchProfileFromDefaultRelaysDataloader.load(pubkey)
|
||||
const profileFromDefaultRelays =
|
||||
await this.fetchProfileEventFromDefaultRelaysDataloader.load(pubkey)
|
||||
if (profileFromDefaultRelays) {
|
||||
return profileFromDefaultRelays
|
||||
}
|
||||
|
|
@ -524,15 +530,11 @@ class ClientService extends EventTarget {
|
|||
},
|
||||
true
|
||||
)
|
||||
const profile = profileEvent
|
||||
? this.parseProfileFromEvent(profileEvent)
|
||||
: { pubkey, username: formatPubkey(pubkey) }
|
||||
|
||||
if (pubkey !== id) {
|
||||
this.profileDataloader.prime(pubkey, Promise.resolve(profile))
|
||||
this.profileEventDataloader.prime(pubkey, Promise.resolve(profileEvent))
|
||||
}
|
||||
|
||||
return profile
|
||||
return profileEvent
|
||||
}
|
||||
|
||||
private async tryHarderToFetchEvent(
|
||||
|
|
@ -567,7 +569,7 @@ class ClientService extends EventTarget {
|
|||
return ids.map((id) => eventsMap.get(id))
|
||||
}
|
||||
|
||||
private async profileBatchLoadFn(pubkeys: readonly string[]) {
|
||||
private async profileEventBatchLoadFn(pubkeys: readonly string[]) {
|
||||
const events = await this.pool.querySync(this.defaultRelayUrls, {
|
||||
authors: Array.from(new Set(pubkeys)),
|
||||
kinds: [kinds.Metadata],
|
||||
|
|
@ -583,12 +585,11 @@ class ClientService extends EventTarget {
|
|||
}
|
||||
|
||||
return pubkeys.map((pubkey) => {
|
||||
const event = eventsMap.get(pubkey)
|
||||
return event ? this.parseProfileFromEvent(event) : undefined
|
||||
return eventsMap.get(pubkey)
|
||||
})
|
||||
}
|
||||
|
||||
private async relayListBatchLoadFn(pubkeys: readonly string[]) {
|
||||
private async relayListEventBatchLoadFn(pubkeys: readonly string[]) {
|
||||
const events = await this.pool.querySync(this.defaultRelayUrls, {
|
||||
authors: pubkeys as string[],
|
||||
kinds: [kinds.RelayList],
|
||||
|
|
@ -603,34 +604,7 @@ class ClientService extends EventTarget {
|
|||
}
|
||||
}
|
||||
|
||||
return pubkeys.map((pubkey) => {
|
||||
const event = eventsMap.get(pubkey)
|
||||
const relayList = { write: [], read: [] } as TRelayList
|
||||
if (!event) {
|
||||
return { write: BIG_RELAY_URLS, read: BIG_RELAY_URLS }
|
||||
}
|
||||
|
||||
event.tags.filter(tagNameEquals('r')).forEach(([, url, type]) => {
|
||||
if (!url || !isWebsocketUrl(url)) return
|
||||
|
||||
const normalizedUrl = normalizeUrl(url)
|
||||
switch (type) {
|
||||
case 'w':
|
||||
relayList.write.push(normalizedUrl)
|
||||
break
|
||||
case 'r':
|
||||
relayList.read.push(normalizedUrl)
|
||||
break
|
||||
default:
|
||||
relayList.write.push(normalizedUrl)
|
||||
relayList.read.push(normalizedUrl)
|
||||
}
|
||||
})
|
||||
return {
|
||||
write: relayList.write.length ? relayList.write.slice(0, 10) : BIG_RELAY_URLS,
|
||||
read: relayList.read.length ? relayList.read.slice(0, 10) : BIG_RELAY_URLS
|
||||
}
|
||||
})
|
||||
return pubkeys.map((pubkey) => eventsMap.get(pubkey))
|
||||
}
|
||||
|
||||
private async _fetchFollowListEvent(pubkey: string) {
|
||||
|
|
@ -645,31 +619,6 @@ class ClientService extends EventTarget {
|
|||
|
||||
return followListEvents.sort((a, b) => b.created_at - a.created_at)[0]
|
||||
}
|
||||
|
||||
private parseProfileFromEvent(event: NEvent): TProfile {
|
||||
try {
|
||||
const profileObj = JSON.parse(event.content)
|
||||
return {
|
||||
pubkey: event.pubkey,
|
||||
banner: profileObj.banner,
|
||||
avatar: profileObj.picture,
|
||||
username:
|
||||
profileObj.display_name?.trim() ||
|
||||
profileObj.name?.trim() ||
|
||||
profileObj.nip05?.split('@')[0]?.trim() ||
|
||||
formatPubkey(event.pubkey),
|
||||
nip05: profileObj.nip05,
|
||||
about: profileObj.about,
|
||||
created_at: event.created_at
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return {
|
||||
pubkey: event.pubkey,
|
||||
username: formatPubkey(event.pubkey)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const instance = ClientService.getInstance()
|
||||
|
|
|
|||
|
|
@ -1,15 +1,8 @@
|
|||
import { StorageKey } from '@/constants'
|
||||
import { isSameAccount } from '@/lib/account'
|
||||
import { randomString } from '@/lib/random'
|
||||
import {
|
||||
TAccount,
|
||||
TAccountPointer,
|
||||
TFeedType,
|
||||
TProfile,
|
||||
TRelayList,
|
||||
TRelaySet,
|
||||
TThemeSetting
|
||||
} from '@/types'
|
||||
import { TAccount, TAccountPointer, TFeedType, TRelaySet, TThemeSetting } from '@/types'
|
||||
import { Event } from 'nostr-tools'
|
||||
|
||||
const DEFAULT_RELAY_SETS: TRelaySet[] = [
|
||||
{
|
||||
|
|
@ -33,9 +26,9 @@ class StorageService {
|
|||
private themeSetting: TThemeSetting = 'system'
|
||||
private accounts: TAccount[] = []
|
||||
private currentAccount: TAccount | null = null
|
||||
private accountRelayListMap: Record<string, TRelayList | undefined> = {} // pubkey -> relayList
|
||||
private accountFollowingsMap: Record<string, string[] | undefined> = {} // pubkey -> followings
|
||||
private accountProfileMap: Record<string, TProfile> = {} // pubkey -> profile
|
||||
private accountRelayListEventMap: Record<string, Event | undefined> = {} // pubkey -> relayListEvent
|
||||
private accountFollowListEventMap: Record<string, Event | undefined> = {} // pubkey -> followListEvent
|
||||
private accountProfileEventMap: Record<string, Event> = {} // pubkey -> profileEvent
|
||||
|
||||
constructor() {
|
||||
if (!StorageService.instance) {
|
||||
|
|
@ -54,12 +47,25 @@ class StorageService {
|
|||
this.currentAccount = currentAccountStr ? JSON.parse(currentAccountStr) : null
|
||||
const feedTypeStr = window.localStorage.getItem(StorageKey.FEED_TYPE)
|
||||
this.feedType = feedTypeStr ? JSON.parse(feedTypeStr) : 'relays'
|
||||
const accountRelayListMapStr = window.localStorage.getItem(StorageKey.ACCOUNT_RELAY_LIST_MAP)
|
||||
this.accountRelayListMap = accountRelayListMapStr ? JSON.parse(accountRelayListMapStr) : {}
|
||||
const accountFollowingsMapStr = window.localStorage.getItem(StorageKey.ACCOUNT_FOLLOWINGS_MAP)
|
||||
this.accountFollowingsMap = accountFollowingsMapStr ? JSON.parse(accountFollowingsMapStr) : {}
|
||||
const accountProfileMapStr = window.localStorage.getItem(StorageKey.ACCOUNT_PROFILE_MAP)
|
||||
this.accountProfileMap = accountProfileMapStr ? JSON.parse(accountProfileMapStr) : {}
|
||||
|
||||
const accountRelayListEventMapStr = window.localStorage.getItem(
|
||||
StorageKey.ACCOUNT_RELAY_LIST_EVENT_MAP
|
||||
)
|
||||
this.accountRelayListEventMap = accountRelayListEventMapStr
|
||||
? JSON.parse(accountRelayListEventMapStr)
|
||||
: {}
|
||||
const accountFollowListEventMapStr = window.localStorage.getItem(
|
||||
StorageKey.ACCOUNT_FOLLOW_LIST_EVENT_MAP
|
||||
)
|
||||
this.accountFollowListEventMap = accountFollowListEventMapStr
|
||||
? JSON.parse(accountFollowListEventMapStr)
|
||||
: {}
|
||||
const accountProfileEventMapStr = window.localStorage.getItem(
|
||||
StorageKey.ACCOUNT_PROFILE_EVENT_MAP
|
||||
)
|
||||
this.accountProfileEventMap = accountProfileEventMapStr
|
||||
? JSON.parse(accountProfileEventMapStr)
|
||||
: {}
|
||||
|
||||
const relaySetsStr = window.localStorage.getItem(StorageKey.RELAY_SETS)
|
||||
if (!relaySetsStr) {
|
||||
|
|
@ -155,21 +161,21 @@ class StorageService {
|
|||
|
||||
removeAccount(account: TAccount) {
|
||||
this.accounts = this.accounts.filter((act) => !isSameAccount(act, account))
|
||||
delete this.accountFollowingsMap[account.pubkey]
|
||||
delete this.accountRelayListMap[account.pubkey]
|
||||
delete this.accountProfileMap[account.pubkey]
|
||||
delete this.accountFollowListEventMap[account.pubkey]
|
||||
delete this.accountRelayListEventMap[account.pubkey]
|
||||
delete this.accountProfileEventMap[account.pubkey]
|
||||
window.localStorage.setItem(StorageKey.ACCOUNTS, JSON.stringify(this.accounts))
|
||||
window.localStorage.setItem(
|
||||
StorageKey.ACCOUNT_FOLLOWINGS_MAP,
|
||||
JSON.stringify(this.accountFollowingsMap)
|
||||
StorageKey.ACCOUNT_FOLLOW_LIST_EVENT_MAP,
|
||||
JSON.stringify(this.accountFollowListEventMap)
|
||||
)
|
||||
window.localStorage.setItem(
|
||||
StorageKey.ACCOUNT_RELAY_LIST_MAP,
|
||||
JSON.stringify(this.accountRelayListMap)
|
||||
StorageKey.ACCOUNT_RELAY_LIST_EVENT_MAP,
|
||||
JSON.stringify(this.accountRelayListEventMap)
|
||||
)
|
||||
window.localStorage.setItem(
|
||||
StorageKey.ACCOUNT_PROFILE_MAP,
|
||||
JSON.stringify(this.accountProfileMap)
|
||||
StorageKey.ACCOUNT_PROFILE_EVENT_MAP,
|
||||
JSON.stringify(this.accountProfileEventMap)
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -185,40 +191,64 @@ class StorageService {
|
|||
window.localStorage.setItem(StorageKey.CURRENT_ACCOUNT, JSON.stringify(act))
|
||||
}
|
||||
|
||||
getAccountRelayList(pubkey: string) {
|
||||
return this.accountRelayListMap[pubkey]
|
||||
getAccountRelayListEvent(pubkey: string) {
|
||||
return this.accountRelayListEventMap[pubkey]
|
||||
}
|
||||
|
||||
setAccountRelayList(pubkey: string, relayList: TRelayList) {
|
||||
this.accountRelayListMap[pubkey] = relayList
|
||||
setAccountRelayListEvent(relayListEvent: Event) {
|
||||
const pubkey = relayListEvent.pubkey
|
||||
if (
|
||||
this.accountRelayListEventMap[pubkey] &&
|
||||
this.accountRelayListEventMap[pubkey].created_at > relayListEvent.created_at
|
||||
) {
|
||||
return false
|
||||
}
|
||||
this.accountRelayListEventMap[pubkey] = relayListEvent
|
||||
window.localStorage.setItem(
|
||||
StorageKey.ACCOUNT_RELAY_LIST_MAP,
|
||||
JSON.stringify(this.accountRelayListMap)
|
||||
StorageKey.ACCOUNT_RELAY_LIST_EVENT_MAP,
|
||||
JSON.stringify(this.accountRelayListEventMap)
|
||||
)
|
||||
return true
|
||||
}
|
||||
|
||||
getAccountFollowings(pubkey: string) {
|
||||
return this.accountFollowingsMap[pubkey]
|
||||
getAccountFollowListEvent(pubkey: string) {
|
||||
return this.accountFollowListEventMap[pubkey]
|
||||
}
|
||||
|
||||
setAccountFollowings(pubkey: string, followings: string[]) {
|
||||
this.accountFollowingsMap[pubkey] = followings
|
||||
setAccountFollowListEvent(followListEvent: Event) {
|
||||
const pubkey = followListEvent.pubkey
|
||||
if (
|
||||
this.accountFollowListEventMap[pubkey] &&
|
||||
this.accountFollowListEventMap[pubkey].created_at > followListEvent.created_at
|
||||
) {
|
||||
return false
|
||||
}
|
||||
this.accountFollowListEventMap[pubkey] = followListEvent
|
||||
window.localStorage.setItem(
|
||||
StorageKey.ACCOUNT_FOLLOWINGS_MAP,
|
||||
JSON.stringify(this.accountFollowingsMap)
|
||||
StorageKey.ACCOUNT_FOLLOW_LIST_EVENT_MAP,
|
||||
JSON.stringify(this.accountFollowListEventMap)
|
||||
)
|
||||
return true
|
||||
}
|
||||
|
||||
getAccountProfile(pubkey: string) {
|
||||
return this.accountProfileMap[pubkey]
|
||||
getAccountProfileEvent(pubkey: string) {
|
||||
return this.accountProfileEventMap[pubkey]
|
||||
}
|
||||
|
||||
setAccountProfile(pubkey: string, profile: TProfile) {
|
||||
this.accountProfileMap[pubkey] = profile
|
||||
setAccountProfileEvent(profileEvent: Event) {
|
||||
const pubkey = profileEvent.pubkey
|
||||
if (
|
||||
this.accountProfileEventMap[pubkey] &&
|
||||
this.accountProfileEventMap[pubkey].created_at > profileEvent.created_at
|
||||
) {
|
||||
return false
|
||||
}
|
||||
this.accountProfileEventMap[pubkey] = profileEvent
|
||||
window.localStorage.setItem(
|
||||
StorageKey.ACCOUNT_PROFILE_MAP,
|
||||
JSON.stringify(this.accountProfileMap)
|
||||
StorageKey.ACCOUNT_PROFILE_EVENT_MAP,
|
||||
JSON.stringify(this.accountProfileEventMap)
|
||||
)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue