| 1 | /* Guardian PWA client (FEP-633c): renders the dashboard, adopts wards, and
|
|---|
| 2 | manages the guardian push channel. No framework, no inline scripts (CSP).
|
|---|
| 3 | All user-facing text comes from state.strings (server i18n). */
|
|---|
| 4 | (function () {
|
|---|
| 5 | 'use strict';
|
|---|
| 6 | // A crash here used to fail silently (buttons just do nothing). Surface it on
|
|---|
| 7 | // the page AND the console so the cause is visible instead of "everything hangs".
|
|---|
| 8 | function fatal(msg) {
|
|---|
| 9 | try {
|
|---|
| 10 | var b = document.getElementById('g-fatal') || document.createElement('div');
|
|---|
| 11 | b.id = 'g-fatal'; b.className = 'g-msg err';
|
|---|
| 12 | b.style.cssText = 'display:block;margin:12px 0;padding:10px 14px';
|
|---|
| 13 | b.textContent = 'Guardian: ' + msg;
|
|---|
| 14 | var root = document.querySelector('main') || document.body;
|
|---|
| 15 | if (!b.parentNode && root) root.insertBefore(b, root.firstChild);
|
|---|
| 16 | } catch (e) { /* last resort */ }
|
|---|
| 17 | try { console.error('[guardian]', msg); } catch (e) { /* no console */ }
|
|---|
| 18 | }
|
|---|
| 19 | try {
|
|---|
| 20 | var S = JSON.parse(document.getElementById('guardian-state').textContent || '{}');
|
|---|
| 21 | var T = S.strings || {};
|
|---|
| 22 |
|
|---|
| 23 | function el(tag, cls, text) {
|
|---|
| 24 | var n = document.createElement(tag);
|
|---|
| 25 | if (cls) n.className = cls;
|
|---|
| 26 | if (text != null) n.textContent = text;
|
|---|
| 27 | return n;
|
|---|
| 28 | }
|
|---|
| 29 | function handleOf(uri, cached) {
|
|---|
| 30 | if (cached && cached.charAt(0) === '@') return cached; // trust only real @handles
|
|---|
| 31 | try { var u = new URL(uri); return '@' + u.pathname.split('/').filter(Boolean).pop() + '@' + u.host; }
|
|---|
| 32 | catch (e) { return uri; }
|
|---|
| 33 | }
|
|---|
| 34 | function when(s) { return String(s || '').slice(0, 16).replace('T', ' '); }
|
|---|
| 35 | function show(id, on) { document.getElementById(id).hidden = !on; }
|
|---|
| 36 |
|
|---|
| 37 | // ── 1. Help requests ───────────────────────────────────────────────────
|
|---|
| 38 | function renderHelp() {
|
|---|
| 39 | var list = document.getElementById('help-list');
|
|---|
| 40 | list.textContent = '';
|
|---|
| 41 | var help = S.help || [];
|
|---|
| 42 | help.forEach(function (h) {
|
|---|
| 43 | var card = el('div', 'g-card help');
|
|---|
| 44 | var row = el('div', 'row');
|
|---|
| 45 | row.appendChild(el('span', 'who grow', h.actor_name || handleOf(h.actor_uri, h.actor_handle)));
|
|---|
| 46 | row.appendChild(el('span', 'when', when(h.published || h.created_at)));
|
|---|
| 47 | card.appendChild(row);
|
|---|
| 48 | var body = el('div', 'body');
|
|---|
| 49 | body.innerHTML = h.content || ''; // sanitized server-side on ingest
|
|---|
| 50 | card.appendChild(body);
|
|---|
| 51 | if (h.note_url) {
|
|---|
| 52 | var a = el('a', 'g-link', T.open || 'open');
|
|---|
| 53 | a.href = h.note_url; a.target = '_blank'; a.rel = 'noopener';
|
|---|
| 54 | card.appendChild(a);
|
|---|
| 55 | }
|
|---|
| 56 | list.appendChild(card);
|
|---|
| 57 | });
|
|---|
| 58 | var badge = document.getElementById('help-count');
|
|---|
| 59 | badge.textContent = help.length; badge.hidden = help.length === 0;
|
|---|
| 60 | show('help-empty', help.length === 0);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | // ── 3. Offers I am a party to (sent, or a co-guardianship to co-approve) ─
|
|---|
| 64 | function answer(offerId, decision, btn) {
|
|---|
| 65 | if (btn) btn.disabled = true;
|
|---|
| 66 | fetch('/guardian/offer', {
|
|---|
| 67 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 68 | body: JSON.stringify({ offer: offerId, answer: decision, site: S.site }),
|
|---|
| 69 | }).then(refresh);
|
|---|
| 70 | }
|
|---|
| 71 | function offerCard(o) {
|
|---|
| 72 | var card = el('div', 'g-card');
|
|---|
| 73 | var row = el('div', 'row');
|
|---|
| 74 | var subject = o['shaer:iAmCandidate']
|
|---|
| 75 | ? handleOf(o['shaer:ward'], o['shaer:wardHandle']) // my sent offer: about the ward
|
|---|
| 76 | : handleOf(o['shaer:candidate'], o['shaer:candidateHandle']); // co-guard: who wants in
|
|---|
| 77 | row.appendChild(el('span', 'who grow', subject));
|
|---|
| 78 | if (o['shaer:iAmCandidate']) {
|
|---|
| 79 | // My own offer, waiting for the others to accept.
|
|---|
| 80 | row.appendChild(el('span', 'tag wait', T.pending));
|
|---|
| 81 | var rt = el('button', 'quiet small', T.retract);
|
|---|
| 82 | rt.addEventListener('click', function () { answer(o.id, 'reject', rt); });
|
|---|
| 83 | row.appendChild(rt);
|
|---|
| 84 | } else if (o['shaer:needsMyAccept']) {
|
|---|
| 85 | // A co-guardianship offer for a ward I already guard: my call.
|
|---|
| 86 | row.appendChild(el('span', 'tag co', T.coguard));
|
|---|
| 87 | var ac = el('button', 'small', T.accept);
|
|---|
| 88 | ac.addEventListener('click', function () { answer(o.id, 'accept', ac); });
|
|---|
| 89 | var rj = el('button', 'quiet small', T.reject);
|
|---|
| 90 | rj.addEventListener('click', function () { answer(o.id, 'reject', rj); });
|
|---|
| 91 | row.appendChild(ac); row.appendChild(rj);
|
|---|
| 92 | } else {
|
|---|
| 93 | row.appendChild(el('span', 'tag wait', T.awaiting_others));
|
|---|
| 94 | }
|
|---|
| 95 | card.appendChild(row);
|
|---|
| 96 | return card;
|
|---|
| 97 | }
|
|---|
| 98 | function renderPending() {
|
|---|
| 99 | var list = document.getElementById('pending-list');
|
|---|
| 100 | list.textContent = '';
|
|---|
| 101 | var offers = S.offers || [];
|
|---|
| 102 | offers.forEach(function (o) { list.appendChild(offerCard(o)); });
|
|---|
| 103 | show('pending-section', offers.length > 0);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // ── 4. Accepted wards ──────────────────────────────────────────────────
|
|---|
| 107 | function renderWards() {
|
|---|
| 108 | var list = document.getElementById('wards-list');
|
|---|
| 109 | list.textContent = '';
|
|---|
| 110 | var wards = S.wards || [];
|
|---|
| 111 | wards.forEach(function (w) {
|
|---|
| 112 | var card = el('div', 'g-card');
|
|---|
| 113 | var row = el('div', 'row');
|
|---|
| 114 | row.appendChild(el('span', 'who grow', handleOf(w.other_uri, w.other_handle)));
|
|---|
| 115 | row.appendChild(el('span', 'tag ok', T.active));
|
|---|
| 116 | var wave = el('button', 'small', T.wave || '👋 Wave');
|
|---|
| 117 | wave.addEventListener('click', function () { sendWave(w.other_uri, wave); });
|
|---|
| 118 | row.appendChild(wave);
|
|---|
| 119 | var btn = el('button', 'quiet small', T.release);
|
|---|
| 120 | btn.addEventListener('click', function () { remove(w.other_uri, btn); });
|
|---|
| 121 | row.appendChild(btn);
|
|---|
| 122 | card.appendChild(row);
|
|---|
| 123 | list.appendChild(card);
|
|---|
| 124 | });
|
|---|
| 125 | show('wards-empty', wards.length === 0);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | function sendWave(uri, btn) {
|
|---|
| 129 | btn.disabled = true;
|
|---|
| 130 | fetch('/guardian/api/wave', {
|
|---|
| 131 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 132 | body: JSON.stringify({ ward: uri, site: S.site }),
|
|---|
| 133 | }).then(function (r) { return r.json(); })
|
|---|
| 134 | .then(function (j) { btn.disabled = false; btn.textContent = (j && j.ok) ? (T.waved || '👋 sent') : (T.wave || '👋 Wave'); })
|
|---|
| 135 | .catch(function () { btn.disabled = false; });
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | function remove(uri, btn) {
|
|---|
| 139 | btn.disabled = true;
|
|---|
| 140 | fetch('/guardian/wards/remove', {
|
|---|
| 141 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 142 | body: JSON.stringify({ uri: uri, site: S.site }),
|
|---|
| 143 | }).then(refresh);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | function renderAll() { renderHelp(); renderPending(); renderWards(); }
|
|---|
| 147 |
|
|---|
| 148 | // ── 0. Wards' corner: read-only feed of your wards' posts ───────────────
|
|---|
| 149 | function renderFeed(items) {
|
|---|
| 150 | var list = document.getElementById('feed-list');
|
|---|
| 151 | list.textContent = '';
|
|---|
| 152 | (items || []).forEach(function (p) {
|
|---|
| 153 | var card = el('div', 'g-card feed');
|
|---|
| 154 | var head = el('div', 'row');
|
|---|
| 155 | head.appendChild(el('span', 'who grow', p.author));
|
|---|
| 156 | if (p.published) head.appendChild(el('span', 'g-when', when(p.published)));
|
|---|
| 157 | card.appendChild(head);
|
|---|
| 158 | var body = el('div', 'feed-body');
|
|---|
| 159 | if (p.cw) {
|
|---|
| 160 | var d = document.createElement('details');
|
|---|
| 161 | var sum = document.createElement('summary'); sum.textContent = p.cw; d.appendChild(sum);
|
|---|
| 162 | var inner = el('div'); inner.innerHTML = p.content || ''; d.appendChild(inner);
|
|---|
| 163 | body.appendChild(d);
|
|---|
| 164 | } else {
|
|---|
| 165 | body.innerHTML = p.content || ''; // server-sanitized HTML (same as Berichten)
|
|---|
| 166 | }
|
|---|
| 167 | card.appendChild(body);
|
|---|
| 168 | list.appendChild(card);
|
|---|
| 169 | });
|
|---|
| 170 | show('feed-section', (items || []).length > 0);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | function loadFeed() {
|
|---|
| 174 | return fetch('/guardian/api/feed?site=' + encodeURIComponent(S.site))
|
|---|
| 175 | .then(function (r) { return r.json(); })
|
|---|
| 176 | .then(function (f) { if (f && !f.error) renderFeed(f.items); })
|
|---|
| 177 | .catch(function () { /* corner just stays hidden */ });
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | // ── 0b. Follow requests on your wards (§5.3) ────────────────────────────
|
|---|
| 181 | function answerFollow(id, decision, btn) {
|
|---|
| 182 | if (btn) btn.disabled = true;
|
|---|
| 183 | fetch('/guardian/api/follow/' + encodeURIComponent(id), {
|
|---|
| 184 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 185 | body: JSON.stringify({ decision: decision, site: S.site }),
|
|---|
| 186 | }).then(loadFollowReqs);
|
|---|
| 187 | }
|
|---|
| 188 | function renderFollowReqs(items) {
|
|---|
| 189 | var list = document.getElementById('follow-list');
|
|---|
| 190 | list.textContent = '';
|
|---|
| 191 | (items || []).forEach(function (f) {
|
|---|
| 192 | var card = el('div', 'g-card');
|
|---|
| 193 | var row = el('div', 'row');
|
|---|
| 194 | row.appendChild(el('span', 'who grow', f.follower + ' → ' + f.ward));
|
|---|
| 195 | var ok = el('button', 'small', T.accept || 'Accept');
|
|---|
| 196 | ok.addEventListener('click', function () { answerFollow(f.id, 'approve', ok); });
|
|---|
| 197 | var no = el('button', 'quiet small', T.reject || 'Deny');
|
|---|
| 198 | no.addEventListener('click', function () { answerFollow(f.id, 'reject', no); });
|
|---|
| 199 | row.appendChild(ok); row.appendChild(no);
|
|---|
| 200 | card.appendChild(row);
|
|---|
| 201 | list.appendChild(card);
|
|---|
| 202 | });
|
|---|
| 203 | show('follow-section', (items || []).length > 0);
|
|---|
| 204 | }
|
|---|
| 205 | function loadFollowReqs() {
|
|---|
| 206 | return fetch('/guardian/api/follow-requests?site=' + encodeURIComponent(S.site))
|
|---|
| 207 | .then(function (r) { return r.json(); })
|
|---|
| 208 | .then(function (f) { if (f && !f.error) renderFollowReqs(f.items); })
|
|---|
| 209 | .catch(function () { /* stays hidden */ });
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | function refresh() {
|
|---|
| 213 | return fetch('/guardian/api/state?site=' + encodeURIComponent(S.site))
|
|---|
| 214 | .then(function (r) { return r.json(); })
|
|---|
| 215 | .then(function (s) { if (s && !s.error) { S = s; T = s.strings || T; renderAll(); } })
|
|---|
| 216 | .then(loadFeed).then(loadFollowReqs);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | // ── 2. Adopt ───────────────────────────────────────────────────────────
|
|---|
| 220 | var form = document.getElementById('adopt-form');
|
|---|
| 221 | var input = document.getElementById('adopt-handle');
|
|---|
| 222 | var adoptBtn = document.getElementById('adopt-btn');
|
|---|
| 223 | var msg = document.getElementById('adopt-msg');
|
|---|
| 224 | function setMsg(text, isErr) { msg.hidden = false; msg.className = 'g-msg' + (isErr ? ' err' : ''); msg.textContent = text; }
|
|---|
| 225 |
|
|---|
| 226 | form.addEventListener('submit', function (ev) {
|
|---|
| 227 | ev.preventDefault();
|
|---|
| 228 | var handle = input.value.trim();
|
|---|
| 229 | if (!handle) return;
|
|---|
| 230 | adoptBtn.disabled = true;
|
|---|
| 231 | setMsg(T.sending || '…', false);
|
|---|
| 232 | fetch('/guardian/adopt', {
|
|---|
| 233 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 234 | body: JSON.stringify({ handle: handle, site: S.site }),
|
|---|
| 235 | }).then(function (r) { return r.json().then(function (j) { return { ok: r.ok, j: j }; }); })
|
|---|
| 236 | .then(function (res) {
|
|---|
| 237 | adoptBtn.disabled = false;
|
|---|
| 238 | if (res.ok) {
|
|---|
| 239 | input.value = '';
|
|---|
| 240 | // Always refresh: the offer is recorded even if delivery is still
|
|---|
| 241 | // in flight. Show it under "Verzonden aanvragen".
|
|---|
| 242 | setMsg(res.j.delivered === false ? T.sent_retry : T.sent, false);
|
|---|
| 243 | refresh();
|
|---|
| 244 | } else {
|
|---|
| 245 | setMsg((res.j.error === 'not_found' ? T.not_found : T.failed) , true);
|
|---|
| 246 | }
|
|---|
| 247 | })
|
|---|
| 248 | .catch(function () { adoptBtn.disabled = false; setMsg(T.network, true); });
|
|---|
| 249 | });
|
|---|
| 250 |
|
|---|
| 251 | // ── Site picker ────────────────────────────────────────────────────────
|
|---|
| 252 | var picker = document.getElementById('site-picker');
|
|---|
| 253 | if (picker) picker.addEventListener('change', function () {
|
|---|
| 254 | location.href = '/guardian?site=' + encodeURIComponent(picker.value);
|
|---|
| 255 | });
|
|---|
| 256 |
|
|---|
| 257 | // ── 5. Push ────────────────────────────────────────────────────────────
|
|---|
| 258 | var toggle = document.getElementById('push-toggle');
|
|---|
| 259 | var pmsg = document.getElementById('push-msg');
|
|---|
| 260 | function pushState() {
|
|---|
| 261 | if (!('serviceWorker' in navigator) || !('PushManager' in window)) { toggle.disabled = true; return; }
|
|---|
| 262 | navigator.serviceWorker.register('/sw.js').catch(function () {});
|
|---|
| 263 | navigator.serviceWorker.ready
|
|---|
| 264 | .then(function (reg) { return reg.pushManager.getSubscription(); })
|
|---|
| 265 | .then(function (sub) {
|
|---|
| 266 | toggle.textContent = sub ? toggle.dataset.onLabel : toggle.dataset.offLabel;
|
|---|
| 267 | toggle.dataset.subscribed = sub ? '1' : '';
|
|---|
| 268 | toggle.classList.toggle('is-on', !!sub);
|
|---|
| 269 | });
|
|---|
| 270 | }
|
|---|
| 271 | function urlB64(base64) {
|
|---|
| 272 | var pad = '='.repeat((4 - (base64.length % 4)) % 4);
|
|---|
| 273 | var b = (base64 + pad).replace(/-/g, '+').replace(/_/g, '/');
|
|---|
| 274 | var raw = atob(b); var arr = new Uint8Array(raw.length);
|
|---|
| 275 | for (var i = 0; i < raw.length; i++) arr[i] = raw.charCodeAt(i);
|
|---|
| 276 | return arr;
|
|---|
| 277 | }
|
|---|
| 278 | toggle.addEventListener('click', function () {
|
|---|
| 279 | pmsg.hidden = true;
|
|---|
| 280 | navigator.serviceWorker.ready.then(function (reg) {
|
|---|
| 281 | if (toggle.dataset.subscribed) {
|
|---|
| 282 | reg.pushManager.getSubscription().then(function (sub) {
|
|---|
| 283 | if (!sub) return;
|
|---|
| 284 | fetch('/push/unsubscribe', {
|
|---|
| 285 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 286 | body: JSON.stringify({ endpoint: sub.endpoint }),
|
|---|
| 287 | }).then(function () { return sub.unsubscribe(); }).then(pushState);
|
|---|
| 288 | });
|
|---|
| 289 | return;
|
|---|
| 290 | }
|
|---|
| 291 | fetch('/push/vapid').then(function (r) { return r.json(); }).then(function (v) {
|
|---|
| 292 | if (!v.publicKey) throw new Error('no key');
|
|---|
| 293 | return reg.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlB64(v.publicKey) });
|
|---|
| 294 | }).then(function (sub) {
|
|---|
| 295 | return fetch('/push/subscribe', {
|
|---|
| 296 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 297 | body: JSON.stringify({
|
|---|
| 298 | subscription: sub.toJSON(),
|
|---|
| 299 | alerts: { help: 1, guardian: 1, dm: 1, follow: 0, reply: 0, like: 0, boost: 0 },
|
|---|
| 300 | uaLabel: 'Guardian PWA',
|
|---|
| 301 | }),
|
|---|
| 302 | });
|
|---|
| 303 | }).then(pushState).catch(function (e) {
|
|---|
| 304 | pmsg.hidden = false; pmsg.className = 'g-msg err';
|
|---|
| 305 | pmsg.textContent = (T.push_unavailable || 'Push unavailable') + ': ' + e.message;
|
|---|
| 306 | });
|
|---|
| 307 | });
|
|---|
| 308 | });
|
|---|
| 309 |
|
|---|
| 310 | renderAll(); pushState(); loadFeed(); loadFollowReqs();
|
|---|
| 311 | setInterval(refresh, 45000); // live-ish while open
|
|---|
| 312 | } catch (e) {
|
|---|
| 313 | fatal((e && e.message) || String(e));
|
|---|
| 314 | }
|
|---|
| 315 | })();
|
|---|