|
| 1 | +/** |
| 2 | + * Crow's Nest Panel — Lemmy: instance status + subscribed communities + hot posts. |
| 3 | + * XSS-safe (textContent / createElement only). |
| 4 | + */ |
| 5 | + |
| 6 | +export default { |
| 7 | + id: "lemmy", |
| 8 | + name: "Lemmy", |
| 9 | + icon: "message-circle", |
| 10 | + route: "/dashboard/lemmy", |
| 11 | + navOrder: 76, |
| 12 | + category: "federated-social", |
| 13 | + |
| 14 | + async handler(req, res, { layout }) { |
| 15 | + const content = ` |
| 16 | + <style>${styles()}</style> |
| 17 | + <div class="lm-panel"> |
| 18 | + <h1>Lemmy <span class="lm-subtitle">federated link aggregator</span></h1> |
| 19 | +
|
| 20 | + <div class="lm-section"> |
| 21 | + <h3>Status</h3> |
| 22 | + <div id="lm-status" class="lm-status"><div class="np-loading">Loading…</div></div> |
| 23 | + </div> |
| 24 | +
|
| 25 | + <div class="lm-section"> |
| 26 | + <h3>Local Communities</h3> |
| 27 | + <div id="lm-communities" class="lm-communities"><div class="np-loading">Loading…</div></div> |
| 28 | + </div> |
| 29 | +
|
| 30 | + <div class="lm-section"> |
| 31 | + <h3>Hot Posts (local)</h3> |
| 32 | + <div id="lm-posts" class="lm-posts"><div class="np-loading">Loading…</div></div> |
| 33 | + </div> |
| 34 | +
|
| 35 | + <div class="lm-section lm-notes"> |
| 36 | + <h3>Notes</h3> |
| 37 | + <ul> |
| 38 | + <li>Lemmy federation is community-scoped. A single large federated community can pull heavy content; monitor disk.</li> |
| 39 | + <li>Moderation reports from all federated instances land in your admin queue. Review regularly.</li> |
| 40 | + <li>pict-rs cache grows with federated image content. Tune retention via <code>lemmy_media_prune</code>.</li> |
| 41 | + </ul> |
| 42 | + </div> |
| 43 | + </div> |
| 44 | + <script>${script()}</script> |
| 45 | + `; |
| 46 | + res.send(layout({ title: "Lemmy", content })); |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +function script() { |
| 51 | + return ` |
| 52 | + function clear(el) { while (el.firstChild) el.removeChild(el.firstChild); } |
| 53 | + function row(label, value) { |
| 54 | + const r = document.createElement('div'); r.className = 'lm-row'; |
| 55 | + const b = document.createElement('b'); b.textContent = label; |
| 56 | + const s = document.createElement('span'); s.textContent = value == null ? '—' : String(value); |
| 57 | + r.appendChild(b); r.appendChild(s); return r; |
| 58 | + } |
| 59 | + function err(msg) { const d = document.createElement('div'); d.className = 'np-error'; d.textContent = msg; return d; } |
| 60 | +
|
| 61 | + async function loadStatus() { |
| 62 | + const el = document.getElementById('lm-status'); clear(el); |
| 63 | + try { |
| 64 | + const res = await fetch('/api/lemmy/status'); const d = await res.json(); |
| 65 | + if (d.error) { el.appendChild(err(d.error)); return; } |
| 66 | + const card = document.createElement('div'); card.className = 'lm-card'; |
| 67 | + card.appendChild(row('Site name', d.site_name || '(unset)')); |
| 68 | + card.appendChild(row('Hostname', d.hostname || '—')); |
| 69 | + card.appendChild(row('Version', d.version || '—')); |
| 70 | + card.appendChild(row('Users', d.users ?? '—')); |
| 71 | + card.appendChild(row('Posts', d.posts ?? '—')); |
| 72 | + card.appendChild(row('Communities', d.communities ?? '—')); |
| 73 | + card.appendChild(row('Federation', d.federation_enabled ? 'enabled' : 'disabled')); |
| 74 | + card.appendChild(row('Registration', d.registration_mode || '—')); |
| 75 | + card.appendChild(row('Authenticated', d.my_user || '(no JWT)')); |
| 76 | + el.appendChild(card); |
| 77 | + } catch (e) { el.appendChild(err('Cannot reach Lemmy.')); } |
| 78 | + } |
| 79 | +
|
| 80 | + async function loadCommunities() { |
| 81 | + const el = document.getElementById('lm-communities'); clear(el); |
| 82 | + try { |
| 83 | + const res = await fetch('/api/lemmy/communities'); const d = await res.json(); |
| 84 | + if (d.error) { el.appendChild(err(d.error)); return; } |
| 85 | + if (!d.communities || d.communities.length === 0) { |
| 86 | + const i = document.createElement('div'); i.className = 'np-idle'; |
| 87 | + i.textContent = 'No local communities yet. Create one in the web UI.'; |
| 88 | + el.appendChild(i); return; |
| 89 | + } |
| 90 | + for (const c of d.communities) { |
| 91 | + const li = document.createElement('div'); li.className = 'lm-community'; |
| 92 | + const t = document.createElement('b'); t.textContent = c.title || c.name; |
| 93 | + li.appendChild(t); |
| 94 | + const meta = document.createElement('div'); meta.className = 'lm-community-meta'; |
| 95 | + meta.textContent = '!' + c.name + ' · ' + (c.subscribers || 0) + ' subs · ' + (c.posts || 0) + ' posts'; |
| 96 | + li.appendChild(meta); |
| 97 | + el.appendChild(li); |
| 98 | + } |
| 99 | + } catch (e) { el.appendChild(err('Cannot load communities: ' + e.message)); } |
| 100 | + } |
| 101 | +
|
| 102 | + async function loadPosts() { |
| 103 | + const el = document.getElementById('lm-posts'); clear(el); |
| 104 | + try { |
| 105 | + const res = await fetch('/api/lemmy/posts'); const d = await res.json(); |
| 106 | + if (d.error) { el.appendChild(err(d.error)); return; } |
| 107 | + if (!d.posts || d.posts.length === 0) { |
| 108 | + const i = document.createElement('div'); i.className = 'np-idle'; |
| 109 | + i.textContent = 'No posts yet.'; |
| 110 | + el.appendChild(i); return; |
| 111 | + } |
| 112 | + for (const p of d.posts) { |
| 113 | + const c = document.createElement('div'); c.className = 'lm-post'; |
| 114 | + const t = document.createElement('b'); t.textContent = p.name; |
| 115 | + c.appendChild(t); |
| 116 | + const meta = document.createElement('div'); meta.className = 'lm-post-meta'; |
| 117 | + meta.textContent = '!' + (p.community || '?') + ' · ' + (p.score || 0) + ' pts · ' + (p.comments || 0) + ' comments · ' + (p.creator || '?'); |
| 118 | + c.appendChild(meta); |
| 119 | + el.appendChild(c); |
| 120 | + } |
| 121 | + } catch (e) { el.appendChild(err('Cannot load posts: ' + e.message)); } |
| 122 | + } |
| 123 | +
|
| 124 | + loadStatus(); |
| 125 | + loadCommunities(); |
| 126 | + loadPosts(); |
| 127 | + `; |
| 128 | +} |
| 129 | + |
| 130 | +function styles() { |
| 131 | + return ` |
| 132 | + .lm-panel h1 { margin: 0 0 1rem; font-size: 1.5rem; } |
| 133 | + .lm-subtitle { font-size: 0.85rem; color: var(--crow-text-muted); font-weight: 400; margin-left: .5rem; } |
| 134 | + .lm-section { margin-bottom: 1.8rem; } |
| 135 | + .lm-section h3 { font-size: 0.8rem; color: var(--crow-text-muted); text-transform: uppercase; |
| 136 | + letter-spacing: 0.05em; margin: 0 0 0.7rem; } |
| 137 | + .lm-card { background: var(--crow-bg-elevated); border: 1px solid var(--crow-border); |
| 138 | + border-radius: 10px; padding: 1rem; } |
| 139 | + .lm-row { display: flex; justify-content: space-between; padding: .25rem 0; font-size: .9rem; color: var(--crow-text-primary); } |
| 140 | + .lm-row b { color: var(--crow-text-muted); font-weight: 500; min-width: 160px; } |
| 141 | + .lm-community, .lm-post { background: var(--crow-bg-elevated); border: 1px solid var(--crow-border); |
| 142 | + border-radius: 8px; padding: .6rem .9rem; margin-bottom: .4rem; } |
| 143 | + .lm-community b, .lm-post b { color: var(--crow-text-primary); font-size: .9rem; } |
| 144 | + .lm-community-meta, .lm-post-meta { font-size: .75rem; color: var(--crow-text-muted); margin-top: .2rem; font-family: ui-monospace, monospace; } |
| 145 | + .lm-notes ul { margin: 0; padding-left: 1.2rem; color: var(--crow-text-secondary); font-size: .88rem; } |
| 146 | + .lm-notes li { margin-bottom: .3rem; } |
| 147 | + .lm-notes code { font-family: ui-monospace, monospace; background: var(--crow-bg); |
| 148 | + padding: 1px 4px; border-radius: 3px; font-size: .8em; } |
| 149 | + .np-idle, .np-loading { color: var(--crow-text-muted); font-size: 0.9rem; padding: 1rem; |
| 150 | + background: var(--crow-bg-elevated); border-radius: 10px; text-align: center; } |
| 151 | + .np-error { color: #ef4444; font-size: 0.9rem; padding: 1rem; |
| 152 | + background: var(--crow-bg-elevated); border-radius: 10px; text-align: center; } |
| 153 | + `; |
| 154 | +} |
0 commit comments