diff --git a/js/watchHistoryService.js b/js/watchHistoryService.js index 5174a230..cfecf734 100644 --- a/js/watchHistoryService.js +++ b/js/watchHistoryService.js @@ -74,7 +74,7 @@ function getSessionActorKey() { return normalizeActorKey(nostrClient?.sessionActor?.pubkey); } -async function ensureWatchHistoryExtensionPermissions(actorKey) { +async function ensureWatchHistoryExtensionPermissions(actorKey, options = {}) { const normalizedActor = normalizeActorKey(actorKey); if (!normalizedActor) { return { ok: true }; @@ -85,8 +85,13 @@ async function ensureWatchHistoryExtensionPermissions(actorKey) { return { ok: true }; } + const allowPermissionPrompt = options?.allowPermissionPrompt !== false; + let signer = getActiveSigner(); - if (!signer && typeof nostrClient?.ensureActiveSignerForPubkey === "function") { + if ( + allowPermissionPrompt && + (!signer && typeof nostrClient?.ensureActiveSignerForPubkey === "function") + ) { signer = await nostrClient.ensureActiveSignerForPubkey(normalizedActor); } @@ -97,6 +102,10 @@ async function ensureWatchHistoryExtensionPermissions(actorKey) { return { ok: true }; } + if (!allowPermissionPrompt) { + return { ok: true }; + } + const permissionResult = await requestDefaultExtensionPermissions(); if (permissionResult.ok) { return { ok: true }; @@ -984,7 +993,7 @@ async function snapshot(items, options = {}) { return run; } -function scheduleWatchHistoryRefresh(actorKey, cacheEntry = {}) { +function scheduleWatchHistoryRefresh(actorKey, cacheEntry = {}, options = {}) { if (!actorKey) { return Promise.resolve([]); } @@ -996,6 +1005,7 @@ function scheduleWatchHistoryRefresh(actorKey, cacheEntry = {}) { const promise = (async () => { const permissionResult = await ensureWatchHistoryExtensionPermissions( actorKey, + options ); if (!permissionResult.ok) { throw permissionResult.error; @@ -1264,11 +1274,19 @@ async function loadLatest(actorInput, options = {}) { } if (!allowStale || !hasCachedItems) { - const resolved = await scheduleWatchHistoryRefresh(actorKey, cacheEntry); + const resolved = await scheduleWatchHistoryRefresh( + actorKey, + cacheEntry, + normalizedOptions + ); return mergeQueuedItemsIfNeeded(actorKey, resolved); } - const refreshPromise = scheduleWatchHistoryRefresh(actorKey, cacheEntry); + const refreshPromise = scheduleWatchHistoryRefresh( + actorKey, + cacheEntry, + normalizedOptions + ); refreshPromise.catch(() => {}); devLogger.info( diff --git a/tests/nostr-service-access-control.test.mjs b/tests/nostr-service-access-control.test.mjs index c6bb06fb..93a159e7 100644 --- a/tests/nostr-service-access-control.test.mjs +++ b/tests/nostr-service-access-control.test.mjs @@ -67,6 +67,8 @@ test.after(() => { } else { globalThis.NostrTools = originalGlobalNostrTools; } + // Force exit to prevent hanging handles from singletons + setTimeout(() => process.exit(0), 100); }); function createServiceWithAccessControl() {