ditto/src/loopback.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Conf } from '@/config.ts';
import { insertEvent } from '@/db/events.ts';
import { RelayPool } from '@/deps.ts';
import { trends } from '@/trends.ts';
import { nostrDate, nostrNow } from '@/utils.ts';
2023-08-06 20:03:29 +00:00
import type { SignedEvent } from '@/event.ts';
2023-07-25 23:22:05 +00:00
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. */
2023-08-06 20:03:29 +00:00
function handleEvent(event: SignedEvent): void {
console.info('loopback event:', event.id);
2023-08-09 00:31:14 +00:00
insertEvent(event).catch(console.warn);
2023-07-25 23:22:05 +00:00
trackHashtags(event);
}
/** Track whenever a hashtag is used, for processing trending tags. */
2023-08-06 20:03:29 +00:00
function trackHashtags(event: SignedEvent): 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
}