# DiscVault App (Developer README) This README is focused on developing and operating DiscVault from source. For product overview and screenshots, use the repository root README. ## Project layout ```text app/ ├── backend/ DiscVault Next API (next_app.py, Flask + Gunicorn, PostgreSQL) ├── frontend/ Shared static assets (i18n/next, flags, icons, PWA, service worker) │ └── js/ Standalone SPA modules served via /api/next/app/js/.js ├── mcp-server/ MCP HTTP server ├── deploy/ │ ├── v26/ Supervisor config for the v26 (Next) single-container image │ ├── next/ Compose + docs for running the published image │ └── unraid/ Unraid CA template files ├── scripts/ Build helper scripts ├── Dockerfile.v26 Production image (DiscVault Next stack) └── docker-compose.next.yml Local multi-service development setup (PostgreSQL) ``` ## Local development ### Prerequisites - Docker + Docker Compose - Optional metadata API keys: - OMDb: https://www.omdbapi.com/apikey.aspx - TMDb: https://www.themoviedb.org/settings/api ### Configure environment ```bash cp .env.example .env ``` Required: ```env RP_ID=localhost RP_ORIGIN=http://localhost:6080 TZ=Europe/Amsterdam JWT_SECRET= ``` Optional: ```env OMDB_API_KEY=... TMDB_API_KEY=... MCP_API_KEY=... DISCVAULT_ADMIN_DEDUP_EXECUTE_ENABLED=false ``` `DISCVAULT_ADMIN_DEDUP_EXECUTE_ENABLED` is a fail-closed safety gate for the destructive admin duplicate merge endpoint. Leave it `false`; authorized admins can still generate and inspect duplicate reports while execution remains hidden in the UI and rejected by the API. ### Start stack ```bash docker compose -f docker-compose.next.yml up -d --build ``` ### Run backend tests The test package supplies an explicit test-only secret, so production configuration is not required: ```bash cd backend python -m unittest discover -s tests -t . -p "test_*.py" ``` Services: - Web UI / API: http://localhost:6180 - Backend health: http://localhost:6180/api/next/health ### Stop stack ```bash docker compose -f docker-compose.next.yml down ``` ## Runtime architecture - API and embedded web UI are served by `next_app.py` (Flask + Gunicorn) - Data is stored in PostgreSQL (`DATABASE_URL`) - A `next_worker.py` process handles background jobs - MCP server exposes a streamable HTTP endpoint and calls the backend API ## Production image (DiscVault Next stack) Build from `app/` using the v26 Dockerfile: ```bash docker build -f Dockerfile.v26 -t ghcr.io/helmerznl/discvault:dev --build-arg BUILD_VERSION=dev . ``` The image runs the API on `5000` and the MCP server on `6090` via Supervisor (`deploy/v26/supervisord.conf`) and expects a reachable PostgreSQL `DATABASE_URL`. For running the published image with a bundled PostgreSQL service, see `deploy/next/`. ## Release flow Before a production release, review and commit the version-specific notes at `docs/releases/vX.Y.Z.md`, matching the version in `app/VERSION`. The `Release (tag main)` workflow refuses to create or push a tag when that exact notes file is missing. 1. Promote the verified beta changes to `main` with a merge commit. 2. Confirm `app/VERSION` and `docs/releases/vX.Y.Z.md` contain the approved release version and notes. 3. Run **Release (tag main)** from the `main` branch in GitHub Actions. The workflow tags the current `main` commit, publishes the committed notes verbatim as the GitHub Release, and triggers `.github/workflows/docker-publish.yml` to publish the multi-arch production images to GHCR. ## GitHub Pages website Website source is under `website/` and is deployed by `.github/workflows/pages.yml`. For domain setup, configure DNS and custom domain in repository Pages settings. ## MCP endpoint usage Two ways in, and they are not the same path: - **Direct**, on the MCP server's own published port: `http://:6090/mcp`. - **Through the API**, which proxies `/mcp` to the MCP server: `http://:/mcp` (`6180` by default in `deploy/next/`). The proxy forwards to `DISCVAULT_MCP_URL`, default `http://127.0.0.1:6090`. That default is correct for the all-in-one image, where Supervisor runs the API and the MCP server in one container. **When they are separate containers, set it** — `deploy/next/` sets `http://next-mcp:6090` — because `127.0.0.1` is then the API's own loopback and the proxy answers: ```json {"status":"error","error":"MCP server is not reachable","target":"http://127.0.0.1:6090"} ``` The address is the *container's*, not the published one: `DISCVAULT_NEXT_MCP_PORT` republishes the host port and leaves `6090` inside. `GET /mcp-health` proxies to the MCP server's own health endpoint and is the quickest way to tell a broken proxy from a broken MCP server. ### MCP authentication Each user can generate a **personal API key** in their profile settings: **Settings → Profiel → API-sleutels → Sleutel aanmaken** Use it as a Bearer token in your MCP client config: ```json { "mcpServers": { "discvault": { "transport": "streamable-http", "url": "http://your-server:6180/mcp", "headers": { "Authorization": "Bearer your-personal-api-key" } } } } ``` The MCP server automatically scopes all responses to your account. You will only ever see your own watchlist, watch history, collection and groups. **Available MCP tools:** | Tool | Description | |------|-------------| | `search_collection` | Search discs by title, director or genre | | `list_all_movies` | List your entire collection | | `get_movie_details` | Full details for a movie by ID | | `get_collection_stats` | Count + format breakdown | | `add_movie` | Add a disc to your collection | | `delete_movie` | Remove a disc | | `lookup_barcode` | Look up a barcode without saving | | `get_watchlist` | Your personal watchlist | | `get_watch_history` | Recently watched titles | | `get_groups` | Groups you are a member of | ## API surface (summary) ```text GET /api/health GET /api/stats GET /api/movies GET /api/movies/:id POST /api/movies PUT /api/movies/:id DELETE /api/movies/:id GET /api/lookup/:barcode GET /api/search_title?q= GET /api/next/collection/movies?limit=&offset= GET /api/next/app/js/:name.js ``` ## Library paging and hydration The library used to be hard-capped: the dashboard snapshot returned at most 200 movies and the SPA rendered only the first 80 rows. Both caps are gone. How it works now: 1. **First paint** — `collection_dashboard_snapshot()` still embeds only the first page (`COLLECTION_MOVIE_PAGE_SIZE`, 200 movies) so the inline `initialState` stays small. The snapshot also reports `moviesTotal`, `moviesPageSize` and `moviesHasMore`, so counters show the real collection size immediately. 2. **Background hydration** — `app/frontend/js/library-paging.js` loads the remaining movies in chunks of 500 via `GET /api/next/collection/movies?limit=&offset=` (see `app/backend/next_library_data.py`) and appends them through the `window.DiscVaultLibrary` bridge. Progress and failures are surfaced with the `collection.loadingMoreRows` and `collection.hydrationFailed` strings. 3. **Incremental rendering** — the list, poster grid and location rail render a growing window (`LIBRARY_RENDER_STEP`, 120 rows). An `IntersectionObserver` watches a sentinel at the bottom of the list and grows the window as you scroll, so a 5000-movie library stays responsive. Standalone SPA modules under `app/frontend/js/` are served by `app/backend/next_static.py` through `GET /api/next/app/js/.js`. Only files listed in `NEXT_SCRIPT_ASSETS` are addressable; the prefix is public because the scripts contain no secrets. ## Library export (CSV / XLSX / PDF) The library toolbar has an **Export** button that turns the current list view into a file. The export is WYSIWYG: it honours the active search, filters and sort order, and writes **one row per film** (containers and box sets are expanded). Columns are defined once in `app/backend/next_export_columns.py`: `title`, `year`, `barcode`, `format`, `director`, `actors`, `studio`, `contentRating`, `tags` and `watchActivity` — the columns of the list view plus the barcode. The frontend fetches that catalogue to build its column picker, so the picker and the renderers can never drift apart. Endpoints (`app/backend/next_export.py`): | Endpoint | Purpose | | --- | --- | | `GET /api/next/collection/export/columns` | Column catalogue for the picker | | `POST /api/next/collection/export` | Renders the posted rows and returns the file as an attachment | **Why the rows are posted by the client.** The list view already computes exactly what it shows: de-duplicated director/actor credits, the preferred content rating, format badge labels, aggregated tags and the viewing-activity summary. Re-deriving all of that server-side would duplicate a dozen helpers and guarantee drift, so `app/frontend/js/library-export.js` sends the rendered cell values (plus the translated headers) and the backend only lays them out. The POST body is capped at `MAX_EXPORT_ROWS` (100 000 rows). Renderers: CSV (stdlib, `;` delimiter with a UTF-8 BOM so Excel opens it cleanly), XLSX (`openpyxl`, bold frozen header row and autofilter) and PDF (`reportlab`, **A4 landscape**, repeating header row and proportional column widths). Both libraries are pure Python, so the `python:3.12-slim` image needs no extra system packages. ## Data and backups Persistent files are stored in `/data` in the container. Manual backup example: ```bash cp /mnt/user/appdata/discvault/discvault.db ./discvault-backup.db ``` ## DiscVault Next preparation DiscVault Next planning starts with a read-only audit of the current SQLite database and data volume. The audit script does not import the Flask app and does not run migrations. Local example: ```bash python scripts/db_audit.py --db .local/discvault.db ``` Production-copy example: ```bash python scripts/db_audit.py \ --db /path/to/copied/discvault.db \ --data-dir /path/to/copied/data \ --output-dir /path/to/audit-output ``` The generated JSON contains schema objects, row counts, table classifications, foreign keys, indexes, media file inventory, integrity checks and SQLite-specific SQL findings for PostgreSQL migration planning. Create a canonical export package for PostgreSQL import planning: ```bash python scripts/sqlite_export.py --db .local/discvault.db ``` By default this export excludes security, device-specific and log/control data such as passkeys, invite codes, push subscriptions and logs. Include those only for an explicit admin/security migration rehearsal: ```bash python scripts/sqlite_export.py \ --db /path/to/copied/discvault.db \ --data-dir /path/to/copied/data \ --output-dir /path/to/export-output \ --include-security \ --include-device \ --include-logs ``` ### DiscVault Next PostgreSQL skeleton `docker-compose.next.yml` starts a PostgreSQL service and provides tool profiles for the first DiscVault Next migrations on a Docker host. It does not replace the current SQLite runtime yet. For a Docker host that should use the published `:dev` image instead of building from source, use `deploy/next/docker-compose.yml`. Local Docker is not required for every developer step. The GitHub Actions workflow `DiscVault Next PostgreSQL Smoke` validates the migration runner against a temporary PostgreSQL service and runs a minimal importer smoke test. Start PostgreSQL: ```bash docker compose -f docker-compose.next.yml up -d postgres ``` Apply pending DiscVault Next migrations: ```bash docker compose -f docker-compose.next.yml --profile tools run --rm migrate ``` Show migration status: ```bash docker compose -f docker-compose.next.yml --profile tools run --rm migration-status ``` Start the minimal PostgreSQL-backed Next API on a Docker host: ```bash docker compose -f docker-compose.next.yml up -d next-api ``` Initial endpoints: ```text GET http://localhost:6180/api/next/health GET http://localhost:6180/api/next/stats GET http://localhost:6180/api/next/settings GET http://localhost:6180/api/next/digital-sources GET http://localhost:6180/api/next/digital-items GET http://localhost:6180/api/next/plugins/registry GET http://localhost:6180/api/next/metadata/plugins GET http://localhost:6180/api/next/movies GET http://localhost:6180/api/next/containers GET http://localhost:6180/api/next/sync/state GET http://localhost:6180/api/next/sync/bootstrap GET http://localhost:6180/api/next/sync/delta?since=0 POST http://localhost:6180/api/next/sync/mutations GET http://localhost:6180/api/next/jobs POST http://localhost:6180/api/next/jobs GET http://localhost:6180/api/next/jobs/ ``` The first migration set creates the PostgreSQL foundation for users/passkeys, RBAC, movies, people, containers, media assets, metadata plugins, plugin runtime state, digital media source sync, events, offline sync, push notifications, entitlements and migration import state. ### Container Member Ordering DiscVault Next stores container member position on the membership row, not on the movie or container row itself. For `box_set` and `vault` containers, direct movie members are stored in `container_movies`: ```sql container_movies ( container_id uuid, movie_id uuid, sort_order integer not null default 0, created_at timestamptz ) ``` `container_id` + `movie_id` is the primary key. `sort_order` is the persisted member position for that movie inside that specific box-set or vault. The same movie can therefore have different positions in different containers. For `collection` containers, mixed members are stored in `collection_items`: ```sql collection_items ( collection_id uuid, item_type text, -- movie, vault, box_set or collection item_id uuid, sort_order integer not null default 0, created_at timestamptz ) ``` `collection_id` + `item_type` + `item_id` is the primary key. `sort_order` is the persisted position of that item inside the collection. When members are added through the bulk APIs, DiscVault appends them after the current maximum `sort_order` for that container. Reordering writes dense 1-based positions back to the relevant membership table: ```text POST /api/next/bulk/containers//movies PATCH /api/next/containers//movies/order POST /api/next/bulk/collections//items PATCH /api/next/collections//items/order ``` The reorder endpoints require the request to include every currently linked member. This prevents partial order updates from accidentally dropping or duplicating positions. Read paths order by the stored position first and then use a stable title/year fallback for ties or legacy rows with `sort_order=0`: ```sql ORDER BY sort_order, lower(title), year NULLS LAST ``` Imports preserve source ordering where available. Legacy SQLite `box_set_movies.sort_order`, `vault_movies.sort_order` and `collection_items.sort_order` are copied into the Next membership tables. Metadata-driven imports and receiver payloads use `sortOrder`/`sort_order` from the provider payload, falling back to the member index when no explicit order is supplied. The initial sync API is shared by future PWA, iOS and Android clients. Clients use bootstrap to fill a local cache, delta to catch up by revision, and mutations to send idempotent offline changes back to the server. The worker foundation uses `background_jobs` and row locks. API requests create jobs and return quickly; `next-worker` claims pending jobs with `FOR UPDATE SKIP LOCKED`. Implemented job types include `sync.noop`, SQLite import, and generic plugin execution. Digital media source sync jobs persist Plex/Jellyfin items and match them to imported movies by TMDb, IMDb or title/year. Run a read-only dry-run against a copied legacy data directory: ```bash python backend/next_import.py \ --db /path/to/copied/data/discvault.db \ --data-dir /path/to/copied/data \ --dry-run ``` Import the copied SQLite data into the Next PostgreSQL schema on a Docker host: ```bash DISCVAULT_SQLITE_IMPORT_DATA=/path/to/copied/data \ docker compose -f docker-compose.next.yml --profile tools run --rm import-sqlite ``` If Docker is not available locally, run this on the beta host or another machine that has access to the copied data directory. The default import migrates functional collection data, people, credits, containers and media references. Users/passkeys, watch history and other personal/device data require explicit importer flags and should only be used for an intentional security migration rehearsal.