Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions public/js/deck/owner-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,18 @@ export function ago(ms, now = Date.now()) {
if (s < 30 * 86400) return Math.round(s / 86400) + " d ago";
return new Date(ms).toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" });
}

// The one-time reminder email (worker/winback.js): sent = [{ uid, sentAt, off }] from
// /api/admin/winback. -> { sent, off, cameBack }: cameBack = used Darknode after it arrived.
export function winbackStats(users = [], sent = [], now = Date.now()) {
const by = new Map(users.map((u) => [u.uid, u]));
let n = 0, off = 0, back = 0;
for (const w of sent) {
if (w.off) off++;
if (!w.sentAt) continue;
n++;
const u = by.get(w.uid);
if (u && personOf(u, now).last > w.sentAt) back++;
}
return { sent: n, off, cameBack: back };
}
49 changes: 44 additions & 5 deletions public/js/deck/owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { db } from "/js/firebase.js?v=20261021a";
import {
collection, getDocs, doc, getDoc, setDoc, deleteDoc, addDoc, updateDoc, query, orderBy, serverTimestamp,
} from "https://www.gstatic.com/firebasejs/12.17.0/firebase-firestore.js";
import { retention, peopleRows, tsMs, isOwnerRow, ago, FILTERS } from "./owner-stats.js?v=1";
import { retention, peopleRows, tsMs, isOwnerRow, ago, FILTERS, winbackStats } from "./owner-stats.js?v=1";

const S = { el: null, ctx: null, seq: 0, users: [], usage: new Map(), banned: [], filter: "all", q: "", open: "", inbox: null, ann: null };
const S = { el: null, ctx: null, seq: 0, users: [], usage: new Map(), banned: [], filter: "all", q: "", open: "", inbox: null, ann: null, wb: null };
const E = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]));
const toast = (m, t) => { try { S.ctx && S.ctx.toast ? S.ctx.toast(m, t) : window.showToast && window.showToast(m, t); } catch (_) {} };
const confirmIt = (title, body, o) => (window.dnConfirm ? window.dnConfirm(title, body, o) : Promise.resolve(false));
Expand Down Expand Up @@ -118,6 +118,7 @@ export function mountOwner(el, ctx) {
<div id="ownKpis"><div class="dk-loading"><span class="sp"></span>Loading…</div></div>
<div class="own-grid"><div class="own-panel" id="ownFunnel"></div><div class="own-panel" id="ownCohorts"></div></div>
<div class="own-panel" id="ownPeople" style="margin:0 0 14px"></div>
<div class="own-panel" id="ownWb" style="margin:0 0 14px"></div>
<div class="own-grid"><div class="own-panel" id="ownInbox"></div><div class="own-panel" id="ownSite"></div></div>
</div>`;
el.querySelector("#ownReload").onclick = () => load();
Expand All @@ -132,9 +133,9 @@ async function load() {
if (!live()) return;
S.users = users; S.usage = usage; S.banned = banned; S.ann = ann;
paintTop(); paintPeople(); paintSite();
const inbox = await loadInbox();
const [inbox, wb] = await Promise.all([loadInbox(), loadWb()]);
if (!live()) return;
S.inbox = inbox; paintInbox();
S.inbox = inbox; S.wb = wb; paintInbox(); paintWb(); if (S.open) paintPeople();
}

// users/{uid} docs plus the Auth accounts that never got one (an email sign-up that never verified)
Expand Down Expand Up @@ -163,6 +164,9 @@ async function loadBanned() {
async function loadAnn() {
try { const s = await getDoc(doc(db, "announcements", "current")); return s.exists() ? s.data() : {}; } catch (_) { return {}; }
}
async function loadWb() {
try { return await api("/api/admin/winback"); } catch (e) { return { error: e.message || String(e) }; }
}
async function loadInbox() {
const out = { upgrades: null, deletions: null, feedback: null, errors: null, fail: {} };
const byTs = (a, b) => tsMs(b.ts) - tsMs(a.ts);
Expand Down Expand Up @@ -238,7 +242,7 @@ function detailHtml(u, p, owner, banned, uses, now) {
${kv("Account id", u.uid)}${kv("Sign-in", u.provider || "email")}${kv("Joined", p.joined ? new Date(p.joined).toLocaleString() : "-")}
${kv("Last seen", p.last ? new Date(p.last).toLocaleString() : "-")}${kv("Days used", p.days + (u.activeDays ? "" : " (estimated)"))}
${kv("Email verified", p.verified ? "Yes" : "No")}${kv("Terms accepted", u.tosAccepted ? "Yes" + (u.tosVersion ? ", " + u.tosVersion : "") : "No")}
${uses.map((x) => kv(x.label, ago(x.last, now) + (x.version ? " (" + x.version + ")" : ""))).join("")}
${wbLine(u.uid, now)}${uses.map((x) => kv(x.label, ago(x.last, now) + (x.version ? " (" + x.version + ")" : ""))).join("")}
</div>
<div style="font-size:11px;color:#5a6680">Days the Deck was opened, last 60 days${days.size ? "" : ": none recorded yet"}</div>
<div class="own-days">${strip}</div>
Expand All @@ -247,6 +251,13 @@ function detailHtml(u, p, owner, banned, uses, now) {
</div>`;
}

function wbLine(uid, now) {
const w = S.wb && (S.wb.sent || []).find((x) => x.uid === uid);
if (!w) return "";
const v = (w.sentAt ? "Sent " + ago(w.sentAt, now) : "Not sent") + (w.off ? ", turned off" : "");
return `<div><span>Reminder email</span><b>${E(v)}</b></div>`;
}

function itemsHtml(list, fail, empty, row) {
if (fail) return `<p class="own-err">Couldn't load: ${E(fail)}</p>`;
if (!list) return `<p class="own-empty">Loading…</p>`;
Expand Down Expand Up @@ -282,6 +293,28 @@ function paintInbox() {
${ib.errors && ib.errors.length > 20 ? `<p class="own-empty">Showing the newest 20 of ${ib.errors.length}.</p>` : ""}`;
}

// The one-time reminder email to people who came once (worker/winback.js).
function paintWb() {
const host = S.el.querySelector("#ownWb"); if (!host) return;
const w = S.wb;
const head = (badge) => `<h2>Reminder email ${badge}</h2>
<p class="own-note">One email, once per person, to people who used Darknode on a single day and have no watch or weekly summary: it asks them to set up the daily domain watch. Sent daily at 15:00 UTC to at most 25 people who joined 2 to 45 days ago.</p>`;
if (!w) { host.innerHTML = head("") + '<p class="own-empty">Loading…</p>'; return; }
if (w.error) { host.innerHTML = head("") + `<p class="own-err">Couldn't load: ${E(w.error)}</p>`; return; }
const st = winbackStats(S.users, w.sent || []);
const email = new Map(S.users.map((u) => [u.uid, u.email || u.uid]));
const next = (w.next || []).map((uid) => email.get(uid) || uid);
host.innerHTML = head(`<span class="n${w.on ? "" : " hot"}">${w.on ? "On" : "Off on this server"}</span>`) + `
<div class="own-kv">
<div><span>Sent</span><b>${st.sent}</b></div>
<div><span>Came back after it</span><b>${st.sent ? st.cameBack + " (" + pct(st.cameBack, st.sent) + "%)" : "-"}</b></div>
<div><span>Turned reminders off</span><b>${st.off}</b></div>
<div><span>Next run</span><b>${next.length ? next.length + (next.length === 1 ? " person" : " people") : "No one"}</b></div>
</div>
${next.length ? `<p class="own-note" style="margin-top:8px">${next.map(E).join(", ")}</p>` : ""}
<div class="own-row"><button class="own-btn" data-act="wbtest">Send me a test</button></div>`;
}

function paintSite() {
const host = S.el.querySelector("#ownSite"); if (!host) return;
const a = S.ann || {};
Expand Down Expand Up @@ -333,6 +366,12 @@ async function doAct(act, btn) {
await updateDoc(doc(db, "upgradeRequests", item.dataset.rid), { status: "dismissed" });
S.inbox.upgrades = S.inbox.upgrades.filter((r) => r.id !== item.dataset.rid); paintInbox(); return;
}
if (act === "wbtest") {
btn.disabled = true;
try { const r = await api("/api/admin/winback", { method: "POST", body: { op: "preview" } }); toast("Test sent to " + ((r && r.to) || "your address"), "success"); }
finally { btn.disabled = false; }
return;
}
if (act === "delacct") {
const uid = item.dataset.del, who = item.dataset.email || uid;
if (!(await confirmIt("Delete " + who + "?", "This permanently deletes the account and everything stored with it, as they asked. It cannot be undone.", { submitText: "Delete account", danger: true }))) return;
Expand Down
3 changes: 2 additions & 1 deletion public/js/docs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading