From 2eeb647792d97b650d9f06f64b671be72a2ea149 Mon Sep 17 00:00:00 2001 From: Keith Jones Date: Fri, 13 Jan 2023 19:48:57 -0600 Subject: [PATCH] Adding filter arguments to relay URLs --- src/js/Nostr.ts | 4 ++-- src/js/lib/nostr-tools/relay.ts | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/js/Nostr.ts b/src/js/Nostr.ts index e25cf1066..a11e20793 100644 --- a/src/js/Nostr.ts +++ b/src/js/Nostr.ts @@ -194,7 +194,7 @@ export default { const relay = relayInit(url, this.eventsById); relay.on('connect', () => { for (const [name, filters] of this.subscribedFiltersByName.entries()) { - const sub = relay.sub(filters, {}); + const sub = relay.sub(filters, {}, relay.filters); if (!this.subscriptionsByName.has(name)) { this.subscriptionsByName.set(name, new Set()); } @@ -378,7 +378,7 @@ export default { } for (const relay of this.relays.values()) { - const sub = relay.sub(filters, {}); + const sub = relay.sub(filters, {}, relay.filters); // TODO update relay lastSeen sub.on('event', (event) => this.handleEvent(event)); if (once) { diff --git a/src/js/lib/nostr-tools/relay.ts b/src/js/lib/nostr-tools/relay.ts index 86d599423..a64e93c8e 100644 --- a/src/js/lib/nostr-tools/relay.ts +++ b/src/js/lib/nostr-tools/relay.ts @@ -8,6 +8,7 @@ import {Filter, matchFilters} from './filter' export type Relay = { url: string + filters: Filter[] status: number connect: () => void close: () => void @@ -68,6 +69,18 @@ export function relayInit(url: string, knownEvents?: Map): Relay let nextAttemptSeconds = 1 let isConnected = false + const split = url.split('?'); + let filters = []; + + if (split.length > 1) { + filters = split[1].split('&') + .map((filter) => { + if(filter.split('=')[0] == 'z') console.log('FOUND ONE') + let rVal: Filter[] = { ['#' + filter.split('=')[0]]: [filter.split('=')[1]] } + return rVal; + }) + } + function resetOpenState() { untilOpen = new Promise(resolve => { resolveOpen = resolve @@ -75,7 +88,7 @@ export function relayInit(url: string, knownEvents?: Map): Relay } function connectRelay() { - ws = new WebSocket(url) + ws = new WebSocket(url.split('?')[0]) ws.onopen = () => { listeners.connect.forEach(cb => cb()) @@ -230,10 +243,15 @@ export function relayInit(url: string, knownEvents?: Map): Relay { skipVerification = false, id = Math.random().toString().slice(2) - }: SubscriptionOptions = {} + }: SubscriptionOptions = {}, + relayFilters?: Filter[], ): Sub => { let subid = id + if(relayFilters) { + filters = filters.map((item, index) => Object.assign({}, item, relayFilters[index])); + } + openSubs[subid] = { id: subid, filters, @@ -268,6 +286,7 @@ export function relayInit(url: string, knownEvents?: Map): Relay return { url, + filters, sub, on: (type: 'connect' | 'disconnect' | 'notice', cb: any): void => { listeners[type].push(cb)