' + P.map((p, i) => i === P.length - 1 ? '
' + esc(p) + '
' : '
' + esc(p).replace(esc(link), '' + esc(link) + '') + '
').join('') + '
';
+ return { subject: sponsor + ' made you a video', text: P.join('\n\n'), html };
+}
+function cleanNote(n) {
+ const note = String(n || '').replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim();
+ if (!note) return { note: '' };
+ if (note.length > 400) return { error: 'Keep your note under 400 characters.' };
+ const P = R.prohibited;
+ if (P && P.hasUrl && P.hasUrl(note)) return { error: 'Your note cannot contain a link. The email already carries your video.' };
+ if (P && P.checkText) { const r = P.checkText(note, 'InstantAdPay'); if (r && r.error) return { error: r.error }; }
+ if (/\b(guarantee\w*|risk[ -]?free|passive income|get rich|double your)\b/i.test(note)) return { error: 'Your note cannot promise income or results.' };
+ return { note };
+}
+const linkOk = l => { try { const u = new URL(String(l)); return u.protocol === 'https:' && hosts().includes(u.hostname.toLowerCase()) && /^\/p\/[A-Za-z0-9]{4,20}(\/[A-Za-z0-9]{4,20})?$/.test(u.pathname) && !u.search && !u.hash; } catch (e) { return false; } };
+
+// ---- the two calls ----
+async function eligible(b) {
+ const site = SITES[lc(b.site)]; if (!site) return { error: 'unknown site', code: 'site' };
+ const sender = await senderOf(b.sender); if (!sender) return { error: 'The sender has no InstantAdPay account with a username.', code: 'sender' };
+ const lv = await R.accounts.downline(sender.email, 1), kids = (lv[0] && lv[0].members) || [];
+ const sent = await S().sentTo(lc(b.site), kids.map(k => lc(k.email))), directs = [];
+ for (const k of kids) { if (!k.username) continue; directs.push({ username: k.username, state: sent.has(lc(k.email)) ? 'sent' : (await refusal(k, sender)) ? 'no' : 'ok' }); }
+ return { ok: true, sender: sender.username, directs, left: await left(sender), paused: cfg().paused };
+}
+const sleep = ms => new Promise(r => setTimeout(r, ms));
+async function send(b) {
+ const siteId = lc(b.site), site = SITES[siteId]; if (!site) return { error: 'unknown site', code: 'site' };
+ const rid = String(b.rid || ''); if (!/^[A-Za-z0-9_.:-]{8,80}$/.test(rid)) return { error: 'rid needed', code: 'rid' };
+ const before = await S().byRid(rid); if (before.length) return { ok: true, already: true, results: before.map(r => ({ username: r.username, status: r.status })), sent: before.filter(r => r.status === 'sent').length };
+ if (cfg().paused) return { error: 'Invites are paused right now.', code: 'paused' };
+ if (!R.mailer || !R.mailer.hasKey()) return { error: 'Email is not available right now.', code: 'mail' };
+ const sender = await senderOf(b.sender); if (!sender) return { error: 'The sender has no InstantAdPay account with a username.', code: 'sender' };
+ const items = Array.isArray(b.items) ? b.items : []; if (!items.length || items.length > 25) return { error: 'Send 1 to 25 invites at a time.', code: 'count' };
+ const cn = cleanNote(b.note); if (cn.error) return { error: cn.error, code: 'note' };
+ const results = []; let sentN = 0; const seen = new Set();
+ for (const it of items) {
+ const uname = String((it && it.username) || '').trim(), res = { username: uname, status: '' }; results.push(res);
+ if (seen.has(lc(uname))) { res.status = 'duplicate'; continue; } seen.add(lc(uname));
+ if (!linkOk(it && it.link)) { res.status = 'bad link'; continue; }
+ const a = await R.accounts.byUsername(uname); if (!a) { res.status = 'no'; continue; }
+ if (!(await R.accounts.isDownlineOf(sender.email, a.email, 1))) { res.status = 'not your direct'; continue; }
+ if (await refusal(a, sender)) { res.status = 'no'; continue; }
+ const lf = await left(sender); if (lf.sender < 1) { res.status = 'your daily limit'; continue; } if (lf.network < 1) { res.status = 'network daily limit'; continue; }
+ const row = { site: siteId, recipient: lc(a.email), username: a.username, sender: lc(sender.email), senderName: sender.username, link: String(it.link), rid, ts: clock(), status: 'sending' };
+ if (!(await S().claim(row))) { res.status = 'sent before'; continue; }
+ try {
+ const m = compose(site, sender.username, a.username, row.link, cn.note);
+ const okSent = await R.mailer.send(a.email, m.subject, m.text, m.html);
+ if (okSent === false) { await S().unclaim(siteId, row.recipient); res.status = 'no'; continue; } // unsubscribed between the check and the send
+ await S().mark(siteId, row.recipient, 'sent'); res.status = 'sent'; sentN++;
+ } catch (e) { console.error('site invite send', e.message); await S().unclaim(siteId, row.recipient).catch(() => {}); res.status = 'failed'; }
+ await sleep(R.gapMs == null ? 400 : R.gapMs);
+ }
+ console.log('site invite', siteId, 'from', sender.username, 'sent', sentN, 'of', items.length);
+ return { ok: true, results, sent: sentN, left: await left(sender) };
+}
+async function handle(op, b) { if (op === 'eligible') return eligible(b || {}); if (op === 'send') return send(b || {}); return { error: 'unknown op' }; }
+async function adminView() { const since = dayStart(clock()), c = cfg(); return { on: enabled(), paused: c.paused, perSender: c.perSender, perDay: c.perDay, today: await S().count(since), recent: (await S().recent(200)).map(r => ({ site: r.site, to: r.username, from: r.senderName, ts: r.ts, status: r.status, link: r.link })) }; }
+// one sample to one address, for the owner to read before the feature opens. Not counted, not recorded.
+async function sample(to, siteId) { const site = SITES[lc(siteId) || 'videoappincome']; if (!site) return { error: 'unknown site' }; const m = compose(site, 'yoursponsor', 'yourname', 'https://' + hosts()[0] + '/p/faceless', ''); await R.mailer.send(to, '[SAMPLE] ' + m.subject, m.text, m.html); return { ok: true, to, subject: m.subject, text: m.text }; }
+
+module.exports = { init, enabled, verify, sign, handle, adminView, sample, compose, cleanNote, linkOk, DEFAULTS, SITES };