From b4bb503bdd005995826099d59372153133d344b8 Mon Sep 17 00:00:00 2001
From: OFF0 <offbyn@protonmail.com>
Date: Sat, 22 Jul 2023 17:36:54 +0200
Subject: [PATCH] feed: subscribe to pow events

if difficulty-filter is set the subscription of the global feed
can filter only ids with 0-prefix to save bandwidth.

as there may not be many pow events within the last 24h, the
date range is now only enabled if there is no diffculty-filter
set so that the feed is not empty.

if this works as expected it could also only subscripe to
reactions and profile info with pow ids.
---
 src/main.ts          | 2 +-
 src/subscriptions.ts | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/main.ts b/src/main.ts
index 51f16f0..d044984 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -3,10 +3,10 @@ import {zeroLeadingBitsCount} from './utils/crypto';
 import {elem} from './utils/dom';
 import {bounce} from './utils/time';
 import {isWssUrl} from './utils/url';
+import {closeSettingsView, config, toggleSettingsView} from './settings';
 import {sub24hFeed, subNote, subProfile} from './subscriptions'
 import {getReplyTo, hasEventTag, isMention, sortByCreatedAt, sortEventCreatedAt} from './events';
 import {clearView, getViewContent, getViewElem, getViewOptions, setViewElem, view} from './view';
-import {closeSettingsView, config, toggleSettingsView} from './settings';
 import {handleReaction, handleUpvote} from './reactions';
 import {closePublishView, openWriteInput, togglePublishView} from './write';
 import {handleMetadata, renderProfile} from './profiles';
diff --git a/src/subscriptions.ts b/src/subscriptions.ts
index 4dd408a..0f12680 100644
--- a/src/subscriptions.ts
+++ b/src/subscriptions.ts
@@ -1,5 +1,6 @@
 import {Event} from 'nostr-tools';
 import {getReplyTo, hasEventTag, isMention} from './events';
+import {config} from './settings';
 import {sub, subOnce, unsubAll} from './relays';
 
 type SubCallback = (
@@ -13,6 +14,7 @@ export const sub24hFeed = (onEvent: SubCallback) => {
   const now = Math.floor(Date.now() * 0.001);
   const pubkeys = new Set<string>();
   const notes = new Set<string>();
+  const prefix = Math.floor(config.filterDifficulty * 0.25);
   sub({ // get past events
     cb: (evt, relay) => {
       pubkeys.add(evt.pubkey);
@@ -20,9 +22,10 @@ export const sub24hFeed = (onEvent: SubCallback) => {
       onEvent(evt, relay);
     },
     filter: {
+      ...(prefix && {ids: ['0'.repeat(prefix)]}),
       kinds: [1],
       until: now,
-      since: Math.floor(now - (24 * 60 * 60)),
+      ...(!prefix && {since: Math.floor(now - (24 * 60 * 60))}),
       limit: 100,
     },
     unsub: true