Message kinds from InstantAdPay (broadcast / alert / notice): carry-over notices are quiet inbox notices, never the sign-in modal; a one-time startup pass reclassifies the ones already stored
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+30
-10
@@ -27,32 +27,38 @@ async function lastBroadcastAt(fromEmail) {
|
||||
return mine.length ? Math.max(...mine) : 0;
|
||||
}
|
||||
// deliver one message to many recipients (already-resolved emails). Returns count.
|
||||
async function deliver(fromMember, fromEmail, recipients, subject, body) {
|
||||
// `kind` (ported from InstantAdPay 2026-10-15): 'broadcast' = a person wrote it (may pop the sign-in modal);
|
||||
// 'alert' = system message where the member loses money by not acting (may pop); 'notice' = system message,
|
||||
// nothing required: inbox only, never a modal (carry-over notices, Marty: quiet inbox, seen once they log in).
|
||||
async function deliver(fromMember, fromEmail, recipients, subject, body, kind) {
|
||||
const k = ['notice', 'alert'].includes(kind) ? kind : 'broadcast';
|
||||
const now = Date.now();
|
||||
let n = 0;
|
||||
if (db.enabled()) {
|
||||
for (const to of recipients) {
|
||||
await db.q("INSERT INTO sponsor_messages (from_member,from_email,to_email,subject,body,sent,kind) VALUES (?,?,?,?,?,?,'broadcast')",
|
||||
[fromMember || 0, fromEmail, to, subject, body, now]);
|
||||
await db.q("INSERT INTO sponsor_messages (from_member,from_email,to_email,subject,body,sent,kind) VALUES (?,?,?,?,?,?,?)",
|
||||
[fromMember || 0, fromEmail, to, subject, body, now, k]);
|
||||
n++;
|
||||
}
|
||||
} else {
|
||||
if (!J.db) J.load();
|
||||
for (const to of recipients) {
|
||||
J.db.items.push({ id: J.db.nextId++, kind: 'broadcast', fromMember: fromMember || 0, fromEmail, toEmail: to, subject, body, sent: now, readTs: 0 });
|
||||
J.db.items.push({ id: J.db.nextId++, kind: k, fromMember: fromMember || 0, fromEmail, toEmail: to, subject, body, sent: now, readTs: 0 });
|
||||
n++;
|
||||
}
|
||||
J.save();
|
||||
}
|
||||
return n;
|
||||
}
|
||||
const isBroadcast = i => (i.kind || 'broadcast') === 'broadcast';
|
||||
// every non-chat kind is inbox mail; only a human broadcast or a money-at-stake alert may pop the modal
|
||||
const isBroadcast = i => (i.kind || 'broadcast') !== 'chat';
|
||||
const popsModal = i => ['broadcast', 'alert'].includes(i.kind || 'broadcast');
|
||||
const isChat = i => i.kind === 'chat';
|
||||
async function inbox(email) {
|
||||
const e = String(email || '').toLowerCase();
|
||||
if (db.enabled()) {
|
||||
const rows = await db.q(`SELECT id, from_member, subject, body, sent, read_ts FROM sponsor_messages
|
||||
WHERE to_email=? AND kind='broadcast' ORDER BY sent DESC LIMIT 100`, [e]);
|
||||
WHERE to_email=? AND kind<>'chat' ORDER BY sent DESC LIMIT 100`, [e]);
|
||||
return rows.map(r => ({ id: r.id, fromMember: r.from_member, subject: r.subject, body: r.body,
|
||||
sent: Number(r.sent), read: !!r.read_ts }));
|
||||
}
|
||||
@@ -145,7 +151,7 @@ async function threadList(email) {
|
||||
async function unreadCount(email) {
|
||||
const e = String(email || '').toLowerCase();
|
||||
if (db.enabled()) {
|
||||
const r = await db.q("SELECT COUNT(*) n FROM sponsor_messages WHERE to_email=? AND kind='broadcast' AND read_ts IS NULL", [e]);
|
||||
const r = await db.q("SELECT COUNT(*) n FROM sponsor_messages WHERE to_email=? AND kind<>'chat' AND read_ts IS NULL", [e]);
|
||||
return r[0].n;
|
||||
}
|
||||
if (!J.db) J.load();
|
||||
@@ -156,13 +162,13 @@ async function newestUnread(email) {
|
||||
const e = String(email || '').toLowerCase();
|
||||
if (db.enabled()) {
|
||||
const rows = await db.q(`SELECT id, from_member, subject, body, sent FROM sponsor_messages
|
||||
WHERE to_email=? AND kind='broadcast' AND read_ts IS NULL ORDER BY sent DESC LIMIT 1`, [e]);
|
||||
WHERE to_email=? AND kind IN ('broadcast','alert') AND read_ts IS NULL ORDER BY sent DESC LIMIT 1`, [e]);
|
||||
if (!rows.length) return null;
|
||||
const r = rows[0];
|
||||
return { id: r.id, fromMember: r.from_member, subject: r.subject, body: r.body, sent: Number(r.sent) };
|
||||
}
|
||||
if (!J.db) J.load();
|
||||
const u = J.db.items.filter(i => i.toEmail === e && isBroadcast(i) && !i.readTs).sort((a, b) => b.sent - a.sent)[0];
|
||||
const u = J.db.items.filter(i => i.toEmail === e && popsModal(i) && !i.readTs).sort((a, b) => b.sent - a.sent)[0];
|
||||
return u ? { id: u.id, fromMember: u.fromMember, subject: u.subject, body: u.body, sent: u.sent } : null;
|
||||
}
|
||||
async function markRead(email, id) {
|
||||
@@ -177,5 +183,19 @@ async function markRead(email, id) {
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
// one-time reclassify: system notices stored as 'broadcast' before the kinds existed become quiet 'notice' (idempotent)
|
||||
const CARRY_SUBJECTS = /(Your LinkSpin payouts are switched on|We set up your LinkSpin position|just joined LinkSpin under you|was placed under you on LinkSpin|You missed .* on LinkSpin|is ready to buy on LinkSpin|The seat for .* passed to)/;
|
||||
async function reclassifyCarryNotices(fromEmail) {
|
||||
const f = String(fromEmail || '').toLowerCase();
|
||||
if (db.enabled()) {
|
||||
const rows = await db.q("SELECT id, subject FROM sponsor_messages WHERE kind='broadcast' AND from_member=1 AND LOWER(from_email)=?", [f]);
|
||||
let n = 0; for (const r of rows) if (CARRY_SUBJECTS.test(r.subject)) { await db.q("UPDATE sponsor_messages SET kind='notice' WHERE id=?", [r.id]); n++; }
|
||||
return n;
|
||||
}
|
||||
if (!J.db) J.load(); let n = 0;
|
||||
for (const i of J.db.items) if ((i.kind || 'broadcast') === 'broadcast' && Number(i.fromMember) === 1 && String(i.fromEmail || '').toLowerCase() === f && CARRY_SUBJECTS.test(i.subject || '')) { i.kind = 'notice'; n++; }
|
||||
if (n) J.save();
|
||||
return n;
|
||||
}
|
||||
module.exports = { init, lastFrom, lastBroadcastAt, deliver, inbox, unreadCount, newestUnread, markRead,
|
||||
sendChat, thread, markChatRead, chatUnread, threadList };
|
||||
sendChat, thread, markChatRead, chatUnread, threadList, reclassifyCarryNotices };
|
||||
|
||||
Reference in New Issue
Block a user