From a0dff12ca04b9907cf511c0520b033cd422852c9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 24 Aug 2023 17:26:46 -0500 Subject: [PATCH] pipeline: don't stream events older than 10 seconds --- src/pipeline.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pipeline.ts b/src/pipeline.ts index 770f1bd..9c6905c 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -5,7 +5,7 @@ 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'; +import { isRelay, nostrDate, nostrNow, Time } from '@/utils.ts'; import type { EventData } from '@/types.ts'; @@ -75,8 +75,13 @@ function trackRelays(event: Event) { return addRelays([...relays]); } +/** Determine if the event is being received in a timely manner. */ +const isFresh = ({ created_at }: Event): boolean => created_at >= nostrNow() - Time.seconds(10); + /** Distribute the event through active subscriptions. */ function streamOut(event: Event, data: EventData) { + if (!isFresh(event)) return; + for (const { socket, id } of Sub.matches(event, data)) { socket.send(JSON.stringify(['EVENT', id, event])); }