Files
webworthit/db.js
T
martbost 47de249a8b Free check partly gated behind joining the network (Marty 2026-11-16)
Not signed in: grade, verdict, first finding and the opinion line; the rest (scores, findings,
chain facts, screenshot, evidence) stays on the server. Join or sign in by one-time email link
(hub /v1/join with the wwi.ref sponsor when HUB_JOIN=1; staging reads only). 1 free check a day
without an account, 3 for members. Red flags deduped (deterministic VERIFIED one kept), grade
recomputed on read. Privacy: joining and emails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:36:29 -06:00

49 lines
4.5 KiB
JavaScript

// WebWorthIt data: MySQL only (Marty's rule: the database is the gold standard, no JSON file stores).
// DOCSTORE_URL = mysql://user:pass@host:3306/webworthit (the same database also holds docstore.js documents: kb, sso nonces).
'use strict';
const mysql = require('mysql2/promise');
let pool = null;
const SCHEMA = [
`CREATE TABLE IF NOT EXISTS checks (
id CHAR(12) PRIMARY KEY, created BIGINT NOT NULL, ip_hash CHAR(24), holder VARCHAR(120), member VARCHAR(190),
kind VARCHAR(8) NOT NULL, website VARCHAR(500), chain VARCHAR(12), address CHAR(42), picked_note TEXT,
status VARCHAR(16) NOT NULL, step VARCHAR(40), steps_json TEXT, started BIGINT, finished BIGINT, timings_json TEXT,
pack_chars INT, model VARCHAR(80), cost_usd DECIMAL(12,6) DEFAULT 0, tokens_in INT, tokens_out INT,
report_json MEDIUMTEXT, factcheck_json MEDIUMTEXT, facts_json MEDIUMTEXT, site_json MEDIUMTEXT, evidence_json LONGTEXT, pack_md LONGTEXT,
error TEXT, parent_id CHAR(12), public_label VARCHAR(120), is_public TINYINT DEFAULT 0,
INDEX (created), INDEX (address), INDEX (is_public)) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS screenshots (check_id CHAR(12) PRIMARY KEY, mime VARCHAR(40), bytes MEDIUMBLOB, created BIGINT) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS daily_free (day CHAR(10) NOT NULL, ip_hash CHAR(24) NOT NULL, n INT NOT NULL DEFAULT 0, PRIMARY KEY (day, ip_hash)) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS unlock_codes (code_hash CHAR(64) PRIMARY KEY, label VARCHAR(120), created BIGINT, uses INT DEFAULT 0, disabled TINYINT DEFAULT 0) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS promo_codes (code VARCHAR(40) PRIMARY KEY, kind VARCHAR(16) NOT NULL, amount INT NOT NULL, max_uses INT NOT NULL DEFAULT 1,
uses INT NOT NULL DEFAULT 0, per_holder_once TINYINT NOT NULL DEFAULT 1, expires BIGINT, note VARCHAR(300), disabled TINYINT NOT NULL DEFAULT 0, created BIGINT) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS promo_redemptions (id INT AUTO_INCREMENT PRIMARY KEY, code VARCHAR(40) NOT NULL, holder VARCHAR(190) NOT NULL, at BIGINT NOT NULL,
kind VARCHAR(16), amount INT, ip_hash CHAR(24), INDEX (code), INDEX (holder)) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS credits (holder VARCHAR(190) PRIMARY KEY, deep INT NOT NULL DEFAULT 0, free_extra INT NOT NULL DEFAULT 0, updated BIGINT) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS purchases (id INT AUTO_INCREMENT PRIMARY KEY, at BIGINT NOT NULL, buyer VARCHAR(190), buyer_username VARCHAR(60), buyer_wallet CHAR(42),
item VARCHAR(20) NOT NULL, usd DECIMAL(10,2) NOT NULL, via VARCHAR(16) NOT NULL, check_id CHAR(12), ref VARCHAR(60),
line_json TEXT, split_json TEXT, note VARCHAR(300), real_money TINYINT NOT NULL DEFAULT 0, INDEX (at), INDEX (buyer), INDEX (buyer_username), INDEX (buyer_wallet)) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS members (email VARCHAR(190) PRIMARY KEY, hub_id VARCHAR(40), username VARCHAR(60), wallet CHAR(42), sponsor VARCHAR(60), created BIGINT, last_seen BIGINT,
INDEX (username), INDEX (wallet)) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS joins (token_hash CHAR(64) PRIMARY KEY, email VARCHAR(190) NOT NULL, username VARCHAR(40), ref VARCHAR(60), optin TINYINT NOT NULL DEFAULT 0,
to_path VARCHAR(200), created BIGINT NOT NULL, expires BIGINT NOT NULL, confirmed_at BIGINT, ip_hash CHAR(24), status VARCHAR(16) NOT NULL DEFAULT 'pending',
mailed TINYINT NOT NULL DEFAULT 0, hub_result VARCHAR(300), INDEX (email), INDEX (created)) ENGINE=InnoDB`,
`CREATE TABLE IF NOT EXISTS sessions (id CHAR(48) PRIMARY KEY, email VARCHAR(190) NOT NULL, created BIGINT NOT NULL, expires BIGINT NOT NULL, INDEX (email)) ENGINE=InnoDB`
];
async function init(url) {
if (!url) throw new Error('DOCSTORE_URL is required (MySQL). WebWorthIt never runs on files.');
pool = mysql.createPool({ uri: url, connectionLimit: 8, waitForConnections: true, supportBigNumbers: true, bigNumberStrings: false, charset: 'utf8mb4' });
for (let i = 0; i < 30; i++) {
try { await pool.query('SELECT 1'); break; } catch (e) { if (i === 29) throw e; await new Promise(r => setTimeout(r, 2000)); }
}
for (const s of SCHEMA) await pool.query(s);
for (const a of ['ALTER TABLE members ADD COLUMN optin TINYINT NOT NULL DEFAULT 0', "ALTER TABLE members ADD COLUMN via VARCHAR(20)"]) {
try { await pool.query(a); } catch (e) { if (!/Duplicate column/i.test(e.message)) throw e; }
}
}
const q = (sql, args) => pool.query(sql, args).then(r => r[0]);
const one = async (sql, args) => (await q(sql, args))[0] || null;
module.exports = { init, q, one, pool: () => pool };