diff --git a/README.md b/README.md index d2f9dd2..fa2d661 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ Prometheus ◄── API :9100/metrics, postgres-exporter ──► Grafana ## Docs -- **[Deploy](docs/deploy.md)** — running a real instance: images, configuration, TLS, - monitoring, backups, releases. +- **[Admin guide](docs/admin-guide.md)** — running a real instance: images, + configuration, TLS, monitoring, backups, upgrades, troubleshooting. +- **[User guide](docs/user-guide.md)** — accounts and apps, connecting a player, + importing history, sharing on the fediverse. - **[Set up](docs/setup.md)** — working on the code: local dev environment, migrations, tests, CI/CD. -- **[Use](docs/usage.md)** — accounts and apps, connecting a player, importing history, - sharing on the fediverse. - **API reference**: `/api/openapi.json`, Swagger UI at `/api/docs`, ReDoc at `/api/redoc` (a copy is committed at `docs/openapi.json`). - **Federation internals**: `src/scrobbler/federation/README.md`. Design docs for how it @@ -34,7 +34,7 @@ docker compose up -d # db, api (:5050), worker, ui (:8080) ``` Open http://localhost:8080, create an account, then go to **Apps** and create an app for -a player. See [docs/deploy.md](docs/deploy.md) for the published images, full +a player. See [docs/admin-guide.md](docs/admin-guide.md) for the published images, full configuration reference and production notes. ## Licence diff --git a/docs/deploy.md b/docs/admin-guide.md similarity index 55% rename from docs/deploy.md rename to docs/admin-guide.md index 9f9cd84..c081244 100644 --- a/docs/deploy.md +++ b/docs/admin-guide.md @@ -1,11 +1,11 @@ -# Deploying Scrobbler +# Admin guide -This covers running a real instance: the published images, configuration, putting it -behind TLS, monitoring, backups and releases. For running the code locally to work on -it, see [setup.md](setup.md). For what the service does once it's running, see -[usage.md](usage.md). +Running a real Scrobbler instance: architecture, installation, configuration, TLS, +fediverse sharing, monitoring, backups, upgrades and troubleshooting. If you're using an +instance someone else runs, see the [user guide](user-guide.md) instead. If you're +working on Scrobbler's own code, see [setup.md](setup.md). -## Services +## Architecture ``` players / scrobblers ──► API /2.0/ Last.fm-compatible @@ -21,7 +21,10 @@ Prometheus ◄── API :9100/metrics, postgres-exporter ──► Grafana - **`db`**: PostgreSQL. - With the `monitoring` profile: `prometheus`, `grafana`, `postgres-exporter`. -## Quick start +Everything Scrobbler needs to run is in these five services plus optional monitoring — +there's no separate cache, queue or search service to operate. + +## Installing ```sh cp .env.example .env # then set SECRET_KEY and POSTGRES_PASSWORD @@ -35,6 +38,9 @@ Published images: `pkgs.daubney.dev/scrobbler/scrobbler-api` and `pkgs.daubney.dev/scrobbler/scrobbler-ui`, tagged `main` (latest push to `main`), `sha-`, and ``/`latest` for tagged releases. +This is enough for evaluating Scrobbler. Before putting it in front of real users, also +read **Putting it behind TLS** below — the quick start above serves plain HTTP only. + ## Configuration | Variable | Used by | Default | Purpose | @@ -57,8 +63,10 @@ In `docker compose`, host ports are set with `DB_PORT`, `API_PORT`, `UI_PORT`, `CORS_ORIGINS` and `UI_BASE_URL` answer different questions and can differ: the first is *which browser origins may call the JSON API*, the second is *where the public identity -and sign-in redirects point*. Don't assume setting one sets the other — `docker-compose.yml` -passes them through independently. +and sign-in redirects point*. Don't assume setting one sets the other — +`docker-compose.yml` passes them through independently, and forgetting to set +`CORS_ORIGINS` to your real public origin is the single most common cause of "the UI +can't reach the API" once you're off `localhost` (see Troubleshooting). ### Fediverse sharing (optional) @@ -87,13 +95,18 @@ Optional: `FEDERATION_BLOCKED_DOMAINS` (comma-separated, subdomains included), `draft-cavage,rfc9421`). The `worker` service must be running — it delivers outgoing activities and processes incoming ones. +Once it's on, tell your users: they still need to opt in themselves under **Sharing** in +the web UI — see the [user guide](user-guide.md#sharing-on-the-fediverse). + ## Putting it behind TLS Scrobbler itself only speaks plain HTTP; something in front of it needs to terminate TLS. Any reverse proxy works — nginx, Caddy, or a tunnel like Cloudflare Tunnel for testing without a public IP (see `docs/design/activitypub-phase4.md` for how the -project's own manual federation test used one). A minimal Caddy example, proxying -everything to the `ui` container (which itself forwards the fediverse paths to `api`): +project's own manual federation test used one). + +Caddy, proxying everything to the `ui` container (which itself forwards the fediverse +paths to `api`), gets you a certificate automatically with no extra steps: ``` scrobble.example { @@ -101,11 +114,39 @@ scrobble.example { } ``` +nginx with certbot, doing the same job: + +```nginx +server { + listen 80; + server_name scrobble.example; + location / { return 301 https://$host$request_uri; } +} + +server { + listen 443 ssl; + server_name scrobble.example; + ssl_certificate /etc/letsencrypt/live/scrobble.example/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/scrobble.example/privkey.pem; + + location / { + proxy_pass http://localhost:8080; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-Proto https; + } +} +``` + +```sh +certbot --nginx -d scrobble.example +``` + Point `UI_BASE_URL`/`FEDERATION_BASE_URL`/`FEDERATION_DOMAIN` at that same public `https://scrobble.example` origin. The API (`API_PORT`, default 5050) doesn't need its own public hostname unless you want browsers to reach it directly instead of through the UI's proxy — set `API_BASE_URL` accordingly either way, since the browser calls it -directly for `/api/v1`. +directly for `/api/v1`, and make sure `CORS_ORIGINS` includes `https://scrobble.example` +regardless of which layout you pick. ## Monitoring @@ -121,13 +162,41 @@ With `--profile monitoring`, Grafana (`GRAFANA_ADMIN_PASSWORD`) provisions a evaluated by Prometheus and visible in Grafana, but nothing routes them anywhere without Alertmanager. -## Backups +## Backups and restore Back up, at minimum: - The Postgres database (all scrobble history, accounts, federation state). - `FEDERATION_KEY_SECRET`, if federation is on — it's the only thing that can decrypt stored signing keys, and it's never derivable from the database alone. +- Your `.env` (or equivalent secrets store), so `SECRET_KEY` and the rest survive a + rebuild without inventing new ones. + +```sh +docker compose exec db pg_dump -U scrobbler scrobbler | gzip > backup-$(date +%F).sql.gz +``` + +To restore onto a fresh `db` volume: + +```sh +gunzip -c backup-2026-01-01.sql.gz | docker compose exec -T db psql -U scrobbler scrobbler +``` + +Restoring an older dump with federation on will leave followers pointed at posts that no +longer exist and profile data that's gone stale — expected after any point-in-time +restore, not a sign anything's broken. + +## Upgrades + +```sh +docker compose pull # or set SCROBBLER_IMAGE_TAG to a specific version +docker compose up -d +``` + +Migrations run automatically on API container start (`RUN_MIGRATIONS=1`, the default). +Take a database backup first for anything beyond a patch version — check +`docs/design/` and the Gitea release notes for the version you're jumping to if it spans +a major version. ## Releases @@ -143,3 +212,52 @@ and on `v*` tags, once lint, tests and the Prometheus config checks all pass: To release: tag `vX.Y.Z` on `main` and push the tag. That tag sets both the package and image version. See [setup.md](setup.md) for the secrets/variables the workflow needs and how to run it locally before tagging. + +## Troubleshooting + +**The web UI loads but can't reach the API ("Can't reach the Scrobbler API at …" or +requests failing silently).** Almost always `CORS_ORIGINS` on the `api` service not +listing the exact origin the browser loads the UI from (scheme and host both have to +match — `https://scrobble.example` is not the same origin as `http://scrobble.example` +or `www.scrobble.example`). Check with: + +```sh +curl -i -X OPTIONS https://scrobble.example/api/v1/auth/me \ + -H "Origin: https://scrobble.example" -H "Access-Control-Request-Method: GET" +``` + +A missing `access-control-allow-origin` header in the response confirms it. This is +separate from `UI_BASE_URL` — setting one does not set the other. + +**Federation looks enabled but the "Sharing" page/nav link doesn't appear for a user.** +First confirm federation is actually on server-side, independent of what any log line +says (gunicorn drops `log.info` by default unless something configures logging): + +```sh +curl -o /dev/null -w '%{http_code}\n' https://scrobble.example/actor # 200 = federation is on +``` + +If that's `200`, the server is fine and the problem is client-side: the web UI caches +whether federation is available in the browser's `sessionStorage` for the tab, and +caches static JS in the ordinary browser cache. Ask the user to hard-refresh +(Ctrl/Cmd+Shift+R) or try a private window — if it works there, it's a stale cache, not +a server problem. + +**A fediverse profile 404s from a specific remote server (e.g. mastodon.social) right +after you enable sharing, even though WebFinger looks correct from curl.** Large +instances cache a *negative* WebFinger lookup for a while. Confirm the server's own +response is correct first: + +```sh +curl -H 'Accept: application/jrd+json' \ + 'https://scrobble.example/.well-known/webfinger?resource=acct:name@scrobble.example' +``` + +If that's correct, the fix is time (or trying a smaller/different instance to confirm +it's not a Scrobbler-side problem) — there's nothing to change on the server. + +**A legacy scrobbler client (e.g. VLC's built-in scrobbler) fails to connect at all.** +Scrobbler implements the Last.fm 2.0 API only, not the legacy 1.2 Audioscrobbler +handshake (`hs=true&p=1.2&...`). Clients that only speak 1.2 can't be pointed at +Scrobbler regardless of configuration — point users at a client with 2.0 support +instead. \ No newline at end of file diff --git a/docs/setup.md b/docs/setup.md index 5043dcb..684aa74 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -1,7 +1,8 @@ # Working on Scrobbler -Setting up a local development environment. For running a real instance, see -[deploy.md](deploy.md). For what the service does, see [usage.md](usage.md). +Setting up a local development environment. For running a real instance, see the +[admin guide](admin-guide.md). For what the service does, see the +[user guide](user-guide.md). ## Prerequisites @@ -88,7 +89,7 @@ Everything runs from `.gitea/workflows/ci.yml`: - **Every push and PR**: lint, the migrations-vs-models check, tests against Postgres, and `promtool` checks of the Prometheus config and alert rules. - **Pushes to `main` and tags**, once those all pass: images and assets publish (see - [deploy.md](deploy.md#releases)). + [admin guide](admin-guide.md#releases)). - **Every run** posts to the `ScrobblingService` topic on notify.daubney.dev. Repository secrets: `PKGS_USER`, `PKGS_PASSWORD`, `PKGS_API_KEY`, `NTFY_USER`, diff --git a/docs/usage.md b/docs/usage.md deleted file mode 100644 index 3afcf9a..0000000 --- a/docs/usage.md +++ /dev/null @@ -1,94 +0,0 @@ -# Using Scrobbler - -What the service does, once it's running. For getting an instance running, see -[deploy.md](deploy.md); for working on the code, see [setup.md](setup.md). - -## Account and apps - -Register in the web UI, then go to **Apps** and create one app per player or scrobbler -client you use — each gets its own API key and shared secret, and can be revoked on its -own from the **Sessions** page without affecting the others. - -## Connecting a player - -In the player's scrobbling settings, choose a custom or self-hosted Last.fm server: - -- **API URL**: `https://your-host/2.0/` -- **API key and shared secret**: from an app on the **Apps** page -- **Username and password**: your Scrobbler account - -Two sign-in flows are supported: - -- **Mobile**: `auth.getMobileSession` with a username and password, sent as a POST body - over HTTPS. -- **Desktop**: `auth.getToken`, then the user approves at `/api/auth/` (redirects to the - web UI), then `auth.getSession`. - -The legacy `authToken = md5(username + md5(password))` form isn't supported — checking -it would mean storing unsalted password hashes. pylast's `password_hash` option uses -that form; use `SessionKeyGenerator` (the desktop flow) instead. - -Supported methods: `auth.getMobileSession`, `auth.getToken`, `auth.getSession`, -`track.scrobble` (up to 50 per request), `track.updateNowPlaying`, `user.getInfo`, -`user.getRecentTracks`, `user.getTopArtists`, `user.getTopAlbums`, `user.getTopTracks`. -`/api/docs` has the full parameters, rules and example responses for each. - -A scrobble is ignored (not rejected) if the artist or track is empty, or the timestamp -is more than 14 days old or more than 5 minutes in the future. Resending the same -scrobble is accepted and stored once, so client retries are safe. - -## Importing history - -Open **Import** in the web UI: - -- **An export file**: - - CSV from [lastfm-to-csv](https://benjaminbenben.com/lastfm-to-csv/), no header row, - artist/album/track/date columns. - - Any CSV or TSV with a header row naming artist, track and a date or Unix-time - column. Comma, semicolon and tab delimiters all work. - - A JSON export of `user.getRecentTracks` pages (such as lastfm.ghan.nl's), a single - `recenttracks` response, or a flat list of tracks. - - Files can be gzipped. -- **Last.fm directly**: enter a Last.fm username and the worker pages through that - account's public history. Only appears when the server has `LASTFM_API_KEY` set. - -Imports run in the background; the page shows progress and can cancel one in flight. -Unlike live scrobbles, imports keep plays of any age (still skipping rows with no artist -or track, or plays dated in the future). Plays already in your history are skipped, so -importing the same export again is safe. - -## Sharing on the fediverse - -Only available when the server has federation enabled (ask whoever runs it, or see -[deploy.md](deploy.md#fediverse-sharing-optional) if that's you). Turn it on under -**Sharing**, and you're findable as `@name@` from Mastodon and other -fediverse apps: - -- Follows are accepted automatically, or wait for your approval if you turn that on. -- **Followers** lists requests, followers and blocked accounts, with approve, decline, - remove, block and unblock. - -Once someone follows, **Sharing**'s **What to post** section controls what they see: - -- **Weekly summary**: play count and top artist(s), posted once a week (Monday 09:00 in - your own time zone, for the week that just ended). A live preview shows what it would - say so far. -- **Milestones**: total scrobbles, an artist's plays, and a new entry into your all-time - top 10 — each posted once, the first time it's reached. History already on file when - you turn sharing on, or added by an import, is never posted about retroactively. - -Both are on by default and can be switched off independently. Posts use the same -visibility as your profile (followers only by default, or unlisted/public); **Posts** -lists your own posts with delete (sends a `Delete` to whoever received it). - -**Now playing** is a separate, three-way choice: - -- **Off** (default): nothing shown or posted. -- **Show on my profile**: a "Now playing: *Track* by Artist" field, refreshed at most - every 5 minutes while something's playing and cleared a few minutes after it stops. -- **Show on my profile and post**: the profile field, plus a post for each track played - 30 seconds or more, at most one every 30 minutes, with the previous now-playing post - deleted once the new one is confirmed published. - -Handles and actor URLs **can't change once anyone follows you**, so if you're the one -who gets to choose the server's domain, choose it carefully. diff --git a/docs/user-guide.md b/docs/user-guide.md new file mode 100644 index 0000000..ef0bd2e --- /dev/null +++ b/docs/user-guide.md @@ -0,0 +1,119 @@ +# User guide + +What Scrobbler does once you're signed in: connecting a player, importing your old +history, and sharing your listening on the fediverse. If you're setting up or running an +instance rather than using one, see the [admin guide](admin-guide.md) instead. + +## Getting started + +Register in the web UI, then go to **Apps** and create one app per player or scrobbler +client you use — each gets its own API key and shared secret, and can be revoked on its +own from the **Sessions** page without affecting the others. If a friend runs your +instance, ask them for its address; that's the only thing you need from them to get +going. + +## Connecting a player + +In the player's scrobbling settings, choose a custom or self-hosted Last.fm server: + +- **API URL**: `https://your-host/2.0/` +- **API key and shared secret**: from an app on the **Apps** page +- **Username and password**: your Scrobbler account + +Two sign-in flows are supported, and most players use one or the other automatically: + +- **Mobile**: you enter your username and password directly in the player. +- **Desktop**: the player opens a browser page for you to approve, then signs itself in + — no password ever passed to the player. + +A sign-in method that hashes your password before sending it (sometimes offered as a +"stored password" or "password hash" option, e.g. pylast's `password_hash`) isn't +supported — use the plain username/password or desktop-approval flow instead. + +Supported methods, if your player lets you check: `auth.getMobileSession`, +`auth.getToken`, `auth.getSession`, `track.scrobble` (up to 50 per request), +`track.updateNowPlaying`, `user.getInfo`, `user.getRecentTracks`, `user.getTopArtists`, +`user.getTopAlbums`, `user.getTopTracks`. `/api/docs` on your instance has the full +parameters and example responses for each, if you're curious or debugging a client. + +A play is silently skipped (not rejected as an error) if the artist or track name is +empty, or its timestamp is more than 14 days old or more than 5 minutes in the future. +Sending the same play twice — a common side effect of a player retrying after a network +blip — is accepted both times but only stored once, so retries are always safe. + +**A legacy scrobbler that speaks the old 1.2 "handshake" protocol (rather than 2.0) won't +work here** — this is a client limitation, not a setting to fix. VLC's built-in +scrobbler is one example; look for a plugin or a different player with 2.0 support. + +## Importing history + +Open **Import** in the web UI: + +- **An export file**: + - CSV from [lastfm-to-csv](https://benjaminbenben.com/lastfm-to-csv/), no header row, + artist/album/track/date columns. + - Any CSV or TSV with a header row naming artist, track and a date or Unix-time + column. Comma, semicolon and tab delimiters all work. + - A JSON export of `user.getRecentTracks` pages (such as lastfm.ghan.nl's), a single + `recenttracks` response, or a flat list of tracks. + - Files can be gzipped. +- **Last.fm directly**: enter a Last.fm username and the import runs against that + account's public history. Only appears if your instance's admin has set this up. + +Imports run in the background — the page shows progress and you can cancel one in +flight. Unlike live scrobbles, imports keep plays of any age (still skipping rows with no +artist or track, or plays dated in the future). Plays already in your history are +skipped, so importing the same export again, or importing overlapping exports, is safe. + +## Sharing on the fediverse + +Only available if your instance's admin has turned this on — ask them, or check whether +**Sharing** appears in the navigation. Turn it on there, and you're findable as +`@name@` from Mastodon and other fediverse apps. + +- Follows are accepted automatically, or wait for your approval if you turn that on + under **Followers and discovery**. +- **Followers** lists requests, followers and blocked accounts, with approve, decline, + remove, block and unblock. + +Once someone follows you, **Sharing**'s **What to post** section controls what they see: + +- **Weekly summary**: your play count and top artist(s), posted once a week (Monday + 09:00 in your own time zone, for the week that just ended). A live preview shows what + it would say so far. +- **Milestones**: total scrobbles, an artist's plays, and a new entry into your all-time + top 10 — each posted once, the first time you reach it. History already on file when + you turn sharing on, or added by an import, is never posted about retroactively. + +Both are on by default and can be switched off independently. Posts use the same +visibility as your profile (followers only by default, or unlisted/public); **Posts** +lists your own posts, each with a delete button. + +**Now playing** is a separate, three-way choice: + +- **Off** (default): nothing shown or posted. +- **Show on my profile**: a "Now playing: *Track* by Artist" field on your profile, + refreshed at most every 5 minutes while something's playing and cleared a few minutes + after it stops. +- **Show on my profile and post**: the profile field, plus a post for each track played + 30 seconds or more, at most one every 30 minutes, with the previous now-playing post + deleted once the new one is confirmed published. + +**Your handle can't change once anyone follows you** — if you get to choose the +instance's domain yourself, or you're choosing which instance to join, bear that in mind +before you start sharing. + +## Troubleshooting + +**I turned Sharing on but can't see it in the menu.** Try a hard refresh +(Ctrl/Cmd+Shift+R) or a private/incognito window. The menu remembers whether sharing is +available for the length of a browser tab, so it can lag behind a setting your admin just +changed. + +**My profile 404s on a big instance like mastodon.social right after I start sharing.** +Some servers cache a "not found" for a little while after they first look you up. Give it +an hour, or try searching from a smaller instance to confirm your handle really works. + +**A change I made under Sharing doesn't seem to have taken effect.** Make sure you +clicked **Save sharing settings** at the bottom of the page — ticking a box alone doesn't +save it. \ No newline at end of file