From 79ec5dd4e0677197d8d074def52e6cb1b2cabf9b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 25 Jul 2023 18:22:05 -0500 Subject: [PATCH] Refactor loopback.ts --- src/loopback.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/loopback.ts b/src/loopback.ts index b971501..d7b8069 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 } -}); +}