Video Spin zone (/vz/<code>): a rotating player for any web page; house Video Spin admin route
/vz/<code> sends a frame to Social Tube's small player for one of the spin's videos on each page view; no click is recorded for a view (rotator.peek), plays and taps are counted by Video App Income under the spin's code. Embed code on the Video Spin screen. vsCreate / vsAdd shared by the member routes and POST /api/admin/house-videospin (admin key, house account, admin action log). Chatbot lines updated (Marty 2026-11-22). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -262,6 +262,35 @@ async function vaiCall(op, body) {
|
||||
}
|
||||
// the video's page: the address Video App Income gives for it when it is a plain watch address for that id, else the known one
|
||||
const vsWatchUrl = x => { const u = String((x && x.url) || ''), id = String((x && x.id) || ''); return u.startsWith('http') && !u.includes('?') && !u.includes('#') && u.endsWith('/watch/' + id) && u.length < 200 && (!IS_PROD || u.startsWith('https://socialtube.club/')) ? u : 'https://socialtube.club/watch/' + id; };
|
||||
async function vsCreate(email, name) { // -> { code, body }
|
||||
if (!vsOn()) return { code: 503, body: { error: 'Video Spins are not switched on yet.' } };
|
||||
const t = await toolTier(email), lim = VS_LIMITS[t.key] || VS_LIMITS.free, have = (await rotator.list(email)).filter(x => x.kind === 'video').length;
|
||||
if (!lim.spins) return { code: 400, body: { error: 'Video Spins start at the Starter tier (any package of your own).', tier: t } };
|
||||
if (have >= lim.spins) return { code: 400, body: { error: 'Your ' + t.label + ' tier holds ' + lim.spins + ' Video Spins and you have ' + have + '.', tier: t } };
|
||||
let mine = { member: null }; try { mine = await vsMine(email); } catch (e) {}
|
||||
const ref = mine.member ? mine.member.username || mine.member.id : '';
|
||||
const r = await rotator.create(email, { name, fallback: 'https://socialtube.club/' + (ref ? '?ref=' + encodeURIComponent(ref) : ''), kind: 'video', vref: ref });
|
||||
return { code: r.error ? 400 : 200, body: r };
|
||||
}
|
||||
async function vsAdd(email, id, b) { // b: { ids: [own video ids], link: a Social Tube video address } -> { code, body }
|
||||
if (!vsOn()) return { code: 503, body: { error: 'Video Spins are not switched on yet.' } };
|
||||
const rot = (await rotator.list(email)).find(x => x.id === Number(id) && x.kind === 'video'); if (!rot) return { code: 404, body: { error: 'No such Video Spin.' } };
|
||||
const t = await toolTier(email), lim = VS_LIMITS[t.key] || VS_LIMITS.free, raw = rotator._json.state().rotations.find(x => x.id === rot.id);
|
||||
const picks = []; // { id, title, owner, poster, url }
|
||||
try {
|
||||
const ids = (Array.isArray(b.ids) ? b.ids : []).map(String).filter(x => /^[A-Za-z0-9]{1,20}$/.test(x)).slice(0, 25);
|
||||
if (ids.length) { const mine = await vsMine(email); for (const vid of ids) { const vd = mine.videos.find(x => x.id === vid); if (vd) picks.push(vd); } if (picks.length !== ids.length) return { code: 400, body: { error: 'One of those is not a video of yours on Social Tube. Reload and pick again.' } }; }
|
||||
const link = String(b.link || '').trim();
|
||||
if (link) { const x = await vaiCall('video', { link }); if (x.s !== 200 || !x.j.video) return { code: 400, body: { error: (x.j && x.j.error) || 'That video link could not be checked.' } }; picks.push(x.j.video); }
|
||||
} catch (e) { return { code: 502, body: { error: 'Video App Income is not answering right now. Nothing was added. Try again in a minute.' } }; }
|
||||
if (!picks.length) return { code: 400, body: { error: 'Pick at least one video, or paste a Social Tube video link.' } };
|
||||
const fresh = picks.filter((x, i) => !rot.destinations.some(d => d.vid === x.id) && picks.findIndex(y => y.id === x.id) === i);
|
||||
if (!fresh.length) return { code: 400, body: { error: picks.length === 1 ? 'That video is already in this Video Spin.' : 'Those videos are already in this Video Spin.' } };
|
||||
if (rot.destinations.length + fresh.length > lim.videos) return { code: 400, body: { error: 'Your ' + t.label + ' tier holds ' + lim.videos + ' videos in one Video Spin. This one has ' + rot.destinations.length + '.', tier: t } };
|
||||
let out = null;
|
||||
for (const x of fresh) { out = await rotator.addDest(email, rot.id, { url: vsWatchUrl(x) + '?vs=' + rot.code + (raw && raw.vref ? '&ref=' + encodeURIComponent(raw.vref) : ''), label: String(x.title || 'Video').slice(0, 60), weight: 10 }, { video: x }); if (out.error) return { code: 400, body: out }; }
|
||||
return { code: 200, body: { ok: true, added: fresh.length, rotation: out.rotation } };
|
||||
}
|
||||
const vsMineCache = new Map(); // email -> { ts, v }: the member's own Social Tube videos, kept one minute
|
||||
async function vsMine(email) {
|
||||
const e = String(email || '').toLowerCase(), hit = vsMineCache.get(e); if (hit && Date.now() - hit.ts < 60000) return hit.v;
|
||||
@@ -1491,6 +1520,16 @@ const server = http.createServer(async (req, res) => {
|
||||
// every /go/<site> and every ?return=<site> link goes to networksso (2026-11-17): it knows the hub's sign-in sites, so a new site no longer needs a line here
|
||||
if (req.method === 'GET' && (p.startsWith('/go/') || p.startsWith('/sso/') || p === '/api/sso/status' || u.searchParams.has('return')) && !isPagesHost(req) && !isLinkHost(req) && await networksso.handle(req, res, u)) return;
|
||||
|
||||
// -- Video Spin zone (2026-11-22): <iframe src="https://linkspin.app/vz/<code>"> shows a different video of the spin on each page view.
|
||||
// No click is recorded for a view (rotator.peek); plays and button taps are counted by Video App Income under the spin's code.
|
||||
{ const zm = /^\/vz\/([a-z0-9]{4,12})$/i.exec(p);
|
||||
if (zm && (req.method === 'GET' || req.method === 'HEAD')) {
|
||||
const x = await rotator.peek(zm[1]); const zr = x && x.rotation;
|
||||
if (!zr || zr.kind !== 'video') { res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }); return res.end('No such Video Spin.'); }
|
||||
const d = x.dest && x.dest.vid ? x.dest : null;
|
||||
const to = d ? String(d.url).replace('/watch/' + d.vid, '/embed/' + d.vid) : 'https://socialtube.club/embed/none';
|
||||
res.writeHead(302, { Location: to, 'Cache-Control': 'no-store, private' }); return res.end();
|
||||
} }
|
||||
// -- the rotator redirect: one hop, recorded, on any host (LinkSpin)
|
||||
let rm = /^\/r\/([a-z0-9]{4,12})$/i.exec(p);
|
||||
if (rm && (req.method === 'GET' || req.method === 'HEAD')) {
|
||||
@@ -2835,35 +2874,12 @@ const server = http.createServer(async (req, res) => {
|
||||
}
|
||||
if (p === '/api/my/videospins' && req.method === 'POST') {
|
||||
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
|
||||
if (!vsOn()) return json(res, 503, { error: 'Video Spins are not switched on yet.' });
|
||||
const b = await readBody(req), t = await toolTier(s.email), lim = VS_LIMITS[t.key] || VS_LIMITS.free, have = (await rotator.list(s.email)).filter(x => x.kind === 'video').length;
|
||||
if (!lim.spins) return json(res, 400, { error: 'Video Spins start at the Starter tier (any package of your own).', tier: t });
|
||||
if (have >= lim.spins) return json(res, 400, { error: 'Your ' + t.label + ' tier holds ' + lim.spins + ' Video Spins and you have ' + have + '.', tier: t });
|
||||
let mine = { member: null }; try { mine = await vsMine(s.email); } catch (e) {}
|
||||
const ref = mine.member ? mine.member.username || mine.member.id : '';
|
||||
const r = await rotator.create(s.email, { name: b.name, fallback: 'https://socialtube.club/' + (ref ? '?ref=' + encodeURIComponent(ref) : ''), kind: 'video', vref: ref });
|
||||
return json(res, r.error ? 400 : 200, r);
|
||||
{ const b = await readBody(req), x = await vsCreate(s.email, b.name); return json(res, x.code, x.body); }
|
||||
}
|
||||
m = /^\/api\/my\/videospins\/(\d+)\/add$/.exec(p);
|
||||
if (m && req.method === 'POST') {
|
||||
const s = await auth.fromRequest(req); if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
|
||||
if (!vsOn()) return json(res, 503, { error: 'Video Spins are not switched on yet.' });
|
||||
const rot = (await rotator.list(s.email)).find(x => x.id === Number(m[1]) && x.kind === 'video'); if (!rot) return json(res, 404, { error: 'No such Video Spin.' });
|
||||
const b = await readBody(req), t = await toolTier(s.email), lim = VS_LIMITS[t.key] || VS_LIMITS.free, raw = rotator._json.state().rotations.find(x => x.id === rot.id);
|
||||
const picks = []; // { id, title, owner, poster }
|
||||
try {
|
||||
const ids = (Array.isArray(b.ids) ? b.ids : []).map(String).filter(x => /^[A-Za-z0-9]{1,20}$/.test(x)).slice(0, 25);
|
||||
if (ids.length) { const mine = await vsMine(s.email); for (const id of ids) { const vd = mine.videos.find(x => x.id === id); if (vd) picks.push(vd); } if (picks.length !== ids.length) return json(res, 400, { error: 'One of those is not a video of yours on Social Tube. Reload and pick again.' }); }
|
||||
const link = String(b.link || '').trim();
|
||||
if (link) { const x = await vaiCall('video', { link }); if (x.s !== 200 || !x.j.video) return json(res, 400, { error: (x.j && x.j.error) || 'That video link could not be checked.' }); picks.push(x.j.video); }
|
||||
} catch (e) { return json(res, 502, { error: 'Video App Income is not answering right now. Nothing was added. Try again in a minute.' }); }
|
||||
if (!picks.length) return json(res, 400, { error: 'Pick at least one video, or paste a Social Tube video link.' });
|
||||
const fresh = picks.filter((x, i) => !rot.destinations.some(d => d.vid === x.id) && picks.findIndex(y => y.id === x.id) === i);
|
||||
if (!fresh.length) return json(res, 400, { error: picks.length === 1 ? 'That video is already in this Video Spin.' : 'Those videos are already in this Video Spin.' });
|
||||
if (rot.destinations.length + fresh.length > lim.videos) return json(res, 400, { error: 'Your ' + t.label + ' tier holds ' + lim.videos + ' videos in one Video Spin. This one has ' + rot.destinations.length + '.', tier: t });
|
||||
let out = null;
|
||||
for (const x of fresh) { out = await rotator.addDest(s.email, rot.id, { url: vsWatchUrl(x) + '?vs=' + rot.code + (raw && raw.vref ? '&ref=' + encodeURIComponent(raw.vref) : ''), label: String(x.title || 'Video').slice(0, 60), weight: 10 }, { video: x }); if (out.error) return json(res, 400, out); }
|
||||
return json(res, 200, { ok: true, added: fresh.length, rotation: out.rotation });
|
||||
{ const x = await vsAdd(s.email, m[1], await readBody(req)); return json(res, x.code, x.body); }
|
||||
}
|
||||
m = /^\/api\/my\/videospins\/(\d+)\/plays$/.exec(p);
|
||||
if (m && req.method === 'GET') { // what happened on the video pages themselves, counted by Video App Income for this Video Spin's code
|
||||
@@ -2955,6 +2971,25 @@ const server = http.createServer(async (req, res) => {
|
||||
// Space rows), through the same bannerspin.js calls as the member Links screen. Admin auth only (portal session or the admin key);
|
||||
// no member session is read or minted. Every call, denied ones too, goes to the admin action log (adminaudit.js) with who, what
|
||||
// and when. GET /api/admin/house-spins/log reads that log (?limit=, newest first). Body: housespins.js.
|
||||
// House Video Spin (2026-11-22): the admin creates or tops up a Video Spin on the house account (ADMIN_EMAIL) without a member session, through
|
||||
// the same vsCreate / vsAdd the member screen uses, so every check is the member's. Body { name, ids?: [own video ids], links?: [Social Tube addresses] }.
|
||||
// A Video Spin of that name on the account is reused. Logged in the admin action log.
|
||||
if (p === '/api/admin/house-videospin' && req.method === 'POST') {
|
||||
const adm = adminFromRequest(req); const ok = isAdmin(req); const who = adm ? adm.email : ok ? 'admin key' : 'denied';
|
||||
let b = {}; try { b = await readBody(req, 64 * 1024); } catch (e) { b = null; }
|
||||
if (!ok || !b) { await adminaudit.record({ who, action: 'house-videospin', ok: false, ip: clientIp(req), detail: { error: ok ? 'bad body' : 'not admin' } }); return json(res, ok ? 400 : 401, { error: ok ? 'Send JSON.' : 'Admin only.' }); }
|
||||
const owner = ADMIN_EMAIL, name = String(b.name || '').trim().slice(0, 80); if (!owner || !name) return json(res, 400, { error: 'name is required, and ADMIN_EMAIL must be set' });
|
||||
const errors = []; let added = 0, created = false;
|
||||
let rot = (await rotator.list(owner)).find(x => x.kind === 'video' && x.name === name);
|
||||
if (!rot) { const c = await vsCreate(owner, name); if (c.code !== 200) { await adminaudit.record({ who, action: 'house-videospin', target: owner, ok: false, ip: clientIp(req), detail: { name, error: c.body.error } }); return json(res, c.code, c.body); } rot = c.body.rotation; created = true; }
|
||||
const ids = (Array.isArray(b.ids) ? b.ids : []).map(String).slice(0, 25);
|
||||
if (ids.length) { const x = await vsAdd(owner, rot.id, { ids }); if (x.code === 200) added += x.body.added; else errors.push(x.body.error); }
|
||||
for (const link of (Array.isArray(b.links) ? b.links : []).map(String).slice(0, 25)) { const x = await vsAdd(owner, rot.id, { link }); if (x.code === 200) added += x.body.added; else errors.push(link.slice(0, 80) + ': ' + x.body.error); }
|
||||
rot = (await rotator.list(owner)).find(x => x.id === rot.id);
|
||||
const outp = { ok: true, created, added, errors, id: rot.id, code: rot.code, name: rot.name, link: 'https://' + SITE_HOST + '/r/' + rot.code, zone: 'https://' + SITE_HOST + '/vz/' + rot.code, videos: rot.destinations.map(d => ({ id: d.vid, title: d.label, owner: d.vowner, active: d.active })) };
|
||||
await adminaudit.record({ who, action: 'house-videospin', target: owner, ok: true, ip: clientIp(req), detail: { name, created, added, errors, code: rot.code } });
|
||||
return json(res, 200, outp, { 'Cache-Control': 'no-store' });
|
||||
}
|
||||
if (p === '/api/admin/house-spins' && req.method === 'POST') {
|
||||
const adm = adminFromRequest(req); const ok = isAdmin(req);
|
||||
const actor = String(req.headers['x-admin-actor'] || '').replace(/[^\w .:@-]/g, '').slice(0, 60);
|
||||
|
||||
Reference in New Issue
Block a user