Refactor loopback.ts

This commit is contained in:
Alex Gleason 2023-07-25 18:22:05 -05:00
parent 33f87822d4
commit 79ec5dd4e0
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 16 additions and 3 deletions

View File

@ -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
}
});
}