Normalize relay urls before putting them in the database
This commit is contained in:
@@ -23,6 +23,7 @@ export const {
|
||||
indexStore: profilesByPubkey,
|
||||
deriveItem: deriveProfile,
|
||||
loadItem: loadProfile,
|
||||
getItem: getProfile,
|
||||
} = collection({
|
||||
name: "profiles",
|
||||
store: profiles,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -2,6 +2,7 @@ import {writable, derived} from 'svelte/store'
|
||||
import {withGetter} from '@welshman/store'
|
||||
import {ctx, groupBy, indexBy, batch, now, uniq, batcher, postJson} from '@welshman/lib'
|
||||
import type {RelayProfile} from "@welshman/util"
|
||||
import {normalizeRelayUrl} from "@welshman/util"
|
||||
import {AuthStatus, asMessage, type Connection, type SocketMessage} from '@welshman/net'
|
||||
import {createSearch} from './util'
|
||||
import {collection} from './collection'
|
||||
@@ -68,7 +69,8 @@ export const {
|
||||
name: "relays",
|
||||
store: relays,
|
||||
getKey: (relay: Relay) => relay.url,
|
||||
load: batcher(800, async (urls: string[]) => {
|
||||
load: batcher(800, async (rawUrls: string[]) => {
|
||||
const urls = rawUrls.map(normalizeRelayUrl)
|
||||
const fresh = await fetchRelayProfiles(uniq(urls))
|
||||
const stale = relaysByUrl.get()
|
||||
|
||||
|
||||
@@ -41,7 +41,12 @@ thunkWorker.addGlobalHandler(async ({event, relays, resolve}: ThunkWithResolve)
|
||||
const pub = publish({event: signedEvent, relays})
|
||||
|
||||
// Copy the signature over since we had deferred it
|
||||
;(repository.getEvent(signedEvent.id) as SignedEvent).sig = signedEvent.sig
|
||||
const savedEvent = repository.getEvent(signedEvent.id) as SignedEvent
|
||||
|
||||
// The event may already be replaced or deleted
|
||||
if (savedEvent) {
|
||||
savedEvent.sig = signedEvent.sig
|
||||
}
|
||||
|
||||
// Track publish success
|
||||
const {id} = event
|
||||
|
||||
@@ -53,12 +53,8 @@ export const isShareableRelayUrl = (url: string) =>
|
||||
!url.slice(6).match(/\/npub/)
|
||||
)
|
||||
|
||||
type NormalizeRelayUrlOpts = {
|
||||
allowInsecure?: boolean
|
||||
}
|
||||
|
||||
export const normalizeRelayUrl = (url: string, {allowInsecure = false}: NormalizeRelayUrlOpts = {}) => {
|
||||
const prefix = allowInsecure ? url.match(/^wss?:\/\//)?.[0] || "wss://" : "wss://"
|
||||
export const normalizeRelayUrl = (url: string) => {
|
||||
const prefix = url.match(/^wss?:\/\//)?.[0] || "wss://"
|
||||
|
||||
// Use our library to normalize
|
||||
url = normalizeUrl(url, {stripHash: true, stripAuthentication: false})
|
||||
|
||||
Reference in New Issue
Block a user