saturn
- 187 commits
- 2 branches
- 0 tags
- updated
- CI 2/2
README.md
6.7 KB · 111 lines
1# social23likes and comments. everything for them lives in this folder, and none of it is served as a file.45## files67| file | what it does |8| --- | --- |9| `lib.js` | shared by everything: which pages are allowed, hashing, the html that gets written into a page |10| `functions/_middleware.js` | runs before a page is served and fills its `data-social` section from d1 |11| `functions/api/like.js` | the like button posts here |12| `functions/api/moderate.js` | the approve/delete page linked from new-comment emails |13| `functions/projects/` | not social: `/projects/` is showcase at code.danieldaum.net, redirected or proxied |14| `posts.js` | not social: my bluesky posts on `/`, `/blog/` and `/posts/`, called from `_middleware.js`. see [posts](#posts) |15| `email.js` | email worker: turns mail sent to `reply@replies.danieldaum.net` into comments |16| `schema.sql` | the d1 tables: `Likes`, `LikeVoters`, `Comments` |17| `wrangler.toml` | pages config (d1 binding) |18| `email.toml` | email worker config (d1 + send-email bindings) |19| `/_routes.json` | at the site root, because pages only reads it there. the paths the functions run on |2021## how it works2223**a page.** a visitor opens `/now/sep-2026/`. the path is in `_routes.json`, so `_middleware.js` runs. it finds `<section data-social>`, reads the like count and approved comments from d1, and writes them in. if anything fails, the page shows its fallback text ("likes and comments unavailable").2425**a like.** the heart is a plain form (no js) that posts the page path to `/api/like`. the visitor is identified by a hash of ip + browser + today's date, so they get one like per page per day. the hash can't be reversed and is deleted after two days. a second like the same day does nothing.2627**a comment.** "comment by email" opens a mailto with the post's url as the subject. cloudflare email routing hands the mail to `email.js`, which:28291. drops it if it fails dkim/spf, or if 20 comments already came in during the last hour (site-wide)302. bounces it if the subject isn't a real post, the `name:` line is missing, or it's empty, too long, or has more than 2 links313. cuts off quoted replies and signatures, then saves the name, comment, page and time. the sender's address is never saved324. comments without links go live straight away. ones with a link wait for approval335. emails me the comment with a signed link to `/api/moderate`3435**moderating.** the link opens a page with approve/delete buttons. deleting removes the comment and its replies. replying to the notification email posts my answer under that comment, marked "author".3637**replies** nest one level deep. a reply to a reply goes under the same top comment.3839## adding likes and comments to a page4041put this at the end of the post, inside `<article>` if it has one. the template already has it:4243```html44<section class="post-social" id="comments" data-social aria-label="likes and comments">45 <p class="meta">likes and comments unavailable</p>46</section>47```4849this works on `/about/`, `/now/<slug>/` and `/blog/<slug>/`. `/projects/` is showcase (`functions/projects/`), so it has none. take the section out and the page has none.5051`/now/` is a copy of the newest edition, so it points at that edition: `data-social="/now/<slug>/"`. update it with each new now post. comments always go on the edition, never on `/now/`.5253### a new kind of page (e.g. `/books/<slug>/`)54551. add it to `isLikeable()` in `lib.js`: `(blog|now)` → `(blog|now|books)`562. add `"/books/*"` to `/_routes.json`. every path in there runs a function and isn't cached, so keep it narrow573. add the section to the pages584. `mise run social-email`: the worker uses `isLikeable()` too, so it rejects comments for the new pages until it's redeployed5960## posts6162my bluesky posts and reposts, newest first, replies left out:6364| page | `data-posts` | shows |65| --- | --- | --- |66| `/` | `3` | the newest 3 |67| `/blog/` | `highlight` | the newest one as a card |68| `/posts/` | `all` | every post, each with an id so `/posts/#<rkey>` links to it |6970`posts.js` reads which posts exist from my pds (`home.daum.dev`) and asks bluesky's appview for the rest: reposted and quoted text, and image thumbnails, which load from `cdn.bsky.app`. `_middleware.js` hands it the pages in `POST_PAGES`, and it writes each `[data-posts]`. whatever is inside the element in the html is the fallback.7172**it never slows the site down:**7374- it runs on those three pages only. every other page and file is served straight from pages, no function (`/_routes.json`)75- the post data comes from cloudflare's cache, shared by all three pages. once it's 5 minutes old, visitors still get the cached copy and a refresh runs after the response is sent76- only an empty cache (first visit after a deploy, or a new colo) waits, at most 800 ms. past that the page goes out with its fallback and the refresh finishes in the background77- if the pds is down, the cached posts are kept for a day before the fallback shows78- no js in the browser. images are lazy, sized thumbnails7980the micro posts version that came before had no `_routes.json`, so a function ran on every request on the site, and it waited on the pds with no limit.8182**changing it:** `FRESH`, `WAIT` and `CACHE_KEY` are at the top of `posts.js`; bump `CACHE_KEY` when the shape of the cached data changes. a new page needs its path in `POST_PAGES` and `/_routes.json`. pages with images need `https://cdn.bsky.app` (and `https://video.bsky.app` for video thumbnails) in their `img-src`.8384**locally:** `mise run serve` doesn't run functions, so it shows the fallback. `mise run social-dev` shows real posts on `localhost:8788`.8586## deploying8788- **functions:** automatic. on every push, ci (`.gitea/workflows/deploy.yaml`) runs wrangler from this folder89- **email worker:** `mise run social-email`, only when `email.js`, `lib.js` or `email.toml` change90- **secret:** `SOCIAL_SECRET` signs moderation links and like hashes. it's set on both the pages project and the worker:91 - `wrangler pages secret put SOCIAL_SECRET --project-name saturn`92 - `wrangler secret put SOCIAL_SECRET -c email.toml`9394## locally95961. `echo 'SOCIAL_SECRET=anything' > .dev.vars` in this folder (gitignored)972. `mise run social-dev`, then open localhost:87889899this uses a local, empty d1 in `.wrangler/`. comments can't be tested locally, but likes and the moderate page can.100101## looking at the data102103run these from this folder:104105```106wrangler d1 execute saturn-social --remote --command "SELECT page, count FROM Likes"107wrangler d1 execute saturn-social --remote --command "SELECT id, page, name, approved FROM Comments ORDER BY created_at DESC LIMIT 10"108wrangler tail saturn-replies-inbound # live worker logs, for when an email doesn't show up109```110111to take a comment down without the email: `DELETE FROM Comments WHERE id = '...' OR parent = '...'`.