diff --git a/.forgejo/workflows/release.yml b/.forgejo/workflows/release.yml new file mode 100644 index 0000000..023f628 --- /dev/null +++ b/.forgejo/workflows/release.yml @@ -0,0 +1,138 @@ +# What happens at every merge into main: +# 1. semantic-release reads the commits since the last release (Conventional Commits), works out the next version, +# writes CHANGELOG.md, makes the release on the forge and tags it. A merge with nothing to release (docs, tests, ci) +# makes none. +# 2. When there is a release, the image is built from the tag, for amd64 and for arm64, and pushed to +# git.kmalloc.org/squid/sonda as X.Y.Z-ARCH; then X.Y.Z, X.Y and latest are made to point at both. +# The runner has no Docker and cannot emulate another architecture, so the images are built with kaniko, which needs no +# daemon, and the Dockerfile crosses to arm64 without running anything for it. +# Secret needed: FORGEJO_TOKEN, a token of the owner with the scopes write:repository and write:package. +name: release + +on: + push: + branches: [main] + +# One release at a time: two merges close together must not make two versions at once. +concurrency: + group: release + cancel-in-progress: false + +jobs: + release: + runs-on: docker + # The commit that the release makes (CHANGELOG.md) says [skip ci]; this is the belt for those braces. + if: ${{ !contains(github.event.head_commit.message, '[skip ci]') }} + outputs: + version: ${{ steps.version.outputs.version }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # the whole history: the next version depends on every commit since the last tag + token: ${{ secrets.FORGEJO_TOKEN }} + - uses: actions/setup-node@v4 + with: + node-version: 24 + - name: Release + env: + GITEA_TOKEN: ${{ secrets.FORGEJO_TOKEN }} + GITEA_URL: https://git.kmalloc.org + GIT_AUTHOR_NAME: sonda-release + GIT_AUTHOR_EMAIL: squid@kmalloc.org + GIT_COMMITTER_NAME: sonda-release + GIT_COMMITTER_EMAIL: squid@kmalloc.org + run: > + npx --yes + -p semantic-release@25 + -p @semantic-release/commit-analyzer@13.0.1 + -p @semantic-release/release-notes-generator@14.1.1 + -p @semantic-release/changelog@7.0.0 + -p @semantic-release/git@11.0.1 + -p conventional-changelog-conventionalcommits@8.0.0 + -p @saithodev/semantic-release-gitea@2.1.0 + semantic-release + - name: Which version was made, if any + id: version + run: | + # The tag of a release points at the commit the release made, which is where the checkout now is. + tag="$(git tag --points-at HEAD | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1 || true)" + echo "version=${tag#v}" >> "$GITHUB_OUTPUT" + echo "released: ${tag:-nothing}" + + # The Alpine of the arm64 image, by digest: kaniko, which has no emulation here, takes the base of every stage for the + # platform of the runner, so the last stage is told which one to use. + base: + needs: release + if: ${{ needs.release.outputs.version != '' }} + runs-on: docker + container: + image: gcr.io/go-containerregistry/crane:debug + outputs: + arm64: ${{ steps.digest.outputs.arm64 }} + steps: + - id: digest + run: echo "arm64=alpine@$(crane digest --platform linux/arm64 alpine:3.22)" >> "$GITHUB_OUTPUT" + + # One image for each architecture. The Go program crosses to the other one with no emulation, and the last stage of the + # Dockerfile runs no command (see the Dockerfile). + image: + needs: [release, base] + if: ${{ needs.release.outputs.version != '' }} + runs-on: docker + strategy: + matrix: + arch: [amd64, arm64] + container: + image: gcr.io/kaniko-project/executor:v1.23.2-debug + steps: + - name: Build the image from the tag and push it as X.Y.Z-ARCH + env: + VERSION: ${{ needs.release.outputs.version }} + ARCH: ${{ matrix.arch }} + ARM64_BASE: ${{ needs.base.outputs.arm64 }} + REGISTRY_USER: squid + REGISTRY_TOKEN: ${{ secrets.FORGEJO_TOKEN }} + run: | + set -eu + mkdir -p /kaniko/.docker + auth="$(printf '%s:%s' "$REGISTRY_USER" "$REGISTRY_TOKEN" | base64 | tr -d '\n')" + printf '{"auths":{"git.kmalloc.org":{"auth":"%s"}}}' "$auth" > /kaniko/.docker/config.json + base=alpine:3.22 + if [ "$ARCH" = arm64 ]; then base="$ARM64_BASE"; fi + /kaniko/executor \ + --dockerfile Dockerfile \ + --context "git://git.kmalloc.org/squid/sonda.git#refs/tags/v${VERSION}" \ + --build-arg "VERSION=${VERSION}" \ + --build-arg "SONDA_ARCH=${ARCH}" \ + --build-arg "RUNTIME_BASE=${base}" \ + --label "org.opencontainers.image.version=${VERSION}" \ + --destination "git.kmalloc.org/squid/sonda:${VERSION}-${ARCH}" \ + --verbosity info + + # The tags that people use (X.Y.Z, X.Y, latest) point at a list of the two images, so that each machine takes its own. + manifest: + needs: [release, image] + if: ${{ needs.release.outputs.version != '' }} + runs-on: docker + container: + image: gcr.io/go-containerregistry/crane:debug + steps: + - name: Join the images under X.Y.Z, X.Y and latest + env: + VERSION: ${{ needs.release.outputs.version }} + REGISTRY_USER: squid + REGISTRY_TOKEN: ${{ secrets.FORGEJO_TOKEN }} + run: | + set -eu + image=git.kmalloc.org/squid/sonda + crane auth login git.kmalloc.org -u "$REGISTRY_USER" -p "$REGISTRY_TOKEN" + # Kaniko writes the architecture of the runner in both images: say the right one for each. + for arch in amd64 arm64; do + crane mutate "${image}:${VERSION}-${arch}" --set-platform "linux/${arch}" --tag "${image}:${VERSION}-${arch}" + done + crane index append \ + --tag "${image}:${VERSION}" \ + --manifest "${image}:${VERSION}-amd64" \ + --manifest "${image}:${VERSION}-arm64" + crane tag "${image}:${VERSION}" "${VERSION%.*}" + crane tag "${image}:${VERSION}" latest diff --git a/.releaserc.json b/.releaserc.json new file mode 100644 index 0000000..a042e35 --- /dev/null +++ b/.releaserc.json @@ -0,0 +1,35 @@ +{ + "branches": ["main"], + "tagFormat": "v${version}", + "plugins": [ + ["@semantic-release/commit-analyzer", { "preset": "conventionalcommits" }], + [ + "@semantic-release/release-notes-generator", + { + "preset": "conventionalcommits", + "presetConfig": { + "types": [ + { "type": "feat", "section": "Features" }, + { "type": "fix", "section": "Bug fixes" }, + { "type": "perf", "section": "Performance" }, + { "type": "refactor", "section": "Refactoring" }, + { "type": "docs", "section": "Documentation" }, + { "type": "style", "hidden": true }, + { "type": "test", "hidden": true }, + { "type": "chore", "hidden": true }, + { "type": "ci", "hidden": true } + ] + } + } + ], + ["@semantic-release/changelog", { "changelogFile": "CHANGELOG.md", "changelogTitle": "# Changelog" }], + [ + "@semantic-release/git", + { + "assets": ["CHANGELOG.md"], + "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" + } + ], + ["@saithodev/semantic-release-gitea", { "giteaUrl": "https://git.kmalloc.org" }] + ] +} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c54bc8d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,46 @@ +# Changelog + +## [0.5.0](https://git.kmalloc.org/squid/sonda/compare/v0.4.2...v0.5.0) (2026-11-17) + +### Features + +* **android:** add an Android app ([ba10b8e](https://git.kmalloc.org/squid/sonda/commit/ba10b8ef7bbcf4fe3735e7b931d1b687259cc044)), closes [#47](https://git.kmalloc.org/squid/sonda/issues/47) +* **web:** talk to the Android app ([995f2e5](https://git.kmalloc.org/squid/sonda/commit/995f2e5ae013897a56743b60eeedea909fe04db9)) + +### Bug fixes + +* **web:** show a restored queue on the lock screen ([a2d6d2a](https://git.kmalloc.org/squid/sonda/commit/a2d6d2a925edf8de89a7a4df0afbfa82004ccf87)) + +## [0.4.2](https://git.kmalloc.org/squid/sonda/compare/v0.4.1...v0.4.2) (2026-11-17) + +### Bug fixes + +* **web:** make the seek bar fit the width of a phone ([6220238](https://git.kmalloc.org/squid/sonda/commit/6220238068c446a97d1726697601f7ba5e126028)), closes [#45](https://git.kmalloc.org/squid/sonda/issues/45) + +## [0.4.1](https://git.kmalloc.org/squid/sonda/compare/v0.4.0...v0.4.1) (2026-11-17) + +### Bug fixes + +* **web:** keep the controls of the full-screen player on screen on a phone ([a757743](https://git.kmalloc.org/squid/sonda/commit/a757743161ac3f7a348884a9f7b2490ab09b99e1)), closes [#43](https://git.kmalloc.org/squid/sonda/issues/43) + +## [0.4.0](https://git.kmalloc.org/squid/sonda/compare/v0.3.0...v0.4.0) (2026-11-16) + +### Features + +* **web:** make Sonda feel like an app on a phone ([75a7898](https://git.kmalloc.org/squid/sonda/commit/75a7898c68ca65c8c4efa0f51b72d62ac9a4db6e)) + +### Documentation + +* note that this repo moved to git.kmalloc.org ([ee31e0e](https://git.kmalloc.org/squid/sonda/commit/ee31e0ee7caf9e7510b7cfde1f4e096721966e90)) + +## [0.3.0](https://git.kmalloc.org/squid/sonda/compare/v0.2.0...v0.3.0) (2026-11-16) + +### Features + +* publish the Docker image for ARM as well ([b37e3db](https://git.kmalloc.org/squid/sonda/commit/b37e3db037a442af439c5183cf4937a9be630a14)) + +## [0.2.0](https://git.kmalloc.org/squid/sonda/compare/v0.1.0...v0.2.0) (2026-11-16) + +### Features + +* scan the libraries by themselves when their music changed ([89f1b52](https://git.kmalloc.org/squid/sonda/commit/89f1b5275a3f1d0115f1cdd22512daebebf964d2)) diff --git a/Dockerfile b/Dockerfile index 802b1f8..786dc20 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,11 @@ # syntax=docker/dockerfile:1 +# RUNTIME_BASE is alpine:3.22 unless the CI release asks for another architecture, with the digest of that one. +ARG RUNTIME_BASE=alpine:3.22 + # The web app: built once here, written (by its Vite config) to /src/internal/webui/dist, where the Go # build below embeds it. Nothing of Node reaches the final image. -FROM node:24-alpine AS web +FROM --platform=$BUILDPLATFORM node:24-alpine AS web WORKDIR /src/web COPY web/package.json web/package-lock.json ./ RUN npm ci @@ -12,7 +15,13 @@ RUN npm run build # Build: Sonda has no CGO (SQLite, PostgreSQL and the tag reader are pure Go), so the binary # is static and the final image needs nothing but it and the CA certificates. The web app is inside it # (-tags webui), so sondad serves it on / and the API on /api. -FROM golang:1.27-alpine AS build +FROM --platform=$BUILDPLATFORM golang:1.27-alpine AS build +# Built on the machine that builds, for the machine that runs: Go crosses to another architecture with no emulation +# (TARGETOS and TARGETARCH come from the platform asked for). +ARG TARGETOS +ARG TARGETARCH +# SONDA_ARCH is for a builder that cannot say TARGETARCH (the CI release, see .forgejo/workflows/release.yml). +ARG SONDA_ARCH WORKDIR /src COPY go.mod go.sum ./ RUN go mod download @@ -20,19 +29,28 @@ COPY cmd ./cmd COPY internal ./internal COPY migrations ./migrations COPY --from=web /src/internal/webui/dist ./internal/webui/dist -RUN CGO_ENABLED=0 go build -tags webui -trimpath -ldflags="-s -w" -o /out/sondad ./cmd/sondad +# The version of the release (see .releaserc.json), as GET /version and the User-Agent of the providers say it. +ARG VERSION=dev +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${SONDA_ARCH:-${TARGETARCH:-amd64}} go build -tags webui -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o /out/sondad ./cmd/sondad -FROM alpine:3.22 -LABEL org.opencontainers.image.title="Sonda" \ - org.opencontainers.image.description="A privacy-first, self-hosted music server" \ - org.opencontainers.image.source="https://codeberg.org/squidd/sonda" -# ca-certificates: the providers an admin may switch on are reached over https. -# tzdata: log times in the zone the operator sets with TZ. -RUN apk add --no-cache ca-certificates tzdata \ +# What the image has besides the program (the zones of tzdata, the user, the folders) is made here, on the machine that +# builds. The last stage then runs no command, so it can be built for another architecture with no emulation. +FROM --platform=$BUILDPLATFORM alpine:3.22 AS rootfs +RUN apk add --no-cache tzdata \ && addgroup -S -g 10001 sonda \ && adduser -S -u 10001 -G sonda -h /data -H sonda \ - && mkdir -p /data /music \ - && chown sonda:sonda /data + && mkdir -p /out/data /out/music + +FROM ${RUNTIME_BASE} +LABEL org.opencontainers.image.title="Sonda" \ + org.opencontainers.image.description="A privacy-first, self-hosted music server" \ + org.opencontainers.image.source="https://git.kmalloc.org/squid/sonda" +# The certificates of alpine (ca-certificates-bundle) are what the providers an admin may switch on are reached with, +# over https. tzdata is for the log times in the zone the operator sets with TZ. +COPY --from=rootfs /usr/share/zoneinfo /usr/share/zoneinfo +COPY --from=rootfs /etc/passwd /etc/group /etc/shadow /etc/ +COPY --from=rootfs --chown=10001:10001 /out/data /data +COPY --from=rootfs /out/music /music COPY --from=build /out/sondad /usr/local/bin/sondad # Everything Sonda writes is under /data: the database and the files it makes (covers, pictures). diff --git a/Makefile b/Makefile index c2f008f..75ee9da 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,11 @@ .PHONY: build web test +# The version is the last release tag, and how far this checkout is from it (v1.2.3-4-gabcdef), or "dev" outside git. +VERSION ?= $(shell git describe --tags --always 2>/dev/null | sed 's/^v//' || echo dev) + build: web - CGO_ENABLED=0 go build -tags webui -trimpath -ldflags="-s -w" -o bin/sondad ./cmd/sondad + CGO_ENABLED=0 go build -tags webui -trimpath -ldflags="-s -w -X main.version=$(VERSION)" -o bin/sondad ./cmd/sondad web: cd web && npm ci && npm run build diff --git a/README.md b/README.md index 301a895..0ddc654 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,53 @@ # Sonda +**This repository has moved.** Development continues at +[git.kmalloc.org/squid/sonda](https://git.kmalloc.org/squid/sonda); this +Codeberg mirror is archived and read-only. + Sonda is a privacy-first, self-hosted music server written in Go. It streams the music you already own, from your own disks, and keeps everything it knows about you on your own machine. It is a single executable, one database (SQLite by default, PostgreSQL if you prefer) and the folders that hold your music. There is no Redis, no message broker and no cloud account. -> **Status: early.** The local-playback part works end to end: accounts, libraries, scanning, search, and streaming with HTTP Range. Suggestions of new artists work too (Discover). Radio queues and OpenSubsonic compatibility are not built yet, and the web client in `web/`, which `sondad` serves, is young (see [What is not there yet](#what-is-not-there-yet) and [Web app](#web-app)). The API may still change. +> **Status: early.** The local-playback part works end to end: accounts, libraries, scanning, search, and streaming with HTTP Range. Suggestions of new artists work too (Discover). Radio queues are not built yet, other apps can connect over OpenSubsonic (see [Other apps](#other-apps-opensubsonic)), and the web client in `web/`, which `sondad` serves, is young (see [What is not there yet](#what-is-not-there-yet) and [Web app](#web-app)). The API may still change. ## What works today - **Accounts.** An account is an email address and a password. The first user to register becomes the admin and registration then closes; admins create the other users. The address only identifies the account: Sonda sends no mail, does not check that the address exists, and keeps it on your server. It is trimmed and lowercased, and unique. Accounts made before addresses were asked for keep the plain name they had as their login. Short-lived access tokens (JWT) plus rotating refresh tokens, so a stolen refresh token is detected. - **Libraries.** A library is a name and a folder, managed through the API. Sonda only reads your music folders: it never writes or renames a file, and it deletes one only when an admin asks to remove the extra copy of a song and you turned on `library.allow_delete` (see **Duplicates**), which is off by default. -- **Scanner.** Reads MP3 and FLAC tags and audio properties with several workers in parallel. Rescans are incremental, renamed files keep their identity, vanished files are flagged (never deleted), and a library whose folder looks empty or unreadable (an unmounted NAS) is left alone instead of being wiped. Audio in other formats (m4a, ogg, opus, wav, wma and similar) is not read yet, but it is counted as `unsupported` in the scan result instead of disappearing silently. NAS housekeeping folders (`@Recycle`, `#recycle`, `@eaDir`, `.@__thumb`, `$RECYCLE.BIN`) are skipped. -- **Catalog.** Browse and search artists, albums and tracks, with paging, by library, artist or album. Only tracks you can actually play are listed. -- **Duplicates.** `GET /library/duplicates` (admin) lists the songs that are in more than one file: the same set of main artists, the same title and durations within 3 seconds, possibly on different albums. It is worked out from the index, touches no file, and returns no file path. Each group proposes the copy to keep (lossless first, then the higher bitrate, then the bigger file, then the oldest). A background job (`POST /library/duplicates/check`, like the other bulk jobs) reads the files of the groups and keeps their SHA-256, so a group can be called `identical` (byte for byte). `POST /library/duplicates/clean` **deletes the extra copies of one song from the disk**, and it is the one thing Sonda does that writes in your music folders, so it is off until you set `library.allow_delete` (or `SONDA_ALLOW_DELETE=true`) and mount the folders writable. It deletes only when an admin asks, never by itself, and the server checks again at that moment that no scan is running, that the tracks are copies of one song, and that every file is still the one the last scan saw (same size and date, a regular file, not a link, inside its library): so the last copy of a song is never deleted, a file that changed is not deleted, and every file is checked before the first is deleted. The listens and the playlist items of a deleted copy move to the one that stays, so your taste and your playlists are not lost. Folders are never removed, and nothing is sent anywhere. -- **Streaming.** `GET` and `HEAD` with `Range` support, so players can seek. Opening or prefetching a stream does not record a listen. +- **Scanner.** Reads MP3 and FLAC tags and audio properties with several workers in parallel. Rescans are incremental, renamed files keep their identity, vanished files are flagged (never deleted), and a library whose folder looks empty or unreadable (an unmounted NAS) is left alone instead of being wiped. Audio in other formats (m4a, ogg, opus, wav, wma and similar) is not read yet, but it is counted as `unsupported` in the scan result instead of disappearing silently. NAS housekeeping folders (`@Recycle`, `#recycle`, `@eaDir`, `.@__thumb`, `$RECYCLE.BIN`) are skipped. **Scans by itself.** Every 15 minutes (`library.auto_scan_minutes`) Sonda looks at the folders of each library, which costs only the names of the files, and scans a library whose music changed: a song or a folder came, went or was renamed. A library is also scanned at least once a day, to find a song that was tagged again where it stands. It never scans while another scan runs, and a folder it cannot read (a NAS that is not mounted) is left alone. +- **Catalog.** Browse and search artists, albums and songs, with paging, by library, artist or album. Only songs you can actually play are listed. +- **Duplicates.** `GET /library/duplicates` (admin) lists the songs that are in more than one file: the same set of main artists, the same title and durations within 3 seconds, possibly on different albums. It is worked out from the index, touches no file, and returns no file path. Each group proposes the copy to keep (lossless first, then the higher bitrate, then the bigger file, then the oldest). A background job (`POST /library/duplicates/check`, like the other bulk jobs) reads the files of the groups and keeps their SHA-256, so a group can be called `identical` (byte for byte). `POST /library/duplicates/clean` **deletes the extra copies of one song from the disk**, and it is the one thing Sonda does that writes in your music folders, so it is off until you set `library.allow_delete` (or `SONDA_ALLOW_DELETE=true`) and mount the folders writable. It deletes only when an admin asks, never by itself, and the server checks again at that moment that no scan is running, that the songs are copies of one song, and that every file is still the one the last scan saw (same size and date, a regular file, not a link, inside its library): so the last copy of a song is never deleted, a file that changed is not deleted, and every file is checked before the first is deleted. The listens and the playlist items of a deleted copy move to the one that stays, so your taste and your playlists are not lost. Folders are never removed, and nothing is sent anywhere. The web app has a Duplicates section in Admin: a table of the copies of each song with the copy to keep already chosen (you can pick another), the check with its progress and a Stop button, **Delete the others** for one song and **Clean all suggestions** for all of them, each after a question that lists the files and the space it frees; with deleting off everything is visible and the delete buttons are disabled. +- **Streaming.** `GET` and `HEAD` with `Range` support, so players can seek. Opening or prefetching a stream does not record a listen. `GET /tracks/{id}/peaks` gives the shape of a song for the seek bar: 120 levels from 0 to 255, how loud it is along its length. The server finds it by decoding the file (MP3 and FLAC, in pure Go: nothing else to install), which takes a few seconds, so the first request starts the work in the background and answers `202` with `Retry-After`; it is done once for each file, worked out again if the size or the date of the file change, and one song at a time, so a NAS is not kept busy. A file that cannot be decoded answers `waveform_unavailable`, and that is remembered too. A friend's song has no shape yet. - **API documentation.** The full API is described in OpenAPI and browsable with Swagger UI, served by Sonda itself. The UI is embedded in the binary, so opening it contacts no other server. - **Listening history.** Clients report explicit `play`, `complete` and `skip` events. An event counts as a listen when it is `complete`, or when it was played for at least 20 seconds (`listening.min_listen_seconds`); the rule is applied when the history is read, so changing it re-reads the past. You can read your own history, with a `counted` flag on every event. It stays in your database and is never sent anywhere. - **Your taste.** `GET /me/taste` shows what Sonda has learned from your own listening: the artists you like and the kinds of music (tags) they have in common. A complete listen counts for more than a short one, a skip counts against the artist, and older listens weigh less (they halve every 90 days, `taste.half_life_days`; only the last ten half-lives are read). It is worked out on every request from data already in your database; nothing is sent to any provider. -- **Discover.** `GET /discover` suggests artists you may like, in your library or not, each with a score from 0 to 1 and the parts it is made of: how close it is to the artists you like (affinity), how close its tags are to yours (genre), what your listening says about it (preference) and a bonus for what you have not played (discovery). The weights are in `[scoring]` in the config. A part with no data is left out and the rest rescaled, and every answer says which seeds led to each suggestion. It starts from the artists you name with `?seed=`, or, only if you turned on `discover_from_listening`, from the artists you play most. It needs Last.fm to be switched on to ask for anything new: when Last.fm is off and a seed's similar artists are not stored yet, the answer says `disabled` (what was stored earlier is still used). See [External providers](#external-providers). A listener has one Discover at a time (a second one answers `429` with `Retry-After`), and at most three run at once on the server, so the providers are never lined up with work: one that waits more than 30 seconds for its turn answers `429` too, and one that stops waiting does none. -- **Artist descriptions.** `GET /artists/{id}/description` shows what Wikipedia says about an artist: a short text, with the page it comes from, a link to it and the licence (CC BY-SA 4.0), which you must show next to it. With the `wikipedia` provider and the `musicbrainz` one switched on (the link to Wikidata comes from MusicBrainz), Sonda follows ids and never a name: the artist's MusicBrainz id, then the Wikidata item that MusicBrainz links to it, then the Wikipedia page that Wikidata names in the first of your languages (`[wikipedia] languages`, the code of that Wikipedia, for example `["it", "en"]`) that has one. An artist without a MusicBrainz id has no description. It is kept for 30 days and removed with the provider's data. To read the descriptions of the whole library in one go, an admin starts a background job like a scan: `POST /library/descriptions` (follow it with `GET /library/descriptions`, stop it with `DELETE /library/descriptions`; `?refresh=true` reads every artist again). It only goes through artists that have a MusicBrainz id, never replaces a description by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). -- **Artist photos.** `GET /artists/{id}/image` serves a free picture of an artist from Wikimedia Commons, and `GET /artists/{id}/image/credit` says whose it is and under which licence, which you must show next to it. Only free licences are kept (public domain, CC0, CC BY, CC BY-SA): non-commercial, no-derivatives, fair-use and unknown ones are refused, because Sonda stores a copy (`?size=small` is 300 pixels, `large` 1000) in the data folder. With the `commons` provider and the `musicbrainz` one switched on, Sonda follows ids and never a name: the artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the picture Wikidata names for it, and Commons for its licence. Many artists will have none. Kept for 30 days (a week when there is none) and removed, files included, with the provider's data. To look for the pictures of the whole library in one go, an admin starts a background job like a scan: `POST /library/artist-images` (follow it with `GET /library/artist-images`, stop it with `DELETE /library/artist-images`; `?refresh=true` searches every artist again). It only goes through artists that have a MusicBrainz id, never replaces a picture by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). -- **Album covers.** `GET /albums/{id}/cover` serves the cover of an album from your own files: the picture embedded in them, or else a `cover`, `folder`, `front`, `album` or `albumart` image in the album's folder. It is built the first time it is asked for (a few at a time), scaled (`?size=small` is 300 pixels, `large` 1000) and kept in the data folder (`storage.data_dir`, you can delete it: covers are built again). A picture you replace in a folder is noticed within a week, or at once after a full rescan. When your files have no picture at all and an admin switched on the `coverartarchive` provider, the cover is asked from the Cover Art Archive by the MusicBrainz release group id that the tags of the album's files carry (`MUSICBRAINZ_RELEASEGROUPID`; run a full rescan to read it for music you already indexed); your own files always come first, and only that id leaves the server. An album whose files have no id is looked up in MusicBrainz by its artist and title when the `musicbrainz` provider is on too (see the list of what leaves your server). For an album split in `CD1`/`Disc 2` folders, the picture in the folder above is used. Nothing is sent anywhere unless you switched on the `coverartarchive` provider (see [External providers](#external-providers)). It also takes `?access_token=`, so an `` element can use it. To build the covers of the whole library in one go instead of one album at a time, an admin starts a background job like a scan: `POST /library/covers` (follow it with `GET /library/covers`, stop it with `DELETE /library/covers`; `?refresh=true` searches every album again; it never replaces a cover by "no cover" because a disk was not mounted, and keeps the covers that came from the archive if you switched it off). -- **Playlists.** `POST /playlists` makes a playlist (a name and an optional description) and `GET /playlists` lists yours, the one changed last first. `POST /playlists/{id}/tracks` adds 1 to 500 tracks at the end or at a `position`, `GET /playlists/{id}/tracks` lists them in order, `PATCH /playlists/{id}/tracks/{item}` moves one and `DELETE` takes one out; `PATCH /playlists/{id}` renames and `DELETE /playlists/{id}` deletes (the tracks stay in the library). A track can be in a playlist more than once, which is why each place has its own item id. A playlist is yours alone: someone else's answers `playlist_not_found`, as one that does not exist. A track whose files were deleted or moved away stays in the playlist and shows `available: false`; it plays again when the files come back and a scan finds them. A user can have 1000 playlists of up to 10000 tracks. Nothing is shared and nothing leaves your server. +- **Discover.** `GET /discover` suggests artists you may like, in your library or not, each with a score from 0 to 1 and the parts it is made of: how close it is to the artists you like (affinity), how close its tags are to yours (genre), what your listening says about it (preference) and a bonus for what you have not played (discovery). The weights are in `[scoring]` in the config. A part with no data is left out and the rest rescaled, and every answer says which seeds led to each suggestion. It starts from the artists you name with `?seed=`, or, only if you turned on `discover_from_listening`, from the artists you play most. It needs Last.fm to be turned on to ask for anything new: when Last.fm is off and a seed's similar artists are not stored yet, the answer says `disabled` (what was stored earlier is still used). See [External providers](#external-providers). A listener has one Discover at a time (a second one answers `429` with `Retry-After`), and at most three run at once on the server, so the providers are never lined up with work: one that waits more than 30 seconds for its turn answers `429` too, and one that stops waiting does none. +- **Artist descriptions.** `GET /artists/{id}/description` shows what Wikipedia says about an artist: a short text, with the page it comes from, a link to it and the licence (CC BY-SA 4.0), which you must show next to it. With the `wikipedia` provider and the `musicbrainz` one turned on (the link to Wikidata comes from MusicBrainz), Sonda follows ids and never a name: the artist's MusicBrainz id, then the Wikidata item that MusicBrainz links to it, then the Wikipedia page that Wikidata names in the first of your languages (`[wikipedia] languages`, the code of that Wikipedia, for example `["it", "en"]`) that has one. An artist without a MusicBrainz id has no description. It is kept for 30 days and removed with the provider's data. To read the descriptions of the whole library in one go, an admin starts a background job like a scan: `POST /library/descriptions` (follow it with `GET /library/descriptions`, stop it with `DELETE /library/descriptions`; `?refresh=true` reads every artist again). It only goes through artists that have a MusicBrainz id, never replaces a description by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). +- **Artist photos.** `GET /artists/{id}/image` serves a free picture of an artist from Wikimedia Commons, and `GET /artists/{id}/image/credit` says whose it is and under which licence, which you must show next to it. Only free licences are kept (public domain, CC0, CC BY, CC BY-SA): non-commercial, no-derivatives, fair-use and unknown ones are refused, because Sonda stores a copy (`?size=small` is 300 pixels, `large` 1000) in the data folder. With the `commons` provider and the `musicbrainz` one turned on, Sonda follows ids and never a name: the artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the picture Wikidata names for it, and Commons for its licence. Many artists will have none. Kept for 30 days (a week when there is none) and removed, files included, with the provider's data. To look for the pictures of the whole library in one go, an admin starts a background job like a scan: `POST /library/artist-images` (follow it with `GET /library/artist-images`, stop it with `DELETE /library/artist-images`; `?refresh=true` searches every artist again). It only goes through artists that have a MusicBrainz id, never replaces a picture by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). +- **Album covers.** `GET /albums/{id}/cover` serves the cover of an album from your own files: the picture embedded in them, or else a `cover`, `folder`, `front`, `album` or `albumart` image in the album's folder. It is built the first time it is asked for (a few at a time), scaled (`?size=small` is 300 pixels, `large` 1000) and kept in the data folder (`storage.data_dir`, you can delete it: covers are built again). A picture you replace in a folder is noticed within a week, or at once after a full rescan. When your files have no picture at all and an admin turned on the `coverartarchive` provider, the cover is asked from the Cover Art Archive by the MusicBrainz release group id that the tags of the album's files carry (`MUSICBRAINZ_RELEASEGROUPID`; run a full rescan to read it for music you already indexed); your own files always come first, and only that id leaves the server. An album whose files have no id is looked up in MusicBrainz by its artist and title when the `musicbrainz` provider is on too (see the list of what leaves your server). For an album split in `CD1`/`Disc 2` folders, the picture in the folder above is used. Nothing is sent anywhere unless you turned on the `coverartarchive` provider (see [External providers](#external-providers)). It also takes `?access_token=`, so an `` element can use it. To build the covers of the whole library in one go instead of one album at a time, an admin starts a background job like a scan: `POST /library/covers` (follow it with `GET /library/covers`, stop it with `DELETE /library/covers`; `?refresh=true` searches every album again; it never replaces a cover by "no cover" because a disk was not mounted, and keeps the covers that came from the archive if you switched it off). +- **Mixes.** `GET /mixes` lists playlists that Sonda makes for you, like the mixes of a streaming service, from what you listen to and from the genres of your music: one mix for each genre of your library that has at least 30 songs by 4 artists (the genres you play most first), and, once you have some listening history, *Your favourites* (the artists you like: the ones you play, and the ones you marked with a heart) and *Rediscover* (artists you like and have not played for a month); *New to you* is the songs you never played. `GET /mixes/{id}` gives a mix of up to 50 songs, at most 3 by one artist and never two of the same artist in a row. They are picked by weighted chance: the artists you play weigh more and songs you skipped lately or artists you avoid are left out. A mix stays the same for a day, and `?variant=2` gives another; `POST /mixes/{id}/playlist` keeps the one you are looking at as a playlist of your own. A mix is worked out when you ask for it, from your own history and the genre tags of your files: nothing is stored and nothing is sent to a provider. The id of a genre mix has the genre in it (`genre:hard rock`), so it is percent-encoded in the path. Each mix says which albums stand for it (`album_ids`: up to 4 albums that have a cover, by different artists, with most of the songs the mix draws from), for a picture made of their covers; the covers are the ones the library already has, so a mix has a picture once the covers of its albums were looked up (browsing the albums, or the covers job), and none is fetched for it. +- **ReplayGain.** The scan reads the ReplayGain tags of each file (`REPLAYGAIN_TRACK_GAIN`, `REPLAYGAIN_ALBUM_GAIN` and the two peaks), offline, and keeps them on the file; a tag that makes no sense (not a number, more than 60 dB) is left out. A song in the API has them as `replay_gain` (gains in decibels, peaks as a fraction of full scale), or `null` when its first playable file says nothing; the files indexed before this existed have none until a full rescan (`POST /library/scan?full=true`) reads their tags again. The server never changes a file or the sound: a client uses the numbers to play every song at a similar volume, as the web app does (see [Web app](#web-app)). +- **Smart playlists.** A smart playlist is a set of rules instead of a list of songs, worked out again every time you open it, so new songs and your listening change it by themselves. `POST /smart-playlists` keeps a name, the rules, an order (`random`, `most_played`, `least_played`, `recently_added` or `title`) and a limit (50 by default, 500 at most); `GET /smart-playlists/{id}/tracks` gives the songs now. Every rule that is set must hold: genres, artists and albums (is, or is not), file formats and a minimum bitrate (about the same file), added in the last N days (by the date of the file), never played, played or not played in the last N days, played at least N times (optionally counting only the plays of the last N days, which is what makes the songs in rotation lately and the top of the year), skipped or not skipped lately, only artists you like or without the ones you avoid, the length of the song, and the year it came out (`min_year` and `max_year`, both included: 1980 to 1989 is the eighties). The year comes from the tags of the file (the original date first, then the date); a song with no year never matches a year rule, and the files indexed before this existed have none until a full rescan (`POST /library/scan?full=true`) reads their tags again. `POST /smart-playlists/preview` tries rules without keeping them, and `POST /smart-playlists/{id}/playlist` keeps what you see as a normal playlist (a copy). Only the rules are stored; the songs come from the files and from your own history, nothing is sent to a provider, and a smart playlist is yours alone, and `GET /genres` lists the genres to choose from. A random order stays the same for a day, and `?variant=2` gives another. `GET /smart-playlists/{id}/covers` and `GET /smart-playlists/suggested/{key}/covers` say which albums stand for one (up to 4 that have a cover, in the order of the songs it gives now), for its picture; they are apart from the songs so that the list of smart playlists shows at once and each card fills in its cover when it arrives. Some are there without anybody making them: `GET /smart-playlists/suggested` lists a fixed set (`recently-added`, `on-rotation`, `top-year`, `most-played`, `never-played`, `forgotten`, `high-quality`, `long-tracks`), the same rules for everyone and worked out on your own library and history, nothing stored; `GET /smart-playlists/suggested/{key}/tracks` gives their songs and `POST /smart-playlists/suggested/{key}/playlist` keeps them as a playlist, and `on-rotation` is the songs you played most in the last 30 days and `top-year` the same over a year, so they change as you listen; a copy of their rules becomes a smart playlist of your own with `POST /smart-playlists`. A user can have 200. +- **Playlists.** `POST /playlists` makes a playlist (a name and an optional description) and `GET /playlists` lists yours, the one changed last first. `POST /playlists/{id}/tracks` adds 1 to 500 songs at the end or at a `position`, `GET /playlists/{id}/tracks` lists them in order, `PATCH /playlists/{id}/tracks/{item}` moves one and `DELETE` takes one out; `PATCH /playlists/{id}` renames and `DELETE /playlists/{id}` deletes (the songs stay in the library). A song can be in a playlist more than once, which is why each place has its own item id. A playlist is yours alone: someone else's answers `playlist_not_found`, as one that does not exist. A song whose files were deleted or moved away stays in the playlist and shows `available: false`; it plays again when the files come back and a scan finds them. Each playlist says which albums stand for it (`album_ids`: up to 4 albums that have a cover, in the order they first appear in it, each once), for a picture made of their covers; nothing is fetched for it. A user can have 1000 playlists of up to 10000 songs. Nothing is shared and nothing leaves your server. - **Wishlist and "not interested".** `PUT /wishlist/{artist}` puts an artist on your wishlist (`GET /wishlist` lists it, `DELETE` takes it off), and `POST /discover/{artist}/dismiss` hides an artist from your Discover for good (`DELETE` brings it back, `GET /discover/dismissed` lists them). An artist is wished or dismissed, never both, and both lists are yours alone. Discover marks the wished artists it shows (`wishlisted`) and never suggests a dismissed one. The wishlist is a list for you and sends nothing anywhere; Sonda downloads nothing, but an admin can send an artist that Discover showed to their own Lidarr (see [External providers](#external-providers)), which works for an artist that has a MusicBrainz id and that Sonda knows of from a list or from your files. An artist that only Last.fm named is kept while you have it marked (your lists still show it, but other users cannot see that it exists), and goes with the rest of Last.fm's data if an admin removes Last.fm's data. Removing MusicBrainz's data does not touch it. - **Your own settings.** `GET` and `PATCH /me/settings` hold choices that belong to you, not to the server. Everyone starts private: `discover_from_listening` is off, so this setting never sends your listening to a provider. When you turn it on, Discover may start from the artists you listen to most, and their names are sent to Last.fm (only if an admin switched it on). What Last.fm answers is kept in the shared database like any other lookup, so another local user can see, from `retrieved_at` of `GET /artists/{id}/similar`, that someone looked up an artist, but not who. +- **Importing from Spotify.** `POST /me/import/spotify` brings your listening history and your playlists from a Spotify data export into your own account. Ask Spotify for your data (Account, Privacy, "Extended streaming history" and "Account data") and send what it gives, as a multipart form with a `files` field for each file: the zip itself, or the JSON files in it (`Streaming_History_Audio_*.json`, the older `StreamingHistory_music_*.json` and `Playlist*.json`); a request may be up to 512 MB, and the rest of the export (the account, the searches, the library) is never opened. It all happens on your server and nothing is sent anywhere; the files are read from the request and not kept, and of each play only the song, the artist, the album, the time and how it ended are read, never the IP addresses, the country or the platform in the file. Of a play of a song you do not have that counts as listened, the name of the artist and the time are kept (and nothing else), so that Sonda can learn which artists you play together. A play is matched to a song of your library by artist and title (case, accents and punctuation do not matter, a `Remastered` note on either side does not, any credited artist will do, and a play of a compilation is matched by its album when Spotify says `Various Artists`). A match is recorded as a listen at the time of the play: `complete` when it ran to its end (or nine tenths were played), `skip` when Spotify says it was skipped or it lasted less than a listen, `play` otherwise; nothing is posted to Mastodon. Importing the same files again records nothing twice, and the same play in the extended and in the older history is one. A playlist becomes a playlist of yours with the songs that were found, in order; one whose name you already have is left alone, and one with none of its songs in your library is not made. The report counts what was added, what was already there and what is not in your library (and lists those artists, most played first), so you can see what is missing. One import of yours runs at a time. The report is kept: `GET /me/import/spotify` lists your past imports, newest first, with the numbers of each (never the files). The web app has an Import page for the form and for those numbers. +- **Suggestions from your own listening.** `GET /me/suggestions` learns which artists you play together: your history is split into sittings (plays less than 30 minutes apart) and two artists that turn up in the same sitting often are linked (a cosine over the sittings of each). It is a count over your own data, built on your server in a moment and again when plays are added, with no provider and nothing sent anywhere; it needs `discover_from_listening` in `PATCH /me/settings`. The model reads your listening events and the plays of artists you have no music of that an import kept (the artist and the time only). `library` are artists you have that go with what you play and that you did not play for two months; `missing` are artists you played at least three times and have no music of, the ones you play most that also go with what you play now first. Each says which of your artists it goes with. `POST /me/suggestions/wishlist` puts an artist you played on your wishlist by name (it is made without music and without a MusicBrainz id: Sonda never looks one up by name, so it cannot be sent to Lidarr from here). The web app has a "From your listening" tab in Discover. +- **Listening history by year and month.** `GET /listens/months` says how many listens that count you made in each calendar month, newest first, in the time zone you send as `tz` (UTC without it), and `GET /listens` takes `from` and `until` to read the listens of one of them; each listen now names its artists. The web app has a History page: a button for each year, the months of the year as bars, and the listens of the month a day at a time. +- **Statistics.** `GET /stats/library` says what the server holds: the songs that can be played (a song whose file is missing is not counted), their artists, albums, total length and the space their files take, the files by format, and the share of each genre (a song with several genres is split between them, so the shares add up to 1) and of each decade. `GET /stats/listening` says what you listened to, for all time or from `from` to `until` (dates with an offset, `until` excluded): the listens that count, the time listened, the songs and artists, the listens of each year, hour of the day and weekday in the time zone `tz`, your top ten artists and songs, the share of each genre, and how that share changed over time (`genre_timeline`: a column for each year, or for each month when all your listens are in one year, with the share of your five biggest genres, of the smaller ones and of what has no genre). Nobody else's listens are visible. The web app shows both on the Statistics page. +- **Requests to Lidarr.** `GET /lidarr/requests` (admin) lists the artists you sent to your Lidarr with `POST /artists/{id}/lidarr`, newest first, and says for each whether its music has arrived: `waiting` while there is none in the library, `arrived` once a scan found some (by the artist you sent or by an artist of the same name; the day it was first noticed is kept). Sending an artist again keeps one request with the first date; only what you sent from Sonda is listed. The web app has a Requests page (admins only), with the waiting and the arrived artists apart. +- **Sharing on Mastodon.** `PUT /me/mastodon` connects your own Mastodon account (the address of your instance and an access token that can only write posts); `GET` shows which account is connected and `DELETE` forgets it and the token. Nothing is connected by default. Once it is, each song of this server that counts as a listen is posted to your account as an unlisted status with the title, artists, album and `#NowPlaying`, and no link: at most one post a minute, never music of a friend. The token stays on the server and is never sent back. If the instance refuses the token on a post (it was revoked or expired), `GET /me/mastodon` says `problem: token_refused`, Settings shows it, and nothing is posted until you connect again with a new token. The address of the instance must be a domain name on https, and the server never connects to a private, loopback or link-local address for it, never follows a redirect and ignores proxy settings. - **Tags.** `GET /artists/{id}/tags` shows what Last.fm and MusicBrainz say kind of music an artist plays, each with its own weights, and the genres of the artist's own files, read offline. See [External providers](#external-providers). -- **Similar artists.** With Last.fm switched on, `GET /artists/{id}/similar` lists artists similar to an artist, and says which of them you already have. See [External providers](#external-providers). Without it, Sonda makes no outbound network requests at all. +- **Similar artists.** With Last.fm turned on, `GET /artists/{id}/similar` lists artists similar to an artist, and says which of them you already have. See [External providers](#external-providers). Without it, Sonda makes no outbound network requests at all. +- **Friends (federation).** Two Sonda servers can become friends, and the listeners of one can read what the other chose to share. `PUT /federation` (admin) turns federation on, which is off by default, and gives this server a name and a public address (`https`; plain `http` only on a network of your own); the server then makes its own Ed25519 key, kept in the database and never returned. `POST /federation/invites` makes an invite that works once and expires in 24 hours (only a hash of its code is kept). The other admin uses it with `POST /federation/friends`: that server is asked to accept this one, the key it answers with must be the one named in the invite, and the friend waits as `outgoing` until the first admin approves it with `POST /federation/friends/{id}/approve` (`POST /federation/friends/{id}/check` lets the waiting side find out). Either side can remove a friend with `DELETE /federation/friends/{id}`. Nothing is shared until an admin turns on `shared` for a library (`PATCH /libraries/{id}`). A friend's server then reads, with requests signed by its key (the method, the path and query, a date, a nonce and the hash of the body), the routes under `/peer`: the artists, albums and songs that have a file in a shared library, their covers once they are built, and the audio itself with `Range`. A friend never sees a path, a library id, a listen, a taste or a user, never makes this server build a cover or ask a provider, and has a few streams at a time and a number of KiB a second to share (`max_streams`, `max_kib_per_sec`); the admin sees the last requests it made (`GET /federation/requests`, the route and the size, never what was searched for). Sharing a library lets friends play it and, in practice, copy its files. A listener browses the music of a friend with `GET /friends` (the names of the friends whose friendship both admins agreed to, never an address or a key) and `GET /friends/{friend}/artists`, `albums` and `tracks` with `q`, `limit` and `offset`, and one item at a time: this server asks the friend with a signed request, checks the shape of the answer and passes it on, and nothing is kept; the ids are the friend's own. A friend that cannot be reached or no longer takes this server for a friend is told in words. The web app has a Friends section for it, shown only when there is a friend to browse. A listener plays it with `GET /friends/{friend}/tracks/{id}/stream` and sees its covers with `GET /friends/{friend}/albums/{id}/cover`, which accept the access token in the query like the local ones: this server asks the friend with a signed request, forwards `Range` and the conditional headers, and passes on only audio (or a JPEG for a cover) with `Content-Type`, `Content-Length`, `Content-Range`, `Accept-Ranges`, `Last-Modified` and `ETag`, so the friend sees this server and never the listener, and a friend cannot make this server serve anything else. A friend's song plays in the same player as the others but is never reported as a listen and does not feed taste. The admin area has a Federation section for all of it. + +- **Radio.** Stations that Sonda makes by itself from your music, with nothing to set up, and without end: *Everything*, one for each decade that has at least 30 songs by 4 artists (from the year in the tags), and the mixes you have (one for each genre, *Your favourites*, *Rediscover*, *New to you*), plus the radio of an artist from its page. `GET /radio` lists the stations and `GET /radio/{id}` gives up to 50 songs of one, **other songs every time it is asked**, picked like a mix (your taste and your hearts count, nothing skipped lately, at most 3 by one artist); `artist:` is the station of an artist (their songs weigh four times as much, with the songs of the two genres most of theirs have). The web app has a Radio page and a *Start radio* button on every artist, and its player asks for more songs by itself before the queue runs out. Apps that speak OpenSubsonic see each station as a playlist called *Radio ยท Rock*, with other songs every time it is opened (an app has no way to play a radio that never ends), but only when you turn on `radio_in_apps` (`PATCH /me/settings`; in Settings, *Show the radio stations in apps*): there are about two dozen of them, so it is off until you ask. Nothing is stored and nothing leaves the server. +- **Favourites.** `PUT /me/favorites/{kind}/{id}` marks a song, an album or an artist (`kind` is `tracks`, `albums` or `artists`) as a favourite and `DELETE` takes it back; `GET /me/favorites/{kind}` lists them, the newest first, and `GET /me/favorites/ids` gives every id for the hearts of a list. They are yours alone: never shown to a friend and never sent anywhere. Favourites also teach Sonda your taste: a favourite artist counts as much as three complete listens, a favourite album for its artist 1.5, a favourite song 1 (at most 6 for one artist), and the mixes of what you like give a favourite song more chance (and a song of a favourite album a little more). That feeds Discover, the mixes and the smart playlists that use your taste, and `GET /me/taste`. The web app has a heart on every song, album and artist (and in the player) and a Favourites page. Apps that speak OpenSubsonic see them as starred things. +- **Other apps.** Apps that speak OpenSubsonic (Symfonium, Feishin, Amperfy, Supersonic and others) can browse, search, play, show covers, use your playlists and report what you listened to, with a password of their own for each app (see [Other apps](#other-apps-opensubsonic)). ## What is not there yet -Radio queues, OpenSubsonic compatibility, transcoding, and the ListenBrainz integration. The web client is built into the binary (see [Web app](#web-app)), and its admin area covers everything the API can do for an admin: libraries and scanning, users, the external providers, the bulk jobs and the duplicates. +Radio queues, transcoding, ratings, and the ListenBrainz integration. The web client is built into the binary (see [Web app](#web-app)), and its admin area covers everything the API can do for an admin: libraries and scanning, users, the external providers and the bulk jobs. ## Requirements @@ -51,22 +69,24 @@ A plain `go build -o bin/sondad ./cmd/sondad` needs no Node and gives the API on The image holds `sondad` with the web app inside, and nothing else: it builds the app itself, so you need no Node on your machine. It runs as an ordinary user, keeps the database and the files Sonda makes under `/data`, listens on port 8080 inside the container, and does not start without `SONDA_JWT_SECRET`. +The image is published on the registry of git.kmalloc.org, so you do not have to build it: `git.kmalloc.org/squid/sonda:latest` is the last one, and `git.kmalloc.org/squid/sonda:0.3.0` (a version from [CHANGELOG.md](CHANGELOG.md)) is one you can stay on. It runs on amd64 and on arm64 (a Raspberry Pi, an ARM NAS): one tag gives each machine its own. Anyone can pull it, with no login. To build it yourself instead, run `docker build -t sonda .` and use `sonda` where the commands below say `git.kmalloc.org/squid/sonda:latest`. + ```bash -docker build -t sonda . +docker pull git.kmalloc.org/squid/sonda:latest (umask 077; echo "SONDA_JWT_SECRET=$(openssl rand -hex 32)" > sonda.env) # once: keep this file docker run -d --name sonda --restart unless-stopped --stop-timeout 15 \ -p 127.0.0.1:8080:8080 \ --env-file sonda.env \ -v sonda-data:/data \ -v "$HOME/Music":/music:ro \ - sonda + git.kmalloc.org/squid/sonda:latest ``` Mount your music read-only wherever you like (only if you want Sonda to delete duplicate files, mount it writable and set `SONDA_ALLOW_DELETE=true`: see **Duplicates**), then add the library through the API with the path *inside* the container, `/music` above (not the folder of your host): the [Demo](#demo) works the same way with `BASE=http://127.0.0.1:8080/api/v1`, except that step 4 creates the library with `"path":"/music"`. Keep `sonda.env` and pass it every time you run a new container, for example after an upgrade: a new secret signs out everybody. The container's health check asks the port in `SONDA_LISTEN`, so it works for `0.0.0.0`, `[::]` and a port alone, but not if you bind the server to one interface address. Published like this the server answers only on this machine; to serve your network, publish the port without `127.0.0.1:` and put a reverse proxy with https in front. The other settings are the environment variables of the [table below](#configure) (`SONDA_DB_DRIVER` and `SONDA_DB_DSN` for PostgreSQL); the image sets `SONDA_LISTEN`, `SONDA_DB_PATH` and `SONDA_DATA_DIR` for the volume. To keep the data in a folder of the host instead of a volume, run the container as the owner of that folder, or it cannot write there: add `--user "$(id -u):$(id -g)"` and `-v /path/to/sonda-data:/data`. Back up `sonda.db` (and treat the backup like a secret: it holds the API keys of the providers); the rest of `/data` is built from your music. -`compose.yaml` is the same as an example: put `SONDA_JWT_SECRET` and `MUSIC_DIR` (and `SONDA_PORT` if 8080 is taken) in a `.env` file next to it and run `docker compose up -d --build`. The image has a health check on `/api/v1/health`, which is logged at debug level when it succeeds. +`compose.yaml` is the same as an example: put `SONDA_JWT_SECRET` and `MUSIC_DIR` (and `SONDA_PORT` if 8080 is taken) in a `.env` file next to it and run `docker compose up -d --build` (to use the published image and not build one, swap the `build: .` and `image:` lines for the one in the comment of the file, and run `docker compose pull && docker compose up -d`). The image has a health check on `/api/v1/health`, which is logged at debug level when it succeeds. If you prefer a config file to environment variables (it can hold the secret and the other settings, so a Docker or Compose secret can provide it), put a `sonda.toml` in `/data` or pass `--config /run/secrets/sonda.toml`; the variables the image sets still win over the file. @@ -88,6 +108,7 @@ Precedence is: built-in defaults, then the file, then environment variables. An |---|---|---|---| | `server.listen` | `SONDA_LISTEN` | `127.0.0.1:8080` | Address to listen on. Localhost by default. | | `library.scan_workers` | | `4` | Files whose tags are read in parallel during a scan. | +| `library.auto_scan_minutes` | `SONDA_AUTO_SCAN_MINUTES` | `15` | How often, in minutes, Sonda looks at the folders of the libraries and scans the ones whose music changed (a song or a folder came, went or was renamed); `0` turns it off. Looking reads the names of the files and nothing else, so it is cheap for a NAS. A library is scanned at least once a day anyway, which finds a song that was tagged again where it stands. A folder that cannot be read (a NAS that is not mounted) is left alone. | | `library.allow_delete` | `SONDA_ALLOW_DELETE` | `false` | Lets an admin delete the extra copy of a song from the music folders (see **Duplicates**). It is the one thing that makes Sonda write there, so it needs the folders mounted writable. A value that is not `true` or `false` is an error. | | `database.driver` | `SONDA_DB_DRIVER` | `sqlite` | `sqlite` or `postgres`. | | `database.path` | `SONDA_DB_PATH` | `sonda.db` | SQLite file (driver `sqlite`). | @@ -116,7 +137,7 @@ Sonda creates and migrates its tables on start. The storage tests run against bo ## Demo -This takes you from an empty database to streaming a track. Run the server in one terminal and the commands in another. +This takes you from an empty database to streaming a song. Run the server in one terminal and the commands in another. **1. Get some music.** Point `MUSIC` at a folder of MP3 or FLAC files. The path must be absolute. If you have none, generate two 30-second test tones: @@ -169,7 +190,7 @@ curl -s "$BASE/albums" -H "$AUTH" | jq '.items[] | {id, title, artist: .artist.n curl -s "$BASE/tracks?q=tone" -H "$AUTH" | jq '.items[] | {id, title, artists: [.artists[].name], format}' ``` -**6. Stream a track:** +**6. Stream a song:** ```bash TRACK=$(curl -s "$BASE/tracks?limit=1" -H "$AUTH" | jq -r '.items[0].id') @@ -193,7 +214,7 @@ curl -s -X POST $BASE/listens -H "$AUTH" -d "{\"track_id\":$TRACK,\"event_type\" curl -s "$BASE/listens" -H "$AUTH" | jq '.items[] | {track: .track.title, event_type, duration_played_ms, counted}' ``` -The answer to each report says whether it counts. A reported play time far beyond the length of the track (more than twice its length plus 10 seconds) is rejected. +The answer to each report says whether it counts. A reported play time far beyond the length of the song (more than twice its length plus 10 seconds) is rejected. **8. Refresh the session and log out:** @@ -221,7 +242,7 @@ curl -s -X POST $BASE/users -H "$AUTH" -d '{"email":"bob@example.org","password" Sonda can ask Last.fm, MusicBrainz, Wikipedia, Wikimedia Commons and the Cover Art Archive about artists and albums, and can tell your own Lidarr which artist to add. This is **off by default**: until an admin turns a provider on, nothing leaves your server. Today Last.fm is used for similar artists and tags, MusicBrainz for tags and for the link to Wikidata, Wikipedia (with Wikidata) for descriptions of artists, Wikimedia Commons (with Wikidata) for free photos of artists, and the Cover Art Archive for the cover of an album that has none in your files. - **Last.fm** needs an API key. Create one for your own use at `last.fm/api` (it needs a Last.fm account). No user name or password is involved and the "shared secret" is not needed. Last.fm data is for non-commercial use only, and you should credit Last.fm when you show it. -- **MusicBrainz** needs no key, but it asks every client to identify itself with a contact (a URL or an e-mail address) in the `User-Agent`. By default Sonda uses the address of the project, so no personal address is sent unless you choose to. +- **MusicBrainz** needs no key, but it asks every client to identify itself with a contact (a URL or an email address) in the `User-Agent`. By default Sonda uses the address of the project, so no personal address is sent unless you choose to. Set them up with `PUT /api/v1/providers/{name}` (admin), for example: @@ -256,20 +277,53 @@ The address can have a path if Lidarr runs under a URL base, and it cannot carry The key is stored in the database, in clear text, and is never returned by the API or written to the log: the list only says `has_api_key`. Because the database holds it, treat database backups like secrets. Removing the key turns the provider off, and each provider describes in the list exactly what it sends. -**What leaves your server, when a provider is on:** to Last.fm, the artist name (or MusicBrainz id) being looked up, your API key, your IP address and the `User-Agent`; to MusicBrainz, the MusicBrainz id of the artist (and, only for the covers of albums that have no id, the artist's name and the album's title: see **Covers**), your IP address and a `User-Agent` that carries your contact. Never your users, your listens or file paths. Requests only happen when someone asks for them, never as a bulk pass over your library (the exceptions are the three jobs that an admin starts on purpose, each of them one request that goes through the whole library, one item after the other, spaced out, and each stops when its providers keep failing: `POST /library/covers`, with the Cover Art Archive on, sends the release group id of every album that has no picture in its files (and, for the albums whose files have no id, with MusicBrainz on, the artist and the title, one request a second); `POST /library/artist-images`, with Commons and MusicBrainz on, sends the MusicBrainz id of every artist that has one, to MusicBrainz, Wikidata and Commons; `POST /library/descriptions`, with Wikipedia and MusicBrainz on, sends it to MusicBrainz, Wikidata and Wikipedia), and they are spaced out, retried with backoff, and stopped by a circuit breaker if a provider is down. **Descriptions** (the Wikipedia provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata and the title of the page to Wikipedia, with your IP address and a `User-Agent` that carries a contact (the one set for each provider; Wikimedia refuses requests without one), and never a name. **Photos** (the Commons provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata, the name of the picture to Commons and the address of a copy to Wikimedia, with your IP address and a `User-Agent` with a contact, and never a name. **Covers** (the Cover Art Archive provider) send the MusicBrainz release group id of an album, read from the tags of its own files, and only when the files have no picture. When the files have no id either and the MusicBrainz provider is on too, the name of the album's artist and its title go to MusicBrainz, which is asked for the release group: a result is taken only when MusicBrainz scores it 95 or more and its title and artist are the ones asked for (compared without case, accents or punctuation), and anything doubtful is no cover. A title that ends with a note about the disc or the edition (`(disc 2)`, `(bonus disc)`, `(Deluxe Edition)`) is asked as it is first and, if MusicBrainz is not sure, once more without the note, because a release group is named after the album and not after a disc. The id that comes back is used for that request and is not stored: it is not what your files say, and it is never used to read an artist or to send anything to Lidarr; the redirects of the archive are followed only to archive.org, over https. Its covers have no licence of their own: Sonda keeps a copy for your library and never treats it as free. **Discover** asks Last.fm for the similar artists of each seed (the seed's name or MusicBrainz id) and, for the best suggestions that have no tags yet, for the tags of that suggestion (its name); never MusicBrainz. Seeds you name are sent when you ask. Seeds taken from your listening, which are the names of the artists you play most, are sent only if you turned on `discover_from_listening` yourself; otherwise Discover refuses to run without a `seed`. **Lidarr** (your own server, at the address an admin set) gets the MusicBrainz id of an artist an admin sends, or of an artist someone opens `GET /artists/{id}/lidarr` for, and the API key, nothing else; asking it what it offers and testing the key send only the key. Never a name, your users, your listens or your files. What Lidarr then does is up to its own indexers and download clients. Besides the three jobs, the other exception to "only when someone asks" is `POST /providers/{name}/test`, which an admin sends to check a key (or, for Lidarr, the address and the key); it sends nothing but the key. +**Claude (Anthropic)** is the one provider that names things: with it on, a playlist, a mix or a smart playlist can get a name and a line of description that fit its songs. An admin turns it on with an Anthropic API key (`PUT /providers/anthropic`, the key is write-only, and `POST /providers/anthropic/test` reads one model, which costs nothing and sends no text). `POST /playlists/{id}/suggest-name`, `POST /mixes/{id}/suggest-name` and `POST /smart-playlists/{id}/suggest-name` answer with a name, a description and a `source`. They are a pipeline: the facts about the list (up to 5 genres, up to 8 artist names, how many songs, for a smart playlist a few words about its rules, and the language) go to a namer. With the provider off, or when Claude fails or declines, the local namer builds a name from the genres and the artists on this server, and nothing leaves it. When Claude answers, the name is kept (under a hash of the facts, which holds no artist name), so the same facts are not asked, or paid, twice. A suggestion changes nothing: the client decides what to do with it, and the web app offers it where a playlist is renamed. With the provider on, names are also made by themselves: a mix, a smart playlist or a suggested smart playlist that is kept as a playlist with `auto_name` (the web app always sends it) gets Claude's name and line of description, with `name` as the fallback when Claude is off, fails or declines; a smart playlist can be made with no name at all and Claude names it from the songs its rules give (without Claude that is `invalid_input`); and `GET /naming` says whether names are automatic, so that the editor does not ask for one. Every question to Claude is counted (a made name, a name taken from the kept ones, a failure with its reason, and the tokens that were billed, also for a failure that was billed) and nothing of what was sent is kept; `GET /providers/anthropic/usage?days=30` (admin) gives the totals, a row for each day and an **estimate** of the cost in dollars, worked out from the tokens and the price of the model (the invoice of Anthropic is the truth), and the Costs section of the admin area shows it for 7, 30 or 90 days; records older than a year are removed. The model is `claude-opus-5-5`, at low effort; the key is never taken from the environment of the server, and a redirect is never followed. + +**What leaves your server, when a provider is on:** to Anthropic, only when someone asks for a name, up to 5 genres and 8 artist names of that list, how many songs it has, a few words about the rules of a smart playlist, the language, your API key, your IP address and a User-Agent (never your users, your listens, song titles or file paths); to Last.fm, the artist name (or MusicBrainz id) being looked up, your API key, your IP address and the `User-Agent`; to MusicBrainz, the MusicBrainz id of the artist (and, only for the covers of albums that have no id, the artist's name and the album's title: see **Covers**), your IP address and a `User-Agent` that carries your contact. Never your users, your listens or file paths. Requests only happen when someone asks for them, never as a bulk pass over your library (the exceptions are the three jobs that an admin starts on purpose, each of them one request that goes through the whole library, one item after the other, spaced out, and each stops when its providers keep failing: `POST /library/covers`, with the Cover Art Archive on, sends the release group id of every album that has no picture in its files (and, for the albums whose files have no id, with MusicBrainz on, the artist and the title, one request a second); `POST /library/artist-images`, with Commons and MusicBrainz on, sends the MusicBrainz id of every artist that has one, to MusicBrainz, Wikidata and Commons; `POST /library/descriptions`, with Wikipedia and MusicBrainz on, sends it to MusicBrainz, Wikidata and Wikipedia), and they are spaced out, retried with backoff, and stopped by a circuit breaker if a provider is down. When MusicBrainz answers that an id was merged into another, Sonda follows that one redirect, and only to an artist or a release group of musicbrainz.org over https; the new id is used for that request and is not stored, and any other redirect is refused. **Descriptions** (the Wikipedia provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata and the title of the page to Wikipedia, with your IP address and a `User-Agent` that carries a contact (the one set for each provider; Wikimedia refuses requests without one), and never a name. **Photos** (the Commons provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata, the name of the picture to Commons and the address of a copy to Wikimedia, with your IP address and a `User-Agent` with a contact, and never a name. **Covers** (the Cover Art Archive provider) send the MusicBrainz release group id of an album, read from the tags of its own files, and only when the files have no picture. When the files have no id either and the MusicBrainz provider is on too, the name of the album's artist and its title go to MusicBrainz, which is asked for the release group: a result is taken only when MusicBrainz scores it 95 or more and its title and artist are the ones asked for (compared without case, accents or punctuation), and anything doubtful is no cover. A title that ends with a note about the disc or the edition (`(disc 2)`, `(bonus disc)`, `(Deluxe Edition)`) is asked as it is first and, if MusicBrainz is not sure, once more without the note, because a release group is named after the album and not after a disc. The id that comes back is used for that request and is not stored: it is not what your files say, and it is never used to read an artist or to send anything to Lidarr; the redirects of the archive are followed only to archive.org, over https. Its covers have no licence of their own: Sonda keeps a copy for your library and never treats it as free. **Discover** asks Last.fm for the similar artists of each seed (the seed's name or MusicBrainz id) and, for the best suggestions that have no tags yet, for the tags of that suggestion (its name); never MusicBrainz. Seeds you name are sent when you ask. Seeds taken from your listening, which are the names of the artists you play most, are sent only if you turned on `discover_from_listening` yourself; otherwise Discover refuses to run without a `seed`. **Lidarr** (your own server, at the address an admin set) gets the MusicBrainz id of an artist an admin sends, or of an artist someone opens `GET /artists/{id}/lidarr` for, and the API key, nothing else; asking it what it offers and testing the key send only the key. Never a name, your users, your listens or your files. What Lidarr then does is up to its own indexers and download clients. Besides the three jobs, the other exception to "only when someone asks" is `POST /providers/{name}/test`, which an admin sends to check a key (or, for Lidarr, the address and the key); it sends nothing but the key. **Similar artists.** `GET /api/v1/artists/{id}/similar` asks Last.fm for the artists similar to an artist (one of yours, or one Last.fm named earlier) and keeps the answer in your database: 7 days, or 1 day when Last.fm does not know the artist. Until then a second look costs nothing and sends nothing, and if Last.fm is down you still get the last answer, marked `stale`. If the provider is off the answer says `disabled` instead of failing. Each similar artist says whether you have it (`in_library`); the ones you do not have are stored as artists without files. They can be opened with `GET /artists/{id}` but are never part of your artist lists, and if you later add their music, the scan finds them there. Last.fm's `score` is relative to each answer (the best one is usually 1), so compare artists only inside one answer. **Tags.** `GET /api/v1/artists/{id}/tags` asks each provider that is on, at the same time, and keeps the answers with the same rules (7 days, 1 for an unknown artist, old data if a provider fails). Each provider has its own entry with its own weights: Last.fm's are relative to the artist (1 to 100), MusicBrainz's are counts of votes, and they are never mixed. MusicBrainz also marks the tags it lists as genres. Its tags and genres are CC BY-NC-SA, so show the `attribution` that comes with them. -**Genres from your own files.** The scan also reads the genre tag of each file (several genres separated by `;`, at most five per file) and `GET /artists/{id}/tags` adds a `files` entry: the genres of the playable tracks the artist is credited on, each weighted by how many tracks have it. It needs no provider, asks nobody, and is always there. +**Genres from your own files.** The scan also reads the genre tag of each file (several genres separated by `;`, at most five per file) and `GET /artists/{id}/tags` adds a `files` entry: the genres of the playable songs the artist is credited on, each weighted by how many songs have it. It needs no provider, asks nobody, and is always there. **MusicBrainz ids come from your files, never from a guess.** If your files carry MusicBrainz artist ids in their tags (taggers like Picard write them), the scan reads them, offline, and keeps them on your artists, but only when each id can be paired with its artist for certain. MusicBrainz is asked only about artists whose id is known; for the others its entry says `no_id`. Sonda never searches MusicBrainz for an artist by name, because a search returns look-alikes and picking one would be a guess; the one search there is is for the cover of an album that has no id (see **Covers** below), and it takes a result only when it is sure. A normal scan reads a file's tags when the file is new or changed. To pick up ids and genres for music that is already indexed, scan with `full=true` (`POST /library/scan?full=true`, or `POST /libraries/{id}/scan?full=true`): every file is read again, keeps its identity, and your listening history is untouched. -**Taking it back.** `DELETE /api/v1/providers/{name}/data` (admin) removes everything a provider gave: its similar artists, its tags and the records of when it was asked, and then the artists that only that data named and that you have no files for. Last.fm's terms ask for this when an agreement ends. Your own artists, music and listens stay, and the provider is not switched off. An artist that only that provider's lists named goes too, even if someone had it on their wishlist or dismissed. An artist that no list names any more is also removed when the list that named it is replaced, so they do not pile up. +**Taking it back.** `DELETE /api/v1/providers/{name}/data` (admin) removes everything a provider gave: its similar artists, its tags and the records of when it was asked, and then the artists that only that data named and that you have no files for. Last.fm's terms ask for this when an agreement ends. Your own artists, music and listens stay, and the provider is not turned off. An artist that only that provider's lists named goes too, even if someone had it on their wishlist or dismissed. An artist that no list names any more is also removed when the list that named it is replaced, so they do not pile up. The artist is sent to Last.fm by its MusicBrainz id when your files gave one (if Last.fm does not know the id, by name), otherwise by its name exactly as it is written in your library, and Sonda never lets Last.fm correct it into another artist. Names and ids from Last.fm are never written onto your own artists. +## Other apps (OpenSubsonic) + +Sonda speaks [OpenSubsonic](https://opensubsonic.github.io/docs/) (Subsonic API 1.16.1 and its open extensions) at `/rest`, so the apps that speak it can play your music: Symfonium, Feishin, Amperfy, Supersonic, Tempus, Airsonic Refix and others. + +**Connect an app.** + +1. Open Settings, **Apps and devices**, give the app a name and press **Make password**. The password is shown once: copy it into the app. +2. In the app, give the address of your server, your email as the user name, and that password. An app that asks for an API key takes the same password as the key. + +The API has the same: `POST /me/devices` makes a password (and returns it once), `GET /me/devices` lists them with when each was last used, and `DELETE /me/devices/{id}` takes one back. You can have 20. + +A device password opens your music in that app and nothing else: it cannot sign in to Sonda, change your account or make other passwords, and the password of your account does not work in apps. Make one for each app, and take it back in Settings when you lose the phone: that app stops working at once. The passwords are kept sealed with a key derived from `auth.jwt_secret` (Subsonic's token login needs the server to know the password itself, so it cannot be kept as a hash), so changing that secret makes you make them again. An app sends the password in the address of each call: give Sonda a server address on `https`, and do not put these addresses in a log you share. + +**What an app can do.** Calls are answered in JSON (`f=json`) or XML, and a web app on another address can read the answers (`Access-Control-Allow-Origin: *`; what opens a call is the password in it, never a cookie). + +| Calls | What they do | +| --- | --- | +| `ping`, `getLicense`, `getOpenSubsonicExtensions`, `tokenInfo` | The server says who it is. The extensions need no login. | +| `getMusicFolders`, `getArtists`, `getArtist`, `getAlbum`, `getSong`, `getGenres` | Browse your libraries. | +| `getAlbumList2` | Random, newest, frequent, recent, by name, by artist, by year, by genre. | +| `getRandomSongs`, `getSongsByGenre`, `search3` | Find songs, albums and artists. | +| `stream`, `download`, `getCoverArt` | The original file (with Range), and covers of albums, artists and songs. | +| `getPlaylists`, `getPlaylist`, `createPlaylist`, `updatePlaylist`, `deletePlaylist` | Your playlists, the same ones as in the web app. After them, when you turned on *Show the radio stations in apps*, each station of the radio is a playlist (`rd-โ€ฆ`, *Radio ยท Rock*) with other songs every time it is opened; they cannot be changed or deleted. | +| `scrobble` | A listen is recorded like one made in the web app (and shared on Mastodon if you connected it). A "now playing" message is not recorded. | +| `star`, `unstar`, `getStarred2` | Your favourites (see **Favourites**): songs, albums and artists, with the `starred` date on each, and `getAlbumList2` of type `starred`. | +| `setRating`, `getNowPlaying` | Answered with nothing: there are no ratings, and what you play is nobody else's business. | +| `getUser` | Who the app is logged in as. It is always you, with the roles of an app: play, download, playlists, covers, and never admin. | +| `getArtistInfo`, `getArtistInfo2`, `getAlbumInfo`, `getAlbumInfo2`, `getTopSongs`, `getSimilarSongs`, `getSimilarSongs2`, `getLyrics`, `getLyricsBySongId`, `getInternetRadioStations`, `getScanStatus`, `startScan`, `getPlayQueue`, `savePlayQueue`, `getPlayQueueByIndex`, `savePlayQueueByIndex` | Answered with nothing (a saved queue is not kept), so that apps that ask them right after the login, like Feishin, work. A call Sonda does not know is an error `0` and shows in the server log. | + +Sonda does not transcode: an app always gets the original file, whatever format or bit rate it asks for. The music of friends (federation) is not shown to apps. + ## The API at a glance Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"error":{"code":"...","message":"..."}}`. @@ -279,10 +333,11 @@ Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"er | Who | Method and path | What it does | |---|---|---| | anyone | `GET /health` | Liveness check. | +| signed in | `GET /version` | The version of Sonda and, when the binary knows it, the commit it was built from. The web app shows it under the menu and in Settings. | | anyone | `POST /auth/register` | Create the first user (admin). Closed afterwards. | | anyone | `POST /auth/login`, `/auth/refresh`, `/auth/logout` | Open, renew and end a session. | | signed in | `GET /auth/me`, `POST /auth/password` | Your account; change your password (signs you out everywhere). | -| signed in | `GET /artists`, `/albums`, `/tracks` and `/โ€ฆ/{id}` | Browse and search. Query: `q`, `limit` (default 50, max 200), `offset`, `library_id`, `artist_id`, `album_id`. | +| signed in | `GET /artists`, `/albums`, `/tracks` and `/โ€ฆ/{id}` | Browse and search. Query: `q`, `limit` (default 50, max 200), `offset`, `library_id`, `artist_id`, `album_id`, and for `/albums` `sort=recent` (the albums added last first, by the date of their newest file; the default is by title). | | signed in | `GET /artists/{id}/tags` | What Last.fm and MusicBrainz say about the artist's kind of music, from each provider that is on. | | signed in | `GET`, `HEAD /artists/{id}/image`, `GET /artists/{id}/image/credit` | A free picture of the artist from Wikimedia Commons (query `size=small\|large`, also `?access_token=`) and its credit. | | signed in | `GET /artists/{id}/description` | What Wikipedia says about the artist, with its page and licence, from the Wikipedia provider when an admin switched it on. | @@ -290,10 +345,18 @@ Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"er | signed in | `GET`, `HEAD /tracks/{id}/stream` | Stream the original file. Also accepts `?access_token=`. | | signed in | `POST /listens` | Record a `play`, `complete` or `skip`; the answer says whether it counts as a listen. | | signed in | `GET /listens` | Your own history, newest first, with `counted` per event. Query: `limit`, `offset`, `counted=true`. | +| signed in | `GET /stats/library`, `GET /stats/listening` | What the library holds; what you listened to, for all time or a period. Query of the second: `from`, `until`, `tz`. | | signed in | `GET /discover` | Artists you may like, with a score and its parts. Query: `seed` (an artist id, up to 5 times), `limit` (default 20, max 30), `in_library=true\|false`. | | signed in | `GET`, `HEAD /albums/{id}/cover` | The cover of an album, a JPEG made from your own files. Query: `size=small\|large`. Also accepts `?access_token=`. | +| signed in | `GET /mixes`, `GET /mixes/{id}`, `POST /mixes/{id}/playlist` | Your mixes: list them, read one (query `variant`), keep one as a playlist. Made for you alone, from your history and your genres; nothing is stored. | +| signed in | `GET /genres` | The genres of the library with their number of songs, the biggest first, spellings merged: what the rules of a smart playlist choose from. | +| signed in | `GET /smart-playlists`, `POST /smart-playlists`, `GET`, `PATCH`, `DELETE /smart-playlists/{id}` | Your smart playlists: list them (query `limit`, `offset`), make one from rules, read, change or delete one. Never anybody else's. | +| signed in | `GET /naming` | Whether names are made by themselves (`auto`): true when the `anthropic` provider is on. | +| signed in | `POST /playlists/{id}/suggest-name`, `POST /mixes/{id}/suggest-name`, `POST /smart-playlists/{id}/suggest-name` | A name and a line of description for a list of songs (body: `lang`, `variant`): Claude's when an admin turned the `anthropic` provider on, a local one otherwise (`source`). | +| signed in | `GET /smart-playlists/suggested`, `GET /smart-playlists/suggested/{key}/tracks`, `POST /smart-playlists/suggested/{key}/playlist` | The smart playlists Sonda makes for everybody (a fixed set), their songs for you now (query `variant`), and one kept as a normal playlist. | +| signed in | `GET /smart-playlists/{id}/tracks`, `POST /smart-playlists/preview`, `POST /smart-playlists/{id}/playlist` | The songs the rules give now (query `variant`), a preview of rules that are not kept, and a smart playlist kept as a normal playlist. | | signed in | `GET /playlists`, `POST /playlists`, `GET`, `PATCH`, `DELETE /playlists/{id}` | Your playlists: list them (query `limit`, `offset`), make one, read, rename or delete one. Never anybody else's. | -| signed in | `GET /playlists/{id}/tracks`, `POST /playlists/{id}/tracks`, `PATCH`, `DELETE /playlists/{id}/tracks/{item}` | The tracks of a playlist: list them in order, add some (at the end or at a `position`), move or remove an item. | +| signed in | `GET /playlists/{id}/tracks`, `POST /playlists/{id}/tracks`, `PATCH`, `DELETE /playlists/{id}/tracks/{item}` | The songs of a playlist: list them in order, add some (at the end or at a `position`), move or remove an item. | | signed in | `GET /wishlist`, `PUT /wishlist/{id}`, `DELETE /wishlist/{id}` | Your wishlist: list it (query `limit`, `offset`), add an artist, take one off. | | signed in | `GET /discover/dismissed`, `POST /discover/{id}/dismiss`, `DELETE /discover/{id}/dismiss` | The artists you are not interested in: list them, dismiss one, bring it back. | | signed in | `GET /me/taste` | The artists and tags Sonda learned you like, from your own listening. Query: `limit` (default 20, max 50). | @@ -305,6 +368,7 @@ Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"er | admin | `POST /library/scan`, `POST /libraries/{id}/scan` | Scan everything, or one library. `?full=true` reads every file again. | | admin | `GET /library/duplicates`; `POST`, `GET`, `DELETE /library/duplicates/check`; `POST /library/duplicates/clean` | Find the songs that are in more than one file, read their files to tell the identical ones, and delete the extra copies. See **Duplicates**. | | admin | `GET /providers`, `PUT /providers/{name}`, `POST /providers/{name}/test`, `DELETE /providers/{name}/data` | See, set up and check the providers (Last.fm, MusicBrainz, Wikipedia, Commons, the Cover Art Archive, Lidarr), and remove what they gave. The API key is write-only. | +| admin | `GET /providers/anthropic/usage` | What the Claude provider costs over the last days (query `days`, 1 to 365): totals, a row for each day with calls and an estimate in dollars. | | admin | `GET /providers/lidarr/options` | The profiles and root folders that Lidarr offers, to choose the ones artists are sent with. | | admin | `POST /artists/{id}/lidarr` | Send an artist to your own Lidarr, by its MusicBrainz id, which then looks for the music. Never by name, never by itself. | | signed in | `GET /artists/{id}/lidarr` | Whether Lidarr has the artist and monitors it. | @@ -330,7 +394,7 @@ Back up the database and `sonda.toml`. Your music stays wherever you keep it. ## Web app -A web client lives in `web/` (React, Vite and shadcn/ui). It talks only to the Sonda server: no CDN, no external fonts, no analytics. With the app built into the binary (`make build`, or the Docker image) `sondad` serves it: open the address of the server, for example `http://127.0.0.1:8080`, and sign in. The page carries a strict Content-Security-Policy (only resources of the server itself, no inline scripts), cannot be framed and sends no referrer, and a link inside the app (`/admin/users`) works when you reload it. +A web client lives in `web/` (React, Vite and shadcn/ui). It talks only to the Sonda server: no CDN, no external fonts, no analytics. With the app built into the binary (`make build`, or the Docker image) `sondad` serves it: open the address of the server, for example `http://127.0.0.1:8080`, and sign in. The page carries a strict Content-Security-Policy (only resources of the server itself, no inline scripts), cannot be framed and sends no referrer, and a link inside the app (`/admin/users`) works when you reload it. The player draws a live picture of what is playing, in the browser, from the sound itself (Web Audio). Settings has a choice for it, kept on the device: a spectrogram (the last seconds, low notes at the bottom, the default), the classic bars with falling caps, an oscilloscope, Milkdrop, or nothing. The bars, the oscilloscope and the spectrogram take the colour of this server or, for a friend's song, the colour of the friend. Milkdrop fills the background of the full-screen player with the pictures Winamp made famous, drawn on the device with [butterchurn](https://github.com/jberg/butterchurn) (MIT) and its presets, which are part of the app: nothing is fetched from outside. It is loaded only when chosen, and anyone who asked the system for less motion gets the spectrogram instead, because the pictures flash. The player listens to the sound and changes nothing of it, and draws only while something plays. On a phone the player is one row at the foot of the page (and is not there while nothing plays); a tap or a swipe up opens the full-screen player, a swipe down or the Back gesture closes it, and the lock screen shows the song with its seek bar and follows play and pause. The queue is kept in the browser, so a page that is reloaded, or a tab that Android throws away, comes back paused on the same song at the same second. To work on the interface itself, run the Vite dev server next to a running `sondad` (set `SONDA_URL` to where it listens: the default is `http://127.0.0.1:8080`): @@ -340,10 +404,42 @@ npm install SONDA_URL=http://127.0.0.1:8080 npm run dev # open http://localhost:5173, /api is proxied to Sonda ``` -What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys, a volume slider with mute (remembered in the browser) and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Playlists have a page of their own: make one, put a song in it with the button next to any song (or make a new playlist right there), play it, rename it, delete it, move a song up or down and take one out; a song whose files are gone is shown as not available and skipped when the playlist plays. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it, and to manage accounts: add one by email, give or take away the admin role, reset a password (which ends that person's sessions) and delete an account. The last admin cannot be changed, and your own account has no buttons there. Providers have a card each that says what the provider is sent: turning one on asks first, the API key is write-only (never shown, and gone from the page once saved), and there are a test of the key, the contact or address, removing what a provider gave, and, for Lidarr, the profiles and root folder to use when an artist is added. The bulk jobs (album covers, artist pictures, artist descriptions) each have a card that says what the job sends and to whom: starting one asks first and says it goes through the whole library, reading everything again is a separate choice, a job whose providers are off cannot be started, and the page follows its progress and can stop it. The Duplicates section lists the songs that are in more than one file, with a row for each copy (album, format, bitrate, size) and whether the files are byte for byte the same once the check has read them. The copy Sonda would keep is already chosen and you can choose another; "Delete the others" (one song) and "Clean all suggestions" (every song shown) ask first, list every file that stays and every file that goes with the space freed, and go song by song, stopping at the first refusal and saying why. With `library.allow_delete` off, which is the default, everything is shown and the delete buttons are disabled, and the page says how to turn it on. There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings. +What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys, a volume slider with mute (remembered in the browser), the volume evened out by the ReplayGain tags of your files (Settings: song by song, album by album or off; a quiet song is raised, but never so much that its loudest sample would clip, and one with no tags plays as it is) and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Playlists have a page of their own, shown as cards with a collage of their covers (as the mixes and the smart playlists have): make one, put a song in it with the button next to any song (or make a new playlist right there), play it, rename it, delete it, move a song up or down and take one out; a song whose files are gone is shown as not available and skipped when the playlist plays. Mixes have a page too: a card for each mix, with a collage of the covers of its albums (the ones from your listening first, then one for each genre), and on a mix its songs, Play, Shuffle again (another set of songs) and Save as playlist, which keeps what you see as a playlist of your own. Where a playlist is renamed, and where a smart playlist is edited, a Suggest a name button asks for a name. Smart playlists have their own page too, which begins with the ones Sonda makes for you (ready to play, to keep as a playlist, or to make your own with a click), and then yours: make one with an editor of the rules (genres, format and bitrate, when it was added, your listening and taste, the length, the order and the limit) and a live preview of the songs it gives, play it, shuffle it again (for the random order), keep what you see as a playlist, edit it or delete it; rules by artist and album are in the API only, and an edit keeps them. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. A suggested artist you do not have shows a letter, not a photo, because a photo is a lookup at the providers: a "Show photo" button on the card asks for that one artist when you press it (the card says it goes to Wikimedia Commons by MusicBrainz id, and says so when there is no free photo). An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it, and to manage accounts: add one by email, give or take away the admin role, reset a password (which ends that person's sessions) and delete an account. The last admin cannot be changed, and your own account has no buttons there. Providers have a card each that says what the provider is sent: turning one on asks first, the API key is write-only (never shown, and gone from the page once saved), and there are a test of the key, the contact or address, removing what a provider gave, and, for Lidarr, the profiles and root folder to use when an artist is added. The bulk jobs (album covers, artist pictures, artist descriptions) each have a card that says what the job sends and to whom: starting one asks first and says it goes through the whole library, reading everything again is a separate choice, a job whose providers are off cannot be started, and the page follows its progress and can stop it. The web app is a PWA: it has a manifest and a service worker, so a browser can install it (Settings has an Install button where the browser lets a page ask) and it opens at once from its own icon, and shows its own page when the server cannot be reached. The worker keeps only the files of the app (the page, its scripts and styles, the manifest and the icons) and never anything from `/api/`: the catalog, the covers, the songs and what a person did are private and are not kept. A new version waits until you press Reload, so nothing changes under a song. It needs https, or localhost. The menu on the left folds to a narrow bar with the icons of the pages (a button in it, remembered in the browser; on a phone it stays a row of links). There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings. More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). `npm run build` writes to `internal/webui/dist` (git-ignored), not to `web/dist`. +## Android app + +`android/` holds a small Android app: a Capacitor shell that opens a Sonda server in a WebView, with its own icon and no browser bars. It is for a phone whose browser cannot install a PWA. It has no copy of the web app: it shows the one of your server, so a new version of the server is a new version of the app. + +- The first time it asks for the address of the server (`music.example.org`, or `http://192.168.1.5:8080`) and remembers it. Settings has Change server, shown only inside the app. +- It trusts the certificate authorities installed on the phone, so a server behind a private CA opens once the CA is installed (Settings, Security, Install a certificate). It also opens a server on plain `http`. +- A WebView has no Media Session API, so the app keeps its own: a notification with the song, its cover and the buttons, and the lock screen and headset buttons work. The music goes on with the screen off. From Android 13 it asks to show notifications; without a yes the music still plays and only the notification is hidden. + +To build it you need Node, a JDK 21 and the Android SDK (platform 36 and its build tools; set `ANDROID_HOME`): + +```bash +cd android +npm ci +npm run apk # android/android/app/build/outputs/apk/debug/app-debug.apk +adb install -r android/app/build/outputs/apk/debug/app-debug.apk +``` + +The APK is signed with the debug key, which is fine for your own phone. It is not part of the release pipeline, because the runner has no Android SDK. `npm test` checks how an address typed by hand is read. + +## Releases + +Every merge into `main` can make a release, by itself (`.forgejo/workflows/release.yml`, [semantic-release](https://semantic-release.gitbook.io)). The next version comes from the messages of the commits since the last one, written as [Conventional Commits](https://www.conventionalcommits.org): + +| Commit | Release | +|---|---| +| `fix: ...`, `perf: ...` | patch (0.2.0 to 0.2.1) | +| `feat: ...` | minor (0.2.0 to 0.3.0) | +| `feat!: ...`, or `BREAKING CHANGE:` in the body | major (0.2.0 to 1.0.0) | +| `docs:`, `test:`, `chore:`, `ci:`, `style:` | none by themselves | + +A release adds its entry to [CHANGELOG.md](CHANGELOG.md), tags the commit (`v0.3.0`), makes the release on the forge, and the image is built from the tag and pushed to `git.kmalloc.org/squid/sonda` as `0.3.0`, `0.3` and `latest`. The version is inside the binary: `GET /version` and the User-Agent of the providers say it (a build that no release made says `dev`). The commit that writes `CHANGELOG.md` says `[skip ci]`. The image is made for amd64 and for arm64, and each tag points at both. The runner has no Docker and cannot emulate ARM, so the images are built with [kaniko](https://github.com/GoogleContainerTools/kaniko): the Go program is compiled for ARM with no emulation, the last stage of the Dockerfile runs no command, and a last job (with [crane](https://github.com/google/go-containerregistry)) says the architecture of each image and joins them. The workflow needs the secret `FORGEJO_TOKEN`: a token of the owner with the scopes `write:repository` and `write:package`. + ## Development ```bash @@ -373,11 +469,14 @@ internal/library/ libraries, the scanner, the event hub, the tag reader internal/about/ descriptions of artists, from Wikipedia through MusicBrainz and Wikidata internal/discovery/ similar artists: what the providers said, kept and refreshed internal/providers/ external providers: settings, the outbound HTTP client, the clients of the providers +internal/naming/ names for lists of songs: a local namer, and Claude when it is on internal/listening/ listen events internal/taste/ what the listener likes, learned from the listening history internal/discover/ the score of an artist you may like, and the suggestions internal/feedback/ the wishlist and the artists a user is not interested in internal/playlists/ the playlists of each user +internal/mixes/ the mixes made for a listener, by genre and by listening +internal/smart/ smart playlists: rules that are worked out into songs internal/images/ covers made from the pictures in the user's own files internal/usersettings/ the settings that belong to one user internal/storage/ SQLite and PostgreSQL through bun, the repositories @@ -393,4 +492,4 @@ Migrations are plain SQL files in `migrations/sqlite` and `migrations/postgres`. ## Third-party code -Audio tags are read with `go.senan.xyz/taglib`, which embeds [TagLib](https://github.com/taglib/taglib) compiled to WebAssembly and is licensed under the LGPL 2.1. The Swagger UI assets come from `github.com/swaggest/swgui` (Apache-2.0), which embeds [Swagger UI](https://github.com/swagger-api/swagger-ui) (Apache-2.0). The other dependencies are listed in `go.mod`. +Audio tags are read with `go.senan.xyz/taglib`, which embeds [TagLib](https://github.com/taglib/taglib) compiled to WebAssembly and is licensed under the LGPL 2.1. The Swagger UI assets come from `github.com/swaggest/swgui` (Apache-2.0), which embeds [Swagger UI](https://github.com/swagger-api/swagger-ui) (Apache-2.0). The other dependencies are listed in `go.mod`. The Milkdrop pictures of the player are drawn by [butterchurn](https://github.com/jberg/butterchurn) and [butterchurn-presets](https://github.com/jberg/butterchurn-presets) (both MIT), bundled in the web app. The shape of a song is found with `github.com/hajimehoshi/go-mp3` (Apache-2.0) and `github.com/eaburns/flac` (MIT), two decoders written in Go. diff --git a/android/.gitignore b/android/.gitignore new file mode 100644 index 0000000..b29e650 --- /dev/null +++ b/android/.gitignore @@ -0,0 +1,14 @@ +node_modules/ +# Build output and local settings of the Android project +android/.gradle/ +android/build/ +android/app/build/ +android/local.properties +android/.idea/ +android/captures/ +android/app/src/main/assets/public/ +android/app/src/main/assets/capacitor.config.json +android/app/src/main/assets/capacitor.plugins.json +android/capacitor-cordova-android-plugins/ +*.keystore +*.jks diff --git a/android/android/.gitignore b/android/android/.gitignore new file mode 100644 index 0000000..48354a3 --- /dev/null +++ b/android/android/.gitignore @@ -0,0 +1,101 @@ +# Using Android gitignore template: https://github.com/github/gitignore/blob/HEAD/Android.gitignore + +# Built application files +*.apk +*.aar +*.ap_ +*.aab + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ +# Uncomment the following line in case you need and you don't have the release build type files in your app +# release/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +*.iml +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +# Android Studio 3 in .gitignore file. +.idea/caches +.idea/modules.xml +# Comment next line if keeping position of elements in Navigation Editor is relevant for you +.idea/navEditor.xml + +# Keystore files +# Uncomment the following lines if you do not want to check your keystore files in. +#*.jks +#*.keystore + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild +.cxx/ + +# Google Services (e.g. APIs or Firebase) +# google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md + +# Version control +vcs.xml + +# lint +lint/intermediates/ +lint/generated/ +lint/outputs/ +lint/tmp/ +# lint/reports/ + +# Android Profiling +*.hprof + +# Cordova plugins for Capacitor +capacitor-cordova-android-plugins + +# Copied web assets +app/src/main/assets/public + +# Generated Config files +app/src/main/assets/capacitor.config.json +app/src/main/assets/capacitor.plugins.json +app/src/main/res/xml/config.xml diff --git a/android/android/app/.gitignore b/android/android/app/.gitignore new file mode 100644 index 0000000..043df80 --- /dev/null +++ b/android/android/app/.gitignore @@ -0,0 +1,2 @@ +/build/* +!/build/.npmkeep diff --git a/android/android/app/build.gradle b/android/android/app/build.gradle new file mode 100644 index 0000000..8e18dfd --- /dev/null +++ b/android/android/app/build.gradle @@ -0,0 +1,55 @@ +apply plugin: 'com.android.application' + +android { + namespace = "app.sonda" + compileSdk = rootProject.ext.compileSdkVersion + defaultConfig { + applicationId "app.sonda" + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + aaptOptions { + // Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps. + // Default: https://android.googlesource.com/platform/frameworks/base/+/282e181b58cf72b6ca770dc7ca5f91f135444502/tools/aapt/AaptAssets.cpp#61 + ignoreAssetsPattern = '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~' + } + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +repositories { + flatDir{ + dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs' + } +} + +dependencies { + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" + implementation "androidx.coordinatorlayout:coordinatorlayout:$androidxCoordinatorLayoutVersion" + implementation "androidx.core:core-splashscreen:$coreSplashScreenVersion" + implementation "androidx.media:media:1.7.0" + implementation project(':capacitor-android') + testImplementation "junit:junit:$junitVersion" + androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" + androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" + implementation project(':capacitor-cordova-android-plugins') +} + +apply from: 'capacitor.build.gradle' + +try { + def servicesJSON = file('google-services.json') + if (servicesJSON.text) { + apply plugin: 'com.google.gms.google-services' + } +} catch(Exception e) { + logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work") +} diff --git a/android/android/app/capacitor.build.gradle b/android/android/app/capacitor.build.gradle new file mode 100644 index 0000000..1005ebc --- /dev/null +++ b/android/android/app/capacitor.build.gradle @@ -0,0 +1,19 @@ +// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN + +android { + compileOptions { + sourceCompatibility JavaVersion.VERSION_21 + targetCompatibility JavaVersion.VERSION_21 + } +} + +apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle" +dependencies { + implementation project(':capacitor-app') + +} + + +if (hasProperty('postBuildExtras')) { + postBuildExtras() +} diff --git a/android/android/app/proguard-rules.pro b/android/android/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/android/android/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/android/android/app/src/main/AndroidManifest.xml b/android/android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..fa41fea --- /dev/null +++ b/android/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/android/app/src/main/java/app/sonda/MainActivity.java b/android/android/app/src/main/java/app/sonda/MainActivity.java new file mode 100644 index 0000000..83e23a3 --- /dev/null +++ b/android/android/app/src/main/java/app/sonda/MainActivity.java @@ -0,0 +1,24 @@ +package app.sonda; + +import android.os.Bundle; +import android.webkit.WebView; +import com.getcapacitor.BridgeActivity; + +public class MainActivity extends BridgeActivity { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + WebView web = getBridge().getWebView(); + web.addJavascriptInterface(new SondaNative(this), "SondaNative"); + // The buttons of the notification and of the lock screen go back to the page. + MediaService.listener = (name, ms) -> web.post(() -> web.evaluateJavascript( + "window.sondaNativeAction && window.sondaNativeAction('" + name + "'," + ms + ")", null)); + } + + @Override + public void onDestroy() { + MediaService.listener = null; + if (isFinishing()) MediaService.end(this); + super.onDestroy(); + } +} diff --git a/android/android/app/src/main/java/app/sonda/MediaService.java b/android/android/app/src/main/java/app/sonda/MediaService.java new file mode 100644 index 0000000..552ed9c --- /dev/null +++ b/android/android/app/src/main/java/app/sonda/MediaService.java @@ -0,0 +1,219 @@ +package app.sonda; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ServiceInfo; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Build; +import android.os.IBinder; +import android.support.v4.media.MediaMetadataCompat; +import android.support.v4.media.session.MediaSessionCompat; +import android.support.v4.media.session.PlaybackStateCompat; +import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; +import androidx.media.app.NotificationCompat.MediaStyle; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * Keeps the playback alive while the screen is off or another app is in front, and shows what plays with its + * controls in a notification and on the lock screen. The web app does the playing; this only mirrors it, and + * passes the buttons back through {@link #listener}. + */ +public class MediaService extends Service { + /** Where a press of a button goes: the name is play, pause, next, previous or seekto. */ + interface Listener { + void onAction(String name, long ms); + } + + static volatile Listener listener; + + private static final String CHANNEL = "playback"; + private static final int NOTIFICATION_ID = 1; + private static final String PLAY = "app.sonda.PLAY"; + private static final String PAUSE = "app.sonda.PAUSE"; + private static final String NEXT = "app.sonda.NEXT"; + private static final String PREVIOUS = "app.sonda.PREVIOUS"; + + // The latest word of the page, kept here because the service may start after it was said. + private static NowPlaying latest; + private static MediaService instance; + + private MediaSessionCompat session; + private String artworkUrl = ""; + private Bitmap artwork; + + /** Shows what plays. The first time it starts the service; after that it only updates it. */ + static void show(Context context, NowPlaying now) { + latest = now; + if (instance != null) { + instance.apply(now); + } else { + ContextCompat.startForegroundService(context, new Intent(context, MediaService.class)); + } + } + + /** Nothing plays any more: the notification goes. */ + static void end(Context context) { + latest = null; + if (instance != null) instance.shutDown(); + else context.stopService(new Intent(context, MediaService.class)); + } + + @Override + public void onCreate() { + super.onCreate(); + instance = this; + createChannel(); + session = new MediaSessionCompat(this, "Sonda"); + session.setCallback(new MediaSessionCompat.Callback() { + @Override public void onPlay() { send("play", 0); } + @Override public void onPause() { send("pause", 0); } + @Override public void onSkipToNext() { send("next", 0); } + @Override public void onSkipToPrevious() { send("previous", 0); } + @Override public void onSeekTo(long pos) { send("seekto", pos); } + }); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + String action = intent == null ? null : intent.getAction(); + if (PLAY.equals(action)) send("play", 0); + else if (PAUSE.equals(action)) send("pause", 0); + else if (NEXT.equals(action)) send("next", 0); + else if (PREVIOUS.equals(action)) send("previous", 0); + if (latest != null) { + apply(latest); + } else { + // Started with nothing to show, or after the app was killed: Android wants a notification anyway, then it goes. + startForeground(NOTIFICATION_ID, notification(null)); + shutDown(); + } + return START_NOT_STICKY; + } + + /** The person swiped the app away: the page is gone, so the music and its notification go too. */ + @Override + public void onTaskRemoved(Intent rootIntent) { + latest = null; + shutDown(); + } + + @Override + public void onDestroy() { + instance = null; + if (session != null) { + session.setActive(false); + session.release(); + session = null; + } + super.onDestroy(); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private void send(String name, long ms) { + Listener l = listener; + if (l != null) l.onAction(name, ms); + } + + private void shutDown() { + stopForeground(STOP_FOREGROUND_REMOVE); + stopSelf(); + } + + private void apply(NowPlaying now) { + session.setMetadata( + new MediaMetadataCompat.Builder() + .putString(MediaMetadataCompat.METADATA_KEY_TITLE, now.title) + .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, now.artist) + .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, now.album) + .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, now.durationMs) + .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, now.artworkUrl.equals(artworkUrl) ? artwork : null) + .build()); + long actions = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE + | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_SEEK_TO; + session.setPlaybackState( + new PlaybackStateCompat.Builder() + .setActions(actions) + .setState(now.playing ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED, now.positionNow(), now.playing ? 1f : 0f) + .build()); + session.setActive(true); + + Notification n = notification(now); + if (Build.VERSION.SDK_INT >= 29) { + startForeground(NOTIFICATION_ID, n, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK); + } else { + startForeground(NOTIFICATION_ID, n); + } + if (!now.artworkUrl.isEmpty() && !now.artworkUrl.equals(artworkUrl)) fetchArtwork(now.artworkUrl); + } + + private Notification notification(NowPlaying now) { + NotificationCompat.Builder b = new NotificationCompat.Builder(this, CHANNEL) + .setSmallIcon(R.drawable.ic_stat_sonda) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setOnlyAlertOnce(true) + .setShowWhen(false) + .setContentIntent(PendingIntent.getActivity( + this, 0, new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT)); + if (now == null) return b.setContentTitle("Sonda").build(); + b.setContentTitle(now.title).setContentText(now.artist).setSubText(now.album); + if (now.artworkUrl.equals(artworkUrl) && artwork != null) b.setLargeIcon(artwork); + b.addAction(android.R.drawable.ic_media_previous, getString(R.string.previous), button(PREVIOUS)); + if (now.playing) b.addAction(android.R.drawable.ic_media_pause, getString(R.string.pause), button(PAUSE)); + else b.addAction(android.R.drawable.ic_media_play, getString(R.string.play), button(PLAY)); + b.addAction(android.R.drawable.ic_media_next, getString(R.string.next), button(NEXT)); + b.setStyle(new MediaStyle().setMediaSession(session.getSessionToken()).setShowActionsInCompactView(0, 1, 2)); + return b.build(); + } + + private PendingIntent button(String action) { + return PendingIntent.getService( + this, action.hashCode(), new Intent(this, MediaService.class).setAction(action), PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); + } + + private void createChannel() { + if (Build.VERSION.SDK_INT < 26) return; + NotificationChannel channel = new NotificationChannel(CHANNEL, getString(R.string.channel_playback), NotificationManager.IMPORTANCE_LOW); + channel.setShowBadge(false); + getSystemService(NotificationManager.class).createNotificationChannel(channel); + } + + /** The cover comes from the server with the token in its address; a cover that does not come leaves the notification without one. */ + private void fetchArtwork(String url) { + artworkUrl = url; + artwork = null; + new Thread(() -> { + Bitmap bitmap = null; + try { + HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection(); + c.setConnectTimeout(8000); + c.setReadTimeout(8000); + try (InputStream in = c.getInputStream()) { + bitmap = BitmapFactory.decodeStream(in); + } finally { + c.disconnect(); + } + } catch (Exception ignored) { + // No cover: the notification stays as it is. + } + final Bitmap result = bitmap; + ContextCompat.getMainExecutor(this).execute(() -> { + if (instance != this || !url.equals(artworkUrl) || result == null) return; + artwork = result; + if (latest != null) apply(latest); + }); + }).start(); + } +} diff --git a/android/android/app/src/main/java/app/sonda/NowPlaying.java b/android/android/app/src/main/java/app/sonda/NowPlaying.java new file mode 100644 index 0000000..7c1d696 --- /dev/null +++ b/android/android/app/src/main/java/app/sonda/NowPlaying.java @@ -0,0 +1,51 @@ +package app.sonda; + +import android.os.SystemClock; +import org.json.JSONException; +import org.json.JSONObject; + +/** What the web app says is playing: the song, whether it plays and where it is. */ +final class NowPlaying { + final String title; + final String artist; + final String album; + final String artworkUrl; + final boolean playing; + final long durationMs; + private final long positionMs; + private final long receivedAt; + + private NowPlaying(String title, String artist, String album, String artworkUrl, boolean playing, long positionMs, long durationMs) { + this.title = title; + this.artist = artist; + this.album = album; + this.artworkUrl = artworkUrl; + this.playing = playing; + this.positionMs = positionMs; + this.durationMs = durationMs; + this.receivedAt = SystemClock.elapsedRealtime(); + } + + /** Where the song is now: what the page said, plus the time that has passed if it plays. */ + long positionNow() { + long position = playing ? positionMs + (SystemClock.elapsedRealtime() - receivedAt) : positionMs; + return durationMs > 0 ? Math.min(position, durationMs) : position; + } + + /** The JSON the page sends; null when it is not what was expected. */ + static NowPlaying parse(String json) { + try { + JSONObject o = new JSONObject(json); + return new NowPlaying( + o.optString("title"), + o.optString("artist"), + o.optString("album"), + o.optString("artworkUrl"), + o.optBoolean("playing"), + Math.max(0, o.optLong("positionMs")), + Math.max(0, o.optLong("durationMs"))); + } catch (JSONException e) { + return null; + } + } +} diff --git a/android/android/app/src/main/java/app/sonda/SondaNative.java b/android/android/app/src/main/java/app/sonda/SondaNative.java new file mode 100644 index 0000000..57f2370 --- /dev/null +++ b/android/android/app/src/main/java/app/sonda/SondaNative.java @@ -0,0 +1,47 @@ +package app.sonda; + +import android.Manifest; +import android.app.Activity; +import android.content.pm.PackageManager; +import android.os.Build; +import android.webkit.JavascriptInterface; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; + +/** + * What the web page can ask of the app. A WebView has no Media Session API, so the page tells the app what plays + * and the app keeps a notification with the controls, and the playback alive while the screen is off. + * It is on every page the WebView loads, which is the server the person chose. + */ +final class SondaNative { + private final Activity activity; + private boolean askedForNotifications = false; + + SondaNative(Activity activity) { + this.activity = activity; + } + + @JavascriptInterface + public void update(String json) { + NowPlaying now = NowPlaying.parse(json); + if (now == null) return; + activity.runOnUiThread(() -> { + askForNotifications(); + MediaService.show(activity, now); + }); + } + + @JavascriptInterface + public void stop() { + activity.runOnUiThread(() -> MediaService.end(activity)); + } + + /** From Android 13 a notification needs a yes. Without it the playback goes on, only the notification is hidden. */ + private void askForNotifications() { + if (askedForNotifications || Build.VERSION.SDK_INT < 33) return; + askedForNotifications = true; + if (ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.POST_NOTIFICATIONS}, 1); + } + } +} diff --git a/android/android/app/src/main/res/drawable/ic_stat_sonda.xml b/android/android/app/src/main/res/drawable/ic_stat_sonda.xml new file mode 100644 index 0000000..1a8e985 --- /dev/null +++ b/android/android/app/src/main/res/drawable/ic_stat_sonda.xml @@ -0,0 +1,10 @@ + + + + diff --git a/android/android/app/src/main/res/drawable/splash.xml b/android/android/app/src/main/res/drawable/splash.xml new file mode 100644 index 0000000..9047d46 --- /dev/null +++ b/android/android/app/src/main/res/drawable/splash.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/android/android/app/src/main/res/layout/activity_main.xml b/android/android/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..b5ad138 --- /dev/null +++ b/android/android/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,12 @@ + + + + + diff --git a/android/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/android/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..036d09b --- /dev/null +++ b/android/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/android/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/android/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..036d09b --- /dev/null +++ b/android/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/android/android/app/src/main/res/mipmap-hdpi/ic_launcher.png b/android/android/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..f370e24 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/android/android/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png b/android/android/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..4929e74 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png differ diff --git a/android/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/android/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000..1a7d99e Binary files /dev/null and b/android/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/android/android/app/src/main/res/mipmap-mdpi/ic_launcher.png b/android/android/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..f8fb412 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/android/android/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png b/android/android/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..043dc73 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png differ diff --git a/android/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/android/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000..6b09617 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..dc2937a Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png b/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..d336698 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png differ diff --git a/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000..27e58a4 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..8456688 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png b/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..00694df Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png differ diff --git a/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..b65234e Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..9b8d206 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png b/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..dbeb894 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png differ diff --git a/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..16adc97 Binary files /dev/null and b/android/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/android/android/app/src/main/res/values/ic_launcher_background.xml b/android/android/app/src/main/res/values/ic_launcher_background.xml new file mode 100644 index 0000000..b492c0e --- /dev/null +++ b/android/android/app/src/main/res/values/ic_launcher_background.xml @@ -0,0 +1,4 @@ + + + #08161D + diff --git a/android/android/app/src/main/res/values/strings.xml b/android/android/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..5f3d23e --- /dev/null +++ b/android/android/app/src/main/res/values/strings.xml @@ -0,0 +1,12 @@ + + + Sonda + Sonda + app.sonda + app.sonda + Previous + Pause + Play + Next + Playback + diff --git a/android/android/app/src/main/res/values/styles.xml b/android/android/app/src/main/res/values/styles.xml new file mode 100644 index 0000000..72d8deb --- /dev/null +++ b/android/android/app/src/main/res/values/styles.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/android/android/app/src/main/res/xml/file_paths.xml b/android/android/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..bd0c4d8 --- /dev/null +++ b/android/android/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/android/android/app/src/main/res/xml/network_security_config.xml b/android/android/app/src/main/res/xml/network_security_config.xml new file mode 100644 index 0000000..9237ac6 --- /dev/null +++ b/android/android/app/src/main/res/xml/network_security_config.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/android/android/build.gradle b/android/android/build.gradle new file mode 100644 index 0000000..f8f0e43 --- /dev/null +++ b/android/android/build.gradle @@ -0,0 +1,29 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + + repositories { + google() + mavenCentral() + } + dependencies { + classpath 'com.android.tools.build:gradle:8.13.0' + classpath 'com.google.gms:google-services:4.4.4' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +apply from: "variables.gradle" + +allprojects { + repositories { + google() + mavenCentral() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/android/android/capacitor.settings.gradle b/android/android/capacitor.settings.gradle new file mode 100644 index 0000000..2085c86 --- /dev/null +++ b/android/android/capacitor.settings.gradle @@ -0,0 +1,6 @@ +// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN +include ':capacitor-android' +project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor') + +include ':capacitor-app' +project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android') diff --git a/android/android/gradle.properties b/android/android/gradle.properties new file mode 100644 index 0000000..2e87c52 --- /dev/null +++ b/android/android/gradle.properties @@ -0,0 +1,22 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx1536m + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true + +# AndroidX package structure to make it clearer which packages are bundled with the +# Android operating system, and which are packaged with your app's APK +# https://developer.android.com/topic/libraries/support-library/androidx-rn +android.useAndroidX=true diff --git a/android/android/gradle/wrapper/gradle-wrapper.jar b/android/android/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..1b33c55 Binary files /dev/null and b/android/android/gradle/wrapper/gradle-wrapper.jar differ diff --git a/android/android/gradle/wrapper/gradle-wrapper.properties b/android/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..7705927 --- /dev/null +++ b/android/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-all.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/android/android/gradlew b/android/android/gradlew new file mode 100755 index 0000000..23d15a9 --- /dev/null +++ b/android/android/gradlew @@ -0,0 +1,251 @@ +#!/bin/sh + +# +# Copyright ยฉ 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions ยซ$varยป, ยซ${var}ยป, ยซ${var:-default}ยป, ยซ${var+SET}ยป, +# ยซ${var#prefix}ยป, ยซ${var%suffix}ยป, and ยซ$( cmd )ยป; +# * compound commands having a testable exit status, especially ยซcaseยป; +# * various built-in commands including ยซcommandยป, ยซsetยป, and ยซulimitยป. +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH="\\\"\\\"" + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/android/android/gradlew.bat b/android/android/gradlew.bat new file mode 100644 index 0000000..5eed7ee --- /dev/null +++ b/android/android/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/android/android/settings.gradle b/android/android/settings.gradle new file mode 100644 index 0000000..3b4431d --- /dev/null +++ b/android/android/settings.gradle @@ -0,0 +1,5 @@ +include ':app' +include ':capacitor-cordova-android-plugins' +project(':capacitor-cordova-android-plugins').projectDir = new File('./capacitor-cordova-android-plugins/') + +apply from: 'capacitor.settings.gradle' \ No newline at end of file diff --git a/android/android/variables.gradle b/android/android/variables.gradle new file mode 100644 index 0000000..ee4ba41 --- /dev/null +++ b/android/android/variables.gradle @@ -0,0 +1,16 @@ +ext { + minSdkVersion = 24 + compileSdkVersion = 36 + targetSdkVersion = 36 + androidxActivityVersion = '1.11.0' + androidxAppCompatVersion = '1.7.1' + androidxCoordinatorLayoutVersion = '1.3.0' + androidxCoreVersion = '1.17.0' + androidxFragmentVersion = '1.8.9' + coreSplashScreenVersion = '1.2.0' + androidxWebkitVersion = '1.14.0' + junitVersion = '4.13.2' + androidxJunitVersion = '1.3.0' + androidxEspressoCoreVersion = '3.7.0' + cordovaAndroidVersion = '14.0.1' +} \ No newline at end of file diff --git a/android/capacitor.config.ts b/android/capacitor.config.ts new file mode 100644 index 0000000..757fc1a --- /dev/null +++ b/android/capacitor.config.ts @@ -0,0 +1,27 @@ +import type { CapacitorConfig } from '@capacitor/cli' + +// The app is a window on a Sonda server. The page in www asks for the address the first time and remembers it; +// the settings of Sonda have a way back to that page, for another server. +const config: CapacitorConfig = { + appId: 'app.sonda', + appName: 'Sonda', + webDir: 'www', + server: { + // The page in www is on http, so that it can look for a server on http as well as on https: from an https page a request to http is blocked. + androidScheme: 'http', + // The server is anywhere the user types, on http or https. + allowNavigation: ['*'], + cleartext: true, + }, + plugins: { + // The app is dark: light clock and icons on the bars. + SystemBars: { style: 'DARK' }, + }, + android: { + backgroundColor: '#08161d', + // The web app knows it is inside the app by this word in the user agent. + appendUserAgent: 'SondaApp', + }, +} + +export default config diff --git a/android/package-lock.json b/android/package-lock.json new file mode 100644 index 0000000..9c03d4d --- /dev/null +++ b/android/package-lock.json @@ -0,0 +1,1618 @@ +{ + "name": "sonda-android", + "version": "0.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "sonda-android", + "version": "0.0.0", + "dependencies": { + "@capacitor/android": "^8.6.2", + "@capacitor/app": "^8.1.2", + "@capacitor/core": "^8.6.2" + }, + "devDependencies": { + "@capacitor/cli": "^8.6.2", + "typescript": "^7.0.2" + } + }, + "node_modules/@capacitor/android": { + "version": "8.6.2", + "resolved": "https://registry.npmjs.org/@capacitor/android/-/android-8.6.2.tgz", + "integrity": "sha512-dWXnXmOVTtxU91n5h11MzoQ7PAMjyzU1qguw0mBJZiwSuhGvLKTG/IlsQ75H8qeE0a1acV2f573x15wYvyzuVA==", + "license": "MIT", + "peerDependencies": { + "@capacitor/core": "^8.6.0" + } + }, + "node_modules/@capacitor/app": { + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/@capacitor/app/-/app-8.1.2.tgz", + "integrity": "sha512-uKBlsrXH0IdvUn69e/KKYbZ32A06pcrUdG8LWuZ/6y33YMXZXLEEdNSwl+jKS/SrsTWqnePs8EqSWaiQM3mUGA==", + "license": "MIT", + "peerDependencies": { + "@capacitor/core": ">=8.0.0" + } + }, + "node_modules/@capacitor/cli": { + "version": "8.6.2", + "resolved": "https://registry.npmjs.org/@capacitor/cli/-/cli-8.6.2.tgz", + "integrity": "sha512-3HeNPX3cEWBSljJT0sJviZCxXRoToTLAufhUcIJh+JMj1xOhLFZpuj8oFRnJ0kNYAf7DIBHQDmilIXloK9+YYg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@ionic/cli-framework-output": "^2.2.8", + "@ionic/utils-subprocess": "^3.0.1", + "@ionic/utils-terminal": "^2.3.5", + "commander": "^12.1.0", + "debug": "^4.4.0", + "env-paths": "^2.2.0", + "fs-extra": "^11.2.0", + "kleur": "^4.1.5", + "native-run": "^2.0.3", + "open": "^8.4.0", + "plist": "^3.1.0", + "prompts": "^2.4.2", + "rimraf": "^6.0.1", + "semver": "^7.6.3", + "tar": "^7.5.3", + "tslib": "^2.8.1", + "xcode": "^4.0.0", + "xml2js": "^0.6.2" + }, + "bin": { + "cap": "bin/capacitor", + "capacitor": "bin/capacitor" + }, + "engines": { + "node": ">=22.0.0" + } + }, + "node_modules/@capacitor/core": { + "version": "8.6.2", + "resolved": "https://registry.npmjs.org/@capacitor/core/-/core-8.6.2.tgz", + "integrity": "sha512-UEb9crRuqEAe2Iu2eGOEuQ8rLQxpwg7ZZIpl17UJoytEpnV1e4oXXW0K7yYS2NsCOGmJjyLeKjNlgG7tkAoX8g==", + "license": "MIT", + "dependencies": { + "tslib": "^2.1.0" + } + }, + "node_modules/@ionic/cli-framework-output": { + "version": "2.2.8", + "resolved": "https://registry.npmjs.org/@ionic/cli-framework-output/-/cli-framework-output-2.2.8.tgz", + "integrity": "sha512-TshtaFQsovB4NWRBydbNFawql6yul7d5bMiW1WYYf17hd99V6xdDdk3vtF51bw6sLkxON3bDQpWsnUc9/hVo3g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@ionic/utils-terminal": "2.3.5", + "debug": "^4.0.0", + "tslib": "^2.0.1" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@ionic/utils-array": { + "version": "2.1.6", + "resolved": "https://registry.npmjs.org/@ionic/utils-array/-/utils-array-2.1.6.tgz", + "integrity": "sha512-0JZ1Zkp3wURnv8oq6Qt7fMPo5MpjbLoUoa9Bu2Q4PJuSDWM8H8gwF3dQO7VTeUj3/0o1IB1wGkFWZZYgUXZMUg==", + "dev": true, + "license": "MIT", + "dependencies": { + "debug": "^4.0.0", + "tslib": "^2.0.1" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@ionic/utils-fs": { + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/@ionic/utils-fs/-/utils-fs-3.1.7.tgz", + "integrity": "sha512-2EknRvMVfhnyhL1VhFkSLa5gOcycK91VnjfrTB0kbqkTFCOXyXgVLI5whzq7SLrgD9t1aqos3lMMQyVzaQ5gVA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/fs-extra": "^8.0.0", + "debug": "^4.0.0", + "fs-extra": "^9.0.0", + "tslib": "^2.0.1" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@ionic/utils-fs/node_modules/fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@ionic/utils-object": { + "version": "2.1.6", + "resolved": "https://registry.npmjs.org/@ionic/utils-object/-/utils-object-2.1.6.tgz", + "integrity": "sha512-vCl7sl6JjBHFw99CuAqHljYJpcE88YaH2ZW4ELiC/Zwxl5tiwn4kbdP/gxi2OT3MQb1vOtgAmSNRtusvgxI8ww==", + "dev": true, + "license": "MIT", + "dependencies": { + "debug": "^4.0.0", + "tslib": "^2.0.1" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@ionic/utils-process": { + "version": "2.1.12", + "resolved": "https://registry.npmjs.org/@ionic/utils-process/-/utils-process-2.1.12.tgz", + "integrity": "sha512-Jqkgyq7zBs/v/J3YvKtQQiIcxfJyplPgECMWgdO0E1fKrrH8EF0QGHNJ9mJCn6PYe2UtHNS8JJf5G21e09DfYg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@ionic/utils-object": "2.1.6", + "@ionic/utils-terminal": "2.3.5", + "debug": "^4.0.0", + "signal-exit": "^3.0.3", + "tree-kill": "^1.2.2", + "tslib": "^2.0.1" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@ionic/utils-stream": { + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/@ionic/utils-stream/-/utils-stream-3.1.7.tgz", + "integrity": "sha512-eSELBE7NWNFIHTbTC2jiMvh1ABKGIpGdUIvARsNPMNQhxJB3wpwdiVnoBoTYp+5a6UUIww4Kpg7v6S7iTctH1w==", + "dev": true, + "license": "MIT", + "dependencies": { + "debug": "^4.0.0", + "tslib": "^2.0.1" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@ionic/utils-subprocess": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@ionic/utils-subprocess/-/utils-subprocess-3.0.1.tgz", + "integrity": "sha512-cT4te3AQQPeIM9WCwIg8ohroJ8TjsYaMb2G4ZEgv9YzeDqHZ4JpeIKqG2SoaA3GmVQ3sOfhPM6Ox9sxphV/d1A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@ionic/utils-array": "2.1.6", + "@ionic/utils-fs": "3.1.7", + "@ionic/utils-process": "2.1.12", + "@ionic/utils-stream": "3.1.7", + "@ionic/utils-terminal": "2.3.5", + "cross-spawn": "^7.0.3", + "debug": "^4.0.0", + "tslib": "^2.0.1" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@ionic/utils-terminal": { + "version": "2.3.5", + "resolved": "https://registry.npmjs.org/@ionic/utils-terminal/-/utils-terminal-2.3.5.tgz", + "integrity": "sha512-3cKScz9Jx2/Pr9ijj1OzGlBDfcmx7OMVBt4+P1uRR0SSW4cm1/y3Mo4OY3lfkuaYifMNBW8Wz6lQHbs1bihr7A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/slice-ansi": "^4.0.0", + "debug": "^4.0.0", + "signal-exit": "^3.0.3", + "slice-ansi": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0", + "tslib": "^2.0.1", + "untildify": "^4.0.0", + "wrap-ansi": "^7.0.0" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/@isaacs/fs-minipass": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@isaacs/fs-minipass/-/fs-minipass-4.0.1.tgz", + "integrity": "sha512-wgm9Ehl2jpeqP3zw/7mo3kRHFp5MEDhqAdwy1fTGkHAwnkGOVsgpvQhL8B5n1qlb01jV3n/bI0ZfZp5lWA1k4w==", + "dev": true, + "license": "ISC", + "dependencies": { + "minipass": "^7.0.4" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@isaacs/nonrecursive-glob-match": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@isaacs/nonrecursive-glob-match/-/nonrecursive-glob-match-1.1.0.tgz", + "integrity": "sha512-GPm/Dbvk5RLmhRnxXIa7nTpK0xtl4E0wj0MslsuNiJ1FIROU+2+9lgUC/dgF7Ey4QCWhfEsgxilMRMCdHPfDfw==", + "dev": true, + "license": "BlueOak-1.0.0" + }, + "node_modules/@types/fs-extra": { + "version": "8.1.5", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-8.1.5.tgz", + "integrity": "sha512-0dzKcwO+S8s2kuF5Z9oUWatQJj5Uq/iqphEtE3GQJVRRYm/tD1LglU2UnXi2A8jLq5umkGouOXOR9y0n613ZwQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/node": { + "version": "26.6.6", + "resolved": "https://registry.npmjs.org/@types/node/-/node-26.6.6.tgz", + "integrity": "sha512-MzQj+8Kx3XI40OmUAE6Qoh58ZMNstNNMwn2nfd3qWsfe7nDNSqJNzes4njhz6AHwrSHOffSLwXU6yb474Q4aog==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~8.9.0" + } + }, + "node_modules/@types/slice-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@types/slice-ansi/-/slice-ansi-4.0.0.tgz", + "integrity": "sha512-+OpjSaq85gvlZAYINyzKpLeiFkSC4EsC6IIiT6v6TLSU5k5U83fHGj9Lel8oKEXM0HqgrMVCjXPDPVICtxF7EQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/@typescript/typescript-aix-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz", + "integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz", + "integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz", + "integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz", + "integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz", + "integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz", + "integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz", + "integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-loong64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz", + "integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-mips64el": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz", + "integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz", + "integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-riscv64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz", + "integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-s390x": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz", + "integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz", + "integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz", + "integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz", + "integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz", + "integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz", + "integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-sunos-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz", + "integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz", + "integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz", + "integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@xmldom/xmldom": { + "version": "0.9.12", + "resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.9.12.tgz", + "integrity": "sha512-5AXjrcMClTryPe9LgZrygpB1lj7s0S9E0+W+AHaVKAVyHanafK86iPSvG5xHVSp/jC+VH1UXu0TAEmY279xH7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.6" + } + }, + "node_modules/ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "license": "MIT", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/astral-regex": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-2.0.0.tgz", + "integrity": "sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/at-least-node": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", + "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">= 4.0.0" + } + }, + "node_modules/balanced-match": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz", + "integrity": "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "18 || 20 || >=22" + } + }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/big-integer": { + "version": "1.6.52", + "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.52.tgz", + "integrity": "sha512-QxD8cf2eVqJOOz63z6JIN9BzvVs/dlySa5HGSBH5xtR8dPteIRQnBxxKqkNTiT6jbDTF6jAfrd4oMcND9RGbQg==", + "dev": true, + "license": "Unlicense", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/bplist-creator": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/bplist-creator/-/bplist-creator-0.1.0.tgz", + "integrity": "sha512-sXaHZicyEEmY86WyueLTQesbeoH/mquvarJaQNbjuOQO+7gbFcDEWqKmcWA4cOTLzFlfgvkiVxolk1k5bBIpmg==", + "dev": true, + "license": "MIT", + "dependencies": { + "stream-buffers": "2.2.x" + } + }, + "node_modules/bplist-parser": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/bplist-parser/-/bplist-parser-0.3.2.tgz", + "integrity": "sha512-apC2+fspHGI3mMKj+dGevkGo/tCqVB8jMb6i+OX+E29p0Iposz07fABkRIfVUPNd5A5VbuOz1bZbnmkKLYF+wQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "big-integer": "1.6.x" + }, + "engines": { + "node": ">= 5.10.0" + } + }, + "node_modules/brace-expansion": { + "version": "5.0.13", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.13.tgz", + "integrity": "sha512-Vh3dmFbc4wZeox7M9I/0L9GfFd83Su4+yCck/OF9Gi/P/dAHn0RT7X4K9R7gMNMhDck3DvhR6QXsnQdjjgwNvQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "balanced-match": "^4.0.2" + }, + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/buffer-crc32": { + "version": "0.2.13", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", + "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": "*" + } + }, + "node_modules/chownr": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-3.0.0.tgz", + "integrity": "sha512-+IxzY9BZOQd/XuYPRmrvEVjF/nqj5kgT4kEq7VofrDoM1MxoRjEWkrCC3EtLi59TVawxTAn+orJwFQcrqEN1+g==", + "dev": true, + "license": "BlueOak-1.0.0", + "engines": { + "node": ">=18" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true, + "license": "MIT" + }, + "node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/cross-spawn": { + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", + "dev": true, + "license": "MIT", + "dependencies": { + "path-key": "^3.1.0", + "shebang-command": "^2.0.0", + "which": "^2.0.1" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "dev": true, + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/define-lazy-prop": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/define-lazy-prop/-/define-lazy-prop-2.0.0.tgz", + "integrity": "sha512-Ds09qNh8yw3khSjiJjiUInaGX9xlqZDY7JVryGxdxV7NPeuqQfplOpQ66yJFZut3jLa5zOwkXw1g9EI2uKh4Og==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/elementtree": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/elementtree/-/elementtree-0.1.7.tgz", + "integrity": "sha512-wkgGT6kugeQk/P6VZ/f4T+4HB41BVgNBq5CDIZVbQ02nvTVqAiVTbskxxu3eA/X96lMlfYOwnLQpN2v5E1zDEg==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "sax": "1.1.4" + }, + "engines": { + "node": ">= 0.4.0" + } + }, + "node_modules/emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", + "dev": true, + "license": "MIT" + }, + "node_modules/env-paths": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.1.tgz", + "integrity": "sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/fd-slicer": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", + "integrity": "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==", + "dev": true, + "license": "MIT", + "dependencies": { + "pend": "~1.2.0" + } + }, + "node_modules/fs-extra": { + "version": "11.4.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.4.1.tgz", + "integrity": "sha512-KYAb4c9BJQI6QqGKthV68OHe0badztdXJWKo0WtBA9IuCFPTKvE5ZdUBglP833aMjhaSPNO4A5j/EkzZtGlKjA==", + "dev": true, + "license": "MIT", + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=14.14" + } + }, + "node_modules/glob": { + "version": "14.0.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-14.0.6.tgz", + "integrity": "sha512-OwXjVP2svY5EyLY75Vek96C8qK43gqrt4xsZ+qSILdnaEiJPXkd62mcfUSNx/TQErQRdu5uM5kF67sfONragKw==", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "minimatch": "^11.0.5", + "minipass": "^7.1.3", + "path-scurry": "^3.0.0" + }, + "engines": { + "node": "18 || 20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.12", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.12.tgz", + "integrity": "sha512-iuyGF2E4oafQfPsRrG2wtY2ILupMhvtdrhzrxB+0VcjsPpSPttpfYdjnGFGsTi5MaJFjI1jOKHPQ6UTbTj9TAw==", + "dev": true, + "license": "BlueOak-1.0.0" + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true, + "license": "ISC" + }, + "node_modules/ini": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/ini/-/ini-4.1.3.tgz", + "integrity": "sha512-X7rqawQBvfdjS10YU1y1YVreA3SsLrW9dX2CewP2EbBJM4ypVNLDkO5y04gejPwKIY9lR+7r9gn3rFPt/kmWFg==", + "dev": true, + "license": "ISC", + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0" + } + }, + "node_modules/is-docker": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz", + "integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==", + "dev": true, + "license": "MIT", + "bin": { + "is-docker": "cli.js" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/is-wsl": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-2.2.0.tgz", + "integrity": "sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-docker": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "dev": true, + "license": "ISC" + }, + "node_modules/jsonfile": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.2.1.tgz", + "integrity": "sha512-zwOTdL3rFQ/lRdBnntKVOX6k5cKJwEc1HdilT71BWEu7J41gXIB2MRp+vxduPSwZJPWBxEzv4yH1wYLJGUHX4Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, + "node_modules/kleur": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz", + "integrity": "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/lru-cache": { + "version": "11.5.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.5.3.tgz", + "integrity": "sha512-U4N8FgzmWxc8k1VH8Kr6lQg18U7Fjvby6wXHVRX/ZZ7IwWbRMgrRbP0Wrb5q5NVinryp4SQampHKdvtecItxUg==", + "dev": true, + "license": "BlueOak-1.0.0", + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/minimatch": { + "version": "11.0.9", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-11.0.9.tgz", + "integrity": "sha512-KCFkfyjqerNlSstjlhIGNXIKZGA/6O4V4RlhZ+Dsr621SPPw0mzlypE9D9ZOoPiVefHID+j7/XuMMlqfD/F60Q==", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "@isaacs/nonrecursive-glob-match": "^1.1.0", + "brace-expansion": "^5.0.12" + }, + "engines": { + "node": "20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/minipass": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-7.1.3.tgz", + "integrity": "sha512-tEBHqDnIoM/1rXME1zgka9g6Q2lcoCkxHLuc7ODJ5BxbP5d4c2Z5cGgtXAku59200Cx7diuHTOYfSBD8n6mm8A==", + "dev": true, + "license": "BlueOak-1.0.0", + "engines": { + "node": ">=16 || 14 >=14.17" + } + }, + "node_modules/minizlib": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/minizlib/-/minizlib-3.1.0.tgz", + "integrity": "sha512-KZxYo1BUkWD2TVFLr0MQoM8vUUigWD3LlD83a/75BqC+4qE0Hb1Vo5v1FgcfaNXvfXzr+5EhQ6ing/CaBijTlw==", + "dev": true, + "license": "MIT", + "dependencies": { + "minipass": "^7.1.2" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true, + "license": "MIT" + }, + "node_modules/native-run": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/native-run/-/native-run-2.1.0.tgz", + "integrity": "sha512-JhEph/fyMwBHmMLiZ1OZX8X92+N1gt5+/ecLtgeRbyDHIyIW7EDt0A3kBZu594y1P4yKDr1CIHe+DDYQ7ZlKjg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@ionic/utils-fs": "^3.1.7", + "@ionic/utils-terminal": "^2.3.4", + "bplist-parser": "^0.3.2", + "debug": "^4.3.4", + "elementtree": "^0.1.7", + "ini": "^4.1.1", + "plist": "^3.1.0", + "split2": "^4.2.0", + "through2": "^4.0.2", + "tslib": "^2.6.2", + "yauzl": "^2.10.0" + }, + "bin": { + "native-run": "bin/native-run" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/open": { + "version": "8.4.2", + "resolved": "https://registry.npmjs.org/open/-/open-8.4.2.tgz", + "integrity": "sha512-7x81NCL719oNbsq/3mh+hVrAWmFuEYUqrq/Iw3kUzH8ReypT9QQ0BLoJS7/G9k6N81XjW4qHWtjWwe/9eLy1EQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "define-lazy-prop": "^2.0.0", + "is-docker": "^2.1.1", + "is-wsl": "^2.2.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/package-json-from-dist": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/package-json-from-dist/-/package-json-from-dist-1.0.1.tgz", + "integrity": "sha512-UEZIS3/by4OC8vL3P2dTXRETpebLI2NiI5vIrjaD/5UtrkFX/tNbwjTSRAGC/+7CAo2pIcBaRgWmcBBHcsaCIw==", + "dev": true, + "license": "BlueOak-1.0.0" + }, + "node_modules/path-key": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", + "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/path-scurry": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-3.0.0.tgz", + "integrity": "sha512-1pTbQqH1wIwMi/eleQ5leFTTCkzFsPcgXNjwOA8YGGW/W4/kHT3dssE7Z6NH+zuqNLOzOB0uaD61yl7cAcVhgg==", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "lru-cache": "^11.0.0", + "minipass": "^7.1.2" + }, + "engines": { + "node": "18 || 20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/pend": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", + "integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==", + "dev": true, + "license": "MIT" + }, + "node_modules/plist": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/plist/-/plist-3.1.1.tgz", + "integrity": "sha512-ZIfcLJC+7E7FBFnDxm9MPmt7D+DidyQ26lewieO75AdhA2ayMtsJSES0iWzqJQbcVRSrTufQoy0DR94xHue0oA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@xmldom/xmldom": "^0.9.10", + "base64-js": "^1.5.1", + "xmlbuilder": "^15.1.1" + }, + "engines": { + "node": ">=10.4.0" + } + }, + "node_modules/prompts": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", + "integrity": "sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "kleur": "^3.0.3", + "sisteransi": "^1.0.5" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/prompts/node_modules/kleur": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", + "integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dev": true, + "license": "MIT", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/rimraf": { + "version": "6.1.4", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-6.1.4.tgz", + "integrity": "sha512-Fk3reQDVPZUMbEm7Zwxz03BWQMW3V5buSk9aU8OckTrO8A1nQtbqvYsAantGNermwsIsXcuOBqcoJ3HuMELctw==", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "glob": "^14.0.3", + "package-json-from-dist": "^1.0.1" + }, + "bin": { + "rimraf": "dist/esm/bin.mjs" + }, + "engines": { + "node": "20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/sax": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.1.4.tgz", + "integrity": "sha512-5f3k2PbGGp+YtKJjOItpg3P99IMD84E4HOvcfleTb5joCHNXYLsR9yWFPOYGgaeMPDubQILTCMdsFb2OMeOjtg==", + "dev": true, + "license": "ISC" + }, + "node_modules/semver": { + "version": "7.8.6", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.6.tgz", + "integrity": "sha512-9lpSSU4Mly3mrtruwLs1eLK6+gzbzqeBFwRNjIjE+9LuDpWdYctrQlQ3ej1Ilgi7gknpsTRnPoLP8DpCHqHIJw==", + "dev": true, + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/shebang-command": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", + "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", + "dev": true, + "license": "MIT", + "dependencies": { + "shebang-regex": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/shebang-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", + "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/signal-exit": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", + "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", + "dev": true, + "license": "ISC" + }, + "node_modules/simple-plist": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/simple-plist/-/simple-plist-1.3.1.tgz", + "integrity": "sha512-iMSw5i0XseMnrhtIzRb7XpQEXepa9xhWxGUojHBL43SIpQuDQkh3Wpy67ZbDzZVr6EKxvwVChnVpdl8hEVLDiw==", + "dev": true, + "license": "MIT", + "dependencies": { + "bplist-creator": "0.1.0", + "bplist-parser": "0.3.1", + "plist": "^3.0.5" + } + }, + "node_modules/simple-plist/node_modules/bplist-parser": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/bplist-parser/-/bplist-parser-0.3.1.tgz", + "integrity": "sha512-PyJxiNtA5T2PlLIeBot4lbp7rj4OadzjnMZD/G5zuBNt8ei/yCU7+wW0h2bag9vr8c+/WuRWmSxbqAl9hL1rBA==", + "dev": true, + "license": "MIT", + "dependencies": { + "big-integer": "1.6.x" + }, + "engines": { + "node": ">= 5.10.0" + } + }, + "node_modules/sisteransi": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz", + "integrity": "sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==", + "dev": true, + "license": "MIT" + }, + "node_modules/slice-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-4.0.0.tgz", + "integrity": "sha512-qMCMfhY040cVHT43K9BFygqYbUPFZKHOg7K73mtTWJRb8pyP3fzf4Ixd5SzdEJQ6MRUg/WBnOLxghZtKKurENQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.0.0", + "astral-regex": "^2.0.0", + "is-fullwidth-code-point": "^3.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/slice-ansi?sponsor=1" + } + }, + "node_modules/split2": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", + "integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">= 10.x" + } + }, + "node_modules/stream-buffers": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/stream-buffers/-/stream-buffers-2.2.0.tgz", + "integrity": "sha512-uyQK/mx5QjHun80FLJTfaWE7JtwfRMKBLkMne6udYOmvH0CawotVa7TfgYHzAnpphn4+TweIx1QKMnRIbipmUg==", + "dev": true, + "license": "Unlicense", + "engines": { + "node": ">= 0.10.0" + } + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dev": true, + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, + "node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dev": true, + "license": "MIT", + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-regex": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/tar": { + "version": "7.5.23", + "resolved": "https://registry.npmjs.org/tar/-/tar-7.5.23.tgz", + "integrity": "sha512-lkukzdoGngZHJZXxW4S/E4Fp9ghTDlyZXrQGRNoJ/LXvDKcXUTq2sg5yQe74po7Voea6n/IN09+wE/QvJMiifA==", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "@isaacs/fs-minipass": "^4.0.0", + "chownr": "^3.0.0", + "minipass": "^7.1.2", + "minizlib": "^3.1.0", + "yallist": "^5.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/through2": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/through2/-/through2-4.0.2.tgz", + "integrity": "sha512-iOqSav00cVxEEICeD7TjLB1sueEL+81Wpzp2bY17uZjZN0pWZPuo4suZ/61VujxmqSGFfgOcNuTZ85QJwNZQpw==", + "dev": true, + "license": "MIT", + "dependencies": { + "readable-stream": "3" + } + }, + "node_modules/tree-kill": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/tree-kill/-/tree-kill-1.2.2.tgz", + "integrity": "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==", + "dev": true, + "license": "MIT", + "bin": { + "tree-kill": "cli.js" + } + }, + "node_modules/tslib": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.9.1.tgz", + "integrity": "sha512-a4Ux9VKuIMHsOS7ENbEq6hK2ZGYIT27Rtl0E4eD9KqwxhKiQeMZE/9xb3IdYwWaSOyIBQLExhQgXDTYoiCxnWA==", + "license": "0BSD" + }, + "node_modules/typescript": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz", + "integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc" + }, + "engines": { + "node": ">=16.20.0" + }, + "optionalDependencies": { + "@typescript/typescript-aix-ppc64": "7.0.2", + "@typescript/typescript-darwin-arm64": "7.0.2", + "@typescript/typescript-darwin-x64": "7.0.2", + "@typescript/typescript-freebsd-arm64": "7.0.2", + "@typescript/typescript-freebsd-x64": "7.0.2", + "@typescript/typescript-linux-arm": "7.0.2", + "@typescript/typescript-linux-arm64": "7.0.2", + "@typescript/typescript-linux-loong64": "7.0.2", + "@typescript/typescript-linux-mips64el": "7.0.2", + "@typescript/typescript-linux-ppc64": "7.0.2", + "@typescript/typescript-linux-riscv64": "7.0.2", + "@typescript/typescript-linux-s390x": "7.0.2", + "@typescript/typescript-linux-x64": "7.0.2", + "@typescript/typescript-netbsd-arm64": "7.0.2", + "@typescript/typescript-netbsd-x64": "7.0.2", + "@typescript/typescript-openbsd-arm64": "7.0.2", + "@typescript/typescript-openbsd-x64": "7.0.2", + "@typescript/typescript-sunos-x64": "7.0.2", + "@typescript/typescript-win32-arm64": "7.0.2", + "@typescript/typescript-win32-x64": "7.0.2" + } + }, + "node_modules/undici-types": { + "version": "8.9.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.9.0.tgz", + "integrity": "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg==", + "dev": true, + "license": "MIT" + }, + "node_modules/universalify": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz", + "integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 10.0.0" + } + }, + "node_modules/untildify": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/untildify/-/untildify-4.0.0.tgz", + "integrity": "sha512-KK8xQ1mkzZeg9inewmFVDNkg3l5LUhoq9kN6iWYB/CC9YMG8HA+c1Q8HwDe6dEX7kErrEVNVBO3fWsVq5iDgtw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "dev": true, + "license": "MIT" + }, + "node_modules/which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "dev": true, + "license": "ISC", + "dependencies": { + "isexe": "^2.0.0" + }, + "bin": { + "node-which": "bin/node-which" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/wrap-ansi": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", + "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, + "node_modules/xcode": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/xcode/-/xcode-4.0.0.tgz", + "integrity": "sha512-dDaoWzc9EP2fp9FS9PVZUkyvFxY4y1TOV9pcKaKLk++Ei/qb97XDFEVP7GnW7Osii/TzVABJ0jq2F+9/lq0TVA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "simple-plist": "^1.3.1" + }, + "engines": { + "node": ">=14.17.0" + } + }, + "node_modules/xml2js": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.6.2.tgz", + "integrity": "sha512-T4rieHaC1EXcES0Kxxj4JWgaUQHDk+qwHcYOCFHfiwKz7tOVPLq7Hjq9dM1WCMhylqMEfP7hMcOIChvotiZegA==", + "dev": true, + "license": "MIT", + "dependencies": { + "sax": ">=0.6.0", + "xmlbuilder": "~11.0.0" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/xml2js/node_modules/xmlbuilder": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", + "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=4.0" + } + }, + "node_modules/xmlbuilder": { + "version": "15.1.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-15.1.1.tgz", + "integrity": "sha512-yMqGBqtXyeN1e3TGYvgNgDVZ3j84W4cwkOXQswghol6APgZWaff9lnbvN7MHYJOiXsvGPXtjTYJEiC9J2wv9Eg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8.0" + } + }, + "node_modules/yallist": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-5.0.0.tgz", + "integrity": "sha512-YgvUTfwqyc7UXVMrB+SImsVYSmTS8X/tSrtdNZMImM+n7+QTriRXyXim0mBrTXNeqzVF0KWGgHPeiyViFFrNDw==", + "dev": true, + "license": "BlueOak-1.0.0", + "engines": { + "node": ">=18" + } + }, + "node_modules/yauzl": { + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", + "integrity": "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==", + "dev": true, + "license": "MIT", + "dependencies": { + "buffer-crc32": "~0.2.3", + "fd-slicer": "~1.1.0" + } + } + } +} diff --git a/android/package.json b/android/package.json new file mode 100644 index 0000000..cb90233 --- /dev/null +++ b/android/package.json @@ -0,0 +1,21 @@ +{ + "name": "sonda-android", + "private": true, + "version": "0.0.0", + "description": "Capacitor shell that opens a Sonda server in an Android app", + "scripts": { + "test": "node --test test/*.test.mjs", + "sync": "cap sync android", + "apk": "cap sync android && cd android && ./gradlew assembleDebug" + }, + "dependencies": { + "@capacitor/android": "^8.6.2", + "@capacitor/app": "^8.1.2", + "@capacitor/core": "^8.6.2" + }, + "devDependencies": { + "@capacitor/cli": "^8.6.2", + "typescript": "^7.0.2" + }, + "type": "module" +} diff --git a/android/test/server.test.mjs b/android/test/server.test.mjs new file mode 100644 index 0000000..bdc3361 --- /dev/null +++ b/android/test/server.test.mjs @@ -0,0 +1,23 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { normalizeAddress } from '../www/server.js' + +test('a bare host gets https', () => { + assert.equal(normalizeAddress('music.example.org'), 'https://music.example.org') +}) + +test('a scheme that was typed is kept, and so is the port', () => { + assert.equal(normalizeAddress('http://192.168.1.5:8080'), 'http://192.168.1.5:8080') + assert.equal(normalizeAddress('https://music.example.org:8443'), 'https://music.example.org:8443') +}) + +test('spaces, a trailing slash, a path and a query are dropped', () => { + assert.equal(normalizeAddress(' https://music.example.org/ '), 'https://music.example.org') + assert.equal(normalizeAddress('https://music.example.org/albums/3?x=1#top'), 'https://music.example.org') +}) + +test('nothing, or something that is not an address, is refused', () => { + for (const bad of ['', ' ', null, undefined, 'ftp://music.example.org', 'http://', 'not an address']) { + assert.equal(normalizeAddress(bad), null, String(bad)) + } +}) diff --git a/android/www/index.html b/android/www/index.html new file mode 100644 index 0000000..1a7bda9 --- /dev/null +++ b/android/www/index.html @@ -0,0 +1,94 @@ + + + + + + + Sonda + + + +
+

Sonda

+

Opening your serverโ€ฆ

+ +
+ + + diff --git a/android/www/server.js b/android/www/server.js new file mode 100644 index 0000000..d0cc46e --- /dev/null +++ b/android/www/server.js @@ -0,0 +1,16 @@ +/** Turns what was typed into the address of a Sonda server, or null when it is not one. */ +export function normalizeAddress(input) { + const text = String(input ?? '').trim() + if (text === '') return null + const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(text) ? text : `https://${text}` + let url + try { + url = new URL(withScheme) + } catch { + return null + } + if (url.protocol !== 'https:' && url.protocol !== 'http:') return null + if (url.hostname === '') return null + // The app lives at the root of the server: a path, a query or a fragment typed by mistake is dropped. + return url.origin +} diff --git a/cmd/sondad/buildinfo.go b/cmd/sondad/buildinfo.go new file mode 100644 index 0000000..f5afc0c --- /dev/null +++ b/cmd/sondad/buildinfo.go @@ -0,0 +1,41 @@ +package main + +import ( + "runtime/debug" + + httpapi "git.kmalloc.org/squid/sonda/internal/api/http" +) + +// shortRevision is how many characters of the commit the app shows. +const shortRevision = 7 + +// revision reads the commit the binary was built from, and whether the files had changes that were not committed. +// Go records both in a build made inside a git checkout; a build without them (a copy of the sources, or +// -buildvcs=false) has no revision. +func revision(info *debug.BuildInfo) (rev string, modified bool) { + if info == nil { + return "", false + } + for _, s := range info.Settings { + switch s.Key { + case "vcs.revision": + rev = s.Value + if len(rev) > shortRevision { + rev = rev[:shortRevision] + } + case "vcs.modified": + modified = s.Value == "true" + } + } + if rev == "" { + modified = false + } + return rev, modified +} + +// build says which Sonda this is, for the signed-in users to see. +func build() httpapi.Build { + info, _ := debug.ReadBuildInfo() + rev, modified := revision(info) + return httpapi.Build{Version: version, Revision: rev, Modified: modified} +} diff --git a/cmd/sondad/buildinfo_test.go b/cmd/sondad/buildinfo_test.go new file mode 100644 index 0000000..9c353ce --- /dev/null +++ b/cmd/sondad/buildinfo_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "runtime/debug" + "testing" +) + +func settings(kv ...string) *debug.BuildInfo { + b := &debug.BuildInfo{} + for i := 0; i+1 < len(kv); i += 2 { + b.Settings = append(b.Settings, debug.BuildSetting{Key: kv[i], Value: kv[i+1]}) + } + return b +} + +func TestRevisionIsTheShortCommit(t *testing.T) { + rev, modified := revision(settings("vcs", "git", "vcs.revision", "69c85a7e1d0f4b2c8a9e3f5d6b7a8c9d0e1f2a3b", "vcs.modified", "false")) + if rev != "69c85a7" || modified { + t.Errorf("got %q, %v", rev, modified) + } +} + +func TestRevisionSaysWhenTheTreeWasChanged(t *testing.T) { + rev, modified := revision(settings("vcs.revision", "69c85a7e1d0f", "vcs.modified", "true")) + if rev != "69c85a7" || !modified { + t.Errorf("got %q, %v", rev, modified) + } +} + +func TestRevisionIsEmptyWithoutVersionControlInfo(t *testing.T) { + if rev, modified := revision(settings()); rev != "" || modified { + t.Errorf("got %q, %v", rev, modified) + } + if rev, modified := revision(nil); rev != "" || modified { + t.Errorf("nil: got %q, %v", rev, modified) + } +} + +func TestShortRevisionIsKeptWhole(t *testing.T) { + if rev, _ := revision(settings("vcs.revision", "abc")); rev != "abc" { + t.Errorf("got %q", rev) + } +} diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index b727355..eaadfff 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -10,44 +10,60 @@ import ( "os/signal" "syscall" "time" + _ "time/tzdata" // the time zones that browsers send, also on a machine that has none "github.com/gin-gonic/gin" "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/about" - httpapi "codeberg.org/squidd/sonda/internal/api/http" - "codeberg.org/squidd/sonda/internal/auth" - "codeberg.org/squidd/sonda/internal/catalog" - "codeberg.org/squidd/sonda/internal/config" - "codeberg.org/squidd/sonda/internal/discover" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/duplicates" - "codeberg.org/squidd/sonda/internal/feedback" - "codeberg.org/squidd/sonda/internal/images" - "codeberg.org/squidd/sonda/internal/jobs" - "codeberg.org/squidd/sonda/internal/library" - "codeberg.org/squidd/sonda/internal/listening" - "codeberg.org/squidd/sonda/internal/playlists" - "codeberg.org/squidd/sonda/internal/providers" - "codeberg.org/squidd/sonda/internal/providers/commons" - "codeberg.org/squidd/sonda/internal/providers/coverart" - "codeberg.org/squidd/sonda/internal/providers/lastfm" - "codeberg.org/squidd/sonda/internal/providers/lidarr" - "codeberg.org/squidd/sonda/internal/providers/musicbrainz" - "codeberg.org/squidd/sonda/internal/providers/outbound" - "codeberg.org/squidd/sonda/internal/providers/wikipedia" - "codeberg.org/squidd/sonda/internal/storage" - "codeberg.org/squidd/sonda/internal/streaming" - "codeberg.org/squidd/sonda/internal/taste" - "codeberg.org/squidd/sonda/internal/usersettings" - "codeberg.org/squidd/sonda/internal/webui" - "codeberg.org/squidd/sonda/migrations" + "git.kmalloc.org/squid/sonda/internal/about" + "git.kmalloc.org/squid/sonda/internal/affinity" + httpapi "git.kmalloc.org/squid/sonda/internal/api/http" + "git.kmalloc.org/squid/sonda/internal/auth" + "git.kmalloc.org/squid/sonda/internal/catalog" + "git.kmalloc.org/squid/sonda/internal/config" + "git.kmalloc.org/squid/sonda/internal/devices" + "git.kmalloc.org/squid/sonda/internal/discover" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/duplicates" + "git.kmalloc.org/squid/sonda/internal/favorites" + "git.kmalloc.org/squid/sonda/internal/federation" + "git.kmalloc.org/squid/sonda/internal/feedback" + "git.kmalloc.org/squid/sonda/internal/images" + "git.kmalloc.org/squid/sonda/internal/jobs" + "git.kmalloc.org/squid/sonda/internal/library" + "git.kmalloc.org/squid/sonda/internal/listening" + "git.kmalloc.org/squid/sonda/internal/mastodon" + "git.kmalloc.org/squid/sonda/internal/mixes" + "git.kmalloc.org/squid/sonda/internal/naming" + "git.kmalloc.org/squid/sonda/internal/opensubsonic" + "git.kmalloc.org/squid/sonda/internal/playlists" + "git.kmalloc.org/squid/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/providers/anthropic" + "git.kmalloc.org/squid/sonda/internal/providers/commons" + "git.kmalloc.org/squid/sonda/internal/providers/coverart" + "git.kmalloc.org/squid/sonda/internal/providers/lastfm" + "git.kmalloc.org/squid/sonda/internal/providers/lidarr" + "git.kmalloc.org/squid/sonda/internal/providers/musicbrainz" + "git.kmalloc.org/squid/sonda/internal/providers/outbound" + "git.kmalloc.org/squid/sonda/internal/providers/wikipedia" + "git.kmalloc.org/squid/sonda/internal/smart" + "git.kmalloc.org/squid/sonda/internal/spotifyimport" + "git.kmalloc.org/squid/sonda/internal/stats" + "git.kmalloc.org/squid/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/streaming" + "git.kmalloc.org/squid/sonda/internal/taste" + "git.kmalloc.org/squid/sonda/internal/usersettings" + "git.kmalloc.org/squid/sonda/internal/waveform" + "git.kmalloc.org/squid/sonda/internal/webui" + "git.kmalloc.org/squid/sonda/migrations" ) const defaultConfigFile = "sonda.toml" -// version goes into the User-Agent sent to providers. -const version = "0.1.0" +// version goes into the User-Agent sent to providers, and is what GET /version says. The release sets it +// (-ldflags "-X main.version=1.2.3": see the Dockerfile and the Makefile); a build that was not made by a release says +// "dev". +var version = "dev" // lastfmMinInterval spaces the requests to Last.fm well below its limit. const lastfmMinInterval = 250 * time.Millisecond @@ -139,6 +155,17 @@ func run() error { scanner := library.NewScanner(libraryRepo, library.TaglibReader{}, library.NewHub(), cfg.Library.ScanWorkers) scans := library.NewManager(scanner, libraryRepo) logScanResults(logger, scans) + // The libraries are scanned by themselves when their music changed: looking costs only the names of the files. + if cfg.Library.AutoScanMinutes > 0 { + auto := library.NewAutoScanner(scans, libraryRepo, time.Duration(cfg.Library.AutoScanMinutes)*time.Minute) + auto.OnScan = func(lib library.Library, signatureErr, _ error) { + if signatureErr != nil { // a NAS that is not mounted says this all the time: not news + logger.Debug("auto scan: the folder of a library cannot be read", zap.Int64("library", lib.ID), zap.Error(signatureErr)) + } + } + go auto.Run(ctx) + logger.Info("libraries are scanned by themselves when their music changed", zap.Int("every_minutes", cfg.Library.AutoScanMinutes)) + } libraries := library.NewLibraries(libraryRepo) libraries.SetBusy(func() bool { return scans.Status().Running }) @@ -154,8 +181,9 @@ func run() error { // MusicBrainz wants a contact in the User-Agent, which an admin can change: it // is read again for every request. musicbrainzClient := musicbrainz.New(outbound.New(outbound.Options{ - UserAgentFunc: providerSettings.UserAgentFor(providers.MusicBrainz, version), - MinInterval: musicbrainzMinInterval, + UserAgentFunc: providerSettings.UserAgentFor(providers.MusicBrainz, version), + MinInterval: musicbrainzMinInterval, + FollowRedirect: musicbrainz.RedirectAllowed, // an id that was merged leads to the new one, and to nothing else }), musicbrainz.BaseURL) providerSettings.SetChecker(providers.MusicBrainz, func(ctx context.Context, _ string) error { return musicbrainzClient.Probe(ctx) @@ -246,6 +274,12 @@ func run() error { } return discoveryRepo.Purge(ctx, name) }) + // Claude names playlists when an admin turned the provider on with a key. Redirects are never followed there. + claudeClient := anthropic.New() + namingRepo := storage.NewNamingRepo(db) + namingService := naming.NewService(providerSettings, claudeClient, namingRepo, logger) + namingService.SetLedger(namingRepo) // every question to Claude is counted, to show what it costs + providerSettings.SetChecker(providers.Anthropic, claudeClient.Probe) descriptions := about.NewService(discoveryRepo, about.WikipediaSource{MusicBrainz: musicbrainzClient, Wikipedia: wikipediaClient}, providerSettings, cfg.Wikipedia.Languages, logger) discoveries := discovery.NewService(discoveryRepo, lastfmClient, providerSettings, logger) @@ -258,6 +292,7 @@ func run() error { tasteService := taste.NewService(tasteRepo, taste.Options{MinListen: cfg.Listening.MinListen(), HalfLife: cfg.Taste.HalfLife()}) userSettings := usersettings.NewService(storage.NewUserSettingsRepo(db)) feedbacks := feedback.NewService(storage.NewFeedbackRepo(db)) + affinityRepo := storage.NewAffinityRepo(db) weights := discover.Weights{ Affinity: cfg.Scoring.WeightAffinity, Genre: cfg.Scoring.WeightGenre, Preference: cfg.Scoring.WeightPreference, Discovery: cfg.Scoring.WeightDiscovery, @@ -274,23 +309,54 @@ func run() error { logger.Warn("library.allow_delete is on: an admin can delete duplicate files from the music folders") } + // What a person listens to is shared on their Mastodon account only if they connected one. + playlistSvc := playlists.NewService(storage.NewPlaylistRepo(db)) + mastodonSvc := mastodon.NewService(storage.NewMastodonRepo(db), mastodon.NewRemote(providers.UserAgent(version, "")), logger) + go mastodonSvc.Run(ctx) + + // Other apps (OpenSubsonic) log in with the password of a device, never with the account password. + deviceSvc := devices.NewService(storage.NewDevicesRepo(db), []byte(cfg.Auth.JWTSecret)) + catalogSvc := catalog.NewService(storage.NewCatalogRepo(db)) + streamSvc := streaming.NewService(libraryRepo) + mixSvc := mixes.NewService(storage.NewMixRepo(db), tasteService, cfg.Listening.MinListen()) + favoriteSvc := favorites.NewService(storage.NewFavoritesRepo(db), catalogSvc) + listeningSvc := listening.NewService(storage.NewListeningRepo(db), cfg.Listening.MinListen()) + subsonicRepo := storage.NewSubsonicRepo(db, cfg.Listening.MinListen()) + router := httpapi.NewRouter(httpapi.Deps{ - Logger: logger, - Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, cfg.Auth.RefreshTTL()), - Tokens: tokens, - Listening: listening.NewService(storage.NewListeningRepo(db), cfg.Listening.MinListen()), + Logger: logger, + Build: build(), + Stats: stats.NewService(storage.NewStatsRepo(db), cfg.Listening.MinListen()), + Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, cfg.Auth.RefreshTTL()), + Tokens: tokens, + Listening: listeningSvc, + Mastodon: mastodonSvc, + Devices: deviceSvc, + Favorites: favoriteSvc, + OpenSubsonic: &opensubsonic.Deps{ + Login: deviceSvc, Catalog: catalogSvc, Libraries: libraries, + Extras: subsonicRepo, Lists: subsonicRepo, Streams: streamSvc, Covers: pictures, ArtistImages: pictures, + Playlists: playlistSvc, Listens: listeningSvc, Announcer: mastodonSvc, Favorites: favoriteSvc, Radio: mixSvc, Settings: userSettings, + ServerVersion: version, + }, Scan: scanControl{ctx: ctx, m: scans}, Libraries: libraries, - Catalog: catalog.NewService(storage.NewCatalogRepo(db)), + Catalog: catalogSvc, Providers: providerSettings, Lidarr: lidarrClient, LidarrArtists: lidarr.NewService(discoveryRepo, providerSettings, lidarrClient), + LidarrRequests: lidarr.NewRequests(storage.NewLidarrRequestRepo(db)), Discovery: discoveries, Tags: tags, Taste: tasteService, Discover: discover.NewService(tasteService, discoveries, tags, tasteRepo, userSettings, feedbacks, weights), Feedback: feedbacks, - Playlists: playlists.NewService(storage.NewPlaylistRepo(db)), + Playlists: playlistSvc, + Suggestions: affinity.NewService(affinityRepo, affinityRepo, feedbacks, affinityRepo, userSettings, cfg.Listening.MinListen()), + SpotifyImport: spotifyimport.NewService(storage.NewCatalogRepo(db), storage.NewListeningRepo(db), playlistSvc, storage.NewSpotifyImportRepo(db), storage.NewOutsidePlayRepo(db), storage.SearchKey, cfg.Listening.MinListen()), + Mixes: mixSvc, + Smart: smart.NewService(storage.NewSmartRepo(db), tasteService, cfg.Listening.MinListen()), + Naming: namingService, Images: pictures, CoversJob: jobs.Runner{Ctx: ctx, Job: images.NewCoversJob(pictures, imageRepo)}, ArtistImagesJob: jobs.Runner{Ctx: ctx, Job: images.NewArtistImagesJob(pictures, imageRepo)}, @@ -300,7 +366,13 @@ func run() error { ArtistImages: pictures, About: descriptions, Settings: userSettings, - Streams: streaming.NewService(libraryRepo), + Streams: streamSvc, + Federation: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()), + PeerCatalog: catalog.NewService(storage.NewSharedCatalogRepo(db)), + PeerStreams: streaming.NewService(libraryRepo.Shared()), + PeerWaveform: waveform.NewService(storage.NewWaveformRepo(db), streaming.NewService(libraryRepo.Shared())), + PeerCovers: images.StoredOnly{Service: pictures}, + Waveform: waveform.NewService(storage.NewWaveformRepo(db), streaming.NewService(libraryRepo)), Web: webui.FS(), }) diff --git a/cmd/sondad/scan.go b/cmd/sondad/scan.go index 0679a67..0d976b8 100644 --- a/cmd/sondad/scan.go +++ b/cmd/sondad/scan.go @@ -5,7 +5,7 @@ import ( "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/library" + "git.kmalloc.org/squid/sonda/internal/library" ) // scanControl lets the HTTP layer start scans that live as long as the diff --git a/compose.yaml b/compose.yaml index 041bc88..bf456f3 100644 --- a/compose.yaml +++ b/compose.yaml @@ -6,6 +6,9 @@ # SONDA_PORT=8080 # optional, the port on this machine # # then: docker compose up -d --build +# +# To use the image that is published, and not build one: delete the two lines "build: ." and "image: sonda:local" below, +# put "image: git.kmalloc.org/squid/sonda:latest" in their place, and run: docker compose pull && docker compose up -d services: sonda: build: . diff --git a/go.mod b/go.mod index a8c971e..5fba4b6 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,14 @@ -module codeberg.org/squidd/sonda +module git.kmalloc.org/squid/sonda go 1.27.1 require ( + github.com/anthropics/anthropic-sdk-go/v2 v2.9.0 + github.com/eaburns/flac v0.0.0-20171003200620-9a6fb92396d1 github.com/getkin/kin-openapi v0.153.0 github.com/gin-gonic/gin v1.12.0 github.com/golang-jwt/jwt/v5 v5.3.1 + github.com/hajimehoshi/go-mp3 v0.3.4 github.com/jackc/pgx/v5 v5.12.0 github.com/pelletier/go-toml/v2 v2.4.3 github.com/swaggest/swgui v1.8.9 @@ -21,13 +24,18 @@ require ( ) require ( + github.com/bahlo/generic-list-go v0.2.0 // indirect + github.com/buger/jsonparser v1.1.2 // indirect github.com/bytedance/gopkg v0.1.3 // indirect github.com/bytedance/sonic v1.15.0 // indirect github.com/bytedance/sonic/loader v0.5.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect github.com/dustin/go-humanize v1.1.0 // indirect + github.com/eaburns/bit v0.0.0-20131029213740-7bd5cd37375d // indirect github.com/gabriel-vasile/mimetype v1.4.12 // indirect github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect github.com/go-openapi/jsonpointer v0.22.5 // indirect github.com/go-openapi/swag/jsonname v0.25.5 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -36,6 +44,7 @@ require ( github.com/goccy/go-json v0.10.5 // indirect github.com/goccy/go-yaml v1.19.2 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/invopop/jsonschema v0.14.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.3 // indirect @@ -47,12 +56,18 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/ncruces/go-strftime v1.1.0 // indirect + github.com/pb33f/ordered-map/v2 v2.3.1 // indirect github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect github.com/quic-go/qpack v0.6.0 // indirect github.com/quic-go/quic-go v0.59.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 // indirect + github.com/standard-webhooks/standard-webhooks/libraries v0.0.1 // indirect github.com/tetratelabs/wazero v1.11.1-0.20260428013916-2bbd517b7633 // indirect + github.com/tidwall/gjson v1.18.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/tidwall/sjson v1.2.5 // indirect github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.3.1 // indirect @@ -60,6 +75,10 @@ require ( github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + go.opentelemetry.io/auto/sdk v1.1.0 // indirect + go.opentelemetry.io/otel v1.33.0 // indirect + go.opentelemetry.io/otel/metric v1.33.0 // indirect + go.opentelemetry.io/otel/trace v1.33.0 // indirect go.uber.org/multierr v1.11.0 // indirect go.yaml.in/yaml/v4 v4.0.0 // indirect golang.org/x/arch v0.22.0 // indirect diff --git a/go.sum b/go.sum index 2d5aebe..92a05cd 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,47 @@ +cloud.google.com/go/auth v0.7.2 h1:uiha352VrCDMXg+yoBtaD0tUF4Kv9vrtrWPYXwutnDE= +cloud.google.com/go/auth v0.7.2/go.mod h1:VEc4p5NNxycWQTMQEDQF0bd6aTMb6VgYDXEwiJJQAbs= +cloud.google.com/go/auth/oauth2adapt v0.2.3 h1:MlxF+Pd3OmSudg/b1yZ5lJwoXCEaeedAguodky1PcKI= +cloud.google.com/go/auth/oauth2adapt v0.2.3/go.mod h1:tMQXOfZzFuNuUxOypHlQEXgdfX5cuhwU+ffUuXRJE8I= +cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY= +cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/anthropics/anthropic-sdk-go/v2 v2.9.0 h1:kn8rCJlgcOTd5/yjofmTuUdfMRP14OL9vDS4tsdJqZg= +github.com/anthropics/anthropic-sdk-go/v2 v2.9.0/go.mod h1:LAOInMKgn6reTIrPvKXa5weyqzxsJkxSBuzhnxzukpQ= +github.com/aws/aws-sdk-go-v2 v1.38.0 h1:UCRQ5mlqcFk9HJDIqENSLR3wiG1VTWlyUfLDEvY7RxU= +github.com/aws/aws-sdk-go-v2 v1.38.0/go.mod h1:9Q0OoGQoboYIAJyslFyF1f5K1Ryddop8gqMhWx/n4Wg= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 h1:tW1/Rkad38LA15X4UQtjXZXNKsCgkshC3EbmcUmghTg= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3/go.mod h1:UbnqO+zjqk3uIt9yCACHJ9IVNhyhOCnYk8yA19SAWrM= +github.com/aws/aws-sdk-go-v2/config v1.27.27 h1:HdqgGt1OAP0HkEDDShEl0oSYa9ZZBSOmKpdpsDMdO90= +github.com/aws/aws-sdk-go-v2/config v1.27.27/go.mod h1:MVYamCg76dFNINkZFu4n4RjDixhVr51HLj4ErWzrVwg= +github.com/aws/aws-sdk-go-v2/credentials v1.17.27 h1:2raNba6gr2IfA0eqqiP2XiQ0UVOpGPgDSi0I9iAP+UI= +github.com/aws/aws-sdk-go-v2/credentials v1.17.27/go.mod h1:gniiwbGahQByxan6YjQUMcW4Aov6bLC3m+evgcoN4r4= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 h1:KreluoV8FZDEtI6Co2xuNk/UqI9iwMrOx/87PBNIKqw= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11/go.mod h1:SeSUYBLsMYFoRvHE0Tjvn7kbxaUhl75CJi1sbfhMxkU= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 h1:dT3MqvGhSoaIhRseqw2I0yH81l7wiR2vjs57O51EAm8= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3/go.mod h1:GlAeCkHwugxdHaueRr4nhPuY+WW+gR8UjlcqzPr1SPI= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 h1:HGErhhrxZlQ044RiM+WdoZxp0p+EGM62y3L6pwA4olE= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17/go.mod h1:RkZEx4l0EHYDJpWppMJ3nD9wZJAa8/0lq9aVC+r2UII= +github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 h1:BXx0ZIxvrJdSgSvKTZ+yRBeSqqgPM89VPlulEcl37tM= +github.com/aws/aws-sdk-go-v2/service/sso v1.22.4/go.mod h1:ooyCOXjvJEsUw7x+ZDHeISPMhtwI3ZCB7ggFMcFfWLU= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 h1:yiwVzJW2ZxZTurVbYWA7QOrAaCYQR72t0wrSBfoesUE= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4/go.mod h1:0oxfLkpz3rQ/CHlx5hB7H69YUpFiI1tql6Q6Ne+1bCw= +github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudrvuKpDKgMVRlepGE= +github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ= +github.com/aws/smithy-go v1.22.5 h1:P9ATCXPMb2mPjYBgueqJNCA5S9UfktsW0tTxi+a7eqw= +github.com/aws/smithy-go v1.22.5/go.mod h1:t1ufH5HMublsJYulve2RKmHDC15xu1f26kHCp/HgceI= +github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/bool64/dev v0.2.45 h1:3nLKhAS/6Oklk3Mt2lHYSN/Cb4tdAD77KLwzeP+6eYE= github.com/bool64/dev v0.2.45/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= +github.com/buger/jsonparser v1.1.2 h1:frqHqw7otoVbk5M8LlE/L7HTnIq2v9RX6EJ48i9AxJk= +github.com/buger/jsonparser v1.1.2/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= @@ -15,8 +55,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI= +github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ= github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= +github.com/eaburns/bit v0.0.0-20131029213740-7bd5cd37375d h1:HB5J9+f1xpkYLgWQ/RqEcbp3SEufyOIMYLoyKNKiG7E= +github.com/eaburns/bit v0.0.0-20131029213740-7bd5cd37375d/go.mod h1:CHkHWWZ4kbGY6jEy1+qlitDaCtRgNvCOQdakj/1Yl/Q= +github.com/eaburns/flac v0.0.0-20171003200620-9a6fb92396d1 h1:wl/ggSfTHqAy46hyzw1IlrUYwjqmXYvbJyPdH3rT7YE= +github.com/eaburns/flac v0.0.0-20171003200620-9a6fb92396d1/go.mod h1:frG94byMNy+1CgGrQ25dZ+17tf98EN+OYBQL4Zh612M= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= github.com/getkin/kin-openapi v0.153.0 h1:Qo4qWW+Pj8o90LUTE6MDyG5TJAtWnfvrcWOBcWg7eBw= @@ -25,6 +73,11 @@ github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-openapi/jsonpointer v0.22.5 h1:8on/0Yp4uTb9f4XvTrM2+1CPrV05QPZXu+rvu2o9jcA= github.com/go-openapi/jsonpointer v0.22.5/go.mod h1:gyUR3sCvGSWchA2sUBJGluYMbe1zazrYWIkWPjjMUY0= github.com/go-openapi/swag/jsonname v0.25.5 h1:8p150i44rv/Drip4vWI3kGi9+4W9TdI3US3uUYSFhSo= @@ -45,17 +98,32 @@ github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY= github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/jsonschema-go v0.4.2 h1:tmrUohrwoLZZS/P3x7ex0WAVknEkBZM46iALbcqoRA8= +github.com/google/jsonschema-go v0.4.2/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE= github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50 h1:PlNWJRxu5APSV0pWqfIsZ0qt9OOGHSWfZQ4zvx+WgWs= github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50/go.mod h1:yUo1AMMpsPIDFGuwDK8nLcf8+vvAd5l2O6JDZ6hpwrM= +github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= +github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= +github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68= +github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo= +github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/invopop/jsonschema v0.14.0 h1:MHQqLhvpNUZfw+hM3AZDYK7jxO8FZoQeQM77g8iyZjg= +github.com/invopop/jsonschema v0.14.0/go.mod h1:ygm6C2EaVNMBDPpaPlnOA2pFAxBnxGjFlMZABxm9n2I= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= @@ -74,6 +142,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/modelcontextprotocol/go-sdk v1.3.1 h1:TfqtNKOIWN4Z1oqmPAiWDC2Jq7K9OdJaooe0teoXASI= +github.com/modelcontextprotocol/go-sdk v1.3.1/go.mod h1:DgVX498dMD8UJlseK1S5i1T4tFz2fkBk4xogC3D15nw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -81,6 +151,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk= github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g= +github.com/pb33f/ordered-map/v2 v2.3.1 h1:5319HDO0aw4DA4gzi+zv4FXU9UlSs3xGZ40wcP1nBjY= +github.com/pb33f/ordered-map/v2 v2.3.1/go.mod h1:qxFQgd0PkVUtOMCkTapqotNgzRhMPL7VvaHKbd1HnmQ= github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY= github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -95,6 +167,12 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 h1:1EYB5IzjZawrrnELUi78f9fPu57HuXjmddZPjrls/28= github.com/santhosh-tekuri/jsonschema/v6 v6.0.3/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU= +github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc= +github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg= +github.com/segmentio/encoding v0.5.4 h1:OW1VRern8Nw6ITAtwSZ7Idrl3MXCFwXHPgqESYfvNt0= +github.com/segmentio/encoding v0.5.4/go.mod h1:HS1ZKa3kSN32ZHVZ7ZLPLXWvOVIiZtyJnO1gPH1sKt0= +github.com/standard-webhooks/standard-webhooks/libraries v0.0.1 h1:uOfcYT+3QungH6tIGSVCR/Y3KJmgJiHcojJbMTPDZAI= +github.com/standard-webhooks/standard-webhooks/libraries v0.0.1/go.mod h1:L1MQhA6x4dn9r007T033lsaZMv9EmBAdXyU/+EF40fo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -111,6 +189,16 @@ github.com/swaggest/swgui v1.8.9 h1:cxAgIwouPpZPlvX68jY5fpwarzLbkc8/IL6DMj+H460= github.com/swaggest/swgui v1.8.9/go.mod h1:eTJfgwudbyw9xMwqO26vs82ei2u6//JnUAofx2vGB3M= github.com/tetratelabs/wazero v1.11.1-0.20260428013916-2bbd517b7633 h1:6GN/lazdqr69FIzz1U6c4TF/ppE2dInMR4GzU9QKxjg= github.com/tetratelabs/wazero v1.11.1-0.20260428013916-2bbd517b7633/go.mod h1:3ghOSSWYnzX0zd/3Ns4ni2tKxcXDE9/QgkwuH1PW3Rs= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= +github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= @@ -129,8 +217,26 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= +github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= +github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= +go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 h1:4Pp6oUg3+e/6M4C0A/3kJ2VYa++dsWVTtGgLVj5xtHg= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0/go.mod h1:Mjt1i1INqiaoZOMGR1RIUJN+i3ChKoFRqzrRQhlkbs0= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw= +go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw= +go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I= +go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ= +go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M= +go.opentelemetry.io/otel/sdk v1.33.0 h1:iax7M131HuAm9QkZotNHEfstof92xM+N8sr3uHXc2IM= +go.opentelemetry.io/otel/sdk v1.33.0/go.mod h1:A1Q5oi7/9XaMlIWzPSxLRWOI8nG3FnzHJNbiENQuihM= +go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s= +go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck= go.senan.xyz/taglib v0.15.0 h1:8z25qOiNm4o58kV37INltLGRKHelz1QqGBIZ/HWMyjM= go.senan.xyz/taglib v0.15.0/go.mod h1:+k5CamBu88xgydgNGJjugYVeafoCCswoGjpw5w5CvD4= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= @@ -153,17 +259,30 @@ golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs= golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU= golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI= +golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU= golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= +google.golang.org/api v0.189.0 h1:equMo30LypAkdkLMBqfeIqtyAnlyig1JSZArl4XPwdI= +google.golang.org/api v0.189.0/go.mod h1:FLWGJKb0hb+pU2j+rJqwbnsF+ym+fQs73rbJ+KAUgy8= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade h1:oCRSWfwGXQsqlVdErcyTt4A93Y8fo0/9D4b1gnI++qo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= +google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= +google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/about/about.go b/internal/about/about.go index 17cb658..87974ea 100644 --- a/internal/about/about.go +++ b/internal/about/about.go @@ -23,8 +23,8 @@ import ( "go.uber.org/zap" "golang.org/x/sync/singleflight" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/providers" ) // Provider is the provider the descriptions come from. diff --git a/internal/about/about_test.go b/internal/about/about_test.go index 276bf7a..c37fbb5 100644 --- a/internal/about/about_test.go +++ b/internal/about/about_test.go @@ -10,8 +10,8 @@ import ( "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/providers" ) const ( diff --git a/internal/about/job.go b/internal/about/job.go index 1b56fda..6ff0d31 100644 --- a/internal/about/job.go +++ b/internal/about/job.go @@ -5,8 +5,8 @@ import ( "errors" "time" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/jobs" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/jobs" ) // Outcomes of the descriptions job, as the summary counts them. diff --git a/internal/about/job_test.go b/internal/about/job_test.go index 63314b8..c7ad351 100644 --- a/internal/about/job_test.go +++ b/internal/about/job_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/jobs" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/jobs" ) func TestEnsuringAFreshDescriptionAsksNobodyUnlessARefreshIsWanted(t *testing.T) { diff --git a/internal/about/source.go b/internal/about/source.go index c9ad02c..ef88c14 100644 --- a/internal/about/source.go +++ b/internal/about/source.go @@ -4,8 +4,8 @@ import ( "context" "errors" - "codeberg.org/squidd/sonda/internal/providers/musicbrainz" - "codeberg.org/squidd/sonda/internal/providers/wikipedia" + "git.kmalloc.org/squid/sonda/internal/providers/musicbrainz" + "git.kmalloc.org/squid/sonda/internal/providers/wikipedia" ) // WikipediaSource follows the chain with the real clients: MusicBrainz for the link to diff --git a/internal/about/source_test.go b/internal/about/source_test.go index b2a1f90..8a24858 100644 --- a/internal/about/source_test.go +++ b/internal/about/source_test.go @@ -10,11 +10,11 @@ import ( "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/providers" - "codeberg.org/squidd/sonda/internal/providers/musicbrainz" - "codeberg.org/squidd/sonda/internal/providers/outbound" - "codeberg.org/squidd/sonda/internal/providers/wikipedia" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/providers/musicbrainz" + "git.kmalloc.org/squid/sonda/internal/providers/outbound" + "git.kmalloc.org/squid/sonda/internal/providers/wikipedia" ) // fakeWeb answers for MusicBrainz, Wikidata and Wikipedia on one server. diff --git a/internal/affinity/model.go b/internal/affinity/model.go new file mode 100644 index 0000000..82ecea4 --- /dev/null +++ b/internal/affinity/model.go @@ -0,0 +1,154 @@ +// Package affinity learns which artists a person plays together, from their own listening: the plays are split into +// sittings, and two artists that often turn up in the same sitting are linked. It is a count over the person's own +// data, with no provider and no training service; it can be built again in a moment. +package affinity + +import ( + "math" + "sort" + "time" +) + +// Options are the knobs of the model. +type Options struct { + // Gap is the longest pause inside one sitting: a play that follows the one before by more than this starts another. + Gap time.Duration + // MaxArtists is how many different artists a sitting may have to teach anything: a sitting of a whole library on + // shuffle says nothing about what goes together. + MaxArtists int + // MinTogether is in how many sittings two artists must meet to be linked: once is chance. + MinTogether int + // SeedHalfLife is how long it takes a play to weigh half as much when the artists are ranked by how much they + // are played. It is long, because this is the whole history and not only the last months. + SeedHalfLife time.Duration +} + +// DefaultOptions are what Sonda uses. +func DefaultOptions() Options { + return Options{Gap: 30 * time.Minute, MaxArtists: 60, MinTogether: 2, SeedHalfLife: 2 * 365 * 24 * time.Hour} +} + +// Play is one play that counts as listened: the artist (as a key that is the same for the same artist wherever the +// play comes from) and when it ended. +type Play struct { + Key string + Name string + At time.Time +} + +// Artist is what the model knows of one artist. +type Artist struct { + Key string + Name string + Plays int + Sessions int // the sittings it was in that were used for the links + Last time.Time // its last play + Weight float64 // its plays, each worth 1 and halving every SeedHalfLife +} + +// Link says that two artists are played together. A is before B. Score is from 0 to 1: the sittings they share over +// the square root of the product of the sittings of each (a cosine), so a pair that is always together scores 1 +// and a pair of which one is everywhere scores low. +type Link struct { + A, B string + Score float64 +} + +// Model is what was learned. +type Model struct { + Artists []Artist // by key + Links []Link // by A, then B + Plays int // the plays read + Sessions int // the sittings they made +} + +type pair struct{ a, b string } + +// Build learns from the plays. now is the moment the age of a play is counted to. +func Build(plays []Play, now time.Time, o Options) Model { + sorted := make([]Play, 0, len(plays)) + for _, p := range plays { + if p.Key != "" { + sorted = append(sorted, p) + } + } + sort.SliceStable(sorted, func(i, j int) bool { return sorted[i].At.Before(sorted[j].At) }) + + artists := map[string]*Artist{} + use := func(p Play) *Artist { + a := artists[p.Key] + if a == nil { + a = &Artist{Key: p.Key, Name: p.Name} + artists[p.Key] = a + } + return a + } + sittings := map[string]int{} + together := map[pair]int{} + m := Model{Plays: len(sorted)} + + var current map[string]bool + closeSitting := func() { + if len(current) == 0 { + return + } + m.Sessions++ + if len(current) > o.MaxArtists { + return + } + keys := make([]string, 0, len(current)) + for k := range current { + keys = append(keys, k) + sittings[k]++ + } + sort.Strings(keys) + for i := range keys { + for j := i + 1; j < len(keys); j++ { + together[pair{keys[i], keys[j]}]++ + } + } + } + var previous time.Time + for i, p := range sorted { + if i == 0 || p.At.Sub(previous) > o.Gap { + closeSitting() + current = map[string]bool{} + } + previous = p.At + current[p.Key] = true + + a := use(p) + a.Plays++ + if p.At.After(a.Last) { + a.Last = p.At + } + age := now.Sub(p.At) + if age < 0 { + age = 0 // a play dated after now weighs as one that just happened + } + a.Weight += math.Exp2(-float64(age) / float64(o.SeedHalfLife)) + } + closeSitting() + + for k, n := range sittings { + artists[k].Sessions = n + } + for pr, n := range together { + if n < o.MinTogether { + continue + } + m.Links = append(m.Links, Link{A: pr.a, B: pr.b, Score: float64(n) / math.Sqrt(float64(sittings[pr.a])*float64(sittings[pr.b]))}) + } + sort.Slice(m.Links, func(i, j int) bool { + if m.Links[i].A != m.Links[j].A { + return m.Links[i].A < m.Links[j].A + } + return m.Links[i].B < m.Links[j].B + }) + m.Artists = make([]Artist, 0, len(artists)) + for _, a := range artists { + m.Artists = append(m.Artists, *a) + } + sort.Slice(m.Artists, func(i, j int) bool { return m.Artists[i].Key < m.Artists[j].Key }) + return m +} diff --git a/internal/affinity/model_test.go b/internal/affinity/model_test.go new file mode 100644 index 0000000..275b0e8 --- /dev/null +++ b/internal/affinity/model_test.go @@ -0,0 +1,151 @@ +package affinity + +import ( + "math" + "testing" + "time" +) + +var t0 = time.Date(2023, 3, 1, 9, 0, 0, 0, time.UTC) + +// at builds a play of an artist, minutes after t0. +func at(artist string, minutes int) Play { + return Play{Key: artist, Name: artist, At: t0.Add(time.Duration(minutes) * time.Minute)} +} + +// sittings are plays of the given artists in order, one minute apart, starting a day after the day before. +func sitting(day int, artists ...string) []Play { + var out []Play + for i, a := range artists { + out = append(out, at(a, day*24*60+i)) + } + return out +} + +func link(m Model, a, b string) (float64, bool) { + for _, l := range m.Links { + if l.A == a && l.B == b { + return l.Score, true + } + } + return 0, false +} + +func TestArtistsPlayedInTheSameSittingAreLinked(t *testing.T) { + var plays []Play + for day := 0; day < 3; day++ { + plays = append(plays, sitting(day, "blur", "oasis")...) + } + m := Build(plays, t0.AddDate(0, 0, 10), DefaultOptions()) + score, ok := link(m, "blur", "oasis") + if !ok || math.Abs(score-1) > 1e-9 { + t.Fatalf("link = %v, %v, want 1 (always together)", score, ok) + } + if len(m.Links) != 1 || m.Sessions != 3 || m.Plays != 6 { + t.Errorf("model = %d links, %d sittings, %d plays, want 1, 3, 6", len(m.Links), m.Sessions, m.Plays) + } +} + +func TestTheLinkIsTheCosineOfTheSittingsOfTheTwo(t *testing.T) { + var plays []Play + plays = append(plays, sitting(0, "a", "b")...) + plays = append(plays, sitting(1, "a", "b")...) + plays = append(plays, sitting(2, "a")...) + plays = append(plays, sitting(3, "a")...) + plays = append(plays, sitting(4, "a", "c")...) // a is in five sittings, b in two, together twice + m := Build(plays, t0.AddDate(0, 0, 10), DefaultOptions()) + want := 2 / math.Sqrt(5*2) + if score, ok := link(m, "a", "b"); !ok || math.Abs(score-want) > 1e-9 { + t.Errorf("a-b = %v, %v, want %v", score, ok, want) + } + if _, ok := link(m, "a", "c"); ok { + t.Error("a and c were together once: not enough to be linked") + } +} + +func TestAPauseLongerThanTheGapStartsAnotherSitting(t *testing.T) { + o := DefaultOptions() + gap := int(o.Gap / time.Minute) + together := []Play{at("a", 0), at("b", gap)} // exactly the gap: still one sitting + apart := []Play{at("a", 1000), at("b", 1000+gap+1)} // one minute more: two + m := Build(append(together, apart...), t0.AddDate(0, 0, 10), Options{Gap: o.Gap, MaxArtists: o.MaxArtists, MinTogether: 1, SeedHalfLife: o.SeedHalfLife}) + if m.Sessions != 3 { + t.Fatalf("%d sittings, want 3", m.Sessions) + } + if score, ok := link(m, "a", "b"); !ok || math.Abs(score-1/math.Sqrt(2*2)) > 1e-9 { + t.Errorf("a-b = %v, %v, want together in one of the two sittings each is in", score, ok) + } +} + +func TestAnArtistRepeatedInASittingCountsOnce(t *testing.T) { + var plays []Play + for day := 0; day < 2; day++ { + plays = append(plays, sitting(day, "a", "a", "a", "b")...) + } + m := Build(plays, t0.AddDate(0, 0, 10), DefaultOptions()) + if score, ok := link(m, "a", "b"); !ok || math.Abs(score-1) > 1e-9 { + t.Errorf("a-b = %v, %v, want 1", score, ok) + } + for _, a := range m.Artists { + if a.Key == "a" && (a.Plays != 6 || a.Sessions != 2) { + t.Errorf("a = %+v, want 6 plays in 2 sittings", a) + } + } +} + +func TestAMarathonOfManyArtistsTeachesNothing(t *testing.T) { + o := DefaultOptions() + o.MaxArtists = 3 + var names []string + for _, n := range []string{"a", "b", "c", "d"} { + names = append(names, n) + } + var plays []Play + for day := 0; day < 3; day++ { + plays = append(plays, sitting(day, names...)...) + } + m := Build(plays, t0.AddDate(0, 0, 10), o) + if len(m.Links) != 0 { + t.Errorf("%d links from sittings of more artists than the limit, want none", len(m.Links)) + } + if m.Plays != 12 { + t.Errorf("%d plays, want all 12 still counted", m.Plays) + } +} + +func TestRecentPlaysWeighMoreAndTheFutureIsNow(t *testing.T) { + now := t0.AddDate(3, 0, 0) + o := DefaultOptions() + plays := []Play{ + {Key: "old", Name: "Old", At: now.AddDate(0, 0, -int(o.SeedHalfLife/(24*time.Hour)))}, // one half-life ago + {Key: "new", Name: "New", At: now}, + {Key: "future", Name: "Future", At: now.Add(48 * time.Hour)}, + } + m := Build(plays, now, o) + w := map[string]float64{} + for _, a := range m.Artists { + w[a.Key] = a.Weight + } + if math.Abs(w["old"]-0.5) > 0.01 || math.Abs(w["new"]-1) > 1e-9 || math.Abs(w["future"]-1) > 1e-9 { + t.Errorf("weights = %v, want old 0.5, new 1, future 1", w) + } +} + +func TestAnArtistKnowsItsNameItsLastPlayAndItsOrderIsStable(t *testing.T) { + plays := []Play{{Key: "b", Name: "Bjรถrk", At: t0}, {Key: "a", Name: "Air", At: t0.Add(time.Hour)}, {Key: "b", Name: "BJORK", At: t0.Add(48 * time.Hour)}, {Key: "", Name: "nobody", At: t0}} + m := Build(plays, t0.AddDate(0, 0, 10), DefaultOptions()) + if len(m.Artists) != 2 || m.Artists[0].Key != "a" || m.Artists[1].Key != "b" { + t.Fatalf("artists = %+v, want a then b, and none without a key", m.Artists) + } + b := m.Artists[1] + if b.Name != "Bjรถrk" || b.Plays != 2 || !b.Last.Equal(t0.Add(48*time.Hour)) { + t.Errorf("b = %+v, want the first name seen, 2 plays and the last time", b) + } +} + +func TestNoPlaysMakeAnEmptyModel(t *testing.T) { + m := Build(nil, t0, DefaultOptions()) + if len(m.Artists) != 0 || len(m.Links) != 0 || m.Plays != 0 || m.Sessions != 0 { + t.Errorf("model = %+v, want empty", m) + } +} diff --git a/internal/affinity/service.go b/internal/affinity/service.go new file mode 100644 index 0000000..3651a52 --- /dev/null +++ b/internal/affinity/service.go @@ -0,0 +1,414 @@ +package affinity + +import ( + "context" + "errors" + "fmt" + "math" + "sort" + "strings" + "sync" + "time" + + "golang.org/x/sync/singleflight" + + "git.kmalloc.org/squid/sonda/internal/feedback" + "git.kmalloc.org/squid/sonda/internal/usersettings" +) + +// Limits and rules. +const ( + DefaultLimit = 20 + MaxLimit = 50 + + // seedCount is how many of the most played artists the suggestions start from. + seedCount = 40 + // poolCount is how many of the most played artists are considered at all, besides the ones linked to a seed. + poolCount = 300 + // recentDays: an artist of the library played in this many days is not "forgotten", so it is not suggested. + recentDays = 60 + // minPlays is how many times an artist that is not in the library must have been played to be worth getting. + minPlays = 3 + + // staleAfter and bigChange say when the model is built again: when plays were added and the last build is older + // than staleAfter, or at once when as many as bigChange were added (an import). + staleAfter = 10 * time.Minute + bigChange = 100 +) + +// Errors of the service. +var ( + // ErrNotAllowed means the listener did not allow Discover to start from their listening. + ErrNotAllowed = errors.New("allow Discover to start from your listening to get suggestions") + // ErrInvalidLimit means a negative limit. + ErrInvalidLimit = errors.New("the limit cannot be negative") + // ErrUnknownArtist means the name is not an artist the listener has played. + ErrUnknownArtist = errors.New("that is not an artist you have played") +) + +// Built says when and from what the model was built. +type Built struct { + BuiltAt time.Time + Plays int + Sessions int + Artists int + Links int +} + +// LibraryArtist is an artist that exists on this server. +type LibraryArtist struct { + ID int64 + Name string + Playable bool // there is music of it to play +} + +// Store reads the plays of a person and keeps what was learned from them. +type Store interface { + Plays(ctx context.Context, userID int64, minPlayed time.Duration) ([]Play, error) + PlayCount(ctx context.Context, userID int64, minPlayed time.Duration) (int, error) + Save(ctx context.Context, userID int64, at time.Time, m Model) error + Build(ctx context.Context, userID int64) (Built, bool, error) + Seeds(ctx context.Context, userID int64, limit int) ([]Artist, error) + Artists(ctx context.Context, userID int64, keys []string) (map[string]Artist, error) + Neighbours(ctx context.Context, userID int64, keys []string) ([]Link, error) +} + +// Library says which of the artists exist on this server, by search key. +type Library interface { + LibraryArtists(ctx context.Context, keys []string) (map[string]LibraryArtist, error) +} + +// Marks reads what the listener decided about artists. +type Marks interface { + Marks(ctx context.Context, userID int64) (map[int64]feedback.Kind, error) +} + +// Wishes puts an artist on the wishlist by name and gives its id. +type Wishes interface { + Wishlist(ctx context.Context, userID int64, name string, at time.Time) (int64, error) +} + +// Settings reads the listener's own settings. +type Settings interface { + Get(ctx context.Context, userID int64) (usersettings.Settings, error) +} + +// Reason is an artist the listener plays, because of which another is suggested. +type Reason struct { + Name string + Score float64 +} + +// Suggestion is an artist put forward. +type Suggestion struct { + key string + Name string + ArtistID int64 // 0 when no artist of this name exists on this server yet + Score float64 + Plays int + LastPlayed time.Time + Wishlisted bool + Because []Reason +} + +// Suggestions is the answer. +type Suggestions struct { + // Library are artists the listener has, linked to what they play now, not played for a while. + Library []Suggestion + // Missing are artists the listener plays that they have no music of, the ones they play most and that go with + // what they play now first. + Missing []Suggestion + Built Built +} + +// Service learns from the listening of a person and makes suggestions. +type Service struct { + store Store + library Library + marks Marks + wishes Wishes + settings Settings + minListen time.Duration + opts Options + + now func() time.Time + flight singleflight.Group + mu sync.Mutex +} + +// NewService builds a Service. minListen is how long a play must last to count as a listen. +func NewService(store Store, lib Library, marks Marks, wishes Wishes, settings Settings, minListen time.Duration) *Service { + return &Service{store: store, library: lib, marks: marks, wishes: wishes, settings: settings, minListen: minListen, opts: DefaultOptions(), now: time.Now} +} + +// Rebuild learns again from every play of the person, and keeps it. +func (s *Service) Rebuild(ctx context.Context, userID int64) (Built, error) { + v, err, _ := s.flight.Do(fmt.Sprint(userID), func() (any, error) { return s.rebuild(ctx, userID) }) + if err != nil { + return Built{}, err + } + return v.(Built), nil +} + +func (s *Service) rebuild(ctx context.Context, userID int64) (Built, error) { + plays, err := s.store.Plays(ctx, userID, s.minListen) + if err != nil { + return Built{}, err + } + at := s.now() + m := Build(plays, at, s.opts) + if err := s.store.Save(ctx, userID, at, m); err != nil { + return Built{}, err + } + return Built{BuiltAt: at, Plays: m.Plays, Sessions: m.Sessions, Artists: len(m.Artists), Links: len(m.Links)}, nil +} + +// ensure builds the model when there is none or when plays were added since. +func (s *Service) ensure(ctx context.Context, userID int64) (Built, error) { + have, ok, err := s.store.Build(ctx, userID) + if err != nil { + return Built{}, err + } + if !ok { + return s.Rebuild(ctx, userID) + } + n, err := s.store.PlayCount(ctx, userID, s.minListen) + if err != nil { + return Built{}, err + } + changed := n - have.Plays + if changed < 0 { + changed = -changed + } + if changed >= bigChange || (changed > 0 && s.now().Sub(have.BuiltAt) > staleAfter) { + return s.Rebuild(ctx, userID) + } + return have, nil +} + +// Suggest makes the suggestions for a person. It needs them to have allowed Discover to start from their listening. +func (s *Service) Suggest(ctx context.Context, userID int64, limit int) (Suggestions, error) { + switch { + case limit < 0: + return Suggestions{}, ErrInvalidLimit + case limit == 0: + limit = DefaultLimit + case limit > MaxLimit: + limit = MaxLimit + } + set, err := s.settings.Get(ctx, userID) + if err != nil { + return Suggestions{}, err + } + if !set.DiscoverFromListening { + return Suggestions{}, ErrNotAllowed + } + build, err := s.ensure(ctx, userID) + if err != nil { + return Suggestions{}, err + } + out := Suggestions{Library: []Suggestion{}, Missing: []Suggestion{}, Built: build} + + pool, err := s.store.Seeds(ctx, userID, poolCount) + if err != nil { + return Suggestions{}, err + } + if len(pool) == 0 { + return out, nil + } + seeds := pool[:min(seedCount, len(pool))] + weight := map[string]float64{} + keys := make([]string, 0, len(seeds)) + for _, a := range seeds { + weight[a.Key] = math.Log1p(a.Weight) + keys = append(keys, a.Key) + } + links, err := s.store.Neighbours(ctx, userID, keys) + if err != nil { + return Suggestions{}, err + } + + type reason struct { + key string + score float64 + } + affinity := map[string]float64{} + because := map[string][]reason{} + link := func(seed, other string, score float64) { + w, ok := weight[seed] + if !ok || seed == other { + return + } + affinity[other] += w * score + because[other] = append(because[other], reason{seed, w * score}) + } + for _, l := range links { + link(l.A, l.B, l.Score) + link(l.B, l.A, l.Score) + } + + stats := map[string]Artist{} + for _, a := range pool { + stats[a.Key] = a + } + var unknown []string + for k := range affinity { + if _, ok := stats[k]; !ok { + unknown = append(unknown, k) + } + } + extra, err := s.store.Artists(ctx, userID, unknown) + if err != nil { + return Suggestions{}, err + } + for k, a := range extra { + stats[k] = a + } + + all := make([]string, 0, len(stats)) + for k := range stats { + all = append(all, k) + } + sort.Strings(all) + inLibrary, err := s.library.LibraryArtists(ctx, all) + if err != nil { + return Suggestions{}, err + } + marks, err := s.marks.Marks(ctx, userID) + if err != nil { + return Suggestions{}, err + } + + names := func(key string) string { + if a, ok := stats[key]; ok { + return a.Name + } + return key + } + make1 := func(key string) Suggestion { + a := stats[key] + sg := Suggestion{key: key, Name: a.Name, Plays: a.Plays, LastPlayed: a.Last} + if la, ok := inLibrary[key]; ok { + sg.ArtistID = la.ID + sg.Name = la.Name + sg.Wishlisted = marks[la.ID] == feedback.Wishlist + } + rs := because[key] + sort.Slice(rs, func(i, j int) bool { + if rs[i].score != rs[j].score { + return rs[i].score > rs[j].score + } + return rs[i].key < rs[j].key + }) + for _, r := range rs[:min(3, len(rs))] { + sg.Because = append(sg.Because, Reason{Name: names(r.key), Score: r.score}) + } + return sg + } + dismissed := func(key string) bool { + la, ok := inLibrary[key] + return ok && marks[la.ID] == feedback.Dismissed + } + + // The artists of the library that go with what is played now and have not been played for a while. + recent := s.now().AddDate(0, 0, -recentDays) + var lib []Suggestion + maxAff := 0.0 + for _, key := range all { + la, ok := inLibrary[key] + if !ok || !la.Playable || affinity[key] <= 0 || dismissed(key) || stats[key].Last.After(recent) { + continue + } + maxAff = math.Max(maxAff, affinity[key]) + sg := make1(key) + sg.Score = affinity[key] + lib = append(lib, sg) + } + for i := range lib { + lib[i].Score /= maxAff + } + out.Library = top(lib, limit) + + // The artists that are played and are not in the library: how much they are played and how well they go with + // what is played now, half and half. + var away []Suggestion + maxOwn, maxAway := 0.0, 0.0 + for _, key := range all { + if la, ok := inLibrary[key]; ok && la.Playable { + continue + } + if stats[key].Plays < minPlays || dismissed(key) { + continue + } + maxOwn = math.Max(maxOwn, math.Log1p(stats[key].Weight)) + maxAway = math.Max(maxAway, affinity[key]) + away = append(away, make1(key)) + } + for i := range away { + key := away[i].key + own := math.Log1p(stats[key].Weight) / maxOwn + with := 0.0 + if maxAway > 0 { + with = affinity[key] / maxAway + } + away[i].Score = 0.5*own + 0.5*with + } + out.Missing = top(away, limit) + return out, nil +} + +// top keeps the best of the suggestions, the highest score first, then the most played, then by name. +func top(in []Suggestion, limit int) []Suggestion { + sort.SliceStable(in, func(i, j int) bool { + a, b := in[i], in[j] + switch { + case a.Score != b.Score: + return a.Score > b.Score + case a.Plays != b.Plays: + return a.Plays > b.Plays + default: + return a.Name < b.Name + } + }) + if len(in) > limit { + in = in[:limit] + } + if in == nil { + in = []Suggestion{} + } + return in +} + +// AddToWishlist puts an artist the listener has played on their wishlist, by name, and gives its id. +func (s *Service) AddToWishlist(ctx context.Context, userID int64, name string) (int64, error) { + name = strings.TrimSpace(name) + if name == "" { + return 0, ErrUnknownArtist + } + set, err := s.settings.Get(ctx, userID) + if err != nil { + return 0, err + } + if !set.DiscoverFromListening { + return 0, ErrNotAllowed + } + if _, err := s.ensure(ctx, userID); err != nil { + return 0, err + } + found := false + seeds, err := s.store.Seeds(ctx, userID, math.MaxInt32) + if err != nil { + return 0, err + } + for _, a := range seeds { + if a.Name == name { + found = true + name = a.Name + break + } + } + if !found { + return 0, ErrUnknownArtist + } + return s.wishes.Wishlist(ctx, userID, name, s.now()) +} diff --git a/internal/affinity/service_test.go b/internal/affinity/service_test.go new file mode 100644 index 0000000..c8618eb --- /dev/null +++ b/internal/affinity/service_test.go @@ -0,0 +1,302 @@ +package affinity + +import ( + "context" + "errors" + "sort" + "testing" + "time" + + "git.kmalloc.org/squid/sonda/internal/feedback" + "git.kmalloc.org/squid/sonda/internal/usersettings" +) + +var now = time.Date(2026, 11, 15, 12, 0, 0, 0, time.UTC) + +// memStore keeps the model in memory and counts how often it is built. +type memStore struct { + plays []Play + model Model + built Built + has bool + reads int +} + +func (m *memStore) Plays(context.Context, int64, time.Duration) ([]Play, error) { + m.reads++ + return m.plays, nil +} +func (m *memStore) PlayCount(context.Context, int64, time.Duration) (int, error) { + return len(m.plays), nil +} +func (m *memStore) Save(_ context.Context, _ int64, at time.Time, model Model) error { + m.model, m.has = model, true + m.built = Built{BuiltAt: at, Plays: model.Plays, Sessions: model.Sessions, Artists: len(model.Artists), Links: len(model.Links)} + return nil +} +func (m *memStore) Build(context.Context, int64) (Built, bool, error) { return m.built, m.has, nil } +func (m *memStore) Seeds(_ context.Context, _ int64, limit int) ([]Artist, error) { + out := append([]Artist(nil), m.model.Artists...) + sort.Slice(out, func(i, j int) bool { + if out[i].Weight != out[j].Weight { + return out[i].Weight > out[j].Weight + } + return out[i].Key < out[j].Key + }) + return out[:min(limit, len(out))], nil +} +func (m *memStore) Artists(_ context.Context, _ int64, keys []string) (map[string]Artist, error) { + out := map[string]Artist{} + for _, a := range m.model.Artists { + for _, k := range keys { + if a.Key == k { + out[k] = a + } + } + } + return out, nil +} +func (m *memStore) Neighbours(_ context.Context, _ int64, keys []string) ([]Link, error) { + var out []Link + for _, l := range m.model.Links { + for _, k := range keys { + if l.A == k || l.B == k { + out = append(out, l) + break + } + } + } + return out, nil +} + +type fakeLibrary map[string]LibraryArtist + +func (f fakeLibrary) LibraryArtists(_ context.Context, keys []string) (map[string]LibraryArtist, error) { + out := map[string]LibraryArtist{} + for _, k := range keys { + if a, ok := f[k]; ok { + out[k] = a + } + } + return out, nil +} + +type fakeMarks map[int64]feedback.Kind + +func (f fakeMarks) Marks(context.Context, int64) (map[int64]feedback.Kind, error) { return f, nil } + +type fakeWishes struct { + names []string + id int64 +} + +func (f *fakeWishes) Wishlist(_ context.Context, _ int64, name string, _ time.Time) (int64, error) { + f.names = append(f.names, name) + return f.id, nil +} + +type fakeSettings struct{ on bool } + +func (f fakeSettings) Get(context.Context, int64) (usersettings.Settings, error) { + return usersettings.Settings{DiscoverFromListening: f.on}, nil +} + +// ago is a moment this many days before now, and the sitting of artists played one after the other from there. +func ago(days int, artists ...string) []Play { + var out []Play + for i, a := range artists { + out = append(out, Play{Key: a, Name: a, At: now.AddDate(0, 0, -days).Add(time.Duration(i) * time.Minute)}) + } + return out +} + +type rig struct { + svc *Service + store *memStore + wishes *fakeWishes + marks fakeMarks +} + +// newRig plays out this history: three old sittings of radiohead, pixies and canali; two recent ones of radiohead and +// placebo; one play of an artist met once. pixies and placebo are in the library, canali and oneoff are not. +func newRig(on bool) rig { + var plays []Play + for _, d := range []int{200, 199, 198} { + plays = append(plays, ago(d, "radiohead", "pixies", "canali")...) + } + for _, d := range []int{10, 9} { + plays = append(plays, ago(d, "radiohead", "placebo")...) + } + plays = append(plays, ago(150, "oneoff")...) + store := &memStore{plays: plays} + lib := fakeLibrary{ + "radiohead": {ID: 1, Name: "Radiohead", Playable: true}, + "pixies": {ID: 2, Name: "Pixies", Playable: true}, + "placebo": {ID: 3, Name: "Placebo", Playable: true}, + } + wishes := &fakeWishes{id: 77} + marks := fakeMarks{} + svc := NewService(store, lib, marks, wishes, fakeSettings{on}, 20*time.Second) + svc.now = func() time.Time { return now } + return rig{svc, store, wishes, marks} +} + +func names(in []Suggestion) []string { + out := []string{} + for _, s := range in { + out = append(out, s.Name) + } + return out +} + +func equal(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i := range a { + if a[i] != b[i] { + return false + } + } + return true +} + +func TestSuggestionsNeedTheListenerToAllowListening(t *testing.T) { + r := newRig(false) + if _, err := r.svc.Suggest(context.Background(), 1, 0); !errors.Is(err, ErrNotAllowed) { + t.Fatalf("err = %v, want ErrNotAllowed", err) + } + if r.store.reads != 0 { + t.Error("a refused request read the plays") + } + if _, err := r.svc.AddToWishlist(context.Background(), 1, "canali"); !errors.Is(err, ErrNotAllowed) { + t.Errorf("wishlist err = %v, want ErrNotAllowed", err) + } +} + +func TestTheLibraryListHasTheArtistsThatGoWithWhatIsPlayedAndWereNotPlayedLately(t *testing.T) { + r := newRig(true) + got, err := r.svc.Suggest(context.Background(), 1, 0) + if err != nil { + t.Fatal(err) + } + // placebo goes with radiohead too, but it was played ten days ago; radiohead itself was too. + if !equal(names(got.Library), []string{"Pixies"}) { + t.Fatalf("library = %v, want only Pixies", names(got.Library)) + } + p := got.Library[0] + if p.ArtistID != 2 || p.Score != 1 || p.Plays != 3 || !p.LastPlayed.Equal(now.AddDate(0, 0, -198).Add(time.Minute)) { + t.Errorf("Pixies = %+v", p) + } + if len(p.Because) == 0 || p.Because[0].Name == "Pixies" { + t.Errorf("because = %+v, want the artists it goes with", p.Because) + } +} + +func TestTheMissingListHasTheArtistsPlayedThatAreNotInTheLibrary(t *testing.T) { + r := newRig(true) + got, err := r.svc.Suggest(context.Background(), 1, 0) + if err != nil { + t.Fatal(err) + } + // oneoff was played once: not worth getting. Radiohead, Pixies and Placebo are in the library. + if !equal(names(got.Missing), []string{"canali"}) { + t.Fatalf("missing = %v, want only canali", names(got.Missing)) + } + c := got.Missing[0] + if c.ArtistID != 0 || c.Wishlisted || c.Plays != 3 || c.Score <= 0 || c.Score > 1 { + t.Errorf("canali = %+v", c) + } +} + +func TestAMissingArtistThatIsOnTheWishlistSaysSoAndOneNotWantedIsLeftOut(t *testing.T) { + r := newRig(true) + lib := fakeLibrary{ + "radiohead": {ID: 1, Name: "Radiohead", Playable: true}, + "pixies": {ID: 2, Name: "Pixies", Playable: true}, + "canali": {ID: 9, Name: "Giorgio Canali", Playable: false}, // a row without music + } + r.svc.library = lib + r.marks[9] = feedback.Wishlist + got, _ := r.svc.Suggest(context.Background(), 1, 0) + if len(got.Missing) != 1 || got.Missing[0].ArtistID != 9 || !got.Missing[0].Wishlisted || got.Missing[0].Name != "Giorgio Canali" { + t.Fatalf("missing = %+v, want the row 9 on the wishlist", got.Missing) + } + r.marks[9] = feedback.Dismissed + r.marks[2] = feedback.Dismissed + got, _ = r.svc.Suggest(context.Background(), 1, 0) + if len(got.Missing) != 0 || len(got.Library) != 0 { + t.Errorf("after dismissing: library %v, missing %v, want nothing", names(got.Library), names(got.Missing)) + } +} + +func TestTheModelIsBuiltOnceAndAgainWhenPlaysAreAdded(t *testing.T) { + r := newRig(true) + ctx := context.Background() + for i := 0; i < 3; i++ { + if _, err := r.svc.Suggest(ctx, 1, 0); err != nil { + t.Fatal(err) + } + } + if r.store.reads != 1 { + t.Fatalf("plays read %d times for three requests, want 1", r.store.reads) + } + + // A few plays, soon after: the model is left alone. + r.store.plays = append(r.store.plays, ago(0, "radiohead")...) + r.svc.now = func() time.Time { return now.Add(time.Minute) } + _, _ = r.svc.Suggest(ctx, 1, 0) + if r.store.reads != 1 { + t.Errorf("read %d times after one new play a minute later, want 1", r.store.reads) + } + // The same, but the model is old now. + r.svc.now = func() time.Time { return now.Add(time.Hour) } + _, _ = r.svc.Suggest(ctx, 1, 0) + if r.store.reads != 2 { + t.Errorf("read %d times after an hour, want 2", r.store.reads) + } + // An import adds many at once: at once. + for i := 0; i < bigChange; i++ { + r.store.plays = append(r.store.plays, ago(300+i, "radiohead", "pixies")...) + } + _, _ = r.svc.Suggest(ctx, 1, 0) + if r.store.reads != 3 { + t.Errorf("read %d times after a big import, want 3", r.store.reads) + } +} + +func TestTheLimitIsAppliedAndBadOnesRefused(t *testing.T) { + r := newRig(true) + if _, err := r.svc.Suggest(context.Background(), 1, -1); !errors.Is(err, ErrInvalidLimit) { + t.Errorf("err = %v, want ErrInvalidLimit", err) + } + got, err := r.svc.Suggest(context.Background(), 1, 1) + if err != nil || len(got.Library) > 1 || len(got.Missing) > 1 { + t.Errorf("limit 1 = %v, %v", got, err) + } +} + +func TestNoPlaysGiveEmptyListsAndNotNull(t *testing.T) { + r := newRig(true) + r.store.plays = nil + got, err := r.svc.Suggest(context.Background(), 1, 0) + if err != nil || got.Library == nil || got.Missing == nil || len(got.Library)+len(got.Missing) != 0 { + t.Errorf("got %+v, %v, want two empty lists", got, err) + } +} + +func TestOnlyAnArtistThatWasPlayedGoesOnTheWishlist(t *testing.T) { + r := newRig(true) + ctx := context.Background() + // The model is built by the wish itself when no request for suggestions did it. + if _, err := r.svc.AddToWishlist(ctx, 1, "nobody I played"); !errors.Is(err, ErrUnknownArtist) { + t.Errorf("err = %v, want ErrUnknownArtist", err) + } + if _, err := r.svc.AddToWishlist(ctx, 1, " "); !errors.Is(err, ErrUnknownArtist) { + t.Errorf("a blank name: %v, want ErrUnknownArtist", err) + } + id, err := r.svc.AddToWishlist(ctx, 1, "canali") + if err != nil || id != 77 || len(r.wishes.names) != 1 || r.wishes.names[0] != "canali" { + t.Errorf("AddToWishlist = %d, %v, names %v", id, err, r.wishes.names) + } +} diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index 7eda8af..a5afc88 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -15,24 +15,38 @@ import ( "github.com/uptrace/bun" "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/about" - "codeberg.org/squidd/sonda/internal/auth" - "codeberg.org/squidd/sonda/internal/catalog" - "codeberg.org/squidd/sonda/internal/discover" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/duplicates" - "codeberg.org/squidd/sonda/internal/feedback" - "codeberg.org/squidd/sonda/internal/images" - "codeberg.org/squidd/sonda/internal/jobs" - "codeberg.org/squidd/sonda/internal/library" - "codeberg.org/squidd/sonda/internal/listening" - "codeberg.org/squidd/sonda/internal/playlists" - "codeberg.org/squidd/sonda/internal/providers" - "codeberg.org/squidd/sonda/internal/storage" - "codeberg.org/squidd/sonda/internal/streaming" - "codeberg.org/squidd/sonda/internal/taste" - "codeberg.org/squidd/sonda/internal/usersettings" - "codeberg.org/squidd/sonda/migrations" + "git.kmalloc.org/squid/sonda/internal/about" + "git.kmalloc.org/squid/sonda/internal/affinity" + "git.kmalloc.org/squid/sonda/internal/auth" + "git.kmalloc.org/squid/sonda/internal/catalog" + "git.kmalloc.org/squid/sonda/internal/devices" + "git.kmalloc.org/squid/sonda/internal/discover" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/duplicates" + "git.kmalloc.org/squid/sonda/internal/favorites" + "git.kmalloc.org/squid/sonda/internal/federation" + "git.kmalloc.org/squid/sonda/internal/feedback" + "git.kmalloc.org/squid/sonda/internal/images" + "git.kmalloc.org/squid/sonda/internal/jobs" + "git.kmalloc.org/squid/sonda/internal/library" + "git.kmalloc.org/squid/sonda/internal/listening" + "git.kmalloc.org/squid/sonda/internal/mastodon" + "git.kmalloc.org/squid/sonda/internal/mixes" + "git.kmalloc.org/squid/sonda/internal/naming" + "git.kmalloc.org/squid/sonda/internal/opensubsonic" + "git.kmalloc.org/squid/sonda/internal/playlists" + "git.kmalloc.org/squid/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/providers/anthropic" + "git.kmalloc.org/squid/sonda/internal/providers/lidarr" + "git.kmalloc.org/squid/sonda/internal/smart" + "git.kmalloc.org/squid/sonda/internal/spotifyimport" + "git.kmalloc.org/squid/sonda/internal/stats" + "git.kmalloc.org/squid/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/streaming" + "git.kmalloc.org/squid/sonda/internal/taste" + "git.kmalloc.org/squid/sonda/internal/usersettings" + "git.kmalloc.org/squid/sonda/internal/waveform" + "git.kmalloc.org/squid/sonda/migrations" ) const secret = "0123456789abcdef0123456789abcdef" @@ -87,31 +101,58 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun tokens := auth.NewTokenIssuer(secret, time.Hour) prov := newProviders(db) deps := Deps{ - Logger: zap.NewNop(), - Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, 24*time.Hour), - Tokens: tokens, - Listening: listening.NewService(storage.NewListeningRepo(db), 20*time.Second), - Scan: &fakeScan{}, - Libraries: library.NewLibraries(storage.NewLibraryRepo(db)), - Catalog: catalog.NewService(storage.NewCatalogRepo(db)), - Providers: prov, - Lidarr: &fakeLidarr{}, - LidarrArtists: &fakeSender{}, - Discovery: discovery.NewService(storage.NewDiscoveryRepo(db), &fakeLastfm{}, prov, zap.NewNop()), - Tags: discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()), - Taste: taste.NewService(storage.NewTasteRepo(db), taste.Options{MinListen: 20 * time.Second, HalfLife: 90 * 24 * time.Hour}), - Settings: usersettings.NewService(storage.NewUserSettingsRepo(db)), - Streams: streaming.NewService(noFiles{}), + Logger: zap.NewNop(), + Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, 24*time.Hour), + Tokens: tokens, + Listening: listening.NewService(storage.NewListeningRepo(db), 20*time.Second), + Scan: &fakeScan{}, + Libraries: library.NewLibraries(storage.NewLibraryRepo(db)), + Catalog: catalog.NewService(storage.NewCatalogRepo(db)), + Providers: prov, + Lidarr: &fakeLidarr{}, + LidarrArtists: &fakeSender{}, + LidarrRequests: lidarr.NewRequests(storage.NewLidarrRequestRepo(db)), + Discovery: discovery.NewService(storage.NewDiscoveryRepo(db), &fakeLastfm{}, prov, zap.NewNop()), + Tags: discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()), + Taste: taste.NewService(storage.NewTasteRepo(db), taste.Options{MinListen: 20 * time.Second, HalfLife: 90 * 24 * time.Hour}), + Settings: usersettings.NewService(storage.NewUserSettingsRepo(db)), + Streams: streaming.NewService(noFiles{}), + Federation: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()), + Devices: devices.NewService(storage.NewDevicesRepo(db), []byte(strings.Repeat("k", 32))), + Mastodon: mastodon.NewService(storage.NewMastodonRepo(db), &fakeMastodon{handle: "@test@mastodon.social"}, zap.NewNop()), } + libRepo := storage.NewLibraryRepo(db) + deps.PeerCatalog = catalog.NewService(storage.NewSharedCatalogRepo(db)) + deps.PeerStreams = streaming.NewService(libRepo.Shared()) + deps.PeerWaveform = waveform.NewService(storage.NewWaveformRepo(db), streaming.NewService(libRepo.Shared())) + deps.Waveform = waveform.NewService(storage.NewWaveformRepo(db), streaming.NewService(libRepo)) feedbackSvc := feedback.NewService(storage.NewFeedbackRepo(db)) deps.Feedback = feedbackSvc deps.Playlists = playlists.NewService(storage.NewPlaylistRepo(db)) + deps.SpotifyImport = spotifyimport.NewService(storage.NewCatalogRepo(db), storage.NewListeningRepo(db), deps.Playlists, storage.NewSpotifyImportRepo(db), storage.NewOutsidePlayRepo(db), storage.SearchKey, 20*time.Second) + deps.Mixes = mixes.NewService(storage.NewMixRepo(db), deps.Taste.(mixes.Taster), 20*time.Second) + deps.Smart = smart.NewService(storage.NewSmartRepo(db), deps.Taste.(smart.Taster), 20*time.Second) + namingSvc := naming.NewService(prov, noClaude{}, storage.NewNamingRepo(db), nil) + namingSvc.SetLedger(storage.NewNamingRepo(db)) + deps.Naming = namingSvc imageRepo := storage.NewImageRepo(db) imageSvc := images.NewService(filepath.Join(t.TempDir(), "data"), imageRepo, imageRepo) deps.Images, deps.ArtistImages = imageSvc, imageSvc + deps.PeerCovers = images.StoredOnly{Service: imageSvc} deps.CoversJob = jobs.Runner{Ctx: context.Background(), Job: images.NewCoversJob(imageSvc, imageRepo)} deps.ArtistImagesJob = jobs.Runner{Ctx: context.Background(), Job: images.NewArtistImagesJob(imageSvc, imageRepo)} + deps.Favorites = favorites.NewService(storage.NewFavoritesRepo(db), deps.Catalog.(favorites.Catalog)) mutate(&deps, db) + if deps.OpenSubsonic == nil { + subsonic := storage.NewSubsonicRepo(db, 20*time.Second) + deps.OpenSubsonic = &opensubsonic.Deps{ + Login: deps.Devices.(opensubsonic.Login), Catalog: deps.Catalog.(opensubsonic.Catalog), + Libraries: deps.Libraries.(opensubsonic.Libraries), ServerVersion: "test", + Extras: subsonic, Lists: subsonic, Streams: deps.Streams.(opensubsonic.Streams), + Covers: deps.Images.(opensubsonic.Covers), ArtistImages: deps.ArtistImages.(opensubsonic.ArtistImages), + Playlists: deps.Playlists.(opensubsonic.Playlists), Listens: deps.Listening.(opensubsonic.Listens), Announcer: deps.Mastodon.(opensubsonic.Announcer), Favorites: deps.Favorites.(opensubsonic.Favorites), Radio: deps.Mixes.(opensubsonic.Radio), Settings: deps.Settings.(opensubsonic.Settings), + } + } if deps.About == nil { deps.About = about.NewService(storage.NewDiscoveryRepo(db), noWikipedia{}, prov, []string{"en"}, zap.NewNop()) } @@ -127,6 +168,13 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun } } // Built from the final services, so a test that swaps one gets a Discover that uses it. + if deps.Stats == nil { + deps.Stats = stats.NewService(storage.NewStatsRepo(db), 20*time.Second) + } + if deps.Suggestions == nil { + repo := storage.NewAffinityRepo(db) + deps.Suggestions = affinity.NewService(repo, repo, feedbackSvc, repo, usersettings.NewService(storage.NewUserSettingsRepo(db)), 20*time.Second) + } if deps.Discover == nil { deps.Discover = discover.NewService(deps.Taste.(discover.Taster), deps.Discovery.(discover.Similar), discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()), @@ -156,6 +204,13 @@ func newProviders(db *bun.DB) *providers.Service { return svc } +// noClaude is a Claude that is never there: with the provider off it is never asked. +type noClaude struct{} + +func (noClaude) Name(context.Context, string, anthropic.Request) (anthropic.Result, error) { + return anthropic.Result{}, anthropic.ErrNoName +} + // noFiles is a streaming source with nothing to play. type noFiles struct{} diff --git a/internal/api/http/artist_image_test.go b/internal/api/http/artist_image_test.go index a613da3..4fe2dc9 100644 --- a/internal/api/http/artist_image_test.go +++ b/internal/api/http/artist_image_test.go @@ -15,9 +15,9 @@ import ( "github.com/uptrace/bun" - "codeberg.org/squidd/sonda/internal/images" - "codeberg.org/squidd/sonda/internal/providers" - "codeberg.org/squidd/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/images" + "git.kmalloc.org/squid/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/storage" ) type fakeArtistPictures struct { diff --git a/internal/api/http/catalog_test.go b/internal/api/http/catalog_test.go index 58b0c83..2ec4d9a 100644 --- a/internal/api/http/catalog_test.go +++ b/internal/api/http/catalog_test.go @@ -3,10 +3,11 @@ package httpapi import ( "context" "net/http" + "reflect" "sort" "testing" - "codeberg.org/squidd/sonda/internal/catalog" + "git.kmalloc.org/squid/sonda/internal/catalog" ) // fakeCatalog returns canned data and records the last filter it received. @@ -139,7 +140,7 @@ func TestCatalogListShapeAndPaging(t *testing.T) { r = do(t, h, "GET", "/api/v1/tracks", tok, nil) tr := r.Body["items"].([]any)[0].(map[string]any) // No file path, root or library path may ever appear. - sameKeys(t, "track", tr, "id", "title", "duration_ms", "track_no", "disc_no", "album", "artists", "format") + sameKeys(t, "track", tr, "id", "title", "duration_ms", "track_no", "disc_no", "album", "artists", "format", "replay_gain") sameKeys(t, "track album", tr["album"].(map[string]any), "id", "title") if tr["format"] != "flac" || len(tr["artists"].([]any)) != 1 { t.Errorf("track = %v", tr) @@ -170,13 +171,35 @@ func TestTrackWithoutAlbumHasNullAlbum(t *testing.T) { } } +func TestTrackCarriesItsReplayGain(t *testing.T) { + g := func(v float64) *float64 { return &v } + fc := &fakeCatalog{tracks: []catalog.Track{ + {ID: 1, Title: "Tagged", ReplayGain: &catalog.ReplayGain{TrackGain: g(-6.5), TrackPeak: g(0.98)}}, + {ID: 2, Title: "Plain"}, + }} + h, tok := catalogAPI(t, fc) + items := do(t, h, "GET", "/api/v1/tracks", tok, nil).Body["items"].([]any) + + tagged := items[0].(map[string]any)["replay_gain"].(map[string]any) + sameKeys(t, "replay_gain", tagged, "track_gain_db", "album_gain_db", "track_peak", "album_peak") + if tagged["track_gain_db"] != -6.5 || tagged["track_peak"] != 0.98 { + t.Errorf("replay_gain = %v, want the gain -6.5 and the peak 0.98", tagged) + } + if v, present := tagged["album_gain_db"]; !present || v != nil { + t.Errorf("album_gain_db = %#v, want an explicit null: the file does not say", v) + } + if v, present := items[1].(map[string]any)["replay_gain"]; !present || v != nil { + t.Errorf("replay_gain of a song with none = %#v, want an explicit null", v) + } +} + func TestCatalogQueryParametersReachTheService(t *testing.T) { fc := &fakeCatalog{} h, tok := catalogAPI(t, fc) do(t, h, "GET", "/api/v1/tracks?q=black+sabbath&limit=25&offset=50&library_id=2&artist_id=3&album_id=4", tok, nil) want := catalog.Filter{Query: "black sabbath", Limit: 25, Offset: 50, LibraryID: 2, ArtistID: 3, AlbumID: 4} - if fc.last != want { + if !reflect.DeepEqual(fc.last, want) { t.Errorf("filter = %+v, want %+v", fc.last, want) } @@ -185,6 +208,11 @@ func TestCatalogQueryParametersReachTheService(t *testing.T) { t.Errorf("album filter = %+v (unset paging must stay 0 for the service to default)", fc.last) } + do(t, h, "GET", "/api/v1/albums?sort=recent", tok, nil) + if fc.last.Sort != "recent" { + t.Errorf("album sort = %q", fc.last.Sort) + } + // Special characters survive URL decoding. do(t, h, "GET", "/api/v1/artists?q=100%25+%26+more", tok, nil) if fc.last.Query != "100% & more" { @@ -221,7 +249,7 @@ func TestCatalogDetails(t *testing.T) { r = do(t, h, "GET", "/api/v1/albums/6", tok, nil) sameKeys(t, "album", r.Body, "id", "title", "artist", "track_count", "duration_ms") r = do(t, h, "GET", "/api/v1/tracks/7", tok, nil) - sameKeys(t, "track", r.Body, "id", "title", "duration_ms", "track_no", "disc_no", "album", "artists", "format") + sameKeys(t, "track", r.Body, "id", "title", "duration_ms", "track_no", "disc_no", "album", "artists", "format", "replay_gain") if r.Code != http.StatusOK || r.Body["id"] != float64(7) { t.Errorf("track: %d %v", r.Code, r.Body) } diff --git a/internal/api/http/contract_test.go b/internal/api/http/contract_test.go index dd8c5be..f15d49d 100644 --- a/internal/api/http/contract_test.go +++ b/internal/api/http/contract_test.go @@ -3,17 +3,23 @@ package httpapi import ( "bytes" "context" + "crypto/ed25519" + "crypto/rand" + "encoding/base64" "encoding/json" + "fmt" "image" "image/color" "image/png" "io" "mime" + "mime/multipart" "net/http" "net/http/httptest" "os" "path/filepath" "strconv" + "strings" "sync" "testing" "time" @@ -24,17 +30,18 @@ import ( "github.com/uptrace/bun" "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/catalog" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/duplicates" - "codeberg.org/squidd/sonda/internal/images" - "codeberg.org/squidd/sonda/internal/jobs" - "codeberg.org/squidd/sonda/internal/library" - "codeberg.org/squidd/sonda/internal/providers" - "codeberg.org/squidd/sonda/internal/providers/lastfm" - "codeberg.org/squidd/sonda/internal/providers/lidarr" - "codeberg.org/squidd/sonda/internal/storage" - "codeberg.org/squidd/sonda/internal/streaming" + "git.kmalloc.org/squid/sonda/internal/catalog" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/duplicates" + "git.kmalloc.org/squid/sonda/internal/federation" + "git.kmalloc.org/squid/sonda/internal/images" + "git.kmalloc.org/squid/sonda/internal/jobs" + "git.kmalloc.org/squid/sonda/internal/library" + "git.kmalloc.org/squid/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/providers/lastfm" + "git.kmalloc.org/squid/sonda/internal/providers/lidarr" + "git.kmalloc.org/squid/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/streaming" ) // Contract tests: real requests against the real services, with every request @@ -155,6 +162,7 @@ func newContract(t *testing.T) *contract { pictures.SetArtistSource(&fakeArtistPictures{found: images.Found{Data: testJPEG(t, 800, 600, color.White), Credit: images.Credit{Author: "Someone", License: "CC BY 4.0", LicenseURL: "https://creativecommons.org/licenses/by/4.0", PageURL: "https://commons.wikimedia.org/wiki/File:X.jpg"}}}) d.Images, d.ArtistImages = pictures, pictures + d.PeerCovers = images.StoredOnly{Service: pictures} d.LidarrArtists = lidarr.NewService(storage.NewDiscoveryRepo(db), prov, fakeLidarrRemote{}) }) @@ -185,15 +193,38 @@ func newContract(t *testing.T) *contract { func (c *contract) call(method, path, token string, body any, headers ...string) *httptest.ResponseRecorder { c.t.Helper() var payload []byte + contentType := "" if body != nil { var err error if payload, err = json.Marshal(body); err != nil { c.t.Fatal(err) } + contentType = "application/json" } + return c.callRaw(method, path, token, contentType, payload, headers...) +} + +// upload sends files as a multipart form, in the field `files`. +func (c *contract) upload(path, token string, files ...string) *httptest.ResponseRecorder { + c.t.Helper() + var buf bytes.Buffer + w := multipart.NewWriter(&buf) + for i, content := range files { + part, err := w.CreateFormFile("files", fmt.Sprintf("file%d.json", i)) + if err != nil { + c.t.Fatal(err) + } + _, _ = part.Write([]byte(content)) + } + _ = w.Close() + return c.callRaw("POST", path, token, w.FormDataContentType(), buf.Bytes()) +} + +func (c *contract) callRaw(method, path, token, contentType string, payload []byte, headers ...string) *httptest.ResponseRecorder { + c.t.Helper() req := httptest.NewRequest(method, "/api/v1"+path, bytes.NewReader(payload)) - if body != nil { - req.Header.Set("Content-Type", "application/json") + if contentType != "" { + req.Header.Set("Content-Type", contentType) } if token != "" { req.Header.Set("Authorization", "Bearer "+token) @@ -239,8 +270,8 @@ func (c *contract) validate(req *http.Request, payload []byte, rec *httptest.Res // The library skips HEAD and redirects, and cannot read audio or YAML. // Those are checked by hand: the status above, and the content type here. skipBody := req.Method == http.MethodHead || route.Path == "/openapi.yaml" || - (rec.Code >= 300 && rec.Code < 400) || (route.Path == "/tracks/{id}/stream" && rec.Code/100 == 2) - if skipBody && documented != nil && documented.Value != nil && rec.Code/100 == 2 && req.Method != http.MethodHead && route.Path == "/tracks/{id}/stream" { + (rec.Code >= 300 && rec.Code < 400) || ((route.Path == "/tracks/{id}/stream" || route.Path == "/peer/tracks/{id}/stream" || route.Path == "/friends/{friend}/tracks/{id}/stream") && rec.Code/100 == 2) + if skipBody && documented != nil && documented.Value != nil && rec.Code/100 == 2 && req.Method != http.MethodHead && (route.Path == "/tracks/{id}/stream" || route.Path == "/peer/tracks/{id}/stream" || route.Path == "/friends/{friend}/tracks/{id}/stream") { mediaType, _, err := mime.ParseMediaType(rec.Header().Get("Content-Type")) if err != nil || documented.Value.Content.Get(mediaType) == nil { c.t.Errorf("%s -> %d: Content-Type %q is not one the spec lists", where, rec.Code, rec.Header().Get("Content-Type")) @@ -329,6 +360,16 @@ func TestResponsesMatchTheSpec(t *testing.T) { c.expect(c.call("GET", "/auth/me", "", nil), 401, "me without token", "unauthorized") c.expect(c.call("GET", "/auth/me", token, nil), 200, "me") + c.expect(c.call("GET", "/version", "", nil), 401, "version without token", "unauthorized") + c.expect(c.call("GET", "/version", token, nil), 200, "version") + + c.expect(c.call("GET", "/stats/library", "", nil), 401, "library stats without token", "unauthorized") + c.expect(c.call("GET", "/stats/library", token, nil), 200, "library stats") + c.expect(c.call("GET", "/stats/listening", "", nil), 401, "listening stats without token", "unauthorized") + c.expect(c.call("GET", "/stats/listening", token, nil), 200, "listening stats") + c.expect(c.call("GET", "/stats/listening?tz=Europe/Rome&from=2025-01-01T00:00:00Z", token, nil), 200, "listening stats of a period") + c.expect(c.call("GET", "/stats/listening?tz=Mars/Base", token, nil), 400, "listening stats bad zone", "invalid_input") + renewed := c.expect(c.call("POST", "/auth/refresh", "", map[string]string{"refresh_token": refresh}), 200, "refresh") c.expect(c.call("POST", "/auth/refresh", "", map[string]string{"refresh_token": refresh}), 401, "refresh replay", "invalid_refresh_token") c.expect(c.call("POST", "/auth/refresh", "", "not an object"), 400, "refresh bad body", "invalid_input") @@ -452,8 +493,8 @@ func TestResponsesMatchTheSpec(t *testing.T) { // ---- providers ---- listed := c.expect(c.call("GET", "/providers", token, nil), 200, "list providers")["providers"].([]any) - if len(listed) != 6 || listed[0].(map[string]any)["enabled"] != false || listed[1].(map[string]any)["enabled"] != false || listed[2].(map[string]any)["enabled"] != false || listed[3].(map[string]any)["enabled"] != false || listed[4].(map[string]any)["enabled"] != false || listed[5].(map[string]any)["enabled"] != false { - t.Fatalf("providers = %v, want six, all off", listed) + if len(listed) != 7 || listed[0].(map[string]any)["enabled"] != false || listed[1].(map[string]any)["enabled"] != false || listed[2].(map[string]any)["enabled"] != false || listed[3].(map[string]any)["enabled"] != false || listed[4].(map[string]any)["enabled"] != false || listed[5].(map[string]any)["enabled"] != false { + t.Fatalf("providers = %v, want seven, all off", listed) } // ---- lidarr ---- c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"enabled": true}), 400, "enable lidarr with nothing", "invalid_input") @@ -567,6 +608,13 @@ func TestResponsesMatchTheSpec(t *testing.T) { if onlyCounted["total"] != float64(1) { t.Fatalf("counted history = %v, want one", onlyCounted) } + c.expect(c.call("GET", "/listens?from=2000-01-01T00:00:00Z&until=2100-01-01T00:00:00Z", token, nil), 200, "history of a range") + c.expect(c.call("GET", "/listens?from=never", token, nil), 400, "history of a bad range", "invalid_input") + listenMonths := c.expect(c.call("GET", "/listens/months", token, nil), 200, "listening months") + if months, _ := listenMonths["months"].([]any); len(months) != 1 { + c.t.Fatalf("months = %v, want the month of the one event that counts", listenMonths) + } + c.expect(c.call("GET", "/listens/months", "", nil), 401, "months without token", "unauthorized") c.expect(c.call("GET", "/listens", "", nil), 401, "history without token", "unauthorized") c.expect(c.call("GET", "/listens?counted=maybe", token, nil), 400, "history bad filter", "invalid_input") c.expect(c.call("POST", "/listens", token, map[string]any{"track_id": 999, "event_type": "play", "idempotency_key": "k2"}), 404, "listen unknown track", "track_not_found") @@ -613,6 +661,13 @@ func TestResponsesMatchTheSpec(t *testing.T) { if got := c.expect(c.call("GET", "/artists/"+artistID+"/lidarr", token, nil), 200, "lidarr presence"); got["provider_status"] != "ok" || got["in_lidarr"] != true || got["monitored"] != true { t.Fatalf("presence = %v", got) } + requested := c.expect(c.call("GET", "/lidarr/requests", token, nil), 200, "the artists sent to lidarr") + if items, _ := requested["items"].([]any); len(items) != 1 || requested["total"] != 1.0 { + t.Fatalf("requests = %v, want the one artist that was sent", requested) + } + c.expect(c.call("GET", "/lidarr/requests?status=waiting&limit=5", token, nil), 200, "the requests that wait") + c.expect(c.call("GET", "/lidarr/requests?status=lost", token, nil), 400, "a status that does not exist", "invalid_input") + c.expect(c.call("GET", "/lidarr/requests", "", nil), 401, "requests without a token", "unauthorized") c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"enabled": false}), 200, "switch lidarr off again") c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"api_key": "", "url": ""}), 200, "take lidarr's key and address away again") if _, err := c.db.Exec("UPDATE artists SET mbid = NULL WHERE id = ?", artistID); err != nil { // as it was @@ -819,9 +874,113 @@ func TestResponsesMatchTheSpec(t *testing.T) { c.expect(c.call("PATCH", "/me/settings", token, map[string]any{"nope": true}), 400, "a setting that does not exist", "invalid_input") c.expect(c.call("GET", "/me/settings", "", nil), 401, "settings without a token", "unauthorized") + // Sharing what I listen to on Mastodon: nothing is connected until I do it, and the token never comes back. + if got := c.expect(c.call("GET", "/me/mastodon", token, nil), 200, "no Mastodon account yet"); got["connected"] != false { + c.t.Errorf("connected at the start: %v", got) + } + c.expect(c.call("PUT", "/me/mastodon", token, map[string]any{"instance": "mastodon.social", "token": "secret"}), 200, "connect Mastodon") + if got := c.expect(c.call("GET", "/me/mastodon", token, nil), 200, "the Mastodon account"); got["connected"] != true || got["instance"] != "https://mastodon.social" { + c.t.Errorf("account = %v", got) + } + c.expect(c.call("PUT", "/me/mastodon", token, map[string]any{"instance": "http://mastodon.social", "token": "secret"}), 400, "an instance on plain http", "invalid_instance") + c.expect(c.call("PUT", "/me/mastodon", token, map[string]any{"instance": "mastodon.social", "token": " "}), 400, "an empty token", "invalid_token") + c.expect(c.call("PUT", "/me/mastodon", token, map[string]any{"instance": "mastodon.social", "nope": 1}), 400, "a name that does not exist", "invalid_input") + c.expect(c.call("DELETE", "/me/mastodon", token, nil), 204, "disconnect Mastodon") + c.expect(c.call("GET", "/me/mastodon", "", nil), 401, "Mastodon without a token", "unauthorized") + + // The passwords of other apps: the secret is shown once, and a device is taken back by its owner only. + if got := c.expect(c.call("GET", "/me/devices", token, nil), 200, "no devices yet"); len(got["devices"].([]any)) != 0 { + c.t.Errorf("devices = %v", got) + } + made := c.expect(c.call("POST", "/me/devices", token, map[string]any{"name": "Phone"}), 201, "make a device") + if len(made["secret"].(string)) != 48 { + c.t.Errorf("secret = %v", made["secret"]) + } + c.expect(c.call("POST", "/me/devices", token, map[string]any{"name": " "}), 400, "a device with no name", "invalid_name") + if got := c.expect(c.call("GET", "/me/devices", token, nil), 200, "the devices"); len(got["devices"].([]any)) != 1 { + c.t.Errorf("devices = %v", got) + } + c.expect(c.call("DELETE", "/me/devices/"+idOf(made), token, nil), 204, "take back a device") + c.expect(c.call("DELETE", "/me/devices/"+idOf(made), token, nil), 404, "take back a device twice", "device_not_found") + c.expect(c.call("GET", "/me/devices", "", nil), 401, "devices without a token", "unauthorized") + + // The radio: stations nobody made, and other songs every time. + if got := c.expect(c.call("GET", "/radio", token, nil), 200, "the radio"); len(got["items"].([]any)) == 0 { + c.t.Errorf("radio = %v", got) + } + if got := c.expect(c.call("GET", "/radio/all", token, nil), 200, "everything on the radio"); len(got["tracks"].([]any)) == 0 || got["decade"] != nil { + c.t.Errorf("station = %v", got) + } + if got := c.expect(c.call("GET", "/radio/artist:"+artistID, token, nil), 200, "the radio of an artist"); got["kind"] != "artist" { + c.t.Errorf("station = %v", got) + } + c.expect(c.call("GET", "/radio/nope", token, nil), 404, "a station that is not there", "station_not_found") + c.expect(c.call("GET", "/radio", "", nil), 401, "radio without a token", "unauthorized") + + // Favourites: what is marked is listed, newest first, and only the person's own. + if got := c.expect(c.call("GET", "/me/favorites/ids", token, nil), 200, "no favourites yet"); len(got["tracks"].([]any)) != 0 { + c.t.Errorf("favourites = %v", got) + } + for kind, id := range map[string]string{"tracks": trackID, "albums": albumID, "artists": artistID} { + c.expect(c.call("PUT", "/me/favorites/"+kind+"/"+id, token, nil), 204, "mark a favourite "+kind) + if got := c.expect(c.call("GET", "/me/favorites/"+kind, token, nil), 200, "favourite "+kind); got["total"] != float64(1) { + c.t.Errorf("favourite %s = %v", kind, got) + } + } + if got := c.expect(c.call("GET", "/me/favorites/ids", token, nil), 200, "the favourites"); len(got["albums"].([]any)) != 1 { + c.t.Errorf("favourites = %v", got) + } + c.expect(c.call("DELETE", "/me/favorites/albums/"+albumID, token, nil), 204, "unmark a favourite") + c.expect(c.call("DELETE", "/me/favorites/albums/"+albumID, token, nil), 204, "unmark it again") + c.expect(c.call("DELETE", "/me/favorites/songs/1", token, nil), 404, "unmark a kind that does not exist", "not_found") + c.expect(c.call("PUT", "/me/favorites/tracks/9999", token, nil), 404, "a song that is not there", "track_not_found") + c.expect(c.call("PUT", "/me/favorites/songs/1", token, nil), 404, "a kind that does not exist", "not_found") + c.expect(c.call("GET", "/me/favorites/songs", token, nil), 404, "favourites of no kind", "not_found") + c.expect(c.call("GET", "/me/favorites/tracks?limit=-1", token, nil), 400, "a negative limit", "invalid_input") + c.expect(c.call("GET", "/me/favorites/tracks", "", nil), 401, "favourites without a token", "unauthorized") + + // Bringing a Spotify export in: what it did, and what is refused. + export := `[{"ts":"2023-05-01T10:00:00Z","ms_played":200000,"master_metadata_track_name":"Nowhere","master_metadata_album_artist_name":"Nobody"}]` + imported := c.expect(c.upload("/me/import/spotify", token, export, `{"playlists":[{"name":"P","items":[{"track":{"trackName":"Nowhere","artistName":"Nobody"}}]}]}`), 200, "import a Spotify export") + if h, _ := imported["history"].(map[string]any); h["entries"] != 1.0 || h["not_found"] != 1.0 { + c.t.Errorf("history = %v", h) + } + past := c.expect(c.call("GET", "/me/import/spotify", token, nil), 200, "the past imports") + if items, _ := past["items"].([]any); past["total"] != 1.0 || len(items) != 1 || items[0].(map[string]any)["id"] != imported["id"] { + c.t.Errorf("past imports = %v, want the one just made", past) + } + c.expect(c.call("GET", "/me/import/spotify?limit=-1", token, nil), 400, "a negative limit", "invalid_input") + c.expect(c.call("GET", "/me/import/spotify", "", nil), 401, "past imports without a token", "unauthorized") + c.expect(c.upload("/me/import/spotify", token), 400, "an import with no files", "invalid_input") + c.expect(c.upload("/me/import/spotify", token, `{"hello":"world"}`), 400, "files that are not Spotify's", "no_spotify_data") + c.expect(c.upload("/me/import/spotify", token, export[:len(export)-20]), 400, "a file cut short", "unreadable_file") + c.expect(c.upload("/me/import/spotify", ""), 401, "an import without a token", "unauthorized") + + // Suggestions from the listening: refused until it is allowed, then empty for a history with nothing to learn from. + c.expect(c.call("PATCH", "/me/settings", token, map[string]any{"discover_from_listening": false}), 200, "forbid Discover from the listening") + c.expect(c.call("GET", "/me/suggestions", token, nil), 400, "suggestions before they are allowed", "listening_not_allowed") + c.expect(c.call("POST", "/me/suggestions/wishlist", token, map[string]any{"name": "Nobody"}), 400, "a wish before it is allowed", "listening_not_allowed") + c.expect(c.call("PATCH", "/me/settings", token, map[string]any{"discover_from_listening": true}), 200, "allow Discover from the listening") + suggested := c.expect(c.call("GET", "/me/suggestions?limit=5", token, nil), 200, "suggestions") + if lib, _ := suggested["library"].([]any); lib == nil { + c.t.Errorf("suggestions = %v, want empty lists and not null", suggested) + } + c.expect(c.call("GET", "/me/suggestions?limit=-1", token, nil), 400, "a negative limit", "invalid_input") + c.expect(c.call("GET", "/me/suggestions", "", nil), 401, "suggestions without a token", "unauthorized") + c.expect(c.call("POST", "/me/suggestions/wishlist", token, map[string]any{"name": "Nobody I played"}), 404, "an artist not played", "artist_not_played") + c.expect(c.call("POST", "/me/suggestions/wishlist", "", map[string]any{"name": "x"}), 401, "a wish without a token", "unauthorized") + c.expect(c.call("PATCH", "/me/settings", token, map[string]any{"discover_from_listening": false}), 200, "forbid it again") + // ---- duplicate songs ---- c.duplicates(t, token, libID) + // ---- mixes ---- + c.mixes(t, token, libID) + c.smartPlaylists(t, token) + c.names(t, token) + c.peaks(t, token) + c.federation(t, token, libID, albumID) + // ---- every operation in the spec was exercised ---- var missing []string for _, op := range specOperations(loadSpec(t)) { @@ -979,3 +1138,384 @@ func (c *contract) addOrphan(libraryID string) orphanIDs { } return orphanIDs{album: strconv.FormatInt(album, 10), track: strconv.FormatInt(track, 10)} } + +// mixes exercises the mixes. The library gets 40 songs of one genre by 6 artists, enough for a +// genre mix; they are added last so that nothing that counts songs sees them. +func (c *contract) mixes(t *testing.T, token, libID string) { + t.Helper() + ctx := context.Background() + repo := storage.NewLibraryRepo(c.db) + for i := 0; i < 40; i++ { + artist := "Mix artist " + strconv.Itoa(i%6) + title := "Mix song " + strconv.Itoa(i) + f := library.ParsedFile{ + FileInfo: library.FileInfo{RelPath: "mixes/" + title + ".flac", Size: 100, MTimeUnix: 1}, + Metadata: library.Metadata{ + Tags: library.Tags{Title: title, Album: "Mix album", AlbumArtist: artist, Artists: []string{artist}, TrackNo: i + 1, DiscNo: 1, Genres: []string{"synthwave"}}, + Properties: library.Properties{Format: "flac", DurationMs: 60000, SampleRate: 44100}, + }, + } + if _, err := repo.SaveFile(ctx, mustInt(t, libID), f, 0); err != nil { + t.Fatal(err) + } + } + c.expect(c.call("GET", "/mixes", "", nil), 401, "mixes without a token", "unauthorized") + list := c.expect(c.call("GET", "/mixes", token, nil), 200, "list mixes") + var found bool + for _, x := range list["items"].([]any) { + if x.(map[string]any)["id"] == "genre:synthwave" { + found = true + } + } + if !found { + t.Fatalf("mixes = %v, want a synthwave mix", list) + } + mix := c.expect(c.call("GET", "/mixes/genre%3Asynthwave", token, nil), 200, "get a mix") + if len(mix["tracks"].([]any)) != 40 { + t.Fatalf("mix = %v", mix) + } + c.expect(c.call("GET", "/mixes/genre%3Asynthwave?variant=3", token, nil), 200, "get another variant") + c.expect(c.call("GET", "/mixes/genre%3Asynthwave?variant=-1", token, nil), 400, "a negative variant", "invalid_input") + c.expect(c.call("GET", "/mixes/genre%3Apolka", token, nil), 404, "an unknown mix", "mix_not_found") + c.expect(c.call("POST", "/mixes/genre%3Asynthwave/playlist", token, map[string]any{"name": "Synth", "variant": 3}), 201, "save a mix as a playlist") + c.expect(c.call("POST", "/mixes/genre%3Asynthwave/playlist", token, nil), 201, "save a mix with no body") + c.expect(c.call("POST", "/mixes/genre%3Apolka/playlist", token, nil), 404, "save an unknown mix", "mix_not_found") + c.expect(c.call("POST", "/mixes/genre%3Asynthwave/playlist", token, map[string]any{"variant": -1}), 400, "save with a negative variant", "invalid_input") +} + +// smartPlaylists makes, reads, previews, changes, keeps and deletes smart playlists, over the synthwave +// songs that c.mixes added. +func (c *contract) smartPlaylists(t *testing.T, token string) { + t.Helper() + rules := map[string]any{"genres": []string{"synthwave"}, "formats": []string{"flac"}, "never_played": true} + c.expect(c.call("GET", "/smart-playlists", "", nil), 401, "smart playlists without a token", "unauthorized") + sug := c.expect(c.call("GET", "/smart-playlists/suggested", token, nil), 200, "the suggested smart playlists") + if len(sug["items"].([]any)) < 5 { + t.Fatalf("suggested = %v", sug) + } + c.expect(c.call("GET", "/smart-playlists/suggested/never-played/tracks", token, nil), 200, "the songs of a suggested one") + c.expect(c.call("GET", "/smart-playlists/suggested/never-played/covers", token, nil), 200, "the covers of a suggested one") + c.expect(c.call("GET", "/smart-playlists/suggested/nonsense/covers", token, nil), 404, "the covers of an unknown suggested one", "suggested_not_found") + c.expect(c.call("GET", "/smart-playlists/suggested/never-played/tracks?variant=-1", token, nil), 400, "a negative variant", "invalid_input") + c.expect(c.call("GET", "/smart-playlists/suggested/nonsense/tracks", token, nil), 404, "an unknown suggested one", "suggested_not_found") + c.expect(c.call("POST", "/smart-playlists/suggested/never-played/playlist", token, map[string]any{"name": "Fresh ears", "variant": 1}), 201, "keep a suggested one") + c.expect(c.call("POST", "/smart-playlists/suggested/never-played/playlist", token, nil), 201, "keep a suggested one with no body") + c.expect(c.call("POST", "/smart-playlists/suggested/high-quality/playlist", token, nil), 422, "keep a suggested one with no songs", "smart_playlist_empty") + c.expect(c.call("POST", "/smart-playlists/suggested/nonsense/playlist", token, nil), 404, "keep an unknown suggested one", "suggested_not_found") + genres := c.expect(c.call("GET", "/genres", token, nil), 200, "the genres of the library") + if len(genres["items"].([]any)) == 0 { + t.Fatalf("genres = %v", genres) + } + c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "", "rules": rules}), 400, "a smart playlist with no name", "invalid_input") + c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "x", "sort": "loudest"}), 400, "an unknown order", "invalid_input") + made := c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "Fresh synth", "description": "new", "rules": rules, "sort": "title", "limit": 12}), 201, "make a smart playlist") + id := strconv.Itoa(int(made["id"].(float64))) + list := c.expect(c.call("GET", "/smart-playlists?limit=5&offset=0", token, nil), 200, "list smart playlists") + if list["total"] != float64(1) { + t.Fatalf("smart playlists = %v", list) + } + c.expect(c.call("GET", "/smart-playlists/"+id, token, nil), 200, "get a smart playlist") + c.expect(c.call("GET", "/smart-playlists/999999", token, nil), 404, "an unknown smart playlist", "smart_playlist_not_found") + c.expect(c.call("GET", "/smart-playlists/0", token, nil), 400, "a bad smart playlist id", "invalid_input") + got := c.expect(c.call("GET", "/smart-playlists/"+id+"/tracks", token, nil), 200, "the songs of a smart playlist") + if len(got["tracks"].([]any)) != 12 { + t.Fatalf("smart playlist songs = %v", got) + } + c.expect(c.call("GET", "/smart-playlists/"+id+"/tracks?variant=2", token, nil), 200, "the songs, another variant") + c.expect(c.call("GET", "/smart-playlists/"+id+"/covers?variant=2", token, nil), 200, "the covers of a smart playlist") + c.expect(c.call("GET", "/smart-playlists/999999/covers", token, nil), 404, "the covers of an unknown smart playlist", "smart_playlist_not_found") + c.expect(c.call("GET", "/smart-playlists/"+id+"/covers?variant=-1", token, nil), 400, "the covers with a bad variant", "invalid_input") + c.expect(c.call("GET", "/smart-playlists/"+id+"/tracks?variant=-1", token, nil), 400, "a negative variant", "invalid_input") + c.expect(c.call("GET", "/smart-playlists/999999/tracks", token, nil), 404, "the songs of an unknown smart playlist", "smart_playlist_not_found") + c.expect(c.call("POST", "/smart-playlists/preview", token, map[string]any{"rules": map[string]any{"min_duration_sec": 30, "liked_artists_only": false}, "sort": "most_played", "limit": 5, "variant": 1}), 200, "preview rules") + c.expect(c.call("POST", "/smart-playlists/preview", token, map[string]any{"rules": map[string]any{"min_plays": -1}}), 400, "preview bad rules", "invalid_input") + c.expect(c.call("PATCH", "/smart-playlists/"+id, token, map[string]any{"name": "Fresher", "rules": map[string]any{"added_within_days": 3650}}), 200, "change a smart playlist") + c.expect(c.call("PATCH", "/smart-playlists/"+id, token, map[string]any{"limit": 0, "name": ""}), 400, "change to a bad name", "invalid_input") + c.expect(c.call("PATCH", "/smart-playlists/999999", token, map[string]any{"name": "x"}), 404, "change an unknown smart playlist", "smart_playlist_not_found") + c.expect(c.call("POST", "/smart-playlists/"+id+"/playlist", token, map[string]any{"name": "Kept", "variant": 1}), 201, "keep a smart playlist as a playlist") + c.expect(c.call("POST", "/smart-playlists/"+id+"/playlist", token, nil), 201, "keep with no body") + c.expect(c.call("POST", "/smart-playlists/999999/playlist", token, nil), 404, "keep an unknown smart playlist", "smart_playlist_not_found") + empty := c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "Polka", "rules": map[string]any{"genres": []string{"polka"}}}), 201, "make a smart playlist that gives nothing") + c.expect(c.call("POST", "/smart-playlists/"+strconv.Itoa(int(empty["id"].(float64)))+"/playlist", token, nil), 422, "keep one that gives no songs", "smart_playlist_empty") + c.expect(c.call("DELETE", "/smart-playlists/"+id, token, nil), 204, "delete a smart playlist") + c.expect(c.call("DELETE", "/smart-playlists/"+id, token, nil), 404, "delete it again", "smart_playlist_not_found") +} + +// names asks for names with the Claude provider off, so that the local ones answer. +func (c *contract) names(t *testing.T, token string) { + t.Helper() + c.expect(c.call("GET", "/providers/anthropic/usage", "", nil), 401, "the costs without a token", "unauthorized") + use := c.expect(c.call("GET", "/providers/anthropic/usage?days=7", token, nil), 200, "the costs") + if use["days"] != float64(7) || use["estimate"] != true || len(use["daily"].([]any)) != 0 { + t.Fatalf("costs = %v", use) + } + c.expect(c.call("GET", "/providers/anthropic/usage?days=366", token, nil), 400, "the costs of too many days", "invalid_input") + c.expect(c.call("GET", "/naming", "", nil), 401, "naming status without a token", "unauthorized") + if st := c.expect(c.call("GET", "/naming", token, nil), 200, "naming status"); st["auto"] != false { + t.Fatalf("naming status = %v, want off", st) + } + named := c.expect(c.call("POST", "/mixes/genre%3Asynthwave/playlist", token, map[string]any{"name": "Synth mix", "auto_name": true, "lang": "it"}), 201, "keep a mix with an automatic name, Claude off") + if named["name"] != "Synth mix" { + t.Fatalf("playlist = %v: with Claude off the given name stays", named) + } + c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "", "rules": map[string]any{}, "lang": "it"}), 400, "a smart playlist with no name and Claude off", "invalid_input") + c.expect(c.call("POST", "/mixes/genre%3Asynthwave/suggest-name", "", nil), 401, "a name without a token", "unauthorized") + mix := c.expect(c.call("POST", "/mixes/genre%3Asynthwave/suggest-name", token, map[string]any{"lang": "it", "variant": 1}), 200, "a name for a mix") + if mix["source"] != "local" || mix["name"] != "Mix Synthwave" { + t.Fatalf("mix name = %v", mix) + } + c.expect(c.call("POST", "/mixes/genre%3Asynthwave/suggest-name", token, nil), 200, "a name for a mix with no body") + c.expect(c.call("POST", "/mixes/genre%3Apolka/suggest-name", token, nil), 404, "a name for an unknown mix", "mix_not_found") + c.expect(c.call("POST", "/mixes/genre%3Asynthwave/suggest-name", token, map[string]any{"variant": -1}), 400, "a bad variant", "invalid_input") + pl := c.expect(c.call("POST", "/playlists", token, map[string]any{"name": "tmp"}), 201, "make a playlist to name") + id := strconv.Itoa(int(pl["id"].(float64))) + c.expect(c.call("POST", "/playlists/"+id+"/suggest-name", token, nil), 200, "a name for an empty playlist") + c.expect(c.call("POST", "/playlists/999999/suggest-name", token, nil), 404, "a name for an unknown playlist", "playlist_not_found") + sp := c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "Synth", "rules": map[string]any{"genres": []string{"synthwave"}}, "limit": 10}), 201, "make a smart playlist to name") + sid := strconv.Itoa(int(sp["id"].(float64))) + c.expect(c.call("POST", "/smart-playlists/"+sid+"/suggest-name", token, map[string]any{"lang": "en"}), 200, "a name for a smart playlist") + c.expect(c.call("POST", "/smart-playlists/999999/suggest-name", token, nil), 404, "a name for an unknown smart playlist", "smart_playlist_not_found") +} + +func (c *contract) federation(t *testing.T, token, libID, albumID string) { + t.Helper() + for _, r := range []struct{ method, path string }{ + {"GET", "/federation"}, {"PUT", "/federation"}, {"POST", "/federation/invites"}, {"POST", "/federation/friends"}, + {"POST", "/federation/friends/1/approve"}, {"DELETE", "/federation/friends/1"}, + } { + c.expect(c.call(r.method, r.path, "", map[string]any{}), 401, r.method+" "+r.path+" without a token", "unauthorized") + } + st := c.expect(c.call("GET", "/federation", token, nil), 200, "federation") + if st["enabled"] != false || st["key_id"] != "" { + t.Fatalf("federation = %v, want off and no key", st) + } + c.expect(c.call("POST", "/federation/invites", token, nil), 409, "an invite while federation is off", "federation_off") + c.expect(c.call("PUT", "/federation", token, map[string]any{"address": "http://music.example.org"}), 400, "a public http address", "invalid_input") + on := c.expect(c.call("PUT", "/federation", token, map[string]any{"enabled": true, "name": "Contract"}), 200, "turn federation on") + if on["enabled"] != true || len(on["key_id"].(string)) != 16 { + t.Fatalf("federation = %v", on) + } + c.expect(c.call("POST", "/federation/invites", token, nil), 409, "an invite without an address", "address_required") + c.expect(c.call("PUT", "/federation", token, map[string]any{"address": "https://contract.example.org"}), 200, "give the address") + inv := c.expect(c.call("POST", "/federation/invites", token, nil), 201, "an invite") + + // Another server pairs with this one, then this one approves it and ends the friendship. + c.expect(c.call("POST", "/peer/pair", "", pairBody("wrong", "Other", "https://other.example.org")), 403, "pairing with a wrong code", "invalid_invite") + c.expect(c.call("POST", "/peer/pair", "", map[string]any{"code": "c", "name": "", "address": "https://o.example.org", "public_key": "AAAA"}), 400, "pairing with a bad key", "invalid_input") + code := inviteCode(t, inv["invite"].(string)) + body := pairBody(code, "Other", "https://other.example.org") + reply := c.expect(c.call("POST", "/peer/pair", "", body), 200, "pairing with the invite") + if reply["name"] != "Contract" || reply["address"] != "https://contract.example.org" { + t.Fatalf("pair reply = %v", reply) + } + c.expect(c.call("POST", "/peer/pair", "", pairBody(code, "Other", "https://other.example.org")), 403, "using the invite twice", "invalid_invite") + c.expect(c.call("POST", "/peer/pair", "", body), 409, "pairing a known key", "already_friends") + list := c.expect(c.call("GET", "/federation", token, nil), 200, "federation with a request")["friends"].([]any) + if len(list) != 1 || list[0].(map[string]any)["state"] != "incoming" { + t.Fatalf("friends = %v", list) + } + id := idOf(list[0].(map[string]any)) + c.expect(c.call("POST", "/federation/friends/"+id+"/approve", token, nil), 200, "approve") + c.expect(c.call("POST", "/federation/friends/"+id+"/approve", token, nil), 409, "approve twice", "not_waiting") + c.expect(c.call("POST", "/federation/friends/999999/approve", token, nil), 404, "approve a missing friend", "friend_not_found") + c.expect(c.call("DELETE", "/federation/friends/"+id, token, nil), 204, "end the friendship") + c.expect(c.call("DELETE", "/federation/friends/"+id, token, nil), 404, "end it twice", "friend_not_found") + + c.peer(t, token, libID, albumID) + c.friends(t, token) + + // This server uses an invite of another one, which is a real server of its own. + other := newFriendServer(t, true) + other.setUp(t, "Anna", other.ts.URL) + theirs := other.call(t, "POST", "/federation/invites", nil) + c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": "hello"}), 400, "an invite that is not one", "invalid_invite") + friend := c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": theirs.Body["invite"]}), 201, "use an invite") + if friend["state"] != "outgoing" || friend["name"] != "Anna" { + t.Fatalf("friend = %v", friend) + } + c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": theirs.Body["invite"]}), 409, "use the invite of a friend", "already_friends") + c.expect(c.call("POST", "/federation/friends/"+idOf(friend)+"/check", token, nil), 409, "ask a friend that has not approved", "not_approved") + c.expect(c.call("POST", "/federation/friends/999999/check", token, nil), 404, "check a missing friend", "friend_not_found") + c.expect(c.call("DELETE", "/federation/friends/"+idOf(friend), token, nil), 204, "withdraw the request") + again := other.call(t, "POST", "/federation/invites", nil) + other.ts.Close() + c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": again.Body["invite"]}), 502, "use an invite of a server that is gone", "friend_unreachable") +} + +// inviteCode reads the code out of an invite text, as a test that plays the other server needs to. +func inviteCode(t *testing.T, text string) string { + t.Helper() + raw, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(text, "sonda1.")) + if err != nil { + t.Fatal(err) + } + var p struct { + C string `json:"c"` + } + if err := json.Unmarshal(raw, &p); err != nil || p.C == "" { + t.Fatalf("invite %q has no code: %v", text, err) + } + return p.C +} + +// peer plays the part of a friend: it holds a key of its own, is paired and approved, and reads what is shared. +func (c *contract) peer(t *testing.T, token, libID, albumID string) { + t.Helper() + c.expect(c.call("PATCH", "/libraries/"+libID, token, map[string]any{"shared": true}), 200, "share the library") + + inv := c.expect(c.call("POST", "/federation/invites", token, nil), 201, "an invite for the friend") + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + c.expect(c.call("POST", "/peer/pair", "", map[string]any{ + "code": inviteCode(t, inv["invite"].(string)), "name": "Reader", "address": "https://reader.example.org", + "public_key": base64.RawURLEncoding.EncodeToString(pub), + }), 200, "the friend pairs") + var friendID string + for _, f := range c.expect(c.call("GET", "/federation", token, nil), 200, "federation")["friends"].([]any) { + if f.(map[string]any)["name"] == "Reader" { + friendID = idOf(f.(map[string]any)) + } + } + c.expect(c.call("POST", "/federation/friends/"+friendID+"/approve", token, nil), 200, "approve the friend") + + var n int + signed := func(path string, extra ...string) *httptest.ResponseRecorder { + n++ + h := federation.Sign(priv, "GET", "/api/v1"+path, time.Now(), "contract-nonce-"+strconv.Itoa(n)+"-0123456789") + headers := append([]string{ + federation.HeaderKeyID, federation.KeyID(pub), federation.HeaderDate, h.Date, + federation.HeaderNonce, h.Nonce, federation.HeaderSignature, h.Signature, + }, extra...) + return c.call("GET", path, "", nil, headers...) + } + + c.expect(c.call("GET", "/peer/info", "", nil), 401, "a friend's route without a signature", "unauthorized") + info := c.expect(signed("/peer/info"), 200, "who this server is") + if info["shared_libraries"] != float64(1) { + t.Fatalf("info = %v", info) + } + artists := c.expect(signed("/peer/artists?limit=5"), 200, "the artists of the shared libraries") + if artists["total"].(float64) == 0 { + t.Fatalf("artists = %v", artists) + } + c.expect(signed("/peer/artists/"+idOf(artists["items"].([]any)[0].(map[string]any))), 200, "one artist") + c.expect(signed("/peer/artists/999999"), 404, "an artist that is not there", "artist_not_found") + c.expect(signed("/peer/albums?q=a&limit=5"), 200, "the albums") + c.expect(signed("/peer/albums/"+albumID), 200, "one album") + c.expect(signed("/peer/albums/999999"), 404, "an album that is not there", "album_not_found") + tracks := c.expect(signed("/peer/tracks?limit=5"), 200, "the tracks") + trackID := idOf(tracks["items"].([]any)[0].(map[string]any)) + c.expect(signed("/peer/tracks/"+trackID), 200, "one track") + c.expect(signed("/peer/tracks/999999"), 404, "a track that is not there", "track_not_found") + + if rec := signed("/peer/tracks/" + trackID + "/stream"); rec.Code != 200 { + t.Fatalf("stream: %d %s", rec.Code, rec.Body.String()) + } + if rec := signed("/peer/tracks/"+trackID+"/stream", "Range", "bytes=0-9"); rec.Code != 206 { + t.Fatalf("stream range: %d", rec.Code) + } + c.expect(signed("/peer/tracks/999999/stream"), 404, "the stream of a track that is not there", "track_not_found") + c.expect(signed("/peer/tracks/999999/peaks"), 404, "the shape of a track that is not there", "track_not_found") + c.expect(signed("/peer/tracks/"+trackID+"/peaks"), 404, "the shape of a file that is not audio", "waveform_unavailable") + // A friend gets a cover once a listener's request has built it, never before. + c.expect(signed("/peer/albums/"+albumID+"/cover?size=small"), 404, "a cover that was not asked for yet", "cover_not_found") + if rec := c.call("GET", "/albums/"+albumID+"/cover", token, nil); rec.Code != 200 { + t.Fatalf("the listener's cover: %d %s", rec.Code, rec.Body.String()) + } + if rec := signed("/peer/albums/" + albumID + "/cover"); rec.Code != 200 || rec.Header().Get("Content-Type") != "image/jpeg" { + t.Fatalf("cover: %d %q", rec.Code, rec.Header().Get("Content-Type")) + } + c.expect(signed("/peer/albums/999999/cover"), 404, "the cover of an album that is not there", "album_not_found") + + reqs := c.expect(c.call("GET", "/federation/requests", token, nil), 200, "what friends asked")["requests"].([]any) + if len(reqs) == 0 { + t.Fatal("no request was logged") + } + c.expect(c.call("DELETE", "/federation/friends/"+friendID, token, nil), 204, "remove the reader") + c.expect(c.call("PATCH", "/libraries/"+libID, token, map[string]any{"shared": false}), 200, "stop sharing the library") +} + +// friends plays a listener of this server who browses what a friend shares. The friend is a whole server of its own. +func (c *contract) friends(t *testing.T, token string) { + t.Helper() + p := newPeerFixture(t) + p.share(t, p.shared, true) + ts := httptest.NewServer(p.h) + defer ts.Close() + p.admin1(t, "PUT", "/federation", map[string]any{"enabled": true, "name": "Anna", "address": ts.URL}) + inv := p.admin1(t, "POST", "/federation/invites", nil) + mine := c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": inv.Body["invite"]}), 201, "use the invite of a friend to browse") + fid := idOf(mine) + theirs := p.admin1(t, "GET", "/federation", nil).Body["friends"].([]any) + p.admin1(t, "POST", "/federation/friends/"+idOf(theirs[0].(map[string]any))+"/approve", nil) + + c.expect(c.call("GET", "/friends", "", nil), 401, "the friends without a login", "unauthorized") + if got := c.expect(c.call("GET", "/friends", token, nil), 200, "the friends before it is checked")["friends"].([]any); len(got) != 0 { + t.Fatalf("a friend that is waiting is listed: %v", got) + } + c.expect(c.call("POST", "/federation/friends/"+fid+"/check", token, nil), 200, "check the friend") + list := c.expect(c.call("GET", "/friends", token, nil), 200, "the friends")["friends"].([]any) + if len(list) != 1 || list[0].(map[string]any)["name"] != "Anna" { + t.Fatalf("friends = %v", list) + } + + base := "/friends/" + fid + c.expect(c.call("GET", base+"/tracks", "", nil), 401, "a friend's tracks without a login", "unauthorized") + artists := c.expect(c.call("GET", base+"/artists?limit=5", token, nil), 200, "a friend's artists") + albums := c.expect(c.call("GET", base+"/albums?q=shared", token, nil), 200, "a friend's albums") + tracks := c.expect(c.call("GET", base+"/tracks?limit=5&offset=0", token, nil), 200, "a friend's tracks") + if artists["total"] != float64(1) || albums["total"] != float64(1) || tracks["total"] != float64(1) { + t.Fatalf("what the friend shares: %v %v %v", artists["total"], albums["total"], tracks["total"]) + } + albumID, trackID := idOf(albums["items"].([]any)[0].(map[string]any)), idOf(tracks["items"].([]any)[0].(map[string]any)) + c.expect(c.call("GET", base+"/artists/"+idOf(artists["items"].([]any)[0].(map[string]any)), token, nil), 200, "one artist of a friend") + c.expect(c.call("GET", base+"/albums/"+albumID, token, nil), 200, "one album of a friend") + c.expect(c.call("GET", base+"/tracks/"+trackID, token, nil), 200, "one track of a friend") + c.expect(c.call("GET", base+"/tracks?album_id="+albumID, token, nil), 200, "the tracks of an album of a friend") + c.expect(c.call("GET", base+"/artists/999999", token, nil), 404, "an artist the friend does not have", "artist_not_found") + c.expect(c.call("GET", base+"/albums/999999", token, nil), 404, "an album the friend does not have", "album_not_found") + c.expect(c.call("GET", base+"/tracks/999999", token, nil), 404, "a track the friend does not have", "track_not_found") + c.expect(c.call("GET", "/friends/999999/tracks", token, nil), 404, "a friend that is not there", "friend_not_found") + c.expect(c.call("GET", base+"/tracks?limit=abc", token, nil), 400, "a limit that is no number", "invalid_input") + + // Playing it and seeing its cover, through this server. + stream := base + "/tracks/" + trackID + "/stream" + c.expect(c.call("GET", stream, "", nil), 401, "a friend's stream without a login", "unauthorized") + if rec := c.call("GET", stream, token, nil); rec.Code != 200 { + t.Fatalf("a friend's stream: %d %s", rec.Code, rec.Body.String()) + } + if rec := c.call("GET", stream, token, nil, "Range", "bytes=0-9"); rec.Code != 206 { + t.Fatalf("a friend's stream, a range: %d", rec.Code) + } + c.expect(c.call("GET", base+"/tracks/999999/stream", token, nil), 404, "the stream of a track the friend does not have", "track_not_found") + peaks := base + "/tracks/" + trackID + "/peaks" + c.expect(c.call("GET", peaks, "", nil), 401, "the shape of a friend's track without a login", "unauthorized") + c.expect(c.call("GET", base+"/tracks/999999/peaks", token, nil), 404, "the shape of a track the friend does not have", "track_not_found") + c.expect(c.call("GET", peaks, token, nil), 404, "the shape of a friend's file that is not audio", "waveform_unavailable") + cover := base + "/albums/" + albumID + "/cover" + c.expect(c.call("GET", cover, token, nil), 404, "a cover that is not built there yet", "cover_not_found") + var theirAlbum int64 + _ = p.db.QueryRow("SELECT id FROM albums WHERE title = 'Shared Album'").Scan(&theirAlbum) + if rec := p.serveAs(t, "GET", "/api/v1/albums/"+strconv.FormatInt(theirAlbum, 10)+"/cover"); rec.Code != 200 { + t.Fatalf("the cover on the friend's server: %d", rec.Code) + } + if rec := c.call("GET", cover, token, nil); rec.Code != 200 || rec.Header().Get("Content-Type") != "image/jpeg" { + t.Fatalf("a friend's cover: %d %q", rec.Code, rec.Header().Get("Content-Type")) + } + c.expect(c.call("GET", cover+"?size=huge", token, nil), 400, "a cover of a size that does not exist", "invalid_input") + + // The friend removes this server: the listener is told, and nothing is kept. + p.admin1(t, "DELETE", "/federation/friends/"+idOf(theirs[0].(map[string]any)), nil) + c.expect(c.call("GET", base+"/tracks", token, nil), 409, "a friend that removed this server", "friend_gone") + c.expect(c.call("DELETE", "/federation/friends/"+fid, token, nil), 204, "forget the friend") +} + +// peaks asks for the shape of tracks. The files of this test are not audio, so none can be decoded. +func (c *contract) peaks(t *testing.T, token string) { + t.Helper() + c.expect(c.call("GET", "/tracks/1/peaks", "", nil), 401, "the shape of a track without a login", "unauthorized") + c.expect(c.call("GET", "/tracks/abc/peaks", token, nil), 400, "the shape of a track that is no number", "invalid_input") + c.expect(c.call("GET", "/tracks/999999/peaks", token, nil), 404, "the shape of a track that is not there", "track_not_found") + tracks := c.expect(c.call("GET", "/tracks?limit=1", token, nil), 200, "a track")["items"].([]any) + id := idOf(tracks[0].(map[string]any)) + c.expect(c.call("GET", "/tracks/"+id+"/peaks", token, nil), 404, "the shape of a file that is not audio", "waveform_unavailable") +} diff --git a/internal/api/http/covers_test.go b/internal/api/http/covers_test.go index 6e1100c..3692b59 100644 --- a/internal/api/http/covers_test.go +++ b/internal/api/http/covers_test.go @@ -9,16 +9,19 @@ import ( "image/jpeg" "net/http" "net/http/httptest" + "net/url" "os" "path/filepath" + "strings" "sync/atomic" "testing" "github.com/uptrace/bun" - "codeberg.org/squidd/sonda/internal/images" - "codeberg.org/squidd/sonda/internal/library" - "codeberg.org/squidd/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/images" + "git.kmalloc.org/squid/sonda/internal/library" + "git.kmalloc.org/squid/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/streaming" ) type fakeArchive struct { @@ -80,6 +83,7 @@ func newCoversEnv(t *testing.T) *coversEnv { svc.SetAlbumRemote(e.archive) e.svc = svc d.Images = svc + d.Streams = streaming.NewService(storage.NewLibraryRepo(db)) }) e.token = registerAndLogin(t, e.h) @@ -301,3 +305,160 @@ func TestACoverOfFilesThatCannotBeReadIsServiceUnavailableNotNoCover(t *testing. t.Errorf("the archive was asked %d times about files that could not be read", n) } } + +func TestAnAppGetsCoversAndSongsOverRest(t *testing.T) { + e := newCoversEnv(t) + secret := do(t, e.h, "POST", "/api/v1/me/devices", e.token, map[string]any{"name": "Phone"}).Body["secret"].(string) + get := func(path string, q url.Values) *httptest.ResponseRecorder { + q.Set("apiKey", secret) + rec := httptest.NewRecorder() + e.h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil)) + return rec + } + + // The cover, small and large, is the one the main API makes. + for size, max := range map[string]int{"150": 300, "1200": 1000} { + rec := get("getCoverArt", url.Values{"id": {"al-" + itoa(e.withArt)}, "size": {size}}) + if rec.Code != 200 || rec.Header().Get("Content-Type") != "image/jpeg" { + t.Fatalf("size %s: %d %s %s", size, rec.Code, rec.Header().Get("Content-Type"), rec.Body.String()) + } + img, err := jpeg.Decode(rec.Body) + if err != nil || img.Bounds().Dx() > max || img.Bounds().Dx() < 100 { + t.Fatalf("size %s: %v %v", size, err, img.Bounds()) + } + } + if rec := get("getCoverArt", url.Values{"id": {"al-" + itoa(e.without)}, "f": {"json"}}); errorFrom(t, rec) != 70 { + t.Fatalf("an album with no picture: %s", rec.Body.String()) + } + + // The album, found by search, has a song that plays: the file as it is, with Range. + res := get("search3", url.Values{"query": {"With Art"}, "f": {"json"}}) + var found struct { + R struct { + S struct { + Song []struct{ ID, Suffix, CoverArt string } `json:"song"` + } `json:"searchResult3"` + } `json:"subsonic-response"` + } + if err := json.Unmarshal(res.Body.Bytes(), &found); err != nil || len(found.R.S.Song) != 1 { + t.Fatalf("search: %v %s", err, res.Body.String()) + } + song := found.R.S.Song[0] + if song.Suffix != "mp3" || song.CoverArt != "al-"+itoa(e.withArt) { + t.Fatalf("song %+v", song) + } + if rec := get("stream", url.Values{"id": {song.ID}}); rec.Code != 200 || rec.Body.String() != "not audio" || rec.Header().Get("Content-Type") != "audio/mpeg" { + t.Fatalf("stream: %d %q %q", rec.Code, rec.Body.String(), rec.Header().Get("Content-Type")) + } +} + +func errorFrom(t *testing.T, rec *httptest.ResponseRecorder) int { + t.Helper() + var out struct { + R map[string]any `json:"subsonic-response"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("%v: %s", err, rec.Body.String()) + } + return errorCode(out.R) +} + +func TestPlaylistsAndListensOfAnAppAreThoseOfThePerson(t *testing.T) { + e := newCoversEnv(t) + secret := do(t, e.h, "POST", "/api/v1/me/devices", e.token, map[string]any{"name": "Phone"}).Body["secret"].(string) + var song int64 + if err := e.db.QueryRow("SELECT id FROM tracks WHERE title = 'With Art song'").Scan(&song); err != nil { + t.Fatal(err) + } + rest := func(path string, q url.Values) map[string]any { + q.Set("apiKey", secret) + q.Set("f", "json") + rec := httptest.NewRecorder() + e.h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil)) + var out struct { + R map[string]any `json:"subsonic-response"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || out.R["status"] != "ok" { + t.Fatalf("%s: %v %s", path, err, rec.Body.String()) + } + return out.R + } + + // A playlist made in the app is in the web app, and the other way round. + made := rest("createPlaylist", url.Values{"name": {"From the phone"}, "songId": {"tr-" + itoa(song)}})["playlist"].(map[string]any) + if made["songCount"].(float64) != 1 { + t.Fatalf("made %v", made) + } + listed := do(t, e.h, "GET", "/api/v1/playlists", e.token, nil) + items, _ := listed.Body["items"].([]any) + if len(items) != 1 || items[0].(map[string]any)["name"] != "From the phone" { + t.Fatalf("the web app sees %v", listed.Body) + } + web := do(t, e.h, "POST", "/api/v1/playlists", e.token, map[string]any{"name": "From the web"}) + if web.Code != 201 { + t.Fatalf("%d %v", web.Code, web.Body) + } + own := 0 // the stations of the radio are in the list too: only the playlists of the person are counted + for _, p := range rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any) { + if strings.HasPrefix(p.(map[string]any)["id"].(string), "pl-") { + own++ + } + } + if n := own; n != 2 { + t.Fatalf("the app sees %d playlists", n) + } + rest("deletePlaylist", url.Values{"id": {made["id"].(string)}}) + if n := len(do(t, e.h, "GET", "/api/v1/playlists", e.token, nil).Body["items"].([]any)); n != 1 { + t.Fatalf("after delete the web app sees %d", n) + } + + // A listen reported by the app is in the history of the person, once. + for i := 0; i < 2; i++ { + rest("scrobble", url.Values{"id": {"tr-" + itoa(song)}, "time": {"1700000000000"}}) + } + hist := do(t, e.h, "GET", "/api/v1/listens", e.token, nil) + if got := hist.Body["total"]; got != float64(1) { + t.Fatalf("history %v", hist.Body) + } +} + +func TestAStarInAnAppIsAFavouriteInTheWebApp(t *testing.T) { + e := newCoversEnv(t) + secret := do(t, e.h, "POST", "/api/v1/me/devices", e.token, map[string]any{"name": "Phone"}).Body["secret"].(string) + var song int64 + _ = e.db.QueryRow("SELECT id FROM tracks WHERE title = 'With Art song'").Scan(&song) + rest := func(path string, q url.Values) map[string]any { + q.Set("apiKey", secret) + q.Set("f", "json") + rec := httptest.NewRecorder() + e.h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil)) + var out struct { + R map[string]any `json:"subsonic-response"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || out.R["status"] != "ok" { + t.Fatalf("%s: %v %s", path, err, rec.Body.String()) + } + return out.R + } + + rest("star", url.Values{"id": {"tr-" + itoa(song)}, "albumId": {"al-" + itoa(e.withArt)}}) + ids := do(t, e.h, "GET", "/api/v1/me/favorites/ids", e.token, nil).Body + if len(ids["tracks"].([]any)) != 1 || len(ids["albums"].([]any)) != 1 { + t.Fatalf("the web app sees %v", ids) + } + if n := len(rest("getAlbumList2", url.Values{"type": {"starred"}})["albumList2"].(map[string]any)["album"].([]any)); n != 1 { + t.Fatalf("starred albums %d", n) + } + + // And the other way round: a favourite made in the web app is starred in the app. + do(t, e.h, "DELETE", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil) + do(t, e.h, "PUT", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil) + st := rest("getStarred2", url.Values{})["starred2"].(map[string]any) + if len(st["song"].([]any)) != 1 || st["song"].([]any)[0].(map[string]any)["starred"] == nil { + t.Fatalf("starred %v", st) + } + rest("unstar", url.Values{"id": {"tr-" + itoa(song)}}) + if got := do(t, e.h, "GET", "/api/v1/me/favorites/ids", e.token, nil).Body["tracks"].([]any); len(got) != 0 { + t.Fatalf("after unstar %v", got) + } +} diff --git a/internal/api/http/description_test.go b/internal/api/http/description_test.go index c5ede88..fa8c26f 100644 --- a/internal/api/http/description_test.go +++ b/internal/api/http/description_test.go @@ -11,9 +11,9 @@ import ( "github.com/uptrace/bun" "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/about" - "codeberg.org/squidd/sonda/internal/providers" - "codeberg.org/squidd/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/about" + "git.kmalloc.org/squid/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/storage" ) // fakeWikipedia is the whole chain, in one place: it counts what was asked. diff --git a/internal/api/http/devices_test.go b/internal/api/http/devices_test.go new file mode 100644 index 0000000..01a5ce8 --- /dev/null +++ b/internal/api/http/devices_test.go @@ -0,0 +1,102 @@ +package httpapi + +import ( + "net/http" + "strings" + "testing" +) + +func TestDevicesNeedASignedInUser(t *testing.T) { + h := newTestAPI(t) + for _, c := range [][2]string{{"GET", "/api/v1/me/devices"}, {"POST", "/api/v1/me/devices"}, {"DELETE", "/api/v1/me/devices/1"}} { + if r := do(t, h, c[0], c[1], "", map[string]any{}); r.Code != http.StatusUnauthorized { + t.Errorf("%s %s without a token: %d", c[0], c[1], r.Code) + } + } +} + +func TestADeviceIsMadeShownOnceAndTakenBack(t *testing.T) { + h := newTestAPI(t) + alice := registerAndLogin(t, h) + + r := do(t, h, "GET", "/api/v1/me/devices", alice, nil) + if r.Code != http.StatusOK { + t.Fatalf("%d %v", r.Code, r.Body) + } + if list, ok := r.Body["devices"].([]any); !ok || len(list) != 0 { + t.Fatalf("devices at the start: %v", r.Body) + } + + r = do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "Phone"}) + if r.Code != http.StatusCreated { + t.Fatalf("create: %d %v", r.Code, r.Body) + } + sameKeys(t, "created device", r.Body, "id", "name", "created_at", "secret") + secret, _ := r.Body["secret"].(string) + if len(secret) != 48 || r.Body["name"] != "Phone" { + t.Fatalf("created = %v", r.Body) + } + + r = do(t, h, "GET", "/api/v1/me/devices", alice, nil) + list, _ := r.Body["devices"].([]any) + if len(list) != 1 { + t.Fatalf("list = %v", r.Body) + } + one := list[0].(map[string]any) + sameKeys(t, "listed device", one, "id", "name", "created_at") + if strings.Contains(r.Body["devices"].([]any)[0].(map[string]any)["name"].(string), secret) { + t.Fatal("the secret is in the list") + } + + id := int64(one["id"].(float64)) + path := "/api/v1/me/devices/" + itoa(id) + if r := do(t, h, "DELETE", path, alice, nil); r.Code != http.StatusNoContent { + t.Fatalf("delete: %d %v", r.Code, r.Body) + } + if r := do(t, h, "DELETE", path, alice, nil); r.Code != http.StatusNotFound || errCode(r) != "device_not_found" { + t.Fatalf("delete twice: %d %v", r.Code, r.Body) + } +} + +func TestADeviceNeedsAName(t *testing.T) { + h := newTestAPI(t) + alice := registerAndLogin(t, h) + for _, body := range []any{map[string]any{"name": " "}, map[string]any{}, map[string]any{"name": strings.Repeat("a", 61)}} { + if r := do(t, h, "POST", "/api/v1/me/devices", alice, body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_name" { + t.Errorf("%v: %d %v", body, r.Code, r.Body) + } + } + if r := do(t, h, "POST", "/api/v1/me/devices", alice, "{not json"); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Errorf("bad json: %d %v", r.Code, r.Body) + } + if r := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "ok", "extra": 1}); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Errorf("unknown name: %d %v", r.Code, r.Body) + } +} + +func TestADeviceOfSomeoneElseIsNotFound(t *testing.T) { + h := newTestAPI(t) + alice := registerAndLogin(t, h) + r := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "Phone"}) + id := int64(r.Body["id"].(float64)) + _, bob := addUser(t, h, alice, "bob", false) + if r := do(t, h, "DELETE", "/api/v1/me/devices/"+itoa(id), bob, nil); r.Code != http.StatusNotFound { + t.Fatalf("bob deleted alice's device: %d", r.Code) + } + if r := do(t, h, "GET", "/api/v1/me/devices", bob, nil); len(r.Body["devices"].([]any)) != 0 { + t.Fatalf("bob sees %v", r.Body) + } +} + +func TestTooManyDevices(t *testing.T) { + h := newTestAPI(t) + alice := registerAndLogin(t, h) + for i := 0; i < 20; i++ { + if r := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "d"}); r.Code != http.StatusCreated { + t.Fatalf("%d: %d", i, r.Code) + } + } + if r := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "d"}); r.Code != http.StatusConflict || errCode(r) != "too_many_devices" { + t.Fatalf("over the limit: %d %v", r.Code, r.Body) + } +} diff --git a/internal/api/http/discover_busy_test.go b/internal/api/http/discover_busy_test.go index a682408..ab2334b 100644 --- a/internal/api/http/discover_busy_test.go +++ b/internal/api/http/discover_busy_test.go @@ -9,7 +9,7 @@ import ( "github.com/uptrace/bun" - "codeberg.org/squidd/sonda/internal/discover" + "git.kmalloc.org/squid/sonda/internal/discover" ) type busyDiscover struct{ err error } diff --git a/internal/api/http/discover_test.go b/internal/api/http/discover_test.go index c4f4f9b..f621622 100644 --- a/internal/api/http/discover_test.go +++ b/internal/api/http/discover_test.go @@ -10,13 +10,13 @@ import ( "github.com/uptrace/bun" "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/discover" - "codeberg.org/squidd/sonda/internal/discovery" - "codeberg.org/squidd/sonda/internal/feedback" - "codeberg.org/squidd/sonda/internal/providers" - "codeberg.org/squidd/sonda/internal/storage" - "codeberg.org/squidd/sonda/internal/taste" - "codeberg.org/squidd/sonda/internal/usersettings" + "git.kmalloc.org/squid/sonda/internal/discover" + "git.kmalloc.org/squid/sonda/internal/discovery" + "git.kmalloc.org/squid/sonda/internal/feedback" + "git.kmalloc.org/squid/sonda/internal/providers" + "git.kmalloc.org/squid/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/taste" + "git.kmalloc.org/squid/sonda/internal/usersettings" ) type discoverEnv struct { diff --git a/internal/api/http/docs.go b/internal/api/http/docs.go index 50d4112..1c087cc 100644 --- a/internal/api/http/docs.go +++ b/internal/api/http/docs.go @@ -6,7 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/swaggest/swgui/v5emb" - "codeberg.org/squidd/sonda/internal/api/openapi" + "git.kmalloc.org/squid/sonda/internal/api/openapi" ) const ( diff --git a/internal/api/http/docs_test.go b/internal/api/http/docs_test.go index d30b480..0cd5126 100644 --- a/internal/api/http/docs_test.go +++ b/internal/api/http/docs_test.go @@ -13,7 +13,7 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/gin-gonic/gin" - "codeberg.org/squidd/sonda/internal/api/openapi" + "git.kmalloc.org/squid/sonda/internal/api/openapi" ) func loadSpec(t *testing.T) *openapi3.T { diff --git a/internal/api/http/duplicates_test.go b/internal/api/http/duplicates_test.go index ac38afd..ac13d9e 100644 --- a/internal/api/http/duplicates_test.go +++ b/internal/api/http/duplicates_test.go @@ -15,10 +15,10 @@ import ( "github.com/uptrace/bun" "go.uber.org/zap" - "codeberg.org/squidd/sonda/internal/duplicates" - "codeberg.org/squidd/sonda/internal/jobs" - "codeberg.org/squidd/sonda/internal/library" - "codeberg.org/squidd/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/duplicates" + "git.kmalloc.org/squid/sonda/internal/jobs" + "git.kmalloc.org/squid/sonda/internal/library" + "git.kmalloc.org/squid/sonda/internal/storage" ) // dupAPI is the API over a real library on a real folder. diff --git a/internal/api/http/favorites_test.go b/internal/api/http/favorites_test.go new file mode 100644 index 0000000..0c4ec6d --- /dev/null +++ b/internal/api/http/favorites_test.go @@ -0,0 +1,99 @@ +package httpapi + +import ( + "net/http" + "testing" +) + +func TestFavouritesNeedASignedInUser(t *testing.T) { + h := newTestAPI(t) + for _, c := range [][2]string{{"GET", "/api/v1/me/favorites/ids"}, {"GET", "/api/v1/me/favorites/tracks"}, {"PUT", "/api/v1/me/favorites/tracks/1"}, {"DELETE", "/api/v1/me/favorites/tracks/1"}} { + if r := do(t, h, c[0], c[1], "", map[string]any{}); r.Code != http.StatusUnauthorized { + t.Errorf("%s %s: %d", c[0], c[1], r.Code) + } + } +} + +func TestSongsAlbumsAndArtistsAreMarkedListedAndUnmarked(t *testing.T) { + e := newCoversEnv(t) + var song, artist int64 + if err := e.db.QueryRow("SELECT id FROM tracks WHERE title = 'With Art song'").Scan(&song); err != nil { + t.Fatal(err) + } + if err := e.db.QueryRow("SELECT id FROM artists WHERE name = 'Band'").Scan(&artist); err != nil { + t.Fatal(err) + } + put := func(kind string, id int64) resp { + return do(t, e.h, "PUT", "/api/v1/me/favorites/"+kind+"/"+itoa(id), e.token, nil) + } + + for kind, id := range map[string]int64{"tracks": song, "albums": e.withArt, "artists": artist} { + if r := put(kind, id); r.Code != http.StatusNoContent { + t.Fatalf("PUT %s: %d %v", kind, r.Code, r.Body) + } + if r := put(kind, id); r.Code != http.StatusNoContent { + t.Fatalf("PUT %s again: %d", kind, r.Code) + } + } + ids := do(t, e.h, "GET", "/api/v1/me/favorites/ids", e.token, nil) + sameKeys(t, "ids", ids.Body, "artists", "albums", "tracks") + if got := ids.Body["tracks"].([]any); len(got) != 1 || int64(got[0].(float64)) != song { + t.Fatalf("ids %v", ids.Body) + } + + tracks := do(t, e.h, "GET", "/api/v1/me/favorites/tracks", e.token, nil) + items := tracks.Body["items"].([]any) + if len(items) != 1 || items[0].(map[string]any)["title"] != "With Art song" || tracks.Body["total"] != float64(1) { + t.Fatalf("tracks %v", tracks.Body) + } + if a := do(t, e.h, "GET", "/api/v1/me/favorites/albums", e.token, nil).Body["items"].([]any); len(a) != 1 || a[0].(map[string]any)["title"] != "With Art" { + t.Fatalf("albums %v", a) + } + if a := do(t, e.h, "GET", "/api/v1/me/favorites/artists", e.token, nil).Body["items"].([]any); len(a) != 1 || a[0].(map[string]any)["name"] != "Band" { + t.Fatalf("artists %v", a) + } + + if r := do(t, e.h, "DELETE", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil); r.Code != http.StatusNoContent { + t.Fatalf("DELETE: %d", r.Code) + } + if r := do(t, e.h, "DELETE", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil); r.Code != http.StatusNoContent { + t.Fatalf("DELETE twice: %d", r.Code) + } + if got := do(t, e.h, "GET", "/api/v1/me/favorites/tracks", e.token, nil).Body["items"].([]any); len(got) != 0 { + t.Fatalf("after delete %v", got) + } +} + +func TestWhatIsNotThereCannotBeMarked(t *testing.T) { + e := newCoversEnv(t) + for path, code := range map[string]string{ + "/api/v1/me/favorites/tracks/9999": "track_not_found", + "/api/v1/me/favorites/albums/9999": "album_not_found", + "/api/v1/me/favorites/artists/9999": "artist_not_found", + "/api/v1/me/favorites/songs/1": "not_found", + "/api/v1/me/favorites/tracks/0": "invalid_input", + "/api/v1/me/favorites/tracks/x": "invalid_input", + } { + r := do(t, e.h, "PUT", path, e.token, nil) + if r.Code < 400 || errCode(r) != code { + t.Errorf("PUT %s: %d %v, want %s", path, r.Code, r.Body, code) + } + } + if r := do(t, e.h, "GET", "/api/v1/me/favorites/songs", e.token, nil); r.Code != http.StatusNotFound || errCode(r) != "not_found" { + t.Errorf("an unknown kind: %d %v", r.Code, r.Body) + } + if r := do(t, e.h, "GET", "/api/v1/me/favorites/tracks?limit=-1", e.token, nil); r.Code != http.StatusBadRequest { + t.Errorf("a negative limit: %d", r.Code) + } +} + +func TestFavouritesAreOfEachPersonAlone(t *testing.T) { + e := newCoversEnv(t) + var song int64 + _ = e.db.QueryRow("SELECT id FROM tracks WHERE title = 'With Art song'").Scan(&song) + do(t, e.h, "PUT", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil) + _, bob := addUser(t, e.h, e.token, "bob", false) + if got := do(t, e.h, "GET", "/api/v1/me/favorites/ids", bob, nil).Body["tracks"].([]any); len(got) != 0 { + t.Fatalf("bob sees %v", got) + } +} diff --git a/internal/api/http/federation_test.go b/internal/api/http/federation_test.go new file mode 100644 index 0000000..f02cb99 --- /dev/null +++ b/internal/api/http/federation_test.go @@ -0,0 +1,301 @@ +package httpapi + +import ( + "context" + "crypto/ed25519" + "crypto/rand" + "encoding/base64" + "fmt" + "net/http" + "net/http/httptest" + "regexp" + "strconv" + "strings" + "testing" + "time" + + "github.com/uptrace/bun" + + "git.kmalloc.org/squid/sonda/internal/federation" + "git.kmalloc.org/squid/sonda/internal/storage" +) + +// friendServer is a whole Sonda that other tests can reach over HTTP, with its admin signed in. +type friendServer struct { + h http.Handler + db *bun.DB + ts *httptest.Server + admin string +} + +func newFriendServer(t *testing.T, listen bool) *friendServer { + t.Helper() + h, db := newTestEnvDB(t, func(d *Deps, db *bun.DB) { + d.Federation = federation.NewService(storage.NewFederationRepo(db), federation.NewClient()) + }) + s := &friendServer{h: h, db: db, admin: registerAndLogin(t, h)} + if listen { + s.ts = httptest.NewServer(h) + t.Cleanup(s.ts.Close) + } + return s +} + +func (s *friendServer) call(t *testing.T, method, path string, body any) resp { + t.Helper() + return do(t, s.h, method, "/api/v1"+path, s.admin, body) +} + +func (s *friendServer) setUp(t *testing.T, name, address string) { + t.Helper() + r := s.call(t, "PUT", "/federation", map[string]any{"enabled": true, "name": name, "address": address}) + if r.Code != http.StatusOK { + t.Fatalf("set up federation: %d %v", r.Code, r.Body) + } +} + +func TestFederationRoutesAreForAdminsOnly(t *testing.T) { + h := newTestAPI(t) + admin := registerAndLogin(t, h) + _, bob := addUser(t, h, admin, "bob", false) + + for _, c := range []struct{ method, path string }{ + {"GET", "/api/v1/federation"}, + {"PUT", "/api/v1/federation"}, + {"POST", "/api/v1/federation/invites"}, + {"POST", "/api/v1/federation/friends"}, + {"POST", "/api/v1/federation/friends/1/approve"}, + {"DELETE", "/api/v1/federation/friends/1"}, + } { + if r := do(t, h, c.method, c.path, "", nil); r.Code != http.StatusUnauthorized { + t.Errorf("%s %s without a token: %d", c.method, c.path, r.Code) + } + r := do(t, h, c.method, c.path, bob, map[string]any{}) + if r.Code != http.StatusForbidden || errCode(r) != "admin_required" { + t.Errorf("%s %s as a normal user: %d %v", c.method, c.path, r.Code, r.Body) + } + } +} + +func TestFederationStartsOffAndSaysWhatItSends(t *testing.T) { + s := newFriendServer(t, false) + r := s.call(t, "GET", "/federation", nil) + if r.Code != http.StatusOK { + t.Fatalf("get: %d %v", r.Code, r.Body) + } + sameKeys(t, "federation", r.Body, "enabled", "name", "address", "key_id", "sends", "friends", "max_streams", "max_kib_per_sec") + if r.Body["enabled"] != false || r.Body["key_id"] != "" || r.Body["address"] != "" { + t.Errorf("federation must start off with no key and no address: %v", r.Body) + } + if friends, ok := r.Body["friends"].([]any); !ok || len(friends) != 0 { + t.Errorf("friends = %#v, want an empty list", r.Body["friends"]) + } + if !strings.Contains(r.Body["sends"].(string), "never learns who listens") { + t.Errorf("sends = %q", r.Body["sends"]) + } +} + +func TestSettingUpFederation(t *testing.T) { + s := newFriendServer(t, false) + + // Nothing can be made while it is off, and an invite needs the address of this server. + if r := s.call(t, "POST", "/federation/invites", nil); r.Code != http.StatusConflict || errCode(r) != "federation_off" { + t.Errorf("invite while off: %d %v", r.Code, r.Body) + } + for name, body := range map[string]any{ + "ftp address": map[string]any{"address": "ftp://x.example.org"}, + "public http": map[string]any{"address": "http://music.example.org"}, + "long name": map[string]any{"name": strings.Repeat("n", 61)}, + "bad json": "{nope", + "wrong type": map[string]any{"enabled": "yes"}, + } { + if r := s.call(t, "PUT", "/federation", body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Errorf("%s: %d %v", name, r.Code, r.Body) + } + } + + r := s.call(t, "PUT", "/federation", map[string]any{"enabled": true}) + if r.Code != http.StatusOK || r.Body["enabled"] != true || !regexp.MustCompile(`^[0-9a-f]{16}$`).MatchString(str(r.Body, "key_id")) { + t.Fatalf("turn on: %d %v", r.Code, r.Body) + } + if r := s.call(t, "POST", "/federation/invites", nil); r.Code != http.StatusConflict || errCode(r) != "address_required" { + t.Errorf("invite without an address: %d %v", r.Code, r.Body) + } + r = s.call(t, "PUT", "/federation", map[string]any{"address": "https://Music.Example.org/", "name": " Home "}) + if r.Body["address"] != "https://music.example.org" || r.Body["name"] != "Home" || r.Body["enabled"] != true { + t.Errorf("update: %v (fields left out must be kept)", r.Body) + } + + r = s.call(t, "POST", "/federation/invites", nil) + if r.Code != http.StatusCreated { + t.Fatalf("invite: %d %v", r.Code, r.Body) + } + sameKeys(t, "invite", r.Body, "invite", "expires_at") + if !strings.HasPrefix(str(r.Body, "invite"), "sonda1.") { + t.Errorf("invite = %v", r.Body["invite"]) + } + if _, err := time.Parse(time.RFC3339, str(r.Body, "expires_at")); err != nil { + t.Errorf("expires_at = %v", r.Body["expires_at"]) + } +} + +func TestTwoServersBecomeFriendsOverHTTP(t *testing.T) { + a := newFriendServer(t, true) + b := newFriendServer(t, false) + a.setUp(t, "Anna", a.ts.URL) + b.setUp(t, "Bruno", "https://bruno.example.org") + + inv := a.call(t, "POST", "/federation/invites", nil) + if inv.Code != http.StatusCreated { + t.Fatalf("invite: %d %v", inv.Code, inv.Body) + } + + // Bruno's admin pastes the invite. + r := b.call(t, "POST", "/federation/friends", map[string]any{"invite": inv.Body["invite"]}) + if r.Code != http.StatusCreated { + t.Fatalf("redeem: %d %v", r.Code, r.Body) + } + sameKeys(t, "friend", r.Body, "id", "name", "address", "key_id", "state", "created_at", "approved_at") + if r.Body["name"] != "Anna" || r.Body["address"] != a.ts.URL || r.Body["state"] != "outgoing" || r.Body["approved_at"] != nil { + t.Errorf("Bruno's friend = %v", r.Body) + } + fromB := r.Body + + // Anna sees a request, and nothing is agreed yet. + st := a.call(t, "GET", "/federation", nil) + list := st.Body["friends"].([]any) + if len(list) != 1 { + t.Fatalf("Anna's friends = %v", list) + } + req := list[0].(map[string]any) + if req["name"] != "Bruno" || req["address"] != "https://bruno.example.org" || req["state"] != "incoming" { + t.Errorf("Anna's request = %v", req) + } + // Each side shows the key id of the other, so the two admins can compare them out of band. + if req["key_id"] != b.call(t, "GET", "/federation", nil).Body["key_id"] || fromB["key_id"] != st.Body["key_id"] { + t.Errorf("key ids do not match: %v / %v", req["key_id"], fromB["key_id"]) + } + + id := strconv.Itoa(int(req["id"].(float64))) + ok := a.call(t, "POST", "/federation/friends/"+id+"/approve", nil) + if ok.Code != http.StatusOK || ok.Body["state"] != "active" || ok.Body["approved_at"] == nil { + t.Fatalf("approve: %d %v", ok.Code, ok.Body) + } + if again := a.call(t, "POST", "/federation/friends/"+id+"/approve", nil); again.Code != http.StatusConflict || errCode(again) != "not_waiting" { + t.Errorf("approve twice: %d %v", again.Code, again.Body) + } + + // The invite was good once. + c := newFriendServer(t, false) + c.setUp(t, "Carla", "https://carla.example.org") + if r := c.call(t, "POST", "/federation/friends", map[string]any{"invite": inv.Body["invite"]}); r.Code != http.StatusUnprocessableEntity || errCode(r) != "invite_refused" { + t.Errorf("second use: %d %v", r.Code, r.Body) + } + + // Revoking, on either side. + if r := b.call(t, "DELETE", "/federation/friends/"+strconv.Itoa(int(fromB["id"].(float64))), nil); r.Code != http.StatusNoContent { + t.Errorf("delete: %d %v", r.Code, r.Body) + } + if r := a.call(t, "DELETE", "/federation/friends/"+id, nil); r.Code != http.StatusNoContent { + t.Errorf("delete: %d %v", r.Code, r.Body) + } + if r := a.call(t, "DELETE", "/federation/friends/"+id, nil); r.Code != http.StatusNotFound || errCode(r) != "friend_not_found" { + t.Errorf("delete twice: %d %v", r.Code, r.Body) + } +} + +func TestThePrivateKeyIsNeverReturned(t *testing.T) { + s := newFriendServer(t, false) + s.setUp(t, "Anna", "https://anna.example.org") + var priv string + if err := s.db.NewRaw("SELECT private_key FROM federation_settings").Scan(context.Background(), &priv); err != nil || priv == "" { + t.Fatalf("private key = %q, %v", priv, err) + } + for _, path := range []string{"/federation"} { + if body := fmt.Sprint(s.call(t, "GET", path, nil).Body); strings.Contains(body, priv) { + t.Errorf("GET %s returned the private key", path) + } + } +} + +func TestRedeemChecksTheInvite(t *testing.T) { + s := newFriendServer(t, false) + if r := s.call(t, "POST", "/federation/friends", map[string]any{"invite": "sonda1.x"}); r.Code != http.StatusConflict || errCode(r) != "federation_off" { + t.Errorf("while off: %d %v", r.Code, r.Body) + } + s.setUp(t, "Bruno", "https://bruno.example.org") + for name, body := range map[string]any{ + "garbage": map[string]any{"invite": "hello"}, + "empty": map[string]any{"invite": ""}, + "no field": map[string]any{}, + "bad json": "[", + } { + if r := s.call(t, "POST", "/federation/friends", body); r.Code != http.StatusBadRequest || (errCode(r) != "invalid_invite" && errCode(r) != "invalid_input") { + t.Errorf("%s: %d %v", name, r.Code, r.Body) + } + } + // An invite whose server is gone. + a := newFriendServer(t, true) + a.setUp(t, "Anna", a.ts.URL) + inv := a.call(t, "POST", "/federation/invites", nil) + a.ts.Close() + if r := s.call(t, "POST", "/federation/friends", map[string]any{"invite": inv.Body["invite"]}); r.Code != http.StatusBadGateway || errCode(r) != "friend_unreachable" { + t.Errorf("server gone: %d %v", r.Code, r.Body) + } + if r := s.call(t, "GET", "/federation", nil); len(r.Body["friends"].([]any)) != 0 { + t.Errorf("a failed attempt left a friend: %v", r.Body["friends"]) + } +} + +func TestApprovingWhatIsNotWaiting(t *testing.T) { + s := newFriendServer(t, false) + if r := s.call(t, "POST", "/federation/friends/99/approve", nil); r.Code != http.StatusNotFound || errCode(r) != "friend_not_found" { + t.Errorf("approve missing: %d %v", r.Code, r.Body) + } + if r := s.call(t, "POST", "/federation/friends/zero/approve", nil); r.Code != http.StatusBadRequest { + t.Errorf("approve bad id: %d %v", r.Code, r.Body) + } +} + +func pairBody(code, name, address string) map[string]any { + pub, _, _ := ed25519.GenerateKey(rand.Reader) + return map[string]any{"code": code, "name": name, "address": address, "public_key": base64.RawURLEncoding.EncodeToString(pub)} +} + +func TestThePairingRouteIsPublicAndGivesNothingAway(t *testing.T) { + s := newFriendServer(t, false) + anon := func(body any) resp { return do(t, s.h, "POST", "/api/v1/peer/pair", "", body) } + + // While federation is off the route is as good as missing. + if r := anon(pairBody("c", "B", "https://b.example.org")); r.Code != http.StatusNotFound || errCode(r) != "not_found" { + t.Errorf("while off: %d %v", r.Code, r.Body) + } + s.setUp(t, "Anna", "https://anna.example.org") + + if r := anon(pairBody("not-an-invite", "B", "https://b.example.org")); r.Code != http.StatusForbidden || errCode(r) != "invalid_invite" { + t.Errorf("unknown code: %d %v", r.Code, r.Body) + } + for name, body := range map[string]any{ + "no name": pairBody("c", "", "https://b.example.org"), + "bad address": pairBody("c", "B", "ftp://b"), + "bad key": map[string]any{"code": "c", "name": "B", "address": "https://b.example.org", "public_key": "AAAA"}, + "not base64": map[string]any{"code": "c", "name": "B", "address": "https://b.example.org", "public_key": "!!"}, + "bad json": "{", + } { + if r := anon(body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Errorf("%s: %d %v", name, r.Code, r.Body) + } + } +} + +func TestPairingIsSlowedDown(t *testing.T) { + s := newFriendServer(t, false) + s.setUp(t, "Anna", "https://anna.example.org") + var last resp + for i := 0; i < 11; i++ { + last = do(t, s.h, "POST", "/api/v1/peer/pair", "", pairBody("guess", "B", "https://b.example.org")) + } + if last.Code != http.StatusTooManyRequests || errCode(last) != "too_many_requests" { + t.Errorf("11th attempt: %d %v", last.Code, last.Body) + } +} diff --git a/internal/api/http/friend_waveform_test.go b/internal/api/http/friend_waveform_test.go new file mode 100644 index 0000000..60d9945 --- /dev/null +++ b/internal/api/http/friend_waveform_test.go @@ -0,0 +1,148 @@ +package httpapi + +import ( + "net/http" + "os" + "path/filepath" + "strconv" + "testing" + "time" + + "github.com/uptrace/bun" + + "git.kmalloc.org/squid/sonda/internal/federation" + "git.kmalloc.org/squid/sonda/internal/storage" +) + +// realAudio puts a real MP3 where the shared library of the fixture keeps its song, so that its shape can be worked out. +func (f *peerFixture) realAudio(t *testing.T) { + t.Helper() + data, err := os.ReadFile(filepath.Join("..", "..", "waveform", "testdata", "ramp.mp3")) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(f.root, "shared", "song.mp3"), data, 0o644); err != nil { + t.Fatal(err) + } +} + +// untilPeaks asks again, as the page does, until the answer is not "working", and gives back the last one. +func untilPeaks(t *testing.T, ask func() resp) resp { + t.Helper() + for range 40 { + r := ask() + if r.Code != http.StatusAccepted { + return r + } + time.Sleep(250 * time.Millisecond) + } + t.Fatal("the shape was never ready") + return resp{} +} + +func TestAFriendReadsThePeaksOfASharedTrackOnly(t *testing.T) { + f := newPeerFixture(t) + f.befriend(t) + f.share(t, f.shared, true) + shared := strconv.FormatInt(f.track["Shared Song"], 10) + private := strconv.FormatInt(f.track["Private Song"], 10) + + // The files of the fixture are not audio: the friend is told so, in words. + r := f.get(t, "/tracks/"+shared+"/peaks") + if r.Code != 202 && !(r.Code == 404 && errCode(r) == "waveform_unavailable") { + t.Fatalf("a file that is not audio: %d %v", r.Code, r.Body) + } + r = untilPeaks(t, func() resp { return f.get(t, "/tracks/"+shared+"/peaks") }) + if r.Code != 404 || errCode(r) != "waveform_unavailable" { + t.Errorf("a file that is not audio: %d %v", r.Code, r.Body) + } + + f.realAudio(t) + r = untilPeaks(t, func() resp { return f.get(t, "/tracks/"+shared+"/peaks") }) + if r.Code != 200 { + t.Fatalf("a shared track: %d %v", r.Code, r.Body) + } + sameKeys(t, "peaks", r.Body, "count", "peaks") + if r.Body["count"] != float64(120) || len(r.Body["peaks"].([]any)) != 120 { + t.Errorf("count = %v", r.Body["count"]) + } + + if r := f.get(t, "/tracks/"+private+"/peaks"); r.Code != 404 || errCode(r) != "track_not_found" { + t.Errorf("a track that is not shared: %d %v", r.Code, r.Body) + } + if rec := f.serve(f.signed("GET", "/tracks/"+shared+"/peaks", func(r *http.Request) { r.Header.Del(federation.HeaderSignature) })); rec.Code != 401 { + t.Errorf("without a signature: %d", rec.Code) + } +} + +func TestAListenerSeesThePeaksOfAFriendsTrackThroughThisServer(t *testing.T) { + f := newBrowseFixture(t) + track := f.sharedTrack(t) + path := "/friends/" + f.friendID + "/tracks/" + track + "/peaks" + + if rec := f.listen(path, func(r *http.Request) { r.Header.Del("Authorization") }); rec.Code != 401 { + t.Errorf("without a login: %d", rec.Code) + } + r := untilPeaks(t, func() resp { return f.get(t, path) }) + if r.Code != 404 || errCode(r) != "waveform_unavailable" { + t.Errorf("a file that is not audio: %d %v", r.Code, r.Body) + } + + f.p.realAudio(t) + r = untilPeaks(t, func() resp { return f.get(t, path) }) + if r.Code != 200 { + t.Fatalf("peaks: %d %v", r.Code, r.Body) + } + sameKeys(t, "peaks", r.Body, "count", "peaks") + if r.Body["count"] != float64(120) { + t.Errorf("count = %v", r.Body["count"]) + } + if r := f.get(t, "/friends/"+f.friendID+"/tracks/999999/peaks"); r.Code != 404 || errCode(r) != "track_not_found" { + t.Errorf("a track the friend does not have: %d %v", r.Code, r.Body) + } + if r := f.get(t, "/friends/999/tracks/"+track+"/peaks"); r.Code != 404 || errCode(r) != "friend_not_found" { + t.Errorf("a friend that is not there: %d %v", r.Code, r.Body) + } +} + +func TestWhatAFriendSendsAsPeaksIsCheckedBeforeItIsPassedOn(t *testing.T) { + for name, tc := range map[string]struct { + body string + ok bool + }{ + "good, with something added": {`{"count":3,"peaks":[0,128,255],"path":"/srv/music/x"}`, true}, + "the count is not the length": {`{"count":4,"peaks":[0,128,255]}`, false}, + "a level over 255": {`{"count":1,"peaks":[256]}`, false}, + "a level under 0": {`{"count":1,"peaks":[-1]}`, false}, + "no levels": {`{"count":0,"peaks":[]}`, false}, + "far too many": {`{"count":1,"peaks":[` + manyLevels(5000) + `]}`, false}, + "html": {`hello`, false}, + "empty": {``, false}, + } { + h, _ := newTestEnvDB(t, func(d *Deps, db *bun.DB) { + d.Federation = crafted{federationService: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()), body: tc.body} + }) + tok := registerAndLogin(t, h) + r := do(t, h, "GET", "/api/v1/friends/1/tracks/1/peaks", tok, nil) + if tc.ok { + if r.Code != 200 { + t.Errorf("%s: %d %v", name, r.Code, r.Body) + continue + } + sameKeys(t, name, r.Body, "count", "peaks") + } else if r.Code != http.StatusBadGateway || errCode(r) != "friend_unreachable" { + t.Errorf("%s: %d %v", name, r.Code, r.Body) + } + } +} + +func manyLevels(n int) string { + out := make([]byte, 0, n*2) + for i := range n { + if i > 0 { + out = append(out, ',') + } + out = append(out, '1') + } + return string(out) +} diff --git a/internal/api/http/friends_test.go b/internal/api/http/friends_test.go new file mode 100644 index 0000000..ac14ade --- /dev/null +++ b/internal/api/http/friends_test.go @@ -0,0 +1,442 @@ +package httpapi + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "strings" + "testing" + "time" + + "github.com/uptrace/bun" + + "git.kmalloc.org/squid/sonda/internal/federation" + "git.kmalloc.org/squid/sonda/internal/storage" +) + +// browseFixture is a server C whose listener browses the shared libraries of a friend P, a real server of its own. +type browseFixture struct { + p *peerFixture + ts *httptest.Server + c *friendServer + listener string + friendID string // the id of P among C's friends + pFriend string // the id of C among P's friends +} + +func newBrowseFixture(t *testing.T) *browseFixture { + t.Helper() + p := newPeerFixture(t) + p.share(t, p.shared, true) + ts := httptest.NewServer(p.h) + t.Cleanup(ts.Close) + if r := p.admin1(t, "PUT", "/federation", map[string]any{"enabled": true, "name": "Anna", "address": ts.URL}); r.Code != 200 { + t.Fatalf("turn P on: %d %v", r.Code, r.Body) + } + c := newFriendServer(t, false) + c.setUp(t, "Bruno", "https://bruno.example.org") + + inv := p.admin1(t, "POST", "/federation/invites", nil) + mine := c.call(t, "POST", "/federation/friends", map[string]any{"invite": inv.Body["invite"]}) + if mine.Code != 201 { + t.Fatalf("C pastes the invite: %d %v", mine.Code, mine.Body) + } + f := &browseFixture{p: p, ts: ts, c: c, friendID: strconv.Itoa(int(mine.Body["id"].(float64)))} + list := p.admin1(t, "GET", "/federation", nil).Body["friends"].([]any) + f.pFriend = strconv.Itoa(int(list[0].(map[string]any)["id"].(float64))) + p.admin1(t, "POST", "/federation/friends/"+f.pFriend+"/approve", nil) + if r := c.call(t, "POST", "/federation/friends/"+f.friendID+"/check", nil); r.Code != 200 { + t.Fatalf("C checks: %d %v", r.Code, r.Body) + } + _, f.listener = addUser(t, c.h, c.admin, "lia", false) + return f +} + +func (f *browseFixture) get(t *testing.T, path string) resp { + t.Helper() + return do(t, f.c.h, "GET", "/api/v1"+path, f.listener, nil) +} + +func TestAListenerSeesTheNamesOfTheFriendsAndNothingMore(t *testing.T) { + f := newBrowseFixture(t) + if r := do(t, f.c.h, "GET", "/api/v1/friends", "", nil); r.Code != 401 { + t.Errorf("without a login: %d", r.Code) + } + r := f.get(t, "/friends") + list := r.Body["friends"].([]any) + if r.Code != 200 || len(list) != 1 { + t.Fatalf("friends = %d %v", r.Code, r.Body) + } + sameKeys(t, "friend", list[0].(map[string]any), "id", "name") + if list[0].(map[string]any)["name"] != "Anna" || list[0].(map[string]any)["id"] != mustFloat(t, f.friendID) { + t.Errorf("friend = %v", list[0]) + } + // The address and the key are for the admin. + if body := jsonOf(t, r.Body); strings.Contains(body, "127.0.0.1") || strings.Contains(body, "key") { + t.Errorf("the list says more than a name: %s", body) + } + + // Federation off: nobody to browse. + f.c.call(t, "PUT", "/federation", map[string]any{"enabled": false}) + if r := f.get(t, "/friends"); r.Code != 200 || len(r.Body["friends"].([]any)) != 0 { + t.Errorf("while off: %d %v", r.Code, r.Body) + } +} + +func mustFloat(t *testing.T, s string) float64 { + t.Helper() + n, err := strconv.ParseFloat(s, 64) + if err != nil { + t.Fatal(err) + } + return n +} + +func TestAWaitingFriendIsNotInTheList(t *testing.T) { + f := newPeerFixture(t) + f.admin1(t, "PUT", "/federation", map[string]any{"enabled": true, "address": "https://anna.example.org"}) + _, lia := addUser(t, f.h, f.admin, "lia", false) + inv := f.admin1(t, "POST", "/federation/invites", nil) + do(t, f.h, "POST", "/api/v1/peer/pair", "", pairBody(inviteCode(t, str(inv.Body, "invite")), "Bruno", "https://bruno.example.org")) + if r := do(t, f.h, "GET", "/api/v1/friends", lia, nil); r.Code != 200 || len(r.Body["friends"].([]any)) != 0 { + t.Errorf("a friend nobody approved: %d %v", r.Code, r.Body) + } +} + +func TestAListenerBrowsesWhatAFriendSharesAndNothingElse(t *testing.T) { + f := newBrowseFixture(t) + base := "/friends/" + f.friendID + + for path, want := range map[string]string{"/tracks": "Shared Song", "/albums": "Shared Album", "/artists": "Shared Band"} { + r := f.get(t, base+path) + if r.Code != 200 { + t.Fatalf("%s: %d %v", path, r.Code, r.Body) + } + sameKeys(t, "page", r.Body, "items", "total", "limit", "offset") + if got := titlesIn(r); len(got) != 1 || got[0] != want || r.Body["total"] != float64(1) { + t.Errorf("%s = %v (total %v)", path, got, r.Body["total"]) + } + } + if got := titlesIn(f.get(t, base+"/tracks?q=private")); len(got) != 0 { + t.Errorf("a search found %v in a library that is not shared", got) + } + if got := titlesIn(f.get(t, base+"/tracks?q=shared&limit=1&offset=0")); len(got) != 1 { + t.Errorf("a search = %v", got) + } + + // One item of each kind, found through the lists. + track := f.get(t, base+"/tracks").Body["items"].([]any)[0].(map[string]any) + album := f.get(t, base+"/albums").Body["items"].([]any)[0].(map[string]any) + artist := f.get(t, base+"/artists").Body["items"].([]any)[0].(map[string]any) + for path, title := range map[string]string{ + "/tracks/" + idOf(track): "Shared Song", "/albums/" + idOf(album): "Shared Album", "/artists/" + idOf(artist): "Shared Band", + } { + r := f.get(t, base+path) + if r.Code != 200 || (r.Body["title"] != title && r.Body["name"] != title) { + t.Errorf("%s = %d %v", path, r.Code, r.Body) + } + } + sameKeys(t, "track", f.get(t, base+"/tracks/"+idOf(track)).Body, "id", "title", "duration_ms", "track_no", "disc_no", "album", "artists", "format", "replay_gain") + if got := titlesIn(f.get(t, base+"/tracks?album_id="+idOf(album))); len(got) != 1 { + t.Errorf("the tracks of an album = %v", got) + } + + // What the friend does not share is not there, by id either. + private := strconv.FormatInt(f.p.track["Private Song"], 10) + if r := f.get(t, base+"/tracks/"+private); r.Code != 404 || errCode(r) != "track_not_found" { + t.Errorf("a track that is not shared: %d %v", r.Code, r.Body) + } + for path, code := range map[string]string{"/artists/999999": "artist_not_found", "/albums/999999": "album_not_found"} { + if r := f.get(t, base+path); r.Code != 404 || errCode(r) != code { + t.Errorf("%s: %d %v", path, r.Code, r.Body) + } + } +} + +func TestOnlyTheCatalogueOfAFriendCanBeAsked(t *testing.T) { + f := newBrowseFixture(t) + base := "/friends/" + f.friendID + // The friend's other routes are not reachable through this server. + for _, p := range []string{"/info", "/pair", "/tracks/1/stream", "/albums/1/cover", "/../federation"} { + if r := f.get(t, base+p); r.Code != 404 { + t.Errorf("%s: %d, want 404", p, r.Code) + } + } + // What the routes do not take is dropped, so a listener cannot steer the request. + if r := f.get(t, base+"/tracks?"+url.Values{"library_id": {strconv.FormatInt(f.p.private, 10)}, "evil": {"1"}}.Encode()); r.Code != 200 || r.Body["total"] != float64(1) { + t.Errorf("an unknown parameter: %d %v", r.Code, r.Body) + } + if r := f.get(t, base+"/tracks?q="+strings.Repeat("a", 201)); r.Code != 400 || errCode(r) != "invalid_input" { + t.Errorf("a search that is too long: %d %v", r.Code, r.Body) + } + if r := f.get(t, base+"/tracks?limit=abc"); r.Code != 400 { + t.Errorf("a limit that is no number: %d %v", r.Code, r.Body) + } +} + +func TestAFriendThatIsNotThereIsNotFound(t *testing.T) { + f := newBrowseFixture(t) + for _, p := range []string{"/friends/99999/tracks", "/friends/99999/artists/1"} { + if r := f.get(t, p); r.Code != 404 || errCode(r) != "friend_not_found" { + t.Errorf("%s: %d %v", p, r.Code, r.Body) + } + } + if r := f.get(t, "/friends/abc/tracks"); r.Code != 400 { + t.Errorf("a friend that is no number: %d", r.Code) + } + if r := do(t, f.c.h, "GET", "/api/v1/friends/"+f.friendID+"/tracks", "", nil); r.Code != 401 { + t.Errorf("without a login: %d", r.Code) + } + // Federation off: the friend is not there for a listener. + f.c.call(t, "PUT", "/federation", map[string]any{"enabled": false}) + if r := f.get(t, "/friends/"+f.friendID+"/tracks"); r.Code != 404 || errCode(r) != "friend_not_found" { + t.Errorf("while off: %d %v", r.Code, r.Body) + } +} + +func TestWhenAFriendChangesItsMindTheListenerIsToldInWords(t *testing.T) { + f := newBrowseFixture(t) + base := "/friends/" + f.friendID + + // P stops sharing: the friend has nothing, and that is no error. + f.p.share(t, f.p.shared, false) + if r := f.get(t, base+"/tracks"); r.Code != 200 || r.Body["total"] != float64(0) { + t.Errorf("after unsharing: %d %v", r.Code, r.Body) + } + + // P removes this server from its friends. + f.p.admin1(t, "DELETE", "/federation/friends/"+f.pFriend, nil) + r := f.get(t, base+"/tracks") + if r.Code != 409 || errCode(r) != "friend_gone" { + t.Errorf("after being removed: %d %v", r.Code, r.Body) + } + + // P goes away. + f.ts.Close() + r = f.get(t, base+"/tracks") + if r.Code != 502 || errCode(r) != "friend_unreachable" { + t.Errorf("a friend that is down: %d %v", r.Code, r.Body) + } + if body := jsonOf(t, r.Body); strings.Contains(body, "127.0.0.1") || strings.Contains(body, "dial") { + t.Errorf("the answer names the friend's address: %s", body) + } +} + +// crafted is a Federation whose friend sends what it likes. +type crafted struct { + federationService + body string +} + +func (c crafted) Browse(context.Context, int64, string, int64, url.Values) ([]byte, error) { + return []byte(c.body), nil +} + +func (c crafted) Peaks(context.Context, int64, int64) ([]byte, error) { return []byte(c.body), nil } + +func TestWhatAFriendSendsIsCheckedBeforeItIsPassedOn(t *testing.T) { + h, _ := newTestEnvDB(t, func(d *Deps, db *bun.DB) { + d.Federation = crafted{ + federationService: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()), + body: `{"items":[{"id":1,"name":"","album_count":2,"track_count":3,"path":"/srv/music/secret","token":"x"}],"total":1,"limit":50,"offset":0,"debug":"yes"}`, + } + }) + tok := registerAndLogin(t, h) + r := do(t, h, "GET", "/api/v1/friends/1/artists", tok, nil) + if r.Code != 200 { + t.Fatalf("%d %v", r.Code, r.Body) + } + sameKeys(t, "page", r.Body, "items", "total", "limit", "offset") + item := r.Body["items"].([]any)[0].(map[string]any) + sameKeys(t, "artist", item, "id", "name", "album_count", "track_count") + if strings.Contains(jsonOf(t, r.Body), "secret") { + t.Errorf("a field the friend added was passed on: %v", r.Body) + } +} + +func TestAnAnswerThatIsNotACataloguePageIsNotPassedOn(t *testing.T) { + for name, body := range map[string]string{"html": "hello", "a list": `[1,2]`, "empty": ``} { + h, _ := newTestEnvDB(t, func(d *Deps, db *bun.DB) { + d.Federation = crafted{federationService: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()), body: body} + }) + tok := registerAndLogin(t, h) + r := do(t, h, "GET", "/api/v1/friends/1/tracks", tok, nil) + if r.Code != http.StatusBadGateway || errCode(r) != "friend_unreachable" { + t.Errorf("%s: %d %v", name, r.Code, r.Body) + } + } +} + +// listen asks this server (C) for something as its listener would: with a header, or with the token in the +// query, as an