Move getRelayUrls to getRelaysFromList
This commit is contained in:
@@ -5,34 +5,15 @@ import {
|
|||||||
normalizeRelayUrl,
|
normalizeRelayUrl,
|
||||||
asDecryptedEvent,
|
asDecryptedEvent,
|
||||||
readList,
|
readList,
|
||||||
getListTags,
|
getRelaysFromList,
|
||||||
getRelayTags,
|
|
||||||
getRelayTagValues,
|
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
import {TrustedEvent, PublishedList, List} from "@welshman/util"
|
import {TrustedEvent, PublishedList, RelayMode, List} from "@welshman/util"
|
||||||
import {request} from "@welshman/net"
|
import {request} from "@welshman/net"
|
||||||
import {deriveEventsMapped} from "@welshman/store"
|
import {deriveEventsMapped} from "@welshman/store"
|
||||||
import {Router, RelayMode} from "@welshman/router"
|
import {Router} from "@welshman/router"
|
||||||
import {repository} from "./core.js"
|
import {repository} from "./core.js"
|
||||||
import {collection} from "./collection.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 = {
|
export type OutboxLoaderRequest = {
|
||||||
pubkey: string
|
pubkey: string
|
||||||
relays: string[]
|
relays: string[]
|
||||||
@@ -78,21 +59,10 @@ export const {
|
|||||||
load: makeOutboxLoader(RELAYS),
|
load: makeOutboxLoader(RELAYS),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const getPubkeyRelays = (pubkey: string, mode?: string) => {
|
export const getPubkeyRelays = (pubkey: string, mode?: string) =>
|
||||||
const $relaySelections = relaySelectionsByPubkey.get()
|
mode === RelayMode.Inbox
|
||||||
const $inboxSelections = inboxRelaySelectionsByPubkey.get()
|
? getRelaysFromList(inboxRelaySelectionsByPubkey.get().get(pubkey))
|
||||||
|
: getRelaysFromList(relaySelectionsByPubkey.get().get(pubkey), mode)
|
||||||
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 inboxRelaySelections = deriveEventsMapped<PublishedList>(repository, {
|
export const inboxRelaySelections = deriveEventsMapped<PublishedList>(repository, {
|
||||||
filters: [{kinds: [INBOX_RELAYS]}],
|
filters: [{kinds: [INBOX_RELAYS]}],
|
||||||
|
|||||||
@@ -21,7 +21,8 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@welshman/lib": "workspace:*",
|
"@welshman/lib": "workspace:*",
|
||||||
"@welshman/util": "workspace:*"
|
"@welshman/util": "workspace:*",
|
||||||
|
"@welshman/relay": "workspace:*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"rimraf": "~6.0.0",
|
"rimraf": "~6.0.0",
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
uniq,
|
||||||
intersection,
|
intersection,
|
||||||
mergeLeft,
|
mergeLeft,
|
||||||
first,
|
first,
|
||||||
@@ -29,7 +30,12 @@ import {
|
|||||||
normalizeRelayUrl,
|
normalizeRelayUrl,
|
||||||
TrustedEvent,
|
TrustedEvent,
|
||||||
Filter,
|
Filter,
|
||||||
|
readList,
|
||||||
|
asDecryptedEvent,
|
||||||
|
getRelaysFromList,
|
||||||
|
RelayMode,
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
|
import {Repository} from "@welshman/relay"
|
||||||
|
|
||||||
export const INDEXED_KINDS = [PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS]
|
export const INDEXED_KINDS = [PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS]
|
||||||
|
|
||||||
@@ -38,12 +44,6 @@ export type RelaysAndFilters = {
|
|||||||
filters: Filter[]
|
filters: Filter[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum RelayMode {
|
|
||||||
Read = "read",
|
|
||||||
Write = "write",
|
|
||||||
Inbox = "inbox",
|
|
||||||
}
|
|
||||||
|
|
||||||
export type RouterOptions = {
|
export type RouterOptions = {
|
||||||
/**
|
/**
|
||||||
* Retrieves the user's public key.
|
* Retrieves the user's public key.
|
||||||
@@ -114,7 +114,15 @@ export const addMaximalFallbacks = (count: number, limit: number) => limit - cou
|
|||||||
|
|
||||||
// Router class
|
// 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 {
|
export class Router {
|
||||||
readonly options: RouterOptions
|
readonly options: RouterOptions
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {parseJson, nthEq} from "@welshman/lib"
|
import {parseJson, uniq, nthEq} from "@welshman/lib"
|
||||||
import {Address} from "./Address.js"
|
import {Address} from "./Address.js"
|
||||||
import {uniqTags} from "./Tags.js"
|
import {uniqTags, getRelayTags} from "./Tags.js"
|
||||||
import {isRelayUrl} from "./Relay.js"
|
import {isRelayUrl, normalizeRelayUrl} from "./Relay.js"
|
||||||
import {Encryptable, DecryptedEvent} from "./Encryptable.js"
|
import {Encryptable, DecryptedEvent} from "./Encryptable.js"
|
||||||
import type {EncryptableUpdates} 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])),
|
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])))
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,12 @@ import {last, normalizeUrl, stripProtocol} from "@welshman/lib"
|
|||||||
|
|
||||||
// Constants and types
|
// Constants and types
|
||||||
|
|
||||||
|
export enum RelayMode {
|
||||||
|
Read = "read",
|
||||||
|
Write = "write",
|
||||||
|
Inbox = "inbox",
|
||||||
|
}
|
||||||
|
|
||||||
export type RelayProfile = {
|
export type RelayProfile = {
|
||||||
url: string
|
url: string
|
||||||
icon?: string
|
icon?: string
|
||||||
|
|||||||
Generated
+3
@@ -310,6 +310,9 @@ importers:
|
|||||||
'@welshman/lib':
|
'@welshman/lib':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../lib
|
version: link:../lib
|
||||||
|
'@welshman/relay':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../relay
|
||||||
'@welshman/util':
|
'@welshman/util':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../util
|
version: link:../util
|
||||||
|
|||||||
Reference in New Issue
Block a user