From e5fadafc7aa9a445c08e50429d04501d82ec4d1a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 8 Jun 2024 12:58:59 -0500 Subject: [PATCH] Create AdminStore to filter out banned users --- src/pipeline.ts | 19 +++++++++--------- src/storages.ts | 8 ++++---- src/storages/AdminStore.ts | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 src/storages/AdminStore.ts diff --git a/src/pipeline.ts b/src/pipeline.ts index c85cc0b..f59a1eb 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,14 +1,11 @@ import { NKinds, NostrEvent, NSchema as n } from '@nostrify/nostrify'; -import { PipePolicy } from '@nostrify/nostrify/policies'; import Debug from '@soapbox/stickynotes/debug'; import { sql } from 'kysely'; import { LRUCache } from 'lru-cache'; -import { Conf } from '@/config.ts'; import { DittoDB } from '@/db/DittoDB.ts'; import { deleteAttachedMedia } from '@/db/unattached-media.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts'; -import { MuteListPolicy } from '@/policies/MuteListPolicy.ts'; import { RelayError } from '@/RelayError.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; import { Storages } from '@/storages.ts'; @@ -44,6 +41,15 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise { const debug = Debug('ditto:policy'); - const policy = new PipePolicy([ - new MuteListPolicy(Conf.pubkey, await Storages.admin()), - policyWorker, - ]); - try { - const result = await policy.call(event); + const result = await policyWorker.call(event); debug(JSON.stringify(result)); RelayError.assert(result); } catch (e) { diff --git a/src/storages.ts b/src/storages.ts index f8f206d..4aaca1c 100644 --- a/src/storages.ts +++ b/src/storages.ts @@ -3,15 +3,15 @@ import { RelayPoolWorker } from 'nostr-relaypool'; import { Conf } from '@/config.ts'; import { DittoDB } from '@/db/DittoDB.ts'; +import { AdminStore } from '@/storages/AdminStore.ts'; import { EventsDB } from '@/storages/EventsDB.ts'; import { PoolStore } from '@/storages/pool-store.ts'; import { SearchStore } from '@/storages/search-store.ts'; import { InternalRelay } from '@/storages/InternalRelay.ts'; -import { UserStore } from '@/storages/UserStore.ts'; export class Storages { private static _db: Promise | undefined; - private static _admin: Promise | undefined; + private static _admin: Promise | undefined; private static _client: Promise | undefined; private static _pubsub: Promise | undefined; private static _search: Promise | undefined; @@ -28,9 +28,9 @@ export class Storages { } /** Admin user storage. */ - public static async admin(): Promise { + public static async admin(): Promise { if (!this._admin) { - this._admin = Promise.resolve(new UserStore(Conf.pubkey, await this.db())); + this._admin = Promise.resolve(new AdminStore(await this.db())); } return this._admin; } diff --git a/src/storages/AdminStore.ts b/src/storages/AdminStore.ts new file mode 100644 index 0000000..6285a14 --- /dev/null +++ b/src/storages/AdminStore.ts @@ -0,0 +1,40 @@ +import { NostrEvent, NostrFilter, NStore } from '@nostrify/nostrify'; + +import { Conf } from '@/config.ts'; +import { DittoEvent } from '@/interfaces/DittoEvent.ts'; +import { getTagSet } from '@/utils/tags.ts'; + +/** A store that prevents banned users from being displayed. */ +export class AdminStore implements NStore { + constructor(private store: NStore) {} + + async event(event: NostrEvent, opts?: { signal?: AbortSignal }): Promise { + return await this.store.event(event, opts); + } + + async query(filters: NostrFilter[], opts: { signal?: AbortSignal; limit?: number } = {}): Promise { + const events = await this.store.query(filters, opts); + + const users = await this.store.query([{ + kinds: [30382], + authors: [Conf.pubkey], + '#d': events.map((event) => event.pubkey), + limit: 1, + }]); + + return events.filter((event) => { + const user = users.find( + ({ kind, pubkey, tags }) => + kind === 30382 && pubkey === Conf.pubkey && tags.find(([name]) => name === 'd')?.[1] === event.pubkey, + ); + + const n = getTagSet(user?.tags ?? [], 'n'); + + if (n.has('disable') || n.has('suspend')) { + return false; + } + + return true; + }); + } +}