neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Live updates: Latest and category lists follow new posts; Popular refreshes itself
84bfe6e · gmorell · 2026-12-03 05:56
Message
{commit_body(@commit)}
Files changed
modified
Cargo.lock
+74
−0
| 345 | 345 | checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" |
| 346 | 346 | dependencies = [ |
| 347 | 347 | "axum-core", |
| 348 | + "base64 0.22.1", | |
| 348 | 349 | "bytes", |
| 349 | 350 | "form_urlencoded", |
| 350 | 351 | "futures-util", |
| 364 | 365 | "serde_json", |
| 365 | 366 | "serde_path_to_error", |
| 366 | 367 | "serde_urlencoded", |
| 368 | + "sha1", | |
| 367 | 369 | "sync_wrapper", |
| 368 | 370 | "tokio", |
| 371 | + "tokio-tungstenite 0.29.0", | |
| 369 | 372 | "tower", |
| 370 | 373 | "tower-layer", |
| 371 | 374 | "tower-service", |
| 989 | 992 | "syn 3.0.10", |
| 990 | 993 | ] |
| 991 | 994 | |
| 995 | +[[package]] | |
| 996 | +name = "data-encoding" | |
| 997 | +version = "2.11.1" | |
| 998 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 999 | +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" | |
| 1000 | + | |
| 992 | 1001 | [[package]] |
| 993 | 1002 | name = "der" |
| 994 | 1003 | version = "0.7.10" |
| 3265 | 3274 | "chrono", |
| 3266 | 3275 | "clap", |
| 3267 | 3276 | "flate2", |
| 3277 | + "futures-util", | |
| 3268 | 3278 | "hmac 0.12.1", |
| 3269 | 3279 | "idna", |
| 3270 | 3280 | "log", |
| 3281 | 3291 | "serde_json", |
| 3282 | 3292 | "sha2 0.10.9", |
| 3283 | 3293 | "tokio", |
| 3294 | + "tokio-tungstenite 0.27.0", | |
| 3284 | 3295 | "tower", |
| 3285 | 3296 | "tower-http", |
| 3286 | 3297 | "tracing", |
| 4525 | 4536 | "tokio", |
| 4526 | 4537 | ] |
| 4527 | 4538 | |
| 4539 | +[[package]] | |
| 4540 | +name = "tokio-tungstenite" | |
| 4541 | +version = "0.27.0" | |
| 4542 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4543 | +checksum = "489a59b6730eda1b0171fcfda8b121f4bee2b35cba8645ca35c5f7ba3eb736c1" | |
| 4544 | +dependencies = [ | |
| 4545 | + "futures-util", | |
| 4546 | + "log", | |
| 4547 | + "tokio", | |
| 4548 | + "tungstenite 0.27.0", | |
| 4549 | +] | |
| 4550 | + | |
| 4551 | +[[package]] | |
| 4552 | +name = "tokio-tungstenite" | |
| 4553 | +version = "0.29.0" | |
| 4554 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4555 | +checksum = "8f72a05e828585856dacd553fba484c242c46e391fb0e58917c942ee9202915c" | |
| 4556 | +dependencies = [ | |
| 4557 | + "futures-util", | |
| 4558 | + "log", | |
| 4559 | + "tokio", | |
| 4560 | + "tungstenite 0.29.0", | |
| 4561 | +] | |
| 4562 | + | |
| 4528 | 4563 | [[package]] |
| 4529 | 4564 | name = "tokio-util" |
| 4530 | 4565 | version = "0.7.20" |
| 4692 | 4727 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 4693 | 4728 | checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" |
| 4694 | 4729 | |
| 4730 | +[[package]] | |
| 4731 | +name = "tungstenite" | |
| 4732 | +version = "0.27.0" | |
| 4733 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4734 | +checksum = "eadc29d668c91fcc564941132e17b28a7ceb2f3ebf0b9dae3e03fd7a6748eb0d" | |
| 4735 | +dependencies = [ | |
| 4736 | + "bytes", | |
| 4737 | + "data-encoding", | |
| 4738 | + "http", | |
| 4739 | + "httparse", | |
| 4740 | + "log", | |
| 4741 | + "rand 0.9.5", | |
| 4742 | + "sha1", | |
| 4743 | + "thiserror 2.0.21", | |
| 4744 | + "utf-8", | |
| 4745 | +] | |
| 4746 | + | |
| 4747 | +[[package]] | |
| 4748 | +name = "tungstenite" | |
| 4749 | +version = "0.29.0" | |
| 4750 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4751 | +checksum = "6c01152af293afb9c7c2a57e4b559c5620b421f6d133261c60dd2d0cdb38e6b8" | |
| 4752 | +dependencies = [ | |
| 4753 | + "bytes", | |
| 4754 | + "data-encoding", | |
| 4755 | + "http", | |
| 4756 | + "httparse", | |
| 4757 | + "log", | |
| 4758 | + "rand 0.9.5", | |
| 4759 | + "sha1", | |
| 4760 | + "thiserror 2.0.21", | |
| 4761 | +] | |
| 4762 | + | |
| 4695 | 4763 | [[package]] |
| 4696 | 4764 | name = "typenum" |
| 4697 | 4765 | version = "1.20.1" |
| 4772 | 4840 | "serde_derive", |
| 4773 | 4841 | ] |
| 4774 | 4842 | |
| 4843 | +[[package]] | |
| 4844 | +name = "utf-8" | |
| 4845 | +version = "0.7.6" | |
| 4846 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4847 | +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" | |
| 4848 | + | |
| 4775 | 4849 | [[package]] |
| 4776 | 4850 | name = "utf8_iter" |
| 4777 | 4851 | version = "1.1.0" |
modified
Cargo.toml
+4
−1
| 23 | 23 | |
| 24 | 24 | [dependencies] |
| 25 | 25 | # Web stack |
| 26 | −axum = { version = "0.8.5", features = ["multipart"] } | |
| 26 | +axum = { version = "0.8.5", features = ["multipart", "ws"] } | |
| 27 | 27 | tokio = { version = "1.47.1", features = ["full"] } |
| 28 | 28 | tower-http = { version = "0.6.6", features = ["fs", "trace", "sensitive-headers"] } |
| 29 | 29 | tracing = "0.1.41" |
| 77 | 77 | [dev-dependencies] |
| 78 | 78 | # Drives the real router in tests/api_db.rs (`ServiceExt::oneshot`). |
| 79 | 79 | tower = { version = "0.5", features = ["util"] } |
| 80 | +# The live-update socket end to end in tests/moderation_db.rs. | |
| 81 | +tokio-tungstenite = "0.27" | |
| 82 | +futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] } |
modified
README.md
+36
−0
| 383 | 383 | there). esbuild inlines them (`--loader:.frag=text`, in the Justfile and |
| 384 | 384 | Dockerfile). |
| 385 | 385 | |
| 386 | +### Live updates | |
| 387 | + | |
| 388 | +Signed-in pages keep one WebSocket open (`/ws`, the htmx `ws` extension, | |
| 389 | +bundled from npm like htmx itself; `src/live.rs`, `assets/f/js/live.js`). | |
| 390 | +When a topic gets visible new activity (a new topic or reply that wasn't | |
| 391 | +held, a hold or flag-hide dismissed, a held or hidden post moderated, a topic | |
| 392 | +or post restored) the server tells each connection whose user may see that | |
| 393 | +topic, checked when it's sent with the Latest list's own conditions (readable | |
| 394 | +categories, not deleted, held topics only for author and staff). What it | |
| 395 | +sends is a signal naming the categories touched, never content, at most once | |
| 396 | +every 2 s per connection. The page's live lists then refetch the page through | |
| 397 | +the ordinary route, as that user, and swap in the fresh copy: Latest on the | |
| 398 | +landing page and `/p/latest`, and the category page's list (both layouts). | |
| 399 | +New or moved-up rows flash. Under the open reader, while you're typing, or | |
| 400 | +when you've scrolled past the list's top, the update waits behind an | |
| 401 | +"N new — show" button instead. | |
| 402 | + | |
| 403 | +Popular (on the landing page and `/p/popular`) refreshes itself every 5 | |
| 404 | +minutes for everyone, anonymous visitors included, only while the tab is | |
| 405 | +visible and the reader is closed. | |
| 406 | + | |
| 407 | +The socket needs the session cookie (anonymous: 401, no socket on the page) | |
| 408 | +and an `Origin` equal to `PUBLIC_URL`'s (else 403). There are at most 5 per | |
| 409 | +user and 2000 in all (429). A ping every 25 s keeps proxies from closing it | |
| 410 | +as idle. The session is re-checked every minute, and a signed-out or expired | |
| 411 | +one is closed with 1008, which the extension doesn't retry; drops and | |
| 412 | +restarts reconnect with jittered backoff. Connects and disconnects are logged | |
| 413 | +at debug. | |
| 414 | + | |
| 415 | +Events are in-process (a tokio broadcast), so this assumes **one replica**, | |
| 416 | +as deployed. More than one would need the events fanned out between them, | |
| 417 | +e.g. Postgres `LISTEN/NOTIFY` on a channel carrying the topic id. Posts made | |
| 418 | +by `manage` (the seed) don't announce themselves. The Android app doesn't | |
| 419 | +use the socket; it would need an OkHttp WebSocket with the cookie or a | |
| 420 | +token-authenticated variant of `/ws`, and a JSON signal. | |
| 421 | + | |
| 386 | 422 | ## Link previews |
| 387 | 423 | |
| 388 | 424 | Paste a URL into Slack, Discord, iMessage or <https://www.linkpreview.app> and |
modified
assets/f/css/style.css
+10
−0
| 129 | 129 | .shader-unsupported canvas { |
| 130 | 130 | visibility: hidden; |
| 131 | 131 | } |
| 132 | + | |
| 133 | +/* Live updates (f/js/live.js): a row or tile that just arrived or moved up. */ | |
| 134 | +@keyframes live-new { | |
| 135 | + from { box-shadow: inset 0 0 0 9999px oklch(var(--p) / 0.18); } | |
| 136 | + to { box-shadow: inset 0 0 0 9999px oklch(var(--p) / 0); } | |
| 137 | +} | |
| 138 | +.live-new { animation: live-new 3.5s ease-out; } | |
| 139 | +@media (prefers-reduced-motion: reduce) { | |
| 140 | + .live-new { animation: none; outline: 2px solid oklch(var(--p) / 0.6); outline-offset: -2px; } | |
| 141 | +} |
added
assets/f/js/live.js
+142
−0
| 1 | +// Live updates (src/live.rs). Signed-in pages carry | |
| 2 | +// <div id="live" hx-ext="ws" ws-connect="/ws"> outside #content; the htmx | |
| 3 | +// ws extension keeps it connected (reconnecting with jittered backoff after | |
| 4 | +// a drop) and swaps the server's #live-signal in: which categories have new | |
| 5 | +// activity the user can see. The lists on the page marked | |
| 6 | +// data-live="latest" (landing, /p/latest) | |
| 7 | +// data-live="category" data-live-category="<id>" (a category page) | |
| 8 | +// then refetch this page, as this user, through the ordinary route, and | |
| 9 | +// swap themselves for the fresh copy; rows that are new or moved up flash. | |
| 10 | +// | |
| 11 | +// Never under the reader, mid-typing, or while the list's top is scrolled | |
| 12 | +// past: then the fresh copy waits behind a "N new — show" pill. | |
| 13 | +// | |
| 14 | +// Popular sections poll on their own (hx-trigger="every 300s | |
| 15 | +// [window.livePollOk()]" in the templates); livePollOk is the condition. | |
| 16 | + | |
| 17 | +import htmx from "htmx.org"; | |
| 18 | +import "htmx-ext-ws"; | |
| 19 | + | |
| 20 | +const readerOpen = () => !!document.querySelector("#reader [data-reader]"); | |
| 21 | + | |
| 22 | +// Popular's periodic refresh: only for a visible tab with the reader shut. | |
| 23 | +window.livePollOk = () => document.visibilityState === "visible" && !readerOpen(); | |
| 24 | + | |
| 25 | +const typing = () => { | |
| 26 | + const a = document.activeElement; | |
| 27 | + return !!a && a.matches("input, textarea, select, [contenteditable], [contenteditable] *"); | |
| 28 | +}; | |
| 29 | + | |
| 30 | +const busy = el => readerOpen() || typing() || el.getBoundingClientRect().top < 0; | |
| 31 | + | |
| 32 | +// Lists waiting to be shown: element id -> { fresh copy, how many new }, for | |
| 33 | +// the page at `waitingFor`. | |
| 34 | +const waiting = new Map(); | |
| 35 | +let waitingFor = location.href; | |
| 36 | + | |
| 37 | +function ids(el) { | |
| 38 | + return [...el.querySelectorAll("[data-topic]")].map(e => e.dataset.topic); | |
| 39 | +} | |
| 40 | + | |
| 41 | +function swap(el, fresh) { | |
| 42 | + const before = ids(el); | |
| 43 | + const rank = new Map(before.map((id, i) => [id, i])); | |
| 44 | + el.replaceWith(fresh); | |
| 45 | + htmx.process(fresh); | |
| 46 | + fresh.querySelectorAll("[data-topic]").forEach((row, i) => { | |
| 47 | + const was = rank.get(row.dataset.topic); | |
| 48 | + if (was === undefined || was > i) { | |
| 49 | + row.classList.add("live-new"); | |
| 50 | + setTimeout(() => row.classList.remove("live-new"), 4000); | |
| 51 | + } | |
| 52 | + }); | |
| 53 | +} | |
| 54 | + | |
| 55 | +function pill() { | |
| 56 | + let p = document.getElementById("live-pill"); | |
| 57 | + const n = [...waiting.values()].reduce((sum, w) => sum + w.added, 0); | |
| 58 | + if (!waiting.size) { | |
| 59 | + p?.remove(); | |
| 60 | + return; | |
| 61 | + } | |
| 62 | + if (!p) { | |
| 63 | + p = document.createElement("button"); | |
| 64 | + p.id = "live-pill"; | |
| 65 | + p.type = "button"; | |
| 66 | + p.className = "btn btn-primary btn-sm shadow-lg fixed bottom-4 left-1/2 -translate-x-1/2 z-30"; | |
| 67 | + p.addEventListener("click", showWaiting); | |
| 68 | + document.body.appendChild(p); | |
| 69 | + } | |
| 70 | + p.textContent = n > 0 ? `${n} new — show` : "Updated — show"; | |
| 71 | +} | |
| 72 | + | |
| 73 | +function showWaiting() { | |
| 74 | + let first = null; | |
| 75 | + for (const [id, w] of waiting) { | |
| 76 | + const el = document.getElementById(id); | |
| 77 | + if (el) { | |
| 78 | + swap(el, w.fresh); | |
| 79 | + first ??= w.fresh; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + waiting.clear(); | |
| 83 | + pill(); | |
| 84 | + first?.scrollIntoView({ block: "start", behavior: "smooth" }); | |
| 85 | +} | |
| 86 | + | |
| 87 | +let inflight = false; | |
| 88 | +let again = false; | |
| 89 | + | |
| 90 | +async function refresh(targets) { | |
| 91 | + if (inflight) { | |
| 92 | + again = true; | |
| 93 | + return; | |
| 94 | + } | |
| 95 | + inflight = true; | |
| 96 | + try { | |
| 97 | + const res = await fetch(location.href, { credentials: "same-origin", headers: { Accept: "text/html" } }); | |
| 98 | + if (!res.ok) return; | |
| 99 | + const doc = new DOMParser().parseFromString(await res.text(), "text/html"); | |
| 100 | + for (const el of targets) { | |
| 101 | + const fresh = doc.getElementById(el.id); | |
| 102 | + if (!fresh || !el.isConnected) continue; | |
| 103 | + if (busy(el)) { | |
| 104 | + waitingFor = location.href; | |
| 105 | + const have = new Set(ids(el)); | |
| 106 | + const added = ids(fresh).filter(id => !have.has(id)).length; | |
| 107 | + waiting.set(el.id, { fresh, added }); | |
| 108 | + } else { | |
| 109 | + waiting.delete(el.id); | |
| 110 | + swap(el, fresh); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + pill(); | |
| 114 | + } catch (e) { | |
| 115 | + console.debug("live refresh:", e); | |
| 116 | + } finally { | |
| 117 | + inflight = false; | |
| 118 | + if (again) { | |
| 119 | + again = false; | |
| 120 | + onSignal(); | |
| 121 | + } | |
| 122 | + } | |
| 123 | +} | |
| 124 | + | |
| 125 | +function onSignal() { | |
| 126 | + const signal = document.getElementById("live-signal"); | |
| 127 | + const cats = new Set((signal?.dataset.categories || "").split(" ").filter(Boolean)); | |
| 128 | + if (!cats.size) return; | |
| 129 | + const targets = [...document.querySelectorAll("#content [data-live]")].filter( | |
| 130 | + el => el.id && (el.dataset.live === "latest" || cats.has(el.dataset.liveCategory)), | |
| 131 | + ); | |
| 132 | + if (targets.length) refresh(targets); | |
| 133 | +} | |
| 134 | + | |
| 135 | +document.addEventListener("htmx:wsAfterMessage", onSignal); | |
| 136 | +// A new page: what was waiting belonged to the old one. | |
| 137 | +document.addEventListener("htmx:afterSettle", () => { | |
| 138 | + if (waiting.size && location.href !== waitingFor) { | |
| 139 | + waiting.clear(); | |
| 140 | + pill(); | |
| 141 | + } | |
| 142 | +}); |
modified
assets/f/js/src.js
+4
−0
Click to load diff…
modified
assets/package-lock.json
+9
−0
Click to load diff…
modified
assets/package.json
+1
−0
Click to load diff…
modified
src/actions.rs
+10
−2
Click to load diff…
modified
src/lib.rs
+3
−0
Click to load diff…
added
src/live.rs
+308
−0
Click to load diff…
modified
src/main.rs
+1
−0
Click to load diff…
modified
src/moderation.rs
+10
−0
Click to load diff…
modified
src/web.rs
+2
−0
Click to load diff…
modified
templates/forum/_topic_list.html
+1
−1
Click to load diff…
modified
templates/forum/category.html
+3
−0
Click to load diff…
modified
templates/forum/feed.html
+5
−0
Click to load diff…
modified
templates/forum/focus.html
+1
−0
Click to load diff…
modified
templates/forum/focus_list.html
+4
−0
Click to load diff…
modified
templates/forum/landing.html
+6
−2
Click to load diff…
modified
templates/forum/landing_focus.html
+5
−1
Click to load diff…
modified
templates/root.html
+7
−0
Click to load diff…
modified
tests/api_db.rs
+1
−0
Click to load diff…
modified
tests/moderation_db.rs
+187
−4
Click to load diff…
Parents: 6a20871