diff --git a/src/loopback.ts b/src/loopback.ts index b9715012..d7b8069a 100644 --- a/src/loopback.ts +++ b/src/loopback.ts @@ -3,13 +3,26 @@ import { relayInit } from '@/deps.ts'; import { trends } from '@/trends.ts'; import { nostrNow } from '@/utils.ts'; +import type { Event } from '@/event.ts'; + const relay = relayInit(Conf.relay); await relay.connect(); -const sub = relay.sub([{ kinds: [1], since: nostrNow() }]); +// This file watches all events on your Ditto relay and triggers +// side-effects based on them. This can be used for things like +// notifications, trending hashtag tracking, etc. +relay + .sub([{ kinds: [1], since: nostrNow() }]) + .on('event', handleEvent); -sub.on('event', (event) => { +/** Handle events through the loopback pipeline. */ +function handleEvent(event: Event): void { console.info('loopback event:', event.id); + trackHashtags(event); +} + +/** Track whenever a hashtag is used, for processing trending tags. */ +function trackHashtags(event: Event): void { const tags = event.tags .filter((tag) => tag[0] === 't') .map((tag) => tag[1]); @@ -19,4 +32,4 @@ sub.on('event', (event) => { } catch (_e) { // do nothing } -}); +}