relay: make Nostr streaming work

This commit is contained in:
Alex Gleason
2023-08-23 23:25:38 -05:00
parent 0a4743b1cb
commit a676b71d23
3 changed files with 102 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ import { addRelays } from '@/db/relays.ts';
import { findUser } from '@/db/users.ts';
import { type Event } from '@/deps.ts';
import { isLocallyFollowed } from '@/queries.ts';
import { Sub } from '@/subs.ts';
import { trends } from '@/trends.ts';
import { isRelay, nostrDate } from '@/utils.ts';
@@ -15,6 +16,7 @@ async function handleEvent(event: Event): Promise<void> {
storeEvent(event),
trackRelays(event),
trackHashtags(event),
streamOut(event),
]);
}
@@ -63,6 +65,13 @@ function trackRelays(event: Event) {
return addRelays([...relays]);
}
/** Distribute the event through active subscriptions. */
function streamOut(event: Event) {
for (const sub of Sub.matches(event)) {
sub.socket.send(JSON.stringify(['EVENT', event]));
}
}
/** NIP-20 command line result. */
class RelayError extends Error {
constructor(prefix: 'duplicate' | 'pow' | 'blocked' | 'rate-limited' | 'invalid' | 'error', message: string) {