// The family tree of an ad (Marty approved 2026-11-16: "each reshare = a branch; the ad's family tree dashboard"). // - Every ad has a ROOT share (the owner's own link, step 0). Every member who reshares an ad gets ONE unique reshare link for it // (/s/), whatever page they reshared it from. Its parent is the share link they arrived through (the vas.via tag) when it // is for the same ad, else the root. step = parent.step + 1. branch = the step-1 share it descends from (a step-1 share is its // own branch). // - Each click, view and sign-up on a share link is booked to THAT share (its resharer); the tree sums them up per branch. // - A share alone never earns anything: only verified views (credits), sign-ups (L2) and Boosts (L1) through it do. 'use strict'; const store = require('./store'); const cfgMod = require('./config'); function rootOf(ad) { let r = store.shareOf(ad.id, ad.owner); if (!r) r = store.addShare({ adId: ad.id, owner: ad.owner, parent: null, branch: null, step: 0 }); return r; } // a member reshares an ad. viaCode = the share link they came through (optional). -> { share, created } | { error } function reshare(ad, owner, viaCode) { owner = String(owner).toLowerCase(); if (!ad || ad.status !== 'live') return { error: 'That ad is not running right now.' }; const root = rootOf(ad); if (owner === ad.owner) return { share: root, created: false, own: true }; const have = store.shareOf(ad.id, owner); if (have) return { share: have, created: false }; let parent = viaCode ? store.share(viaCode) : null; if (!parent || parent.adId !== ad.id || parent.owner === owner) parent = root; if (parent.step >= cfgMod.get().maxStep) parent = root; const s = store.addShare({ adId: ad.id, owner, parent: parent.id, branch: parent.step === 0 ? null : (parent.branch || parent.id), step: parent.step + 1 }); if (!s.branch) { s.branch = s.id; store.saveShare(s); } parent.children = (parent.children || 0) + 1; store.saveShare(parent); store.bump('reshares'); return { share: s, created: true }; } // the chain of resharers above a share (nearest first), never the ad owner's root function ancestry(s) { const out = []; let x = s, guard = 0; while (x && x.parent && guard++ < 600) { x = store.share(x.parent); if (x && x.step > 0) out.push(x); } return out; } const name = a => { const m = store.member(a); return m && m.username ? '@' + m.username : a ? a.slice(0, 6) + '...' + a.slice(-4) : ''; }; // the dashboard: every share of the ad, nested by parent, with what came through it and what came through everything under it function tree(adId) { const all = store.sharesOf(adId); const kids = new Map(); let root = null; for (const s of all) { if (s.step === 0) root = s; else { const l = kids.get(s.parent) || kids.set(s.parent, []).get(s.parent); l.push(s); } } const sum = s => { const own = { views: s.views || 0, clicks: s.vclicks || 0, rawClicks: s.clicks || 0, signups: s.signups || 0, boosts: s.boosts || 0 }; const tot = Object.assign({}, own); let under = 0, reach = s.step; const rows = []; for (const c of (kids.get(s.id) || []).sort((a, b) => a.createdAt - b.createdAt)) { const r = sum(c); under += 1 + r.under; reach = Math.max(reach, r.reach); for (const k of Object.keys(tot)) tot[k] += r.total[k]; rows.push(r); } return { code: s.id, who: name(s.owner), step: s.step, at: s.createdAt, own, total: tot, under, reach, children: rows }; }; if (!root) return { adId: Number(adId), branches: [], totals: { shares: 0, reach: 0, views: 0, clicks: 0, signups: 0, boosts: 0 } }; const r = sum(root); // flat rows for the table: depth-first, a branch's step-1 row first, then everything under it const flat = []; const walk = (n, branch) => { if (n.step > 0) flat.push({ code: n.code, who: n.who, step: n.step, branch, at: n.at, under: n.under, views: n.total.views, clicks: n.total.clicks, signups: n.total.signups, boosts: n.total.boosts, own: n.own }); for (const c of n.children) walk(c, branch || c.code); }; walk(r, null); return { adId: Number(adId), root: { code: root.id, views: root.views || 0, clicks: root.vclicks || 0, signups: root.signups || 0 }, branches: r.children.map(b => ({ code: b.code, who: b.who, at: b.at, under: b.under, reach: b.reach, views: b.total.views, clicks: b.total.clicks, signups: b.total.signups, boosts: b.total.boosts })), rows: flat.slice(0, 500), totals: { shares: r.under, reach: r.reach, views: r.total.views, clicks: r.total.clicks, signups: r.total.signups, boosts: r.total.boosts } }; } module.exports = { rootOf, reshare, ancestry, tree };