ditto/src/loopback.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Conf } from '@/config.ts';
import { RelayPool } from '@/deps.ts';
import { trends } from '@/trends.ts';
import { nostrDate, nostrNow } from '@/utils.ts';
2023-07-25 23:22:05 +00:00
import type { Event } from '@/event.ts';
const relay = new RelayPool([Conf.relay]);
2023-07-25 23:22:05 +00:00
// 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.subscribe(
[{ kinds: [1], since: nostrNow() }],
[Conf.relay],
handleEvent,
undefined,
undefined,
);
2023-07-25 23:22:05 +00:00
/** Handle events through the loopback pipeline. */
function handleEvent(event: Event): void {
console.info('loopback event:', event.id);
2023-07-25 23:22:05 +00:00
trackHashtags(event);
}
/** Track whenever a hashtag is used, for processing trending tags. */
function trackHashtags(event: Event): void {
const date = nostrDate(event.created_at);
const tags = event.tags
.filter((tag) => tag[0] === 't')
2023-07-26 14:40:52 +00:00
.map((tag) => tag[1])
.slice(0, 5);
2023-07-26 01:55:12 +00:00
if (!tags.length) return;
try {
2023-07-26 01:55:12 +00:00
console.info('tracking tags:', tags);
trends.addTagUsages(event.pubkey, tags, date);
} catch (_e) {
// do nothing
}
2023-07-25 23:22:05 +00:00
}