// 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/) and the chat hand-off all call InstantAdPay's partner API, signed // exactly like the registry rows in iapsync.js: // POST /api/partner/support/ 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 };