Add watch history bindings module

This commit is contained in:
thePR0M3TH3AN committed 2025-10-22 21:23:23 -04:00
1 parent f103d76e35
commit d98176d6ba
5 files changed
+121 -19

No files matched your search

+1 -10
View File
@@ -2,9 +2,7 @@
import watchHistoryService from "./watchHistoryService.js";
import { nostrClient } from "./nostr.js";
import {
updateWatchHistoryList as updateWatchHistoryListWithManager,
} from "./nostr/index.js";
import { updateWatchHistoryList } from "./nostr/watchHistoryBindings.js";
import { pointerKey, normalizePointerInput } from "./nostr/watchHistory.js";
import {
WATCH_HISTORY_BATCH_RESOLVE,
@@ -36,13 +34,6 @@ const FALLBACK_AVATAR = "assets/svg/default-profile.svg";
const isDevEnv =
typeof process !== "undefined" && process?.env?.NODE_ENV !== "production";
const updateWatchHistoryList = (items, options) =>
updateWatchHistoryListWithManager(
nostrClient.watchHistory,
items,
options,
);
function escapeSelector(value) {
if (typeof value !== "string") {
return "";
+4 -8
View File
@@ -5,20 +5,16 @@ import {
requestDefaultExtensionPermissions,
} from "./nostr/defaultClient.js";
import {
updateWatchHistoryList as updateWatchHistoryListWithManager,
removeWatchHistoryItem as removeWatchHistoryItemWithManager,
} from "./nostr/watchHistory.js";
updateWatchHistoryList,
removeWatchHistoryItem,
} from "./nostr/watchHistoryBindings.js";
export { nostrClient, requestDefaultExtensionPermissions };
export const recordVideoView = (...args) =>
nostrClient.recordVideoView(...args);
export const updateWatchHistoryList = (...args) =>
updateWatchHistoryListWithManager(nostrClient.watchHistory, ...args);
export const removeWatchHistoryItem = (...args) =>
removeWatchHistoryItemWithManager(nostrClient.watchHistory, ...args);
export { updateWatchHistoryList, removeWatchHistoryItem };
export const listVideoViewEvents = (...args) => {
if (typeof nostrClient.listVideoViewEvents !== "function") {
+4 -1
View File
@@ -34,9 +34,12 @@ export {
parseWatchHistoryContentWithFallback,
isNip04EncryptedWatchHistoryEvent,
watchHistoryHelpers,
} from "./watchHistory.js";
export {
updateWatchHistoryList,
removeWatchHistoryItem,
} from "./watchHistory.js";
} from "./watchHistoryBindings.js";
export {
deriveViewEventBucketIndex,
+29
View File
@@ -0,0 +1,29 @@
// js/nostr/watchHistoryBindings.js
import { nostrClient } from "./defaultClient.js";
import {
updateWatchHistoryList as updateWatchHistoryListWithManager,
removeWatchHistoryItem as removeWatchHistoryItemWithManager,
} from "./watchHistory.js";
function getWatchHistoryManager() {
return nostrClient?.watchHistory || null;
}
function assertWatchHistoryManager(operation) {
const manager = getWatchHistoryManager();
if (!manager || typeof manager[operation] !== "function") {
throw new Error("Watch history manager is unavailable in this build.");
}
return manager;
}
export function updateWatchHistoryList(...args) {
const manager = assertWatchHistoryManager("updateList");
return updateWatchHistoryListWithManager(manager, ...args);
}
export function removeWatchHistoryItem(...args) {
const manager = assertWatchHistoryManager("removeItem");
return removeWatchHistoryItemWithManager(manager, ...args);
}
+83
View File
@@ -0,0 +1,83 @@
import assert from "node:assert/strict";
import test from "node:test";
import { nostrClient } from "../../js/nostr/defaultClient.js";
import {
updateWatchHistoryList,
removeWatchHistoryItem,
} from "../../js/nostr/watchHistoryBindings.js";
const originalWatchHistory = nostrClient.watchHistory;
test.afterEach(() => {
nostrClient.watchHistory = originalWatchHistory;
});
test("updateWatchHistoryList delegates to the client's manager", () => {
const items = [{ type: "e", value: "event" }];
const options = { actor: "npub123" };
const expectedResult = { ok: true };
const calls = [];
nostrClient.watchHistory = {
updateList(...args) {
calls.push(args);
return expectedResult;
},
};
const result = updateWatchHistoryList(items, options);
assert.equal(result, expectedResult);
assert.equal(calls.length, 1);
assert.deepEqual(calls[0], [items, options]);
});
test("removeWatchHistoryItem delegates to the client's manager", () => {
const pointer = { type: "a", value: "kind:identifier" };
const options = { reason: "cleanup" };
const expectedResult = { ok: true };
const calls = [];
nostrClient.watchHistory = {
removeItem(...args) {
calls.push(args);
return expectedResult;
},
};
const result = removeWatchHistoryItem(pointer, options);
assert.equal(result, expectedResult);
assert.equal(calls.length, 1);
assert.deepEqual(calls[0], [pointer, options]);
});
test("watch history bindings throw when the manager is unavailable", () => {
nostrClient.watchHistory = null;
assert.throws(
() => updateWatchHistoryList([], {}),
/Watch history manager is unavailable in this build\./,
);
assert.throws(
() => removeWatchHistoryItem({ type: "e", value: "abc" }),
/Watch history manager is unavailable in this build\./,
);
});
test("watch history bindings throw when a required method is missing", () => {
nostrClient.watchHistory = {};
assert.throws(
() => updateWatchHistoryList([], {}),
/Watch history manager is unavailable in this build\./,
);
nostrClient.watchHistory = { updateList: () => {} };
assert.throws(
() => removeWatchHistoryItem({ type: "e", value: "123" }),
/Watch history manager is unavailable in this build\./,
);
});