The site could only reach somebody who was already looking at it. Push existed and adventure used none of it, so the one communal event in the game -- the Siege -- was invisible to anyone not sitting in Matrix, and a player whose adventurer died found out whenever they next opened a tab. Four opt-in categories, every one of them off until asked for: the Siege (realm-wide, begins and ends), your expedition ending, your adventurer wandering off, and a contract landing on you. Turning on news notifications is not consent to be told about the game, so nothing here enrolls anybody automatically. No new wire. Every trigger is a dispatch already landing in adventure_events, so this is Pete-side only and gogobee is untouched. Two things it needed from storage. push_subscriptions now keeps the Matrix localpart alongside the OIDC subject, because every ownership join in the schema is keyed on the localpart and the sender runs on a ticker with no session to read one from -- without it there is no way to answer "whose adventurer is this". And the alerts carry their own watermark, kept apart from the digest's: the two run on different clocks and one column would let each consume the other's backlog. The ownership join is re-read on every pass rather than trusted from the subscription row, so an opt-out or a removal closes the channel at once. It fails closed in both directions, and an unresolved owner can never fall through to a broadcast -- a game alert naming somebody's adventurer, delivered to the wrong phone, is a privacy leak dressed as a feature. An existing subscription carries watermark 0, which read literally means "has never been told anything" and would page every subscriber for the whole history of the realm on the first tick after deploy. Those rows are stamped to now and start from the next dispatch. Verified against a running Pete with a real push service, real P-256 client keys and real encryption: the right person is notified, the wrong one is not, a second pass is silent, and dropping the player from the board takes the channel with it. Claude-Session: https://claude.ai/code/session_012bxpQQJDjC1mTtLN3VVtBQ
77 lines
2.9 KiB
JavaScript
77 lines
2.9 KiB
JavaScript
// Preference sync. Anonymous visitors keep using localStorage exactly as
|
|
// before — this file is a no-op for them. When a user is signed in (Authentik
|
|
// via OIDC), the server is the source of truth: their stored blob is injected
|
|
// as window.PETE_PREFS and seeded into localStorage *synchronously* here, before
|
|
// the feature scripts (settings.js, weather*.js) read it. Those scripts then
|
|
// call PetePrefs.push() after every write to mirror the change back up.
|
|
//
|
|
// This script must run before the others — it's loaded first in layout.html,
|
|
// and all the feature scripts are `defer`, so document order is guaranteed.
|
|
(function () {
|
|
// The localStorage keys we sync. The weather *cache* is deliberately excluded:
|
|
// it's transient and per-device.
|
|
// pete.advPush.v1 is read by the *server* — the adventure alert sender parses
|
|
// it out of the stored blob to decide who to notify — where every other key
|
|
// here is only ever read back by a feature script. If it stops syncing, the
|
|
// toggles keep working locally and no alert is ever sent, which is the kind of
|
|
// failure nobody reports.
|
|
var SYNCED = ["pete.disabledSources.v1", "pete.weather.loc.v1", "pete-weather-off", "pete.sfx.off",
|
|
"pete.advPush.v1"];
|
|
|
|
var user = window.PETE_USER || null;
|
|
var serverPrefs = window.PETE_PREFS || null;
|
|
|
|
function seed(prefs) {
|
|
SYNCED.forEach(function (k) {
|
|
if (!Object.prototype.hasOwnProperty.call(prefs, k)) return;
|
|
var v = prefs[k];
|
|
try {
|
|
if (v === null || v === undefined) localStorage.removeItem(k);
|
|
else localStorage.setItem(k, String(v));
|
|
} catch (e) {}
|
|
});
|
|
}
|
|
|
|
function snapshot() {
|
|
var out = {};
|
|
SYNCED.forEach(function (k) {
|
|
try { var v = localStorage.getItem(k); if (v !== null) out[k] = v; } catch (e) {}
|
|
});
|
|
return out;
|
|
}
|
|
|
|
var timer = null;
|
|
function push() {
|
|
if (!user) return; // anonymous: localStorage only
|
|
if (timer) clearTimeout(timer);
|
|
timer = setTimeout(function () {
|
|
timer = null;
|
|
try {
|
|
fetch("/api/preferences", {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(snapshot()),
|
|
credentials: "same-origin",
|
|
keepalive: true
|
|
}).catch(function () {});
|
|
} catch (e) {}
|
|
}, 600);
|
|
}
|
|
|
|
window.PetePrefs = { push: push, loggedIn: !!user, syncedKeys: SYNCED };
|
|
|
|
if (user) {
|
|
if (serverPrefs && typeof serverPrefs === "object") {
|
|
seed(serverPrefs); // cross-device: server wins on load
|
|
} else {
|
|
push(); // first sign-in: migrate this browser's prefs to the account
|
|
}
|
|
// Swap "saved in this browser" copy for the synced story.
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
document.querySelectorAll("[data-storage-note]").forEach(function (el) {
|
|
el.textContent = "Synced to your account ✓";
|
|
});
|
|
});
|
|
}
|
|
})();
|