Refactor loopback.ts
This commit is contained in:
parent
33f87822d4
commit
79ec5dd4e0
|
@ -3,13 +3,26 @@ import { relayInit } from '@/deps.ts';
|
||||||
import { trends } from '@/trends.ts';
|
import { trends } from '@/trends.ts';
|
||||||
import { nostrNow } from '@/utils.ts';
|
import { nostrNow } from '@/utils.ts';
|
||||||
|
|
||||||
|
import type { Event } from '@/event.ts';
|
||||||
|
|
||||||
const relay = relayInit(Conf.relay);
|
const relay = relayInit(Conf.relay);
|
||||||
await relay.connect();
|
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);
|
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
|
const tags = event.tags
|
||||||
.filter((tag) => tag[0] === 't')
|
.filter((tag) => tag[0] === 't')
|
||||||
.map((tag) => tag[1]);
|
.map((tag) => tag[1]);
|
||||||
|
@ -19,4 +32,4 @@ sub.on('event', (event) => {
|
||||||
} catch (_e) {
|
} catch (_e) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
Loading…
Reference in New Issue