|
| 1 | +(() => { |
| 2 | + const config = window.__ADMIN_DASHBOARD__ || { pathPrefix: '' } |
| 3 | + const adminBase = `${config.pathPrefix || ''}/admin` |
| 4 | + |
| 5 | + const syncLine = document.getElementById('moderation-sync') |
| 6 | + if (!syncLine) { |
| 7 | + return |
| 8 | + } |
| 9 | + |
| 10 | + const elements = { |
| 11 | + error: document.getElementById('moderation-error'), |
| 12 | + success: document.getElementById('moderation-success'), |
| 13 | + syncLine, |
| 14 | + banPubkeyInput: document.getElementById('moderation-ban-pubkey'), |
| 15 | + ipInput: document.getElementById('moderation-ip'), |
| 16 | + banButton: document.getElementById('moderation-ban-button'), |
| 17 | + unbanButton: document.getElementById('moderation-unban-button'), |
| 18 | + whitelistIpButton: document.getElementById('moderation-whitelist-ip-button'), |
| 19 | + unwhitelistIpButton: document.getElementById('moderation-unwhitelist-ip-button'), |
| 20 | + refreshButton: document.getElementById('moderation-refresh-button'), |
| 21 | + topPubkeysBody: document.getElementById('moderation-top-pubkeys-body'), |
| 22 | + topIpsBody: document.getElementById('moderation-top-ips-body'), |
| 23 | + ratePubkeysBody: document.getElementById('moderation-rate-pubkeys-body'), |
| 24 | + pubkeyBlacklist: document.getElementById('moderation-pubkey-blacklist'), |
| 25 | + pubkeyWhitelist: document.getElementById('moderation-pubkey-whitelist'), |
| 26 | + kindBlacklist: document.getElementById('moderation-kind-blacklist'), |
| 27 | + kindWhitelist: document.getElementById('moderation-kind-whitelist'), |
| 28 | + bypassPubkeys: document.getElementById('moderation-bypass-pubkeys'), |
| 29 | + bypassIps: document.getElementById('moderation-bypass-ips'), |
| 30 | + } |
| 31 | + |
| 32 | + const escapeHtml = (value) => |
| 33 | + String(value) |
| 34 | + .replaceAll('&', '&') |
| 35 | + .replaceAll('<', '<') |
| 36 | + .replaceAll('>', '>') |
| 37 | + .replaceAll('"', '"') |
| 38 | + .replaceAll("'", ''') |
| 39 | + |
| 40 | + const adminFetch = async (path, options = {}) => { |
| 41 | + const response = await fetch(`${adminBase}${path}`, { |
| 42 | + credentials: 'same-origin', |
| 43 | + headers: { 'content-type': 'application/json', ...(options.headers || {}) }, |
| 44 | + ...options, |
| 45 | + }) |
| 46 | + if (response.status === 401) { |
| 47 | + throw new Error('Unauthorized') |
| 48 | + } |
| 49 | + return response |
| 50 | + } |
| 51 | + |
| 52 | + const showError = (message) => { |
| 53 | + elements.error.textContent = message |
| 54 | + elements.error.classList.remove('d-none') |
| 55 | + elements.success.classList.add('d-none') |
| 56 | + } |
| 57 | + |
| 58 | + const showSuccess = (message) => { |
| 59 | + elements.success.textContent = message |
| 60 | + elements.success.classList.remove('d-none') |
| 61 | + elements.error.classList.add('d-none') |
| 62 | + } |
| 63 | + |
| 64 | + const hideMessages = () => { |
| 65 | + elements.error.classList.add('d-none') |
| 66 | + elements.success.classList.add('d-none') |
| 67 | + } |
| 68 | + |
| 69 | + const handleUnauthorized = () => { |
| 70 | + globalThis.dispatchEvent(new CustomEvent('admin-unauthorized')) |
| 71 | + } |
| 72 | + |
| 73 | + const renderRows = (body, rows, keyField, valueField, emptyLabel) => { |
| 74 | + if (!body) { |
| 75 | + return |
| 76 | + } |
| 77 | + if (!Array.isArray(rows) || !rows.length) { |
| 78 | + body.innerHTML = `<tr><td colspan="2" class="admin-muted small">${escapeHtml(emptyLabel)}</td></tr>` |
| 79 | + return |
| 80 | + } |
| 81 | + body.innerHTML = rows |
| 82 | + .map( |
| 83 | + (row) => |
| 84 | + `<tr><td><code class="small">${escapeHtml(row[keyField])}</code></td><td>${escapeHtml(row[valueField])}</td></tr>`, |
| 85 | + ) |
| 86 | + .join('') |
| 87 | + } |
| 88 | + |
| 89 | + const renderRateRows = (body, rows) => { |
| 90 | + if (!body) { |
| 91 | + return |
| 92 | + } |
| 93 | + if (!Array.isArray(rows) || !rows.length) { |
| 94 | + body.innerHTML = '<tr><td colspan="2" class="admin-muted small">No rate-limit hits recorded yet.</td></tr>' |
| 95 | + return |
| 96 | + } |
| 97 | + body.innerHTML = rows |
| 98 | + .map((row) => `<tr><td><code class="small">${escapeHtml(row.key)}</code></td><td>${escapeHtml(row.hits)}</td></tr>`) |
| 99 | + .join('') |
| 100 | + } |
| 101 | + |
| 102 | + const renderLimits = (limits) => { |
| 103 | + const pretty = (value) => JSON.stringify(value ?? [], null, 2) |
| 104 | + elements.pubkeyBlacklist.textContent = pretty(limits.pubkey?.blacklist) |
| 105 | + elements.pubkeyWhitelist.textContent = pretty(limits.pubkey?.whitelist) |
| 106 | + elements.kindBlacklist.textContent = pretty(limits.kind?.blacklist) |
| 107 | + elements.kindWhitelist.textContent = pretty(limits.kind?.whitelist) |
| 108 | + elements.bypassPubkeys.textContent = pretty(limits.rateLimitBypass?.pubkeys) |
| 109 | + elements.bypassIps.textContent = pretty(limits.rateLimitBypass?.ipAddresses) |
| 110 | + } |
| 111 | + |
| 112 | + const load = async () => { |
| 113 | + hideMessages() |
| 114 | + try { |
| 115 | + const [limitsResponse, statsResponse] = await Promise.all([ |
| 116 | + adminFetch('/moderation/limits'), |
| 117 | + adminFetch('/moderation/stats?windowHours=168&limit=15'), |
| 118 | + ]) |
| 119 | + |
| 120 | + if (!limitsResponse.ok || !statsResponse.ok) { |
| 121 | + throw new Error('Failed to load moderation data') |
| 122 | + } |
| 123 | + |
| 124 | + const limitsPayload = await limitsResponse.json() |
| 125 | + const statsPayload = await statsResponse.json() |
| 126 | + renderLimits(limitsPayload.limits) |
| 127 | + renderRows(elements.topPubkeysBody, statsPayload.topPublishingPubkeys, 'pubkey', 'eventCount', 'No events in window.') |
| 128 | + renderRows(elements.topIpsBody, statsPayload.topClientIps, 'ip', 'eventCount', 'No client IPs in window.') |
| 129 | + renderRateRows(elements.ratePubkeysBody, statsPayload.rateLimitHits?.pubkeys) |
| 130 | + elements.syncLine.innerHTML = `<span class="prompt-char">></span> moderation: updated ${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}Z$/, ' UTC')}` |
| 131 | + } catch (error) { |
| 132 | + if (error instanceof Error && error.message === 'Unauthorized') { |
| 133 | + handleUnauthorized() |
| 134 | + return |
| 135 | + } |
| 136 | + showError('Unable to load moderation data.') |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + const postAction = async (body) => { |
| 141 | + hideMessages() |
| 142 | + const response = await adminFetch('/moderation/actions', { method: 'POST', body: JSON.stringify(body) }) |
| 143 | + const payload = await response.json().catch(() => ({})) |
| 144 | + if (!response.ok) { |
| 145 | + throw new Error(payload.error || 'Moderation action failed') |
| 146 | + } |
| 147 | + renderLimits(payload.limits) |
| 148 | + showSuccess('Limits updated.') |
| 149 | + elements.syncLine.innerHTML = `<span class="prompt-char">></span> moderation: action ${body.action} applied` |
| 150 | + void load() |
| 151 | + } |
| 152 | + |
| 153 | + const bindAction = (button, handler) => { |
| 154 | + button?.addEventListener('click', () => { |
| 155 | + void handler().catch((error) => { |
| 156 | + if (error instanceof Error && error.message === 'Unauthorized') { |
| 157 | + handleUnauthorized() |
| 158 | + return |
| 159 | + } |
| 160 | + showError(error instanceof Error ? error.message : 'Action failed.') |
| 161 | + }) |
| 162 | + }) |
| 163 | + } |
| 164 | + |
| 165 | + bindAction(elements.banButton, async () => { |
| 166 | + const pubkey = elements.banPubkeyInput?.value?.trim() |
| 167 | + if (!pubkey) { |
| 168 | + throw new Error('Enter a pubkey or npub to ban.') |
| 169 | + } |
| 170 | + await postAction({ action: 'ban-pubkey', pubkey }) |
| 171 | + }) |
| 172 | + |
| 173 | + bindAction(elements.unbanButton, async () => { |
| 174 | + const pubkey = elements.banPubkeyInput?.value?.trim() |
| 175 | + if (!pubkey) { |
| 176 | + throw new Error('Enter a pubkey or npub to unban.') |
| 177 | + } |
| 178 | + await postAction({ action: 'unban-pubkey', pubkey }) |
| 179 | + }) |
| 180 | + |
| 181 | + bindAction(elements.whitelistIpButton, async () => { |
| 182 | + const ip = elements.ipInput?.value?.trim() |
| 183 | + if (!ip) { |
| 184 | + throw new Error('Enter an IP address.') |
| 185 | + } |
| 186 | + await postAction({ action: 'whitelist-ip', ip }) |
| 187 | + }) |
| 188 | + |
| 189 | + bindAction(elements.unwhitelistIpButton, async () => { |
| 190 | + const ip = elements.ipInput?.value?.trim() |
| 191 | + if (!ip) { |
| 192 | + throw new Error('Enter an IP address.') |
| 193 | + } |
| 194 | + await postAction({ action: 'unwhitelist-ip', ip }) |
| 195 | + }) |
| 196 | + |
| 197 | + elements.refreshButton?.addEventListener('click', () => { |
| 198 | + void load() |
| 199 | + }) |
| 200 | + |
| 201 | + window.__ADMIN_MODERATION__ = { load } |
| 202 | +})() |
0 commit comments