Files
viraladsource/public/assets/my.js
T
martbost def20f8ffe AI ad writer: "Write my ad from my link" on the ad form (Marty 2026-11-17: "okay, let's do it.")
adwriter.js: the server reads the member's page (or follows their LinkSpin spin link, max 5 hops),
SSRF-safe (http/https, ports 80/443, every hop's DNS answer checked and the connection pinned to it,
no private/loopback/link-local/metadata address, 8 s, 1.5 MB, text/html only), extracts title, meta
and og description, h1-h3 and the first paragraphs (about 2,500 tokens), and asks OpenRouter
deepseek/deepseek-v4-flash (reasoning off; fallback anthropic/claude-haiku-5.5) for 3 options
{headline <= 60, line <= 90} written only from the page. Every option goes through the new
safety.checkAdCopy (no dashes, income promises, guarantees, links, shouting; every number must be
on the page) plus the length limits; failing ones are dropped, one retry when none survive. The
member picks one, it fills the form, nothing is posted.
Limits: AI_AD_DAILY (5) per member per Central day, AI_AD_GLOBAL_DAILY (500), 12 an hour per IP,
one at a time, a 24 h cache by page (a repeat is free). Off and hidden without AI_AD_KEY (a dedicated
OpenRouter key, $5 a month). Admin status: writes today, tokens, estimated spend.
safety.privateIp: IPv4-mapped IPv6 in hex form (::ffff:7f00:1) and all of fe80::/10 now count as private.
Chatbot fact added (KNOWLEDGE_DATE 2026-11-17). Server wiring + form markup landed in 828abce.
qa/adwriter.test.mjs 82/82, qa/e2e.mjs 63/63, qa/chatbot.test.mjs 108/108.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 07:12:12 -06:00

85 lines
9.6 KiB
JavaScript

// My ad tree (the member dashboard): sign in (network account through the hub, or wallet), the free ad with the spin-link picker,
// the ad's family tree (branch, step, reshares under it, views, clicks, sign-ups, Boosts), the ads I reshared, credits and rewards.
(async function () {
const $ = VAS.$, esc = VAS.esc;
VAS.wire('other').catch(() => {});
const c = await VAS.getConfig().catch(() => ({}));
const me = await VAS.getJSON('/api/me');
const back = new URLSearchParams(location.search).get('back');
if (!me.signedIn) {
try { const ss = await VAS.getJSON('/api/sso/status'); if (ss && ss.check) { location.replace('/sso/start?mode=check&to=' + encodeURIComponent(location.pathname + location.search)); return; } } catch (e) {}
$('signin').hidden = false; if (c.joinUrl) $('joinLink').href = c.joinUrl;
$('walletBtn').onclick = async () => {
try { VAS.status('Check your wallet for the sign-in message.', '', 'status'); await VASWallet.signIn(); location.href = back && /^\/s\/[a-z0-9]{8}$/.test(back) ? back : '/my'; }
catch (e) { VAS.status(String(e && e.message || e).slice(0, 300), 'err'); }
};
return;
}
if (back && /^\/s\/[a-z0-9]{8}$/.test(back)) { location.replace(back); return; }
$('dash').hidden = false;
$('logout').onclick = async () => { await VAS.post('/api/auth/logout'); location.reload(); };
$('logoutAll').onclick = async () => { $('logoutAll').disabled = true; await VAS.post('/api/auth/logout-everywhere'); location.reload(); };
// spin-link picker: only the member's own LinkSpin spin links (LinkSpin confirms again on every save)
let spins = null;
$('loadSpins').onclick = async () => {
const box = $('spinList'); box.innerHTML = '<p class="muted" style="margin:0;padding:10px 12px;font-size:14px">Asking LinkSpin for your spin links...</p>';
const r = await VAS.getJSON('/api/my/spins').catch(() => ({ error: 'LinkSpin did not answer. Try again in a minute.' }));
if (r.error) { box.innerHTML = '<p class="note err" style="margin:8px">' + esc(r.error) + '</p>'; return; }
spins = r.spins || [];
if (!r.account) { box.innerHTML = '<p style="margin:0;padding:10px 12px;font-size:14px">No LinkSpin account was found for your network username yet. <a href="https://linkspin.app" target="_blank" rel="noopener">Open LinkSpin</a></p>'; return; }
if (!spins.length) { box.innerHTML = '<p style="margin:0;padding:10px 12px;font-size:14px">You have no spin links yet. Make one on <a href="https://linkspin.app/links" target="_blank" rel="noopener">LinkSpin</a>, then pick it here.</p>'; return; }
box.innerHTML = spins.map((x, i) => '<button type="button" data-i="' + i + '"' + (x.paused || !x.destinations ? ' disabled' : '') + (x.url === $('link').value ? ' class="on"' : '') + '>' + esc(x.name || x.code) + ' <span>' + (x.paused ? 'paused' : x.destinations + ' page' + (x.destinations === 1 ? '' : 's')) + '</span></button>').join('');
box.querySelectorAll('button[data-i]').forEach(b => { b.onclick = () => { const x = spins[Number(b.dataset.i)]; $('link').value = x.url; box.querySelectorAll('button').forEach(y => y.classList.remove('on')); b.classList.add('on'); $('track').checked = false; VAS.status('Spin link picked. Save your ad to use it.', 'ok'); }; });
};
// the AI ad writer (2026-11-17): 3 suggestions written only from the member's own page; picking one fills the form, nothing is posted
if (c.aiWriter) {
$('aiBox').hidden = false;
let opts = [];
$('aiWrite').onclick = async () => {
const box = $('aiList'), url = $('link').value.trim();
if (!/^https?:\/\/\S+\.\S+/i.test(url)) { box.innerHTML = '<p class="note err" style="margin:8px">Put your link in the Your link box first (starting with https://), then tap the button again.</p>'; $('link').focus(); return; }
$('aiWrite').disabled = true; box.innerHTML = '<p class="muted" style="margin:0;padding:10px 12px;font-size:14px">Reading your page and writing 3 ads. This takes a few seconds...</p>';
const r = await VAS.post('/api/my/adwriter', { url }).catch(() => ({ error: 'The ad writer did not answer. Try again in a minute.' }));
$('aiWrite').disabled = false;
if (r.error) { box.innerHTML = '<p class="note err" style="margin:8px">' + esc(r.error) + '</p>'; return; }
opts = r.options || [];
box.innerHTML = opts.map((o, i) => '<button type="button" data-ai="' + i + '" style="flex-direction:column;align-items:flex-start;padding:10px 12px;gap:2px"><b>' + esc(o.headline) + '</b><span>' + esc(o.line) + '</span></button>').join('')
+ '<p class="muted" style="margin:0;padding:8px 12px;font-size:13px;border-top:1px solid var(--line)">Tap one to use it. Check every word is true for your offer before you post.' + (typeof r.left === 'number' ? ' Ad writes left today: ' + r.left + '.' : '') + '</p>';
box.querySelectorAll('button[data-ai]').forEach(b => { b.onclick = () => { const o = opts[Number(b.dataset.ai)]; $('headline').value = o.headline; $('line1').value = o.line; box.querySelectorAll('button[data-ai]').forEach(y => y.classList.remove('on')); b.classList.add('on'); VAS.status('Filled in. Edit it as you like, then tap the button below to save.', 'ok'); }; });
};
}
$('adForm').addEventListener('submit', async e => {
e.preventDefault(); $('saveAd').disabled = true;
const r = await VAS.post('/api/my/ad', { headline: $('headline').value, line1: $('line1').value, line2: $('line2').value, link: $('link').value.trim(), track: $('track').checked });
$('saveAd').disabled = false;
if (r.error) return VAS.status(r.error, 'err');
VAS.status(r.created ? 'Your ad is live. Share your source link below: every reshare grows a branch.' : 'Saved.', 'ok'); load();
});
$('copySource').onclick = () => VAS.copy($('sourceUrl').textContent, $('copySource'));
async function load() {
const d = await VAS.getJSON('/api/my'); if (d.error) return VAS.status(d.error, 'err');
$('who').innerHTML = 'Signed in as ' + (d.username ? '@' + esc(d.username) : '<span class="mono">' + esc(VAS.short(d.address)) + '</span>') + (d.netVerified ? ' · network member' : ' · <b>not found in the network yet</b>: sign in with your network account to post and reshare') + (d.test ? ' · <span class="chip gold">staging test member</span>' : '');
const a = d.ad, t = d.tree;
if (a) {
$('adTitle').textContent = 'My ad'; $('headline').value = a.headline; $('line1').value = a.line1; $('line2').value = a.line2 || ''; $('link').value = a.link; $('track').checked = a.track !== false; $('saveAd').textContent = 'Save changes';
$('treeChip').textContent = a.status + (a.boostedUntil && a.boostedUntil > Date.now() ? ' · boosted until ' + VAS.when(a.boostedUntil) : '');
}
if (d.source) { $('sourceBox').hidden = false; $('sourceUrl').textContent = d.source.url; }
const cr = d.credits || {}, rw = d.rewards || {};
$('myStats').innerHTML = [[t ? t.totals.shares : 0, 'reshares of my ad'], [t ? t.totals.reach : 0, 'furthest step'], [a ? a.views : 0, 'verified views'], [a ? a.vclicks : 0, 'verified clicks'], [cr.balance, 'ad credits']].map(x => '<div class="stat"><b>' + VAS.n(x[0]) + '</b><span>' + x[1] + '</span></div>').join('');
if (t && t.rows && t.rows.length) {
$('treeStats').innerHTML = [[t.root.views, 'views at the source'], [t.totals.views, 'views through branches'], [t.totals.signups, 'sign-ups through branches'], [t.totals.boosts, 'Boosts through branches']].map(x => '<div class="stat"><b>' + VAS.n(x[0]) + '</b><span>' + x[1] + '</span></div>').join('');
$('treeRows').innerHTML = t.rows.map(r => '<tr class="' + (r.step > 1 ? 'sub' : '') + '" style="--d:' + (r.step - 1) + '"><td>' + esc(r.who) + '</td><td class="n">' + r.step + '</td><td class="n">' + VAS.n(r.under) + '</td><td class="n">' + VAS.n(r.views) + '</td><td class="n">' + VAS.n(r.clicks) + '</td><td class="n">' + VAS.n(r.signups) + '</td><td class="n">' + VAS.n(r.boosts) + '</td></tr>').join('');
}
if ((d.reshares || []).length) $('myShares').innerHTML = d.reshares.map(s => '<tr><td>' + esc(s.headline || 'ad #' + s.adId) + (s.owner ? '<br><span class="muted" style="font-size:13px">by @' + esc(s.owner) + '</span>' : '') + (s.live ? '' : ' <span class="chip grey">not running</span>') + '</td><td class="n">' + s.step + '</td><td class="n">' + VAS.n(s.views) + '</td><td class="n">' + VAS.n(s.clicks) + '</td><td class="n">' + VAS.n(s.signups) + '</td><td class="n">' + VAS.n(s.boosts) + '</td><td><button class="btn sm" type="button" data-copy="' + esc(s.url) + '">Copy link</button></td></tr>').join('');
document.querySelectorAll('button[data-copy]').forEach(b => { b.onclick = () => VAS.copy(b.dataset.copy, b); });
let earnedWei = 0n; try { earnedWei = BigInt((d.earned || {}).wei || 0); } catch (e) {}
$('rewardStats').innerHTML = [[VAS.n(cr.balance), 'ad credits to spend'], [VAS.n(cr.nextAt), 'branch views to the next ' + VAS.n(cr.per) + ' credits'], [VAS.usd(rw.pendingCents), 'sign-up rewards pending'], [VAS.polOf(earnedWei.toString()) + ' POL', 'paid to me by the contract']].map(x => '<div class="stat"><b>' + x[0] + '</b><span>' + x[1] + '</span></div>').join('');
$('rewardNote').textContent = rw.payoutsOn ? '' : 'Sign-up reward payouts are switched off for now. Verified rewards wait here as pending until the amounts are confirmed.';
if ((rw.items || []).length) $('rewardRows').innerHTML = rw.items.map(r => '<tr><td>' + VAS.when(r.at) + '</td><td>' + (r.kind === 'tier' ? 'Tier bonus' : 'New member through my link') + '</td><td class="n">' + VAS.usd(r.cents) + '</td><td>' + esc(r.status) + (r.unlockAt && r.unlockAt > Date.now() ? ' (withdrawable ' + VAS.when(r.unlockAt) + ')' : '') + '</td></tr>').join('');
}
load();
})();