From bc14817967211be12252d8601ea7b618e4d83d14 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Wed, 22 Oct 2025 19:24:54 -0400 Subject: [PATCH] Refactor Nostr helper imports for view metrics --- js/app.js | 3 +-- js/subscriptions.js | 6 ++---- js/viewCounter.js | 40 +++++++++++----------------------------- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/js/app.js b/js/app.js index 44f0a474..3828cebc 100644 --- a/js/app.js +++ b/js/app.js @@ -1,6 +1,6 @@ // js/app.js -import { nostrClient } from "./nostr.js"; +import { nostrClient, recordVideoView as recordVideoViewApi } from "./nostr.js"; import { torrentClient } from "./webtorrent.js"; import { isDevMode, @@ -64,7 +64,6 @@ import { formatViewCount, ingestLocalViewEvent, } from "./viewCounter.js"; -import { recordVideoView as recordVideoViewApi } from "./nostr/viewEvents.js"; import { splitAndZap as splitAndZapDefault } from "./payments/zapSplit.js"; import { showLoginRequiredToZapNotification } from "./payments/zapNotifications.js"; import { diff --git a/js/subscriptions.js b/js/subscriptions.js index 87d6ae33..e8a3e4a5 100644 --- a/js/subscriptions.js +++ b/js/subscriptions.js @@ -4,6 +4,8 @@ import { nostrClient, convertEventToVideo as sharedConvertEventToVideo, requestDefaultExtensionPermissions, + listVideoViewEvents as listVideoViewEventsApi, + subscribeVideoViewEvents as subscribeVideoViewEventsApi, } from "./nostr.js"; import { DEFAULT_RELAY_URLS } from "./nostr/toolkit.js"; import { @@ -21,10 +23,6 @@ import { ALLOW_NSFW_CONTENT } from "./config.js"; import { devLogger, userLogger } from "./utils/logger.js"; import moderationService from "./services/moderationService.js"; import nostrService from "./services/nostrService.js"; -import { - listVideoViewEvents as listVideoViewEventsApi, - subscribeVideoViewEvents as subscribeVideoViewEventsApi, -} from "./nostr/viewEvents.js"; function normalizeHexPubkey(value) { if (typeof value !== "string") { diff --git a/js/viewCounter.js b/js/viewCounter.js index e5b684bf..5971eebe 100644 --- a/js/viewCounter.js +++ b/js/viewCounter.js @@ -17,43 +17,22 @@ import { VIEW_COUNT_DEDUPE_WINDOW_SECONDS, } from "./config.js"; import { userLogger } from "./utils/logger.js"; -import { nostrClient } from "./nostr.js"; import { - countVideoViewEvents as countVideoViewEventsForClient, - listVideoViewEvents as listVideoViewEventsForClient, - subscribeVideoViewEvents as subscribeVideoViewEventsForClient, -} from "./nostr/viewEvents.js"; + countVideoViewEvents as countVideoViewEventsApi, + listVideoViewEvents as listVideoViewEventsApi, + subscribeVideoViewEvents as subscribeVideoViewEventsApi, +} from "./nostr.js"; const VIEW_COUNTER_STORAGE_KEY = "bitvid:view-counter:v1"; const STORAGE_DEBOUNCE_MS = 500; const SECONDS_PER_DAY = 86_400; -const listVideoViewEvents = (pointer, options) => { - if (typeof nostrClient.listVideoViewEvents === "function") { - return nostrClient.listVideoViewEvents(pointer, options); - } - return listVideoViewEventsForClient(nostrClient, pointer, options); -}; -const countVideoViewEvents = (pointer, options) => { - if (typeof nostrClient.countVideoViewEvents === "function") { - return nostrClient.countVideoViewEvents(pointer, options); - } - return countVideoViewEventsForClient(nostrClient, pointer, options); -}; -const subscribeVideoViewEvents = (pointer, options) => { - if (typeof nostrClient.subscribeVideoViewEvents === "function") { - return nostrClient.subscribeVideoViewEvents(pointer, options); - } - return subscribeVideoViewEventsForClient(nostrClient, pointer, options); -}; - /** @type {Map} */ const pointerStates = new Map(); /** @type {Map} */ const pointerListeners = new Map(); let persistTimer = null; let nextTokenId = 1; -let nostrClientRef = null; restoreCacheSnapshot(); @@ -476,11 +455,11 @@ async function hydratePointer(key, listeners) { const options = listeners.options || {}; setPointerStatus(key, "hydrating"); const sinceSeconds = Math.floor(Date.now() / 1000) - VIEW_COUNT_BACKFILL_MAX_DAYS * SECONDS_PER_DAY; - const listPromise = listVideoViewEvents(pointer, { + const listPromise = listVideoViewEventsApi(pointer, { since: sinceSeconds, relays: options.relays, }); - const countPromise = countVideoViewEvents(pointer, { + const countPromise = countVideoViewEventsApi(pointer, { relays: options.relays, signal: options.signal, }); @@ -555,7 +534,7 @@ function ensureLiveSubscription(key, listeners) { const options = listeners.options || {}; try { const since = Math.floor(Date.now() / 1000) - Math.max(1, VIEW_COUNT_DEDUPE_WINDOW_SECONDS); - const unsubscribe = subscribeVideoViewEvents(pointer, { + const unsubscribe = subscribeVideoViewEventsApi(pointer, { relays: options.relays, since, onEvent: (event) => { @@ -584,7 +563,10 @@ function ensureLiveSubscription(key, listeners) { } export function initViewCounter({ nostrClient } = {}) { - nostrClientRef = nostrClient || null; + if (nostrClient && typeof nostrClient === "object") { + // The singleton Nostr client is already registered inside js/nostr.js. + // This hook remains for backwards compatibility. + } } export function subscribeToVideoViewCount(pointerInput, handler, options = {}) {