Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding filter arguments to relay URLs #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/js/Nostr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 21 additions & 2 deletions src/js/lib/nostr-tools/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Filter, matchFilters} from './filter'

export type Relay = {
url: string
filters: Filter[]
status: number
connect: () => void
close: () => void
Expand Down Expand Up @@ -68,14 +69,26 @@ export function relayInit(url: string, knownEvents?: Map<string, Event>): 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
})
}

function connectRelay() {
ws = new WebSocket(url)
ws = new WebSocket(url.split('?')[0])

ws.onopen = () => {
listeners.connect.forEach(cb => cb())
Expand Down Expand Up @@ -230,10 +243,15 @@ export function relayInit(url: string, knownEvents?: Map<string, Event>): 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,
Expand Down Expand Up @@ -268,6 +286,7 @@ export function relayInit(url: string, knownEvents?: Map<string, Event>): Relay

return {
url,
filters,
sub,
on: (type: 'connect' | 'disconnect' | 'notice', cb: any): void => {
listeners[type].push(cb)
Expand Down