From 981f8c5d226a39f9bfd88ed0ac2b49425b9429c0 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 28 May 2024 13:59:57 -0500 Subject: [PATCH] relay: remove enforceFilters function --- src/controllers/nostr/relay.ts | 40 ++++++++-------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index 4d23999..6fc08d0 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -4,29 +4,19 @@ import { NostrClientEVENT, NostrClientMsg, NostrClientREQ, - NostrEvent, - NostrFilter, + NostrRelayMsg, NSchema as n, } from '@nostrify/nostrify'; + +import { AppController } from '@/app.ts'; import { relayInfoController } from '@/controllers/nostr/relay-info.ts'; import * as pipeline from '@/pipeline.ts'; import { RelayError } from '@/RelayError.ts'; import { Storages } from '@/storages.ts'; -import type { AppController } from '@/app.ts'; -import { Conf } from '@/config.ts'; - /** Limit of initial events returned for a subscription. */ const FILTER_LIMIT = 100; -/** NIP-01 relay to client message. */ -type RelayMsg = - | ['EVENT', string, NostrEvent] - | ['NOTICE', string] - | ['EOSE', string] - | ['OK', string, boolean, string] - | ['COUNT', string, { count: number; approximate?: boolean }]; - /** Set up the Websocket connection. */ function connectStream(socket: WebSocket) { const controllers = new Map(); @@ -65,17 +55,15 @@ function connectStream(socket: WebSocket) { } /** Handle REQ. Start a subscription. */ - async function handleReq([_, subId, ...rest]: NostrClientREQ): Promise { - const filters = prepareFilters(rest); - + async function handleReq([_, subId, ...filters]: NostrClientREQ): Promise { const controller = new AbortController(); controllers.get(subId)?.abort(); controllers.set(subId, controller); - const db = await Storages.db(); + const store = await Storages.db(); const pubsub = await Storages.pubsub(); - for (const event of await db.query(filters, { limit: FILTER_LIMIT })) { + for (const event of await store.query(filters, { limit: FILTER_LIMIT })) { send(['EVENT', subId, event]); } @@ -118,30 +106,20 @@ function connectStream(socket: WebSocket) { } /** Handle COUNT. Return the number of events matching the filters. */ - async function handleCount([_, subId, ...rest]: NostrClientCOUNT): Promise { + async function handleCount([_, subId, ...filters]: NostrClientCOUNT): Promise { const store = await Storages.db(); - const { count } = await store.count(prepareFilters(rest)); + const { count } = await store.count(filters); send(['COUNT', subId, { count, approximate: false }]); } /** Send a message back to the client. */ - function send(msg: RelayMsg): void { + function send(msg: NostrRelayMsg): void { if (socket.readyState === WebSocket.OPEN) { socket.send(JSON.stringify(msg)); } } } -/** Enforce the filters with certain criteria. */ -function prepareFilters(filters: NostrClientREQ[2][]): NostrFilter[] { - return filters.map((filter) => { - const narrow = Boolean(filter.ids?.length || filter.authors?.length); - const search = narrow ? filter.search : `domain:${Conf.url.host} ${filter.search ?? ''}`; - // Return only local events unless the query is already narrow. - return { ...filter, search }; - }); -} - const relayController: AppController = (c, next) => { const upgrade = c.req.header('upgrade');