Help desk: signed partner API for LinkSpin tickets (LS- refs on the one shared desk)

POST /api/partner/support/{create,list,view,reply,token,token-reply}, signed exactly
like the registry rows (LS_SYNC_SECRET, X-LS-Ts + X-LS-Sig, now one readLsSigned helper
shared with /api/partner/registry). Only platform 'linkspin' tickets are reachable there.

LinkSpin tickets: requester mail, links and footer say LinkSpin support and point at
LS_URL (/support/t/<token>, /help#t=<ref>); no InstantAdPay in-app notice is sent for
them. Telegram ping, admin mail and the support bot's thread/list views mark them
LinkSpin. The member Help pane here lists InstantAdPay tickets only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-16 18:04:52 -05:00
parent 80885705d2
commit 524ce843dc
3 changed files with 81 additions and 35 deletions
+37 -22
View File
@@ -1,11 +1,14 @@
// Help desk (Marty, 2026-10-16): one shared support desk in the IAP admin portal. A member emailed
// support@instantadpay.com when it did not exist, so nobody had a way to ask for help. Tickets come in
// from the member Help pane ('form'), the public /support page ('public'), the chatbot hand-off
// ('chatbot') and, in a later step, inbound mail to support@ ('email'). LinkSpin tickets will land on
// this same desk later (platform 'linkspin'). Only Marty answers for now, from his admin login.
// ('chatbot') and, in a later step, inbound mail to support@ ('email'). LinkSpin tickets land on this
// same desk through the signed partner API (server.js /api/partner/support/*, LS_SYNC_SECRET), as
// platform 'linkspin': their links, emails and copy point at LinkSpin (LS_URL), and a LinkSpin ticket
// never sends an InstantAdPay in-app notice (LinkSpin shows its own unread badge). Only Marty answers,
// from his admin login or Telegram.
//
// Refs: IAP-<1000 + id> (LS-<1000 + id> for LinkSpin), one id sequence for the whole desk, so a ref
// always names one ticket. The private no-login link is /support/t/<ref>.<32-char HMAC>, signed over
// always names one ticket. The private no-login link is <site>/support/t/<ref>.<32-char HMAC>, signed over
// the ticket id and requester email (DATA_DIR/support.key, or SUPPORT_SECRET), nothing stored.
//
// Email intake (next step) plugs in through ingestEmail({ from, name, subject, text, messageId,
@@ -20,6 +23,13 @@ const db = require('./db');
let DATA_DIR = null, deps = {};
const SITE = 'https://instantadpay.com';
// per-platform face of the desk: where the requester's links point and whose name signs the mail
const BRAND = {
iap: { site: () => SITE, name: 'InstantAdPay', help: ref => SITE + '/my#help?t=' + ref, footer: 'Reply on the page above, or email support@instantadpay.com and keep the ticket number in the subject.' },
linkspin: { site: () => String(process.env.LS_URL || 'https://linkspin.app').replace(/\/+$/, ''), name: 'LinkSpin', help: ref => BRAND.linkspin.site() + '/help#t=' + ref, footer: 'Reply on the page above, or from Help in your LinkSpin Control Room.' }
};
const brandOf = t => BRAND[t && t.platform] || BRAND.iap;
const PLATFORM_NAME = { iap: 'InstantAdPay', linkspin: 'LinkSpin' };
const CATEGORIES = ['Account & sign-in', 'Wallet & payouts', 'Buying packages', 'Ads & campaigns', 'Earning credits & games', 'Team & referrals', 'Other'];
const STATUSES = ['open', 'waiting_on_member', 'closed'];
const PRIORITIES = ['normal', 'high'];
@@ -63,7 +73,7 @@ function secret() {
}
const sig = t => crypto.createHmac('sha256', secret()).update('support:' + t.id + ':' + lc(t.email)).digest('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '').slice(0, 32);
const tokenFor = t => t.ref + '.' + sig(t);
const linkFor = t => SITE + '/support/t/' + tokenFor(t);
const linkFor = t => brandOf(t).site() + '/support/t/' + tokenFor(t);
const TOKEN_RE = /^((?:IAP|LS)-\d{4,9})\.([A-Za-z0-9_-]{32})$/;
// ---- stores ----
@@ -89,7 +99,7 @@ const J = {
async messages(ticketId, internal) { return this.d().messages.filter(m => m.ticketId === Number(ticketId) && (internal || !m.internal)).sort((a, b) => a.id - b.id).map(m => Object.assign({}, m)); },
async byMessageId(mids) { const s = new Set(mids); const m = this.d().messages.find(x => x.emailMessageId && s.has(x.emailMessageId)); return m ? m.ticketId : 0; },
async counts() { const out = { open: 0, waiting_on_member: 0, closed: 0, unread: 0 }; for (const t of this.d().tickets) { out[t.status] = (out[t.status] || 0) + 1; if (t.adminUnread) out.unread++; } return out; },
async memberUnread(email) { return this.d().tickets.filter(t => t.email === email && t.memberUnread).length; }
async memberUnread(email, platform) { return this.d().tickets.filter(t => t.email === email && t.memberUnread && (!platform || t.platform === platform)).length; }
};
const tRow = r => r ? ({ id: r.id, ref: r.ref, platform: r.platform, source: r.source, email: r.email, name: r.name || '', username: r.username || '',
subject: r.subject, category: r.category, status: r.status, priority: r.priority, created: Number(r.created), updated: Number(r.updated),
@@ -132,7 +142,7 @@ const D = {
for (const r of await db.q('SELECT status, COUNT(*) n, SUM(admin_unread) u FROM tickets GROUP BY status')) { out[r.status] = Number(r.n); out.unread += Number(r.u || 0); }
return out;
},
async memberUnread(email) { return Number((await db.q('SELECT COUNT(*) n FROM tickets WHERE email=? AND member_unread=1', [email]))[0].n); }
async memberUnread(email, platform) { return Number((await db.q('SELECT COUNT(*) n FROM tickets WHERE email=? AND member_unread=1' + (platform ? ' AND platform=?' : ''), platform ? [email, platform] : [email]))[0].n); }
};
const impl = () => db.enabled() ? D : J;
@@ -167,7 +177,8 @@ const firstLine = s => oneLine(String(s || '').split('\n').find(l => l.trim()) |
function pingAdmin(kind, t, m) {
const send = items => {
const last = items[items.length - 1];
const head = kind === 'new' ? '\u{1F3AB} <b>' + t.ref + '</b> new ticket' : '\u{1F4AC} <b>' + t.ref + '</b> ' + (items.length > 1 ? items.length + ' member replies' : 'member reply');
const ls = t.platform === 'linkspin'; // LinkSpin tickets say so up front, so Marty knows which site the person is on
const head = (kind === 'new' ? '\u{1F3AB} <b>' + t.ref + '</b> new ' + (ls ? '\u{1F517} <b>LinkSpin</b> ' : '') + 'ticket' : '\u{1F4AC} <b>' + t.ref + '</b> ' + (ls ? '\u{1F517} <b>LinkSpin</b> ' : '') + (items.length > 1 ? items.length + ' member replies' : 'member reply'));
let extra = '';
if (kind === 'new') {
const h = Math.floor(Date.now() / 3600000);
@@ -175,43 +186,46 @@ function pingAdmin(kind, t, m) {
if (++newPings.n > 20) { newPings.skipped++; return; }
if (newPings.skipped) { extra = '\n(+' + newPings.skipped + ' more new tickets earlier were not pinged)'; newPings.skipped = 0; }
}
const text = head + ' · ' + t.platform + ' · ' + escH(t.category) + '\n' + escH(who(t)) + '\n' + escH(t.subject) + '\n“' + escH(firstLine(last.body)) + '”\n' + adminLink(t) + extra;
const text = head + ' · ' + (ls ? '' : (PLATFORM_NAME[t.platform] || t.platform) + ' · ') + escH(t.category) + '\n' + escH(who(t)) + '\n' + escH(t.subject) + '\n“' + escH(firstLine(last.body)) + '”\n' + adminLink(t) + extra;
if (deps.telegram) deps.telegram(text, t.ref).catch(() => {});
if (deps.adminEmail && deps.mailer && deps.mailer.hasKey()) {
const body = items.map(i => i.body).join('\n\n---\n\n');
deps.mailer.send(deps.adminEmail, '[' + t.ref + '] ' + (kind === 'new' ? 'New ticket: ' : 'Reply: ') + t.subject,
(kind === 'new' ? 'New support ticket' : 'Member reply') + ' on ' + t.platform + ' (' + t.category + ')\nFrom: ' + who(t) + '\nSource: ' + t.source + '\n\n' + body + '\n\nAnswer it in the desk: ' + adminLink(t), null, { transactional: true }).catch(e => console.error('support admin mail', e.message));
deps.mailer.send(deps.adminEmail, '[' + t.ref + '] ' + (kind === 'new' ? 'New ' + (t.platform === 'linkspin' ? 'LinkSpin ' : '') + 'ticket: ' : 'Reply: ') + t.subject,
(kind === 'new' ? 'New support ticket' : 'Member reply') + ' on ' + (PLATFORM_NAME[t.platform] || t.platform) + ' (' + t.category + ')\nFrom: ' + who(t) + '\nSource: ' + t.source + '\n\n' + body + '\n\nAnswer it in the desk: ' + adminLink(t), null, { transactional: true }).catch(e => console.error('support admin mail', e.message));
}
};
if (kind === 'new') return send([m]);
throttled('admin:' + t.id, 10 * 60 * 1000, m, send);
}
function htmlMail(intro, blocks, link, linkLabel) {
function htmlMail(intro, blocks, link, linkLabel, brand) {
const b = brand || BRAND.iap;
return '<div style="font-family:Arial,sans-serif;font-size:15px;line-height:1.5;color:#1d2420;max-width:560px">'
+ '<p>' + escH(intro) + '</p>' + blocks.map(b => '<div style="border-left:3px solid #43e8c3;padding:6px 12px;margin:12px 0;white-space:pre-wrap">' + escH(b) + '</div>').join('')
+ '<p><a href="' + link + '" style="display:inline-block;background:#043b2f;color:#fff;padding:10px 16px;border-radius:8px;text-decoration:none">' + escH(linkLabel) + '</a></p>'
+ '<p style="font-size:12px;color:#777">InstantAdPay support. Reply on the page above, or email support@instantadpay.com and keep the ticket number in the subject.</p></div>';
+ '<p style="font-size:12px;color:#777">' + escH(b.name) + ' support. ' + escH(b.footer) + '</p></div>';
}
function confirmRequester(t, m) {
if (!deps.mailer || !deps.mailer.hasKey()) return;
const link = linkFor(t);
const link = linkFor(t), b = brandOf(t);
const text = 'Hi' + (t.name ? ' ' + t.name : '') + ',\n\nWe got your message and opened ticket ' + t.ref + '. Marty reads every one and will reply as soon as he can.\n\n'
+ 'Subject: ' + t.subject + '\n\n' + m.body + '\n\nView the ticket or add more detail here (no sign-in needed):\n' + link + '\n\nInstantAdPay support';
deps.mailer.send(t.email, '[' + t.ref + '] ' + t.subject, text, htmlMail('We got your message and opened ticket ' + t.ref + '. Marty reads every one and will reply as soon as he can.', [m.body], link, 'View your ticket'), { transactional: true })
+ 'Subject: ' + t.subject + '\n\n' + m.body + '\n\nView the ticket or add more detail here (no sign-in needed):\n' + link + '\n\n' + b.name + ' support';
deps.mailer.send(t.email, '[' + t.ref + '] ' + t.subject, text, htmlMail('We got your message and opened ticket ' + t.ref + '. Marty reads every one and will reply as soon as he can.', [m.body], link, 'View your ticket', b), { transactional: true })
.catch(e => console.error('support confirm mail', e.message));
}
function notifyRequester(t, m) {
throttled('member:' + t.id, 2 * 60 * 1000, m, async items => {
const cur = (await impl().getTicket(t.id)) || t;
const link = linkFor(cur);
const link = linkFor(cur), b = brandOf(cur);
const bodies = items.map(i => i.body);
if (deps.mailer && deps.mailer.hasKey()) {
const text = 'Hi' + (cur.name ? ' ' + cur.name : '') + ',\n\nMarty replied to your support ticket ' + cur.ref + ':\n\n' + bodies.join('\n\n---\n\n')
+ '\n\nRead the whole thread and reply here:\n' + link + '\n\nInstantAdPay support';
deps.mailer.send(cur.email, '[' + cur.ref + '] ' + cur.subject, text, htmlMail('Marty replied to your support ticket ' + cur.ref + ':', bodies, link, 'Read and reply'), { transactional: true })
+ '\n\nRead the whole thread and reply here:\n' + link + (cur.platform === 'linkspin' ? '\n\nOr open Help in your LinkSpin Control Room: ' + b.help(cur.ref) : '') + '\n\n' + b.name + ' support';
deps.mailer.send(cur.email, '[' + cur.ref + '] ' + cur.subject, text, htmlMail('Marty replied to your support ticket ' + cur.ref + ':', bodies, link, 'Read and reply', b), { transactional: true })
.catch(e => console.error('support reply mail', e.message));
}
// members also get it in their on-site inbox, as a notice: it never pops the sign-in modal
// members also get it in their on-site inbox, as a notice: it never pops the sign-in modal. InstantAdPay tickets only:
// a LinkSpin ticket is followed on LinkSpin (its Help badge), and its requester may have no real account here at all
if (cur.platform !== 'iap') return;
try {
const acct = deps.accounts && await deps.accounts.byEmail(cur.email);
if (acct && deps.messages) await deps.messages.deliver(1, deps.adminEmail || 'house@instantadpay.com', [cur.email], 'Support replied: [' + cur.ref + '] ' + cur.subject.slice(0, 110),
@@ -288,8 +302,9 @@ async function byToken(tok) {
}
const pubMsg = m => ({ id: m.id, author: m.author, body: m.body, attachments: m.attachments, created: m.created });
const pubTicket = t => ({ ref: t.ref, platform: t.platform, subject: t.subject, category: t.category, status: t.status, created: t.created, updated: t.updated, unread: t.memberUnread });
async function memberList(email) { return (await impl().list({ email: lc(email), limit: 100 })).map(pubTicket); }
async function memberUnread(email) { return impl().memberUnread(lc(email)); }
// platform: only that site's tickets (each site's Help pane shows its own); omitted = all of them
async function memberList(email, platform) { return (await impl().list({ email: lc(email), platform: PLATFORMS.includes(platform) ? platform : '', limit: 100 })).map(pubTicket); }
async function memberUnread(email, platform) { return impl().memberUnread(lc(email), PLATFORMS.includes(platform) ? platform : ''); }
// the requester's view of one ticket (no internal notes); opening it clears their unread flag
async function memberView(t) {
if (t.memberUnread) await impl().updateTicket(t.id, { memberUnread: false });
@@ -319,5 +334,5 @@ async function ingestEmail(e) {
return Object.assign(r, { created: true });
}
module.exports = { init, CATEGORIES, STATUSES, PRIORITIES, PLATFORMS, SOURCES, DEFAULT_TEMPLATES, refOf, parseRef, tokenFor, linkFor,
module.exports = { init, PLATFORM_NAME, CATEGORIES, STATUSES, PRIORITIES, PLATFORMS, SOURCES, DEFAULT_TEMPLATES, refOf, parseRef, tokenFor, linkFor,
create, addMessage, setFields, byRef, byToken, memberList, memberUnread, memberView, adminList, adminView, counts, ingestEmail };