Files
linkspin/loginadclock.js
T
martbost 73bee6c826 Login ad on a clock, hop back to the asking site, 5xYourTeam in the wallets map, signed-out login slot refused (Marty 2026-11-15)
Login ad: every Control Room page asks (cr-shell.js loginAd, the per-tab sessionStorage guard is gone); the server answers with an ad only when the member's last finished view on LinkSpin is older than loginAdEveryHours (site setting, default 3). loginadclock.js (the same file as InstantAdPay), MySQL table login_ad_views through DOCSTORE_URL. POST /api/ads/login/seen at the end of the countdown restarts the clock only if the serve was dwell-2 s earlier. Member erase forgets the row. Admin site setting label.
Signed out: /api/ads/slot?type=login draws nothing and counts no view (bots, probes, previews cannot run up an advertiser's numbers).
Hop: a member who signed in here because another network site asked ("Sign in with LinkSpin", nsso.back) goes back there after the login ad (/sso/resume), from every Control Room screen, not only the retired /my.
networksso.js: the network-wide copy (loop guard 120 s, fivex, unknown r refused); fivex added to the wallets map (FIVEX_URL, default https://5xyourteam.com).
Tests: qa/networksso-partner.e2e.mjs (same file as InstantAdPay, in run.sh): partner via LinkSpin, LinkSpin clock, signed-out slot; networksso e2e retries a keep-alive ECONNRESET. qa sso: ALL PASSED; qa member: ALL PASSED (2 known warnings; networksso e2e flaked on ECONNRESET once, passes with the retry).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 15:03:14 -06:00

77 lines
4.3 KiB
JavaScript

// Login-ad clock (Marty 2026-11-15): "I definitely don't want somebody to not look at a login ad for 7 days... if login ads are
// available on those sites, they need to be seen several times a day." The SAME file ships in InstantAdPay and LinkSpin.
//
// A member sees the login ad on their next member-area page load whenever their last finished login-ad view on THIS site is more
// than loginAdEveryHours old (site setting, default 3), whether or not they just signed in. One row per member in MySQL
// (table login_ad_views: k = lowercased email, or 'w:' + wallet for a wallet-only session; seen_at; served_at).
// due(k) true when the member should get the login ad now (no row, or seen_at older than the setting)
// served(k) the login ad was handed to this member (the countdown starts on their screen)
// seen(k, s) the countdown ended: counts only when a serve to this member happened at least s - 2 seconds ago and within the
// last 30 minutes, so reloading during the countdown never skips the ad
// forget(k) member erase
// No database (local QA): the same rules in memory. A failed database read answers "due": when in doubt the ad shows.
'use strict';
let pool = null, tableReady = null, hours = () => 3;
const mem = new Map(); // k -> { seen, served }
const SERVE_WINDOW = 30 * 60 * 1000;
const keyOf = s => {
if (!s) return '';
const e = String(s.email || '').trim().toLowerCase();
if (e) return e.slice(0, 190);
const a = String(s.address || '').trim().toLowerCase();
return /^0x[0-9a-f]{40}$/.test(a) ? 'w:' + a : '';
};
const everyMs = () => { const h = Number(hours()); return (Number.isFinite(h) && h > 0 ? Math.min(h, 24 * 30) : 3) * 3600 * 1000; };
function init(opts) {
const o = opts || {};
if (typeof o.hours === 'function') hours = o.hours;
const url = String(o.url || '').trim();
pool = null; tableReady = null; mem.clear();
if (url) { const mysql = require('mysql2/promise'); pool = mysql.createPool({ uri: url, connectionLimit: 3, charset: 'utf8mb4' }); }
}
async function table() {
if (!tableReady) tableReady = pool.query('CREATE TABLE IF NOT EXISTS login_ad_views (k VARCHAR(200) PRIMARY KEY, seen_at BIGINT NOT NULL DEFAULT 0, served_at BIGINT NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4').catch(e => { tableReady = null; throw e; });
return tableReady;
}
async function row(k) {
if (!pool) return mem.get(k) || null;
await table();
const [r] = await pool.query('SELECT seen_at, served_at FROM login_ad_views WHERE k = ?', [k]);
return r[0] ? { seen: Number(r[0].seen_at) || 0, served: Number(r[0].served_at) || 0 } : null;
}
async function due(k, now) {
if (!k) return false;
const t = now || Date.now();
try { const r = await row(k); return !r || !r.seen || t - r.seen >= everyMs(); } catch (e) { console.error('login-ad clock read', e.message); return true; }
}
async function served(k, now) {
if (!k) return;
const t = now || Date.now();
if (!pool) { const r = mem.get(k) || { seen: 0, served: 0 }; r.served = t; mem.set(k, r); return; }
try { await table(); await pool.query('INSERT INTO login_ad_views (k, served_at) VALUES (?, ?) ON DUPLICATE KEY UPDATE served_at = VALUES(served_at)', [k, t]); }
catch (e) { console.error('login-ad clock serve', e.message); }
}
// -> { ok: true } or { ok: false, reason }
async function seen(k, dwellSec, now) {
if (!k) return { ok: false, reason: 'signed out' };
const t = now || Date.now();
const need = Math.max(0, (Number(dwellSec) || 10) - 2) * 1000;
let r = null; try { r = await row(k); } catch (e) { console.error('login-ad clock read', e.message); return { ok: false, reason: 'store' }; }
if (!r || !r.served || t - r.served > SERVE_WINDOW) return { ok: false, reason: 'not served' };
if (t - r.served < need) return { ok: false, reason: 'too soon' };
if (!pool) { r.seen = t; mem.set(k, r); return { ok: true }; }
try { await pool.query('UPDATE login_ad_views SET seen_at = ? WHERE k = ?', [t, k]); return { ok: true }; }
catch (e) { console.error('login-ad clock seen', e.message); return { ok: false, reason: 'store' }; }
}
async function forget(k) {
if (!k) return;
mem.delete(k);
if (pool) { try { await table(); await pool.query('DELETE FROM login_ad_views WHERE k = ?', [k]); } catch (e) { console.error('login-ad clock forget', e.message); } }
}
module.exports = { init, keyOf, due, served, seen, forget, everyMs };