130-fix-empty-relay-issue #131

Merged
eugene merged 7 commits from 130-fix-empty-relay-issue into staging 2024-08-07 15:27:20 +00:00
2 changed files with 29 additions and 17 deletions
Showing only changes of commit 2acb353535 - Show all commits

View File

@ -20,7 +20,8 @@ import {
findRelayListAndUpdateCache,
findRelayListInCache,
getDefaultRelaySet,
getUserRelaySet
getUserRelaySet,
isOlderThanOneWeek
} from '../utils/relays.ts'
export class MetadataController extends EventEmitter {
@ -133,10 +134,8 @@ export class MetadataController extends EventEmitter {
// If cached metadata is found, check its validity
if (cachedMetadataEvent) {
const oneWeekInMS = 7 * 24 * 60 * 60 * 1000 // Number of milliseconds in one week
// Check if the cached metadata is older than one week
if (Date.now() - cachedMetadataEvent.cachedAt > oneWeekInMS) {
if (isOlderThanOneWeek(cachedMetadataEvent.cachedAt)) {
// If older than one week, find the metadata from relays in background
this.checkForMoreRecentMetadata(hexKey, cachedMetadataEvent.event)
@ -164,13 +163,18 @@ export class MetadataController extends EventEmitter {
* or a fallback RelaySet with Sigit's Relay
*/
public findRelayListMetadata = async (hexKey: string): Promise<RelaySet> => {
const relayEvent = await findRelayListInCache(hexKey)
|| await findRelayListAndUpdateCache([this.specialMetadataRelay], hexKey)
|| await findRelayListAndUpdateCache(await this.nostrController.getMostPopularRelays(), hexKey)
const relayEvent =
(await findRelayListInCache(hexKey)) ||
(await findRelayListAndUpdateCache(
[this.specialMetadataRelay],
hexKey
)) ||
(await findRelayListAndUpdateCache(
await this.nostrController.getMostPopularRelays(),
hexKey
))
return relayEvent
? getUserRelaySet(relayEvent.tags)
: getDefaultRelaySet()
return relayEvent ? getUserRelaySet(relayEvent.tags) : getDefaultRelaySet()
}
public extractProfileMetadataContent = (event: Event) => {

View File

@ -5,8 +5,8 @@ import { localCache } from '../services'
import { ONE_WEEK_IN_MS, SIGIT_RELAY } from './const.ts'
import { RelayMap, RelaySet } from '../types'
const READ_MARKER = "read"
const WRITE_MARKET = "write"
const READ_MARKER = 'read'
const WRITE_MARKER = 'write'
/**
* Attempts to find a relay list from the provided lookUpRelays.
@ -15,7 +15,10 @@ const WRITE_MARKET = "write"
* @param hexKey
* @return found relay list or null
*/
const findRelayListAndUpdateCache = async (lookUpRelays: string[], hexKey: string): Promise<Event | null> => {
const findRelayListAndUpdateCache = async (
lookUpRelays: string[],
hexKey: string
): Promise<Event | null> => {
try {
const eventFilter: Filter = {
kinds: [RelayList],
@ -42,10 +45,14 @@ const findRelayListAndUpdateCache = async (lookUpRelays: string[], hexKey: strin
const findRelayListInCache = async (hexKey: string): Promise<Event | null> => {
try {
// Attempt to retrieve the metadata event from the local cache
const cachedRelayListMetadataEvent = await localCache.getUserRelayListMetadata(hexKey)
const cachedRelayListMetadataEvent =
await localCache.getUserRelayListMetadata(hexKey)
// Check if the cached event is not older than one week
if (cachedRelayListMetadataEvent && isOlderThanOneWeek(cachedRelayListMetadataEvent.cachedAt)) {
if (
cachedRelayListMetadataEvent &&
isOlderThanOneWeek(cachedRelayListMetadataEvent.cachedAt)
) {
return cachedRelayListMetadataEvent.event
}
@ -87,7 +94,7 @@ const toRelaySet = (obj: RelaySet, tag: string[]): RelaySet => {
if (marker === READ_MARKER) {
obj.read.push(tag[1])
} else if (marker === WRITE_MARKET) {
} else if (marker === WRITE_MARKER) {
obj.write.push(tag[1])
}
}
@ -105,4 +112,5 @@ export {
getUserRelaySet,
getDefaultRelaySet,
getDefaultRelayMap,
}
isOlderThanOneWeek
}