party.js is inert unless a transport attaches. The host publishes route changes and playback snapshots; viewers mirror the route, load the host's stream without scraping and follow its clock. Also: forceProxy playback, bug-tv-tmdb/bug-tv-img meta overrides, relative API bases (/tv), trailer hidden in party mode, and the web package no longer ships web/test.
113 lines
4.1 KiB
JavaScript
113 lines
4.1 KiB
JavaScript
/*
|
|
config.js — where the API lives, the TMDB key, and which providers to query.
|
|
|
|
API base resolution order:
|
|
1. ?api=<url> query param (session override, also read from a hash route)
|
|
2. localStorage (saved from Settings) key: bug-tv.apiBase.v1
|
|
3. <meta name="bug-tv-api" content="…"> in index.html (substituted at build time by nix)
|
|
4. built-in default https://api-tv.bug.tools
|
|
|
|
Providers: until the user toggles anything in Settings, getProviders() returns null and
|
|
the server's defaults apply. Once customized a {id: bool} map is stored (same format as v4,
|
|
key bug-tv.providers.v1); getProviders() returns the enabled ids.
|
|
|
|
TMDB key: catalog calls go straight from the browser to TMDB.
|
|
*/
|
|
import { getParam, safe } from './util.js';
|
|
|
|
export const FALLBACK_BASE = 'https://api-tv.bug.tools';
|
|
export const STORAGE_KEY = 'bug-tv.apiBase.v1';
|
|
export const PROVIDER_KEY = 'bug-tv.providers.v1';
|
|
export const DEFAULT_TMDB_KEY = 'ac014b130a8e6344c91dff4e68b18d47';
|
|
export const TMDB_KEY_STORAGE_KEY = 'bug-tv.tmdbKey.v1';
|
|
|
|
function normalize(value) {
|
|
let v = String(value || '').trim();
|
|
if (!v) return '';
|
|
// "/tv" (Discord Activity URL mapping): same-origin path
|
|
if (v.startsWith('/') && !v.startsWith('//')) {
|
|
const origin = typeof location !== 'undefined' && /^https?:$/.test(location.protocol) ? location.origin : '';
|
|
return (origin + v).replace(/\/+$/, '');
|
|
}
|
|
if (!/^https?:\/\//i.test(v)) v = `${/^(localhost|127\.|\[::1\])/.test(v) ? 'http' : 'https'}://${v}`;
|
|
return v.replace(/\/+$/, '');
|
|
}
|
|
|
|
function metaBase() {
|
|
const el = typeof document !== 'undefined' && document.querySelector('meta[name="bug-tv-api"]');
|
|
const v = el && el.getAttribute('content');
|
|
// ignore unsubstituted placeholders like "@API_BASE@"
|
|
return v && !/^@.*@$/.test(v.trim()) ? normalize(v) : '';
|
|
}
|
|
|
|
export const DEFAULT_BASE = metaBase() || FALLBACK_BASE;
|
|
|
|
export function getBase() {
|
|
const fromQuery = getParam('api');
|
|
if (fromQuery) return normalize(fromQuery);
|
|
const stored = safe(() => localStorage.getItem(STORAGE_KEY), null);
|
|
if (stored) return normalize(stored);
|
|
return DEFAULT_BASE;
|
|
}
|
|
|
|
export function setBase(value) {
|
|
const v = normalize(value);
|
|
safe(() => (v ? localStorage.setItem(STORAGE_KEY, v) : localStorage.removeItem(STORAGE_KEY)));
|
|
return getBase();
|
|
}
|
|
|
|
export function clearBase() {
|
|
safe(() => localStorage.removeItem(STORAGE_KEY));
|
|
}
|
|
|
|
// Resolves API-relative URLs ("/v1/p/…") against the configured base; absolute URLs pass through.
|
|
export function resolveUrl(relOrAbs) {
|
|
const s = String(relOrAbs || '');
|
|
if (/^[a-z][a-z0-9+.-]*:/i.test(s)) return s;
|
|
return getBase() + (s.startsWith('/') ? s : `/${s}`);
|
|
}
|
|
|
|
export function getTmdbKey() {
|
|
const stored = safe(() => localStorage.getItem(TMDB_KEY_STORAGE_KEY), null);
|
|
return stored && stored.trim() ? stored.trim() : DEFAULT_TMDB_KEY;
|
|
}
|
|
|
|
export function setTmdbKey(value) {
|
|
const v = String(value || '').trim();
|
|
safe(() => (v ? localStorage.setItem(TMDB_KEY_STORAGE_KEY, v) : localStorage.removeItem(TMDB_KEY_STORAGE_KEY)));
|
|
return getTmdbKey();
|
|
}
|
|
|
|
export function clearTmdbKey() {
|
|
safe(() => localStorage.removeItem(TMDB_KEY_STORAGE_KEY));
|
|
}
|
|
|
|
// Raw {id: bool} map or null when the user never customized.
|
|
export function getProviderMap() {
|
|
const raw = safe(() => localStorage.getItem(PROVIDER_KEY), null);
|
|
if (!raw) return null;
|
|
const parsed = safe(() => JSON.parse(raw), null);
|
|
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
}
|
|
|
|
// Enabled provider ids, or null → use the server's defaults.
|
|
export function getProviders() {
|
|
const map = getProviderMap();
|
|
return map ? Object.keys(map).filter((n) => map[n] !== false) : null;
|
|
}
|
|
|
|
// Accepts a list of enabled ids or an {id: bool} map; null/undefined clears the override.
|
|
export function setProviders(listOrMap) {
|
|
if (listOrMap == null) return clearProviders();
|
|
let map = listOrMap;
|
|
if (Array.isArray(listOrMap)) {
|
|
map = {};
|
|
for (const id of listOrMap) map[id] = true;
|
|
}
|
|
safe(() => localStorage.setItem(PROVIDER_KEY, JSON.stringify(map)));
|
|
}
|
|
|
|
export function clearProviders() {
|
|
safe(() => localStorage.removeItem(PROVIDER_KEY));
|
|
}
|