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
+1
View File
@@ -23,6 +23,7 @@ export const {
indexStore: profilesByPubkey, indexStore: profilesByPubkey,
deriveItem: deriveProfile, deriveItem: deriveProfile,
loadItem: loadProfile, loadItem: loadProfile,
getItem: getProfile,
} = collection({ } = collection({
name: "profiles", name: "profiles",
store: profiles, store: profiles,
+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 {INBOX_RELAYS, RELAYS, getRelayTags, normalizeRelayUrl, type TrustedEvent} from '@welshman/util'
import {type SubscribeRequestWithHandlers} from "@welshman/net" import {type SubscribeRequestWithHandlers} from "@welshman/net"
import {deriveEvents, withGetter} from '@welshman/store' import {deriveEvents, withGetter} from '@welshman/store'
@@ -5,18 +6,24 @@ import {load, repository} from './core'
import {collection} from './collection' import {collection} from './collection'
export const getRelayUrls = (event?: TrustedEvent): string[] => export const getRelayUrls = (event?: TrustedEvent): string[] =>
getRelayTags(event?.tags || []) uniq(
.map((t: string[]) => normalizeRelayUrl(t[1])) getRelayTags(event?.tags || [])
.map((t: string[]) => normalizeRelayUrl(t[1]))
)
export const getReadRelayUrls = (event?: TrustedEvent): string[] => export const getReadRelayUrls = (event?: TrustedEvent): string[] =>
getRelayTags(event?.tags || []) uniq(
.filter((t: string[]) => !t[2] || t[2] === "read") getRelayTags(event?.tags || [])
.map((t: string[]) => normalizeRelayUrl(t[1])) .filter((t: string[]) => !t[2] || t[2] === "read")
.map((t: string[]) => normalizeRelayUrl(t[1]))
)
export const getWriteRelayUrls = (event?: TrustedEvent): string[] => export const getWriteRelayUrls = (event?: TrustedEvent): string[] =>
getRelayTags(event?.tags || []) uniq(
.filter((t: string[]) => !t[2] || t[2] === "write") getRelayTags(event?.tags || [])
.map((t: string[]) => normalizeRelayUrl(t[1])) .filter((t: string[]) => !t[2] || t[2] === "write")
.map((t: string[]) => normalizeRelayUrl(t[1]))
)
export const relaySelections = withGetter(deriveEvents(repository, {filters: [{kinds: [RELAYS]}]})) export const relaySelections = withGetter(deriveEvents(repository, {filters: [{kinds: [RELAYS]}]}))
@@ -24,6 +31,7 @@ export const {
indexStore: relaySelectionsByPubkey, indexStore: relaySelectionsByPubkey,
deriveItem: deriveRelaySelections, deriveItem: deriveRelaySelections,
loadItem: loadRelaySelections, loadItem: loadRelaySelections,
getItem: getRelaySelections,
} = collection({ } = collection({
name: "relaySelections", name: "relaySelections",
store: relaySelections, store: relaySelections,
@@ -38,6 +46,7 @@ export const {
indexStore: inboxRelaySelectionsByPubkey, indexStore: inboxRelaySelectionsByPubkey,
deriveItem: deriveInboxRelaySelections, deriveItem: deriveInboxRelaySelections,
loadItem: loadInboxRelaySelections, loadItem: loadInboxRelaySelections,
getItem: getInboxRelaySelections,
} = collection({ } = collection({
name: "inboxRelaySelections", name: "inboxRelaySelections",
store: inboxRelaySelections, store: inboxRelaySelections,
+3 -1
View File
@@ -2,6 +2,7 @@ import {writable, derived} from 'svelte/store'
import {withGetter} from '@welshman/store' import {withGetter} from '@welshman/store'
import {ctx, groupBy, indexBy, batch, now, uniq, batcher, postJson} from '@welshman/lib' import {ctx, groupBy, indexBy, batch, now, uniq, batcher, postJson} from '@welshman/lib'
import type {RelayProfile} from "@welshman/util" import type {RelayProfile} from "@welshman/util"
import {normalizeRelayUrl} from "@welshman/util"
import {AuthStatus, asMessage, type Connection, type SocketMessage} from '@welshman/net' import {AuthStatus, asMessage, type Connection, type SocketMessage} from '@welshman/net'
import {createSearch} from './util' import {createSearch} from './util'
import {collection} from './collection' import {collection} from './collection'
@@ -68,7 +69,8 @@ export const {
name: "relays", name: "relays",
store: relays, store: relays,
getKey: (relay: Relay) => relay.url, 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 fresh = await fetchRelayProfiles(uniq(urls))
const stale = relaysByUrl.get() const stale = relaysByUrl.get()
+6 -1
View File
@@ -41,7 +41,12 @@ thunkWorker.addGlobalHandler(async ({event, relays, resolve}: ThunkWithResolve)
const pub = publish({event: signedEvent, relays}) const pub = publish({event: signedEvent, relays})
// Copy the signature over since we had deferred it // 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 // Track publish success
const {id} = event const {id} = event
+2 -6
View File
@@ -53,12 +53,8 @@ export const isShareableRelayUrl = (url: string) =>
!url.slice(6).match(/\/npub/) !url.slice(6).match(/\/npub/)
) )
type NormalizeRelayUrlOpts = { export const normalizeRelayUrl = (url: string) => {
allowInsecure?: boolean const prefix = url.match(/^wss?:\/\//)?.[0] || "wss://"
}
export const normalizeRelayUrl = (url: string, {allowInsecure = false}: NormalizeRelayUrlOpts = {}) => {
const prefix = allowInsecure ? url.match(/^wss?:\/\//)?.[0] || "wss://" : "wss://"
// Use our library to normalize // Use our library to normalize
url = normalizeUrl(url, {stripHash: true, stripAuthentication: false}) url = normalizeUrl(url, {stripHash: true, stripAuthentication: false})