2023-07-25 20:30:58 +00:00
|
|
|
import { Conf } from '@/config.ts';
|
2023-08-07 05:50:12 +00:00
|
|
|
import { insertEvent } from '@/db/events.ts';
|
2023-07-26 17:54:06 +00:00
|
|
|
import { RelayPool } from '@/deps.ts';
|
2023-07-25 22:07:09 +00:00
|
|
|
import { trends } from '@/trends.ts';
|
2023-07-25 23:35:07 +00:00
|
|
|
import { nostrDate, nostrNow } from '@/utils.ts';
|
2023-07-25 20:30:58 +00:00
|
|
|
|
2023-08-06 20:03:29 +00:00
|
|
|
import type { SignedEvent } from '@/event.ts';
|
2023-07-25 23:22:05 +00:00
|
|
|
|
2023-07-26 17:54:06 +00:00
|
|
|
const relay = new RelayPool([Conf.relay]);
|
2023-07-25 20:30:58 +00:00
|
|
|
|
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.
|
2023-07-26 17:54:06 +00:00
|
|
|
relay.subscribe(
|
|
|
|
[{ kinds: [1], since: nostrNow() }],
|
|
|
|
[Conf.relay],
|
|
|
|
handleEvent,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
);
|
2023-07-25 20:30:58 +00:00
|
|
|
|
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 {
|
2023-07-25 22:07:09 +00:00
|
|
|
console.info('loopback event:', event.id);
|
2023-08-07 05:50:12 +00:00
|
|
|
insertEvent(event);
|
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 {
|
2023-07-25 23:35:07 +00:00
|
|
|
const date = nostrDate(event.created_at);
|
|
|
|
|
2023-07-25 20:30:58 +00:00
|
|
|
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-25 20:30:58 +00:00
|
|
|
|
2023-07-26 01:55:12 +00:00
|
|
|
if (!tags.length) return;
|
|
|
|
|
2023-07-25 20:30:58 +00:00
|
|
|
try {
|
2023-07-26 01:55:12 +00:00
|
|
|
console.info('tracking tags:', tags);
|
2023-07-25 23:35:07 +00:00
|
|
|
trends.addTagUsages(event.pubkey, tags, date);
|
2023-07-25 20:30:58 +00:00
|
|
|
} catch (_e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2023-07-25 23:22:05 +00:00
|
|
|
}
|