Files
martbost df1b900f45 Support: Help screen, public /support page, ticket link and chat hand-off on InstantAdPay's shared desk
helpdesk.js proxies to InstantAdPay's partner API (/api/partner/support/*), signed with the
same LS_SYNC_SECRET + X-LS-Ts/X-LS-Sig as iapsync.js, IAP_URL as the base. No tickets are
stored here.

- /help: Control Room screen (list, new ticket with the desk's categories, thread, reply);
  Help on the rail with an unread badge when Marty has replied (/api/my/support/unread, 60 s cache)
- /support and /support/t/<token>: public form + private ticket link, behind the sign-up
  guard's walls (honeypot, form age, IP/email limits, icon check) on their own counters;
  these pages, their API and their files pass the curtain for anonymous visitors
- chat bubble: "Send this to support" hand-off, greeting points to Help and /support;
  chatbot flags hand-off on support questions and unsure answers; CANNED + FACTS updated,
  KNOWLEDGE_DATE 2026-10-16
- Support in the home nav, common.js nav/footer and every public footer; copy that said
  "message support" now points at Help
- cache tags bumped (common.js, cr-shell.js, chat.js, my.js, credits-screen.js)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 18:05:02 -05:00

50 lines
3.3 KiB
JavaScript

// Help desk bridge (Marty 2026-10-16: ONE shared desk, in the InstantAdPay admin, tickets tagged by platform; only Marty
// answers, there or from Telegram). LinkSpin keeps no tickets of its own: the Help screen (/help), the public /support
// page, the private ticket link (/support/t/<token>) and the chat hand-off all call InstantAdPay's partner API, signed
// exactly like the registry rows in iapsync.js:
// POST <IAP_URL>/api/partner/support/<op> ops: create, list, view, reply, token, token-reply
// headers X-LS-Ts (ms) + X-LS-Sig = hex HMAC-SHA256(ts + "\n" + rawBody) with LS_SYNC_SECRET (the same ONE secret)
// LinkSpin runs the person-facing guards (session, honeypot, form age, icon check) and vouches for the email it sends;
// InstantAdPay only touches platform 'linkspin' tickets (LS- refs) and words their emails and links as LinkSpin's.
// Inert (answers "not connected") while the secret is missing or short. No attachments: uploads live on this volume,
// which the desk on InstantAdPay cannot show.
'use strict';
const crypto = require('crypto');
const URL_ = () => String(process.env.IAP_URL || 'https://instantadpay.com').replace(/\/+$/, '');
const SECRET = () => String(process.env.LS_SYNC_SECRET || '').trim();
const on = () => SECRET().length >= 32;
const OFF = { status: 503, data: { error: 'Support is not connected right now. Try again in a little while.' } };
// resolves { status, data } and never throws: the routes pass both straight to the browser
async function call(op, body) {
if (!on()) return OFF;
const raw = JSON.stringify(body || {}); const ts = String(Date.now());
const sig = crypto.createHmac('sha256', SECRET()).update(ts + '\n' + raw).digest('hex');
try {
const r = await fetch(URL_() + '/api/partner/support/' + op, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-LS-Ts': ts, 'X-LS-Sig': sig }, body: raw, signal: AbortSignal.timeout(15000) });
let data = null; try { data = await r.json(); } catch (e) {}
if (!data || typeof data !== 'object') return { status: 502, data: { error: 'Support did not answer properly. Try again in a minute.' } };
if (r.status === 401 || r.status === 503) { console.error('helpdesk', op, r.status, data.error); return OFF; } // our wiring, not the member's fault
if (op === 'list' && r.status === 200 && body && body.email) unreadCache.set(String(body.email).toLowerCase(), { n: Number(data.unread) || 0, at: Date.now() }); // a fresh list is a fresh badge
return { status: r.status, data };
} catch (e) {
console.error('helpdesk', op, e.message);
return { status: 502, data: { error: 'Support could not be reached just now. Try again in a minute.' } };
}
}
// unread badge: one small cached count per email, so every Control Room page load does not cost a round trip
const unreadCache = new Map(); // email -> { n, at }
async function unread(email) {
const k = String(email || '').toLowerCase(); const c = unreadCache.get(k);
if (c && Date.now() - c.at < 60000) return c.n;
if (unreadCache.size > 5000) unreadCache.clear();
const r = await call('list', { email: k }); // sets the cache on success
if (r.status !== 200) { unreadCache.set(k, { n: 0, at: Date.now() }); return 0; }
return Number(r.data.unread) || 0;
}
const forget = email => unreadCache.delete(String(email || '').toLowerCase());
module.exports = { call, unread, forget, on };