Break down relay utils, replace removeNil with removeUndefined

This commit is contained in:
Jon Staab
2025-11-11 14:05:30 -08:00
parent 4c1c138329
commit e582d46afd
8 changed files with 81 additions and 42 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
import {get} from "svelte/store"
import {uniq, nthNe, removeNil, nthEq} from "@welshman/lib"
import {uniq, nthNe, removeUndefined, nthEq} from "@welshman/lib"
import {
sendManagementRequest,
ManagementRequest,
@@ -75,7 +75,7 @@ export const addRelay = async (url: string, mode: RelayMode) => {
const list = get(userRelaySelections) || makeList({kind: RELAYS})
const dup = getRelayTags(getListTags(list)).find(nthEq(1, url))
const tag = removeNil(["r", url, dup && dup[2] !== mode ? undefined : mode])
const tag = removeUndefined(["r", url, dup && dup[2] !== mode ? undefined : mode])
const tags = [...list.publicTags.filter(nthNe(1, url)), tag]
const event = {kind: list.kind, content: list.event?.content || "", tags}
const relays = Router.get().FromUser().policy(addMaximalFallbacks).getUrls()
+52 -24
View File
@@ -1,6 +1,15 @@
import {writable, derived} from "svelte/store"
import {
uniq,
removeUndefined,
prop,
indexBy,
batcher,
fetchJson,
postJson,
Maybe,
} from "@welshman/lib"
import {withGetter} from "@welshman/store"
import {uniq, batcher, postJson} from "@welshman/lib"
import {RelayProfile} from "@welshman/util"
import {normalizeRelayUrl, displayRelayUrl, displayRelayProfile, isRelayUrl} from "@welshman/util"
import {collection} from "@welshman/store"
@@ -8,36 +17,55 @@ import {appContext} from "./context.js"
export const relays = withGetter(writable<RelayProfile[]>([]))
export const fetchRelayProfiles = async (urls: string[]) => {
export const fetchRelayProfileDirectly = async (url: string): Promise<Maybe<RelayProfile>> => {
try {
return fetchJson(url.replace(/^ws/, "http"), {
headers: {
Accept: "application/nostr+json",
},
})
} catch (e) {
// pass
}
}
export const fetchRelayProfilesDirectly = async (
urls: string[],
): Promise<Map<string, RelayProfile>> =>
indexBy(
prop("url"),
removeUndefined(
await Promise.all(
urls.map(async url => {
const profile = await fetchRelayProfileDirectly(url)
if (profile) {
return {...profile, url}
}
}),
),
),
)
export const fetchRelayProfilesUsingProxy = async (
proxy: string,
urls: string[],
): Promise<Map<string, RelayProfile>> => {
const profilesByUrl = new Map<string, RelayProfile>()
const res: any = await postJson(`${proxy}/relay/info`, {urls})
if (appContext.dufflepudUrl) {
const res: any = await postJson(`${appContext.dufflepudUrl}/relay/info`, {urls})
for (const {url, info} of res?.data || []) {
profilesByUrl.set(url, info)
}
} else {
await Promise.all(
urls.map(async url => {
try {
const res = await fetch(url.replace(/^ws/, "http"), {
headers: {
Accept: "application/nostr+json",
},
})
profilesByUrl.set(url, await res.json())
} catch (e) {
// pass
}
}),
)
for (const {url, info} of res?.data || []) {
profilesByUrl.set(url, info)
}
return profilesByUrl
}
export const fetchRelayProfiles = (urls: string[]) =>
appContext.dufflepudUrl
? fetchRelayProfilesUsingProxy(appContext.dufflepudUrl, urls)
: fetchRelayProfilesDirectly(urls)
export const {
indexStore: relaysByUrl,
deriveItem: deriveRelay,
+5 -5
View File
@@ -1,7 +1,7 @@
import {writable, derived} from "svelte/store"
import {Zapper, TrustedEvent, Zap, getTagValues, getLnUrl, zapFromEvent} from "@welshman/util"
import {
removeNil,
removeUndefined,
fetchJson,
uniq,
bech32ToHex,
@@ -22,7 +22,7 @@ export const fetchZappers = async (lnurls: string[]) => {
// Use dufflepud if we it's set up to protect user privacy, otherwise fetch directly
if (base) {
const hexUrls = removeNil(lnurls.map(lnurl => tryCatch(() => bech32ToHex(lnurl))))
const hexUrls = removeUndefined(lnurls.map(lnurl => tryCatch(() => bech32ToHex(lnurl))))
if (hexUrls.length > 0) {
const res: any = await tryCatch(
@@ -102,7 +102,7 @@ export const deriveZapperForPubkey = (pubkey: string, relays: string[] = []) =>
})
export const getLnUrlsForEvent = async (event: TrustedEvent) => {
const lnurls = removeNil(getTagValues("zap", event.tags).map(getLnUrl))
const lnurls = removeUndefined(getTagValues("zap", event.tags).map(getLnUrl))
if (lnurls.length > 0) {
return lnurls
@@ -110,7 +110,7 @@ export const getLnUrlsForEvent = async (event: TrustedEvent) => {
const profile = await loadProfile(event.pubkey)
return removeNil([profile?.lnurl])
return removeUndefined([profile?.lnurl])
}
export const getZapperForZap = async (zap: TrustedEvent, parent: TrustedEvent) => {
@@ -126,7 +126,7 @@ export const getValidZap = async (zap: TrustedEvent, parent: TrustedEvent) => {
}
export const getValidZaps = async (zaps: TrustedEvent[], parent: TrustedEvent) =>
removeNil(await Promise.all(zaps.map(zap => getValidZap(zap, parent))))
removeUndefined(await Promise.all(zaps.map(zap => getValidZap(zap, parent))))
export const deriveValidZaps = (zaps: TrustedEvent[], parent: TrustedEvent) => {
const store = writable<Zap[]>([])