Move getRelayUrls to getRelaysFromList

This commit is contained in:
Jon Staab
2025-04-24 17:10:41 -07:00
parent 3d1a6a106e
commit 3636064f25
6 changed files with 46 additions and 48 deletions
+7 -37
View File
@@ -5,34 +5,15 @@ import {
normalizeRelayUrl,
asDecryptedEvent,
readList,
getListTags,
getRelayTags,
getRelayTagValues,
getRelaysFromList,
} from "@welshman/util"
import {TrustedEvent, PublishedList, List} from "@welshman/util"
import {TrustedEvent, PublishedList, RelayMode, List} from "@welshman/util"
import {request} from "@welshman/net"
import {deriveEventsMapped} from "@welshman/store"
import {Router, RelayMode} from "@welshman/router"
import {Router} from "@welshman/router"
import {repository} from "./core.js"
import {collection} from "./collection.js"
export const getRelayUrls = (list?: List): string[] =>
uniq(getRelayTagValues(getListTags(list)).map(normalizeRelayUrl))
export const getReadRelayUrls = (list?: List): string[] =>
uniq(
getRelayTags(getListTags(list))
.filter((t: string[]) => !t[2] || t[2] === "read")
.map((t: string[]) => normalizeRelayUrl(t[1])),
)
export const getWriteRelayUrls = (list?: List): string[] =>
uniq(
getRelayTags(getListTags(list))
.filter((t: string[]) => !t[2] || t[2] === "write")
.map((t: string[]) => normalizeRelayUrl(t[1])),
)
export type OutboxLoaderRequest = {
pubkey: string
relays: string[]
@@ -78,21 +59,10 @@ export const {
load: makeOutboxLoader(RELAYS),
})
export const getPubkeyRelays = (pubkey: string, mode?: string) => {
const $relaySelections = relaySelectionsByPubkey.get()
const $inboxSelections = inboxRelaySelectionsByPubkey.get()
switch (mode) {
case RelayMode.Read:
return getReadRelayUrls($relaySelections.get(pubkey))
case RelayMode.Write:
return getWriteRelayUrls($relaySelections.get(pubkey))
case RelayMode.Inbox:
return getRelayUrls($inboxSelections.get(pubkey))
default:
return getRelayUrls($relaySelections.get(pubkey))
}
}
export const getPubkeyRelays = (pubkey: string, mode?: string) =>
mode === RelayMode.Inbox
? getRelaysFromList(inboxRelaySelectionsByPubkey.get().get(pubkey))
: getRelaysFromList(relaySelectionsByPubkey.get().get(pubkey), mode)
export const inboxRelaySelections = deriveEventsMapped<PublishedList>(repository, {
filters: [{kinds: [INBOX_RELAYS]}],
+2 -1
View File
@@ -21,7 +21,8 @@
},
"dependencies": {
"@welshman/lib": "workspace:*",
"@welshman/util": "workspace:*"
"@welshman/util": "workspace:*",
"@welshman/relay": "workspace:*"
},
"devDependencies": {
"rimraf": "~6.0.0",
+15 -7
View File
@@ -1,4 +1,5 @@
import {
uniq,
intersection,
mergeLeft,
first,
@@ -29,7 +30,12 @@ import {
normalizeRelayUrl,
TrustedEvent,
Filter,
readList,
asDecryptedEvent,
getRelaysFromList,
RelayMode,
} from "@welshman/util"
import {Repository} from "@welshman/relay"
export const INDEXED_KINDS = [PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS]
@@ -38,12 +44,6 @@ export type RelaysAndFilters = {
filters: Filter[]
}
export enum RelayMode {
Read = "read",
Write = "write",
Inbox = "inbox",
}
export type RouterOptions = {
/**
* Retrieves the user's public key.
@@ -114,7 +114,15 @@ export const addMaximalFallbacks = (count: number, limit: number) => limit - cou
// Router class
export const routerContext: RouterOptions = {}
export const routerContext: RouterOptions = {
getPubkeyRelays: (pubkey: string, mode?: RelayMode) => {
return uniq(
Repository.get()
.query([{kinds: [RELAYS], authors: [pubkey]}])
.flatMap(event => getRelaysFromList(readList(asDecryptedEvent(event)), mode))
)
}
}
export class Router {
readonly options: RouterOptions
+13 -3
View File
@@ -1,7 +1,7 @@
import {parseJson, nthEq} from "@welshman/lib"
import {parseJson, uniq, nthEq} from "@welshman/lib"
import {Address} from "./Address.js"
import {uniqTags} from "./Tags.js"
import {isRelayUrl} from "./Relay.js"
import {uniqTags, getRelayTags} from "./Tags.js"
import {isRelayUrl, normalizeRelayUrl} from "./Relay.js"
import {Encryptable, DecryptedEvent} from "./Encryptable.js"
import type {EncryptableUpdates} from "./Encryptable.js"
@@ -88,3 +88,13 @@ export const addToListPrivately = (list: List, ...tags: string[][]) => {
content: JSON.stringify(uniqTags([...list.privateTags, ...tags])),
})
}
export const getRelaysFromList = (list?: List, mode?: string): string[] => {
let tags = getRelayTags(getListTags(list))
if (mode) {
tags = tags.filter((t: string[]) => !t[2] || t[2] === mode)
}
return uniq(tags.map(t => normalizeRelayUrl(t[1])))
}
+6
View File
@@ -2,6 +2,12 @@ import {last, normalizeUrl, stripProtocol} from "@welshman/lib"
// Constants and types
export enum RelayMode {
Read = "read",
Write = "write",
Inbox = "inbox",
}
export type RelayProfile = {
url: string
icon?: string