Merge branch 'add-sticky-to-relay' into 'main'

Sticky Notes: add "ditto:relay" in /relay endpoint

See merge request soapbox-pub/ditto!512
This commit is contained in:
Alex Gleason 2024-09-23 18:50:58 +00:00
commit e0b966bb6d
2 changed files with 9 additions and 6 deletions

View file

@ -1,3 +1,4 @@
import { Stickynotes } from '@soapbox/stickynotes';
import TTLCache from '@isaacs/ttlcache'; import TTLCache from '@isaacs/ttlcache';
import { import {
NostrClientCLOSE, NostrClientCLOSE,
@ -29,6 +30,8 @@ const limiter = new TTLCache<string, number>();
/** Connections for metrics purposes. */ /** Connections for metrics purposes. */
const connections = new Set<WebSocket>(); const connections = new Set<WebSocket>();
const console = new Stickynotes('ditto:relay');
/** Set up the Websocket connection. */ /** Set up the Websocket connection. */
function connectStream(socket: WebSocket, ip: string | undefined) { function connectStream(socket: WebSocket, ip: string | undefined) {
const controllers = new Map<string, AbortController>(); const controllers = new Map<string, AbortController>();

View file

@ -1,5 +1,5 @@
import { NKinds, NostrEvent, NSchema as n } from '@nostrify/nostrify'; import { NKinds, NostrEvent, NSchema as n } from '@nostrify/nostrify';
import Debug from '@soapbox/stickynotes/debug'; import { Stickynotes } from '@soapbox/stickynotes';
import ISO6391 from 'iso-639-1'; import ISO6391 from 'iso-639-1';
import { Kysely, sql } from 'kysely'; import { Kysely, sql } from 'kysely';
import lande from 'lande'; import lande from 'lande';
@ -23,7 +23,7 @@ import { purifyEvent } from '@/utils/purify.ts';
import { updateStats } from '@/utils/stats.ts'; import { updateStats } from '@/utils/stats.ts';
import { getTagSet } from '@/utils/tags.ts'; import { getTagSet } from '@/utils/tags.ts';
const debug = Debug('ditto:pipeline'); const console = new Stickynotes('ditto:pipeline');
/** /**
* Common pipeline function to process (and maybe store) events. * Common pipeline function to process (and maybe store) events.
@ -41,7 +41,7 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
if (encounterEvent(event)) return; if (encounterEvent(event)) return;
if (await existsInDB(event)) return; if (await existsInDB(event)) return;
debug(`NostrEvent<${event.kind}> ${event.id}`); console.info(`NostrEvent<${event.kind}> ${event.id}`);
pipelineEventsCounter.inc({ kind: event.kind }); pipelineEventsCounter.inc({ kind: event.kind });
if (isProtectedEvent(event)) { if (isProtectedEvent(event)) {
@ -76,18 +76,18 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
} }
async function policyFilter(event: NostrEvent): Promise<void> { async function policyFilter(event: NostrEvent): Promise<void> {
const debug = Debug('ditto:policy'); const console = new Stickynotes('ditto:policy');
try { try {
const result = await policyWorker.call(event); const result = await policyWorker.call(event);
policyEventsCounter.inc({ ok: String(result[2]) }); policyEventsCounter.inc({ ok: String(result[2]) });
debug(JSON.stringify(result)); console.log(JSON.stringify(result));
RelayError.assert(result); RelayError.assert(result);
} catch (e) { } catch (e) {
if (e instanceof RelayError) { if (e instanceof RelayError) {
throw e; throw e;
} else { } else {
console.error('POLICY ERROR:', e); console.error(e);
throw new RelayError('blocked', 'policy error'); throw new RelayError('blocked', 'policy error');
} }
} }