pipeline: refactor, use pipeline from relay

This commit is contained in:
Alex Gleason 2023-08-17 18:07:25 -05:00
parent 51881efde0
commit 45abaf14a4
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 17 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import * as eventsDB from '@/db/events.ts'; import * as eventsDB from '@/db/events.ts';
import { findUser } from '@/db/users.ts'; import * as pipeline from '@/pipeline.ts';
import { jsonSchema } from '@/schema.ts'; import { jsonSchema } from '@/schema.ts';
import { import {
type ClientCLOSE, type ClientCLOSE,
@ -53,10 +53,10 @@ function connectStream(socket: WebSocket) {
} }
async function handleEvent([_, event]: ClientEVENT) { async function handleEvent([_, event]: ClientEVENT) {
if (await findUser({ pubkey: event.pubkey })) { try {
eventsDB.insertEvent(event); await pipeline.handleEvent(event);
send(['OK', event.id, true, '']); send(['OK', event.id, true, '']);
} else { } catch (_e) {
send(['OK', event.id, false, 'blocked: only registered users can post']); send(['OK', event.id, false, 'blocked: only registered users can post']);
} }
} }

View File

@ -1,4 +1,4 @@
import { insertEvent, isLocallyFollowed } from '@/db/events.ts'; import * as eventsDB from '@/db/events.ts';
import { addRelays } from '@/db/relays.ts'; import { addRelays } from '@/db/relays.ts';
import { findUser } from '@/db/users.ts'; import { findUser } from '@/db/users.ts';
import { type Event } from '@/deps.ts'; import { type Event } from '@/deps.ts';
@ -12,16 +12,23 @@ import { isRelay, nostrDate } from '@/utils.ts';
async function handleEvent(event: Event): Promise<void> { async function handleEvent(event: Event): Promise<void> {
console.info(`firehose: Event<${event.kind}> ${event.id}`); console.info(`firehose: Event<${event.kind}> ${event.id}`);
trackHashtags(event); await Promise.all([
trackRelays(event); trackHashtags(event),
storeEvent(event),
trackRelays(event),
]);
}
if (await findUser({ pubkey: event.pubkey }) || await isLocallyFollowed(event.pubkey)) { /** Maybe store the event, if eligible. */
insertEvent(event).catch(console.warn); async function storeEvent(event: Event): Promise<void> {
if (await findUser({ pubkey: event.pubkey }) || await eventsDB.isLocallyFollowed(event.pubkey)) {
await eventsDB.insertEvent(event).catch(console.warn);
} }
} }
/** Track whenever a hashtag is used, for processing trending tags. */ /** Track whenever a hashtag is used, for processing trending tags. */
function trackHashtags(event: Event): void { // deno-lint-ignore require-await
async function trackHashtags(event: Event): Promise<void> {
const date = nostrDate(event.created_at); const date = nostrDate(event.created_at);
const tags = event.tags const tags = event.tags