| 1 | /* Guardian PWA client (FEP-633c): renders the dashboard state, adopts wards,
|
|---|
| 2 | and manages the guardian push channel (web-push slice reused: alert types
|
|---|
| 3 | 'help' + 'guardian'). No framework, no inline scripts (CSP). */
|
|---|
| 4 | (function () {
|
|---|
| 5 | 'use strict';
|
|---|
| 6 | var state = JSON.parse(document.getElementById('guardian-state').textContent || '{}');
|
|---|
| 7 |
|
|---|
| 8 | function el(tag, cls, text) {
|
|---|
| 9 | var n = document.createElement(tag);
|
|---|
| 10 | if (cls) n.className = cls;
|
|---|
| 11 | if (text != null) n.textContent = text;
|
|---|
| 12 | return n;
|
|---|
| 13 | }
|
|---|
| 14 | function handleOf(uri, cached) {
|
|---|
| 15 | if (cached) return cached;
|
|---|
| 16 | try { var u = new URL(uri); return '@' + u.pathname.split('/').filter(Boolean).pop() + '@' + u.host; }
|
|---|
| 17 | catch (e) { return uri; }
|
|---|
| 18 | }
|
|---|
| 19 | function when(s) {
|
|---|
| 20 | return String(s || '').slice(0, 16).replace('T', ' ');
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | // ── Message centre: help requests ──────────────────────────────────────
|
|---|
| 24 | function renderHelp() {
|
|---|
| 25 | var list = document.getElementById('help-list');
|
|---|
| 26 | list.textContent = '';
|
|---|
| 27 | (state.help || []).forEach(function (h) {
|
|---|
| 28 | var card = el('div', 'g-card help');
|
|---|
| 29 | var row = el('div', 'row');
|
|---|
| 30 | var who = el('span', 'who', h.actor_name || handleOf(h.actor_uri, h.actor_handle));
|
|---|
| 31 | var at = el('span', 'when', when(h.published || h.created_at));
|
|---|
| 32 | row.appendChild(who); row.appendChild(at);
|
|---|
| 33 | card.appendChild(row);
|
|---|
| 34 | var body = el('div', 'body');
|
|---|
| 35 | body.innerHTML = h.content || ''; // sanitized server-side on ingest
|
|---|
| 36 | card.appendChild(body);
|
|---|
| 37 | if (h.note_url) {
|
|---|
| 38 | var link = el('a', 'when', 'open');
|
|---|
| 39 | link.href = h.note_url; link.target = '_blank'; link.rel = 'noopener';
|
|---|
| 40 | card.appendChild(link);
|
|---|
| 41 | }
|
|---|
| 42 | list.appendChild(card);
|
|---|
| 43 | });
|
|---|
| 44 | document.getElementById('help-empty').hidden = (state.help || []).length > 0;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | // ── Wards + pending offers ─────────────────────────────────────────────
|
|---|
| 48 | function wardCard(w, pending) {
|
|---|
| 49 | var card = el('div', 'g-card');
|
|---|
| 50 | var row = el('div', 'row');
|
|---|
| 51 | var who = el('span', 'who grow', handleOf(w.other_uri, w.other_handle));
|
|---|
| 52 | row.appendChild(who);
|
|---|
| 53 | if (pending) row.appendChild(el('span', 'pend', state.strings.pending));
|
|---|
| 54 | var btn = el('button', 'quiet', pending ? state.strings.retract : state.strings.release);
|
|---|
| 55 | btn.addEventListener('click', function () {
|
|---|
| 56 | fetch('/guardian/wards/remove', {
|
|---|
| 57 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 58 | body: JSON.stringify({ uri: w.other_uri, site: state.site }),
|
|---|
| 59 | }).then(refresh);
|
|---|
| 60 | });
|
|---|
| 61 | row.appendChild(btn);
|
|---|
| 62 | card.appendChild(row);
|
|---|
| 63 | return card;
|
|---|
| 64 | }
|
|---|
| 65 | function renderWards() {
|
|---|
| 66 | var wl = document.getElementById('wards-list');
|
|---|
| 67 | var ol = document.getElementById('offers-list');
|
|---|
| 68 | wl.textContent = ''; ol.textContent = '';
|
|---|
| 69 | (state.wards || []).forEach(function (w) { wl.appendChild(wardCard(w, false)); });
|
|---|
| 70 | (state.pendingOffers || []).forEach(function (w) { ol.appendChild(wardCard(w, true)); });
|
|---|
| 71 | document.getElementById('wards-empty').hidden =
|
|---|
| 72 | (state.wards || []).length + (state.pendingOffers || []).length > 0;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | function refresh() {
|
|---|
| 76 | fetch('/guardian/api/state?site=' + encodeURIComponent(state.site))
|
|---|
| 77 | .then(function (r) { return r.json(); })
|
|---|
| 78 | .then(function (s) { if (s && !s.error) { state = s; renderHelp(); renderWards(); } });
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | // ── Adopt ──────────────────────────────────────────────────────────────
|
|---|
| 82 | document.getElementById('adopt-form').addEventListener('submit', function (ev) {
|
|---|
| 83 | ev.preventDefault();
|
|---|
| 84 | var input = document.getElementById('adopt-handle');
|
|---|
| 85 | var msg = document.getElementById('adopt-msg');
|
|---|
| 86 | var handle = input.value.trim();
|
|---|
| 87 | if (!handle) return;
|
|---|
| 88 | msg.hidden = false; msg.classList.remove('err'); msg.textContent = '…';
|
|---|
| 89 | fetch('/guardian/adopt', {
|
|---|
| 90 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 91 | body: JSON.stringify({ handle: handle, site: state.site }),
|
|---|
| 92 | }).then(function (r) { return r.json().then(function (j) { return { ok: r.ok, j: j }; }); })
|
|---|
| 93 | .then(function (res) {
|
|---|
| 94 | if (res.ok) { msg.textContent = state.strings.sent; input.value = ''; refresh(); }
|
|---|
| 95 | else { msg.classList.add('err'); msg.textContent = state.strings.failed + ': ' + (res.j.error || '?'); }
|
|---|
| 96 | })
|
|---|
| 97 | .catch(function () { msg.classList.add('err'); msg.textContent = state.strings.network; });
|
|---|
| 98 | });
|
|---|
| 99 |
|
|---|
| 100 | // ── Site picker ────────────────────────────────────────────────────────
|
|---|
| 101 | var picker = document.getElementById('site-picker');
|
|---|
| 102 | if (picker) picker.addEventListener('change', function () {
|
|---|
| 103 | location.href = '/guardian?site=' + encodeURIComponent(picker.value);
|
|---|
| 104 | });
|
|---|
| 105 |
|
|---|
| 106 | // ── Push: the guardian channel (help + guardian alerts) ────────────────
|
|---|
| 107 | var toggle = document.getElementById('push-toggle');
|
|---|
| 108 | var pmsg = document.getElementById('push-msg');
|
|---|
| 109 | function pushState() {
|
|---|
| 110 | if (!('serviceWorker' in navigator) || !('PushManager' in window)) { toggle.disabled = true; return; }
|
|---|
| 111 | navigator.serviceWorker.register('/sw.js').catch(function () {});
|
|---|
| 112 | navigator.serviceWorker.ready
|
|---|
| 113 | .then(function (reg) { return reg.pushManager.getSubscription(); })
|
|---|
| 114 | .then(function (sub) {
|
|---|
| 115 | toggle.textContent = sub ? toggle.dataset.onLabel : toggle.dataset.offLabel;
|
|---|
| 116 | toggle.dataset.subscribed = sub ? '1' : '';
|
|---|
| 117 | });
|
|---|
| 118 | }
|
|---|
| 119 | function urlB64(base64) {
|
|---|
| 120 | var pad = '='.repeat((4 - (base64.length % 4)) % 4);
|
|---|
| 121 | var b = (base64 + pad).replace(/-/g, '+').replace(/_/g, '/');
|
|---|
| 122 | var raw = atob(b); var arr = new Uint8Array(raw.length);
|
|---|
| 123 | for (var i = 0; i < raw.length; i++) arr[i] = raw.charCodeAt(i);
|
|---|
| 124 | return arr;
|
|---|
| 125 | }
|
|---|
| 126 | toggle.addEventListener('click', function () {
|
|---|
| 127 | pmsg.hidden = true;
|
|---|
| 128 | navigator.serviceWorker.ready.then(function (reg) {
|
|---|
| 129 | if (toggle.dataset.subscribed) {
|
|---|
| 130 | reg.pushManager.getSubscription().then(function (sub) {
|
|---|
| 131 | if (!sub) return;
|
|---|
| 132 | fetch('/push/unsubscribe', {
|
|---|
| 133 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 134 | body: JSON.stringify({ endpoint: sub.endpoint }),
|
|---|
| 135 | }).then(function () { return sub.unsubscribe(); }).then(pushState);
|
|---|
| 136 | });
|
|---|
| 137 | return;
|
|---|
| 138 | }
|
|---|
| 139 | fetch('/push/vapid').then(function (r) { return r.json(); }).then(function (v) {
|
|---|
| 140 | if (!v.publicKey) throw new Error('no key');
|
|---|
| 141 | return reg.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlB64(v.publicKey) });
|
|---|
| 142 | }).then(function (sub) {
|
|---|
| 143 | return fetch('/push/subscribe', {
|
|---|
| 144 | method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|---|
| 145 | body: JSON.stringify({
|
|---|
| 146 | subscription: sub.toJSON(),
|
|---|
| 147 | // The guardian channel: calls for help + adoption traffic.
|
|---|
| 148 | alerts: { help: 1, guardian: 1, dm: 1, follow: 0, reply: 0, like: 0, boost: 0 },
|
|---|
| 149 | uaLabel: 'Guardian PWA',
|
|---|
| 150 | }),
|
|---|
| 151 | });
|
|---|
| 152 | }).then(pushState).catch(function (e) {
|
|---|
| 153 | pmsg.hidden = false; pmsg.classList.add('err');
|
|---|
| 154 | pmsg.textContent = 'Push niet beschikbaar: ' + e.message;
|
|---|
| 155 | });
|
|---|
| 156 | });
|
|---|
| 157 | });
|
|---|
| 158 |
|
|---|
| 159 | renderHelp(); renderWards(); pushState();
|
|---|
| 160 | // Live-ish: poll the state every 45s while the PWA is open.
|
|---|
| 161 | setInterval(refresh, 45000);
|
|---|
| 162 | })();
|
|---|