Normalize relay urls before putting them in the database

This commit is contained in:
Jon Staab
2024-10-03 12:20:34 -07:00
parent 78c8c9e501
commit 63761c91c8
5 changed files with 29 additions and 16 deletions
+17 -8
View File
@@ -1,3 +1,4 @@
import {uniq} from '@welshman/lib'
import {INBOX_RELAYS, RELAYS, getRelayTags, normalizeRelayUrl, type TrustedEvent} from '@welshman/util'
import {type SubscribeRequestWithHandlers} from "@welshman/net"
import {deriveEvents, withGetter} from '@welshman/store'
@@ -5,18 +6,24 @@ import {load, repository} from './core'
import {collection} from './collection'
export const getRelayUrls = (event?: TrustedEvent): string[] =>
getRelayTags(event?.tags || [])
.map((t: string[]) => normalizeRelayUrl(t[1]))
uniq(
getRelayTags(event?.tags || [])
.map((t: string[]) => normalizeRelayUrl(t[1]))
)
export const getReadRelayUrls = (event?: TrustedEvent): string[] =>
getRelayTags(event?.tags || [])
.filter((t: string[]) => !t[2] || t[2] === "read")
.map((t: string[]) => normalizeRelayUrl(t[1]))
uniq(
getRelayTags(event?.tags || [])
.filter((t: string[]) => !t[2] || t[2] === "read")
.map((t: string[]) => normalizeRelayUrl(t[1]))
)
export const getWriteRelayUrls = (event?: TrustedEvent): string[] =>
getRelayTags(event?.tags || [])
.filter((t: string[]) => !t[2] || t[2] === "write")
.map((t: string[]) => normalizeRelayUrl(t[1]))
uniq(
getRelayTags(event?.tags || [])
.filter((t: string[]) => !t[2] || t[2] === "write")
.map((t: string[]) => normalizeRelayUrl(t[1]))
)
export const relaySelections = withGetter(deriveEvents(repository, {filters: [{kinds: [RELAYS]}]}))
@@ -24,6 +31,7 @@ export const {
indexStore: relaySelectionsByPubkey,
deriveItem: deriveRelaySelections,
loadItem: loadRelaySelections,
getItem: getRelaySelections,
} = collection({
name: "relaySelections",
store: relaySelections,
@@ -38,6 +46,7 @@ export const {
indexStore: inboxRelaySelectionsByPubkey,
deriveItem: deriveInboxRelaySelections,
loadItem: loadInboxRelaySelections,
getItem: getInboxRelaySelections,
} = collection({
name: "inboxRelaySelections",
store: inboxRelaySelections,