Files
bitvid/js/services/nip71MirrorFlags.js
T
thePR0M3TH3ANandClaude Opus 4.8 783c7df0b7 feat(nip71): My Videos opt-in "Share to apps" mirror toggle (1c/1d)
Makes the NIP-71 mirror user-facing via the My Videos tab — the natural
manage-my-videos surface — without touching the single-writer publish path.

- js/constants.js: new FEATURE_NIP71_MIRROR flag (default on). Independent of
  FEATURE_PUBLISH_NIP71, which is already wired to the LEGACY 21/22 auto-publish in
  videoPublisher.js — flipping that would activate the wrong (non-opt-in) model, so
  the opt-in mirror gets its own flag and that legacy path stays dormant.
- js/services/nip71MirrorFlags.js: per-video opt-in persistence
  (pubkey+videoRootId, off by default) + pure resolveMirrorToggle decision.
- MyVideosController: per-row "Share to apps" / "Shared ✓" toggle (gated by the
  flag), wired to nip71MirrorService.publish/remove. Ineligible videos show a
  disabled "Share off" with the reason (private / NSFW-not-allowed-here / needs
  hosted URL) — the ALLOW_NSFW_CONTENT moderation gate surfaces here too.

tests/nip71-mirror-flags.test.mjs: persistence scoping + toggle decision
(enabled→remove, eligible→publish, ineligible→blocked). Mutation-verified.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-22 09:40:09 -04:00

78 lines
2.2 KiB
JavaScript

// Per-video opt-in state for the NIP-71 mirror, persisted locally per
// pubkey+videoRootId. Off by default — a publisher explicitly opts a video in
// (from the My Videos tab). Local-only is fine: it's a convenience for the
// management UI; the authoritative state is whether the mirror event exists on
// relays.
const FLAG_KEY = "bitvid:nip71-mirror:v1";
function readFlags() {
try {
if (typeof localStorage === "undefined") {
return {};
}
const raw = localStorage.getItem(FLAG_KEY);
const parsed = raw ? JSON.parse(raw) : {};
return parsed && typeof parsed === "object" ? parsed : {};
} catch (error) {
return {};
}
}
function writeFlags(flags) {
try {
if (typeof localStorage !== "undefined") {
localStorage.setItem(FLAG_KEY, JSON.stringify(flags || {}));
}
} catch (error) {
// Best-effort.
}
}
function norm(value) {
return typeof value === "string" ? value.trim() : "";
}
export function isMirrorEnabled(pubkey, videoRootId) {
const pk = norm(pubkey);
const root = norm(videoRootId);
if (!pk || !root) {
return false;
}
return readFlags()[pk]?.[root] === true;
}
export function setMirrorEnabled(pubkey, videoRootId, enabled) {
const pk = norm(pubkey);
const root = norm(videoRootId);
if (!pk || !root) {
return;
}
const flags = readFlags();
const entry = flags[pk] && typeof flags[pk] === "object" ? flags[pk] : {};
if (enabled === true) {
entry[root] = true;
} else {
delete entry[root];
}
flags[pk] = entry;
writeFlags(flags);
}
// Pure decision for a toggle click: given the current opt-in state and the
// service's eligibility check (canMirror), what should happen?
// { action: "remove" } — currently on → tear down
// { action: "publish" } — eligible + off → publish
// { action: "blocked", reason: "<why>" } — off but ineligible (private / nsfw / no-url)
export function resolveMirrorToggle({ enabled, eligibility } = {}) {
if (enabled === true) {
return { action: "remove" };
}
if (!eligibility || eligibility.ok !== true) {
return { action: "blocked", reason: eligibility?.reason || "ineligible" };
}
return { action: "publish" };
}
export const NIP71_MIRROR_FLAG_KEY = FLAG_KEY;