# The Isolink store

The store is where theme packs and cards live outside the app, so the app can stay small. Its packs are kept in a public git repo, [orimovo/isolink-store](https://github.com/orimovo/isolink-store), checked and signed there, and handed to apps by relays. Apps never contact GitHub: they ask their relay, which pulls the store and keeps a copy for its members, as it does with map tiles.

## How a pack gets to an app

1. **Someone opens a pull request** on isolink-store adding a folder (see the repo's README).
2. **The PR is checked** on GitHub's own runners, with no secrets, by `theme_store check`: the app's own validators, the store's size limits and the listing's fields. A signed pack file may be up to 32 MB (`StoreIndex.maxPackBytes`); apps and relays before 0.26.1 took 5 MB and refuse a whole index that lists a bigger one.
3. **A maintainer merges it.** Nothing reaches anyone before that.
4. **home-ci builds the store** (the **Publish theme store** workflow, on every merge to the store's main, with an hourly run as a fallback): `theme_store build` signs every theme pack and one index, and pushes the result to the repo's `store` branch. Official packs and the index are signed with the Isolink publisher key; community packs with the separate store reviewer key, so they never show as Isolink's own. Both keys stay on home-ci.
5. **Relays pull the `store` branch**, check the index's signature, and fetch a pack the first time a member asks for it.
6. **The app checks everything again**: the index's signature (and that its `seq` never goes back), then the file's size and SHA-256 against the index, then, for a theme pack, its own signature and the manifest validator. A card has no signature of its own; the signed index's hash vouches for it, and the sites it reaches must be exactly the ones the index lists.

## The repo

```
themes/official/<id>/     pack.json, listing.json, assets/*.png, icons/*.svg|png, overrides/*.css|tsx
themes/community/<id>/
cards/official/<id>/      card.json or card.isocard, listing.json
cards/community/<id>/
effects/official/<id>/    effects.json, listing.json, assets/*.png
effects/community/<id>/
```

Each folder may add `preview-light.png` and `preview-dark.png` (PNG, at most 640 px a side and 200 KB). A theme pack that brings none gets them drawn when the store is published: its look as a two-bubble chat, the way a store tile shows it once got (`app/test/store_thumbs_test.dart`; `PACKS_DIR=<dir of .isotheme files>` draws them beside the files, to look at first).

What to draw and how it's shown is in [theme-pack-spec.md 1.1](theme-pack-spec.md#11-pictures-for-the-store), and every picture a pack and its store page use, with all their sizes, is in [1.2](theme-pack-spec.md#12-every-picture-at-a-glance). Before opening a pull request, run the pack through the checker (`check_theme_pack`, or isolink.app/tools.html): it warns when the store would refuse a pack (over 32 MB packed) or leave one of its illustrations off the store page (over 512 KB).

A theme pack's store page leads with a **cover** and shows its **illustrations**. The cover is `cover-light.png` and `cover-dark.png` beside the pack when it brings them, else the picture it redraws for `onboarding.welcome`, or the slot its listing's `cover` names. The illustrations are up to six pictures the pack redraws: the slots its listing's `illustrations` names, else the first of `setup.first_contact`, `empty.chats`, `chat.new`, `empty.people`, `empty.nearby` and `community.offer` it has. Each is a PNG of at most 1440 px a side and 512 KB; a slot the listing names must be there and fit, and one picked for it that doesn't fit is left out. They're published as `themes/<id>/<version>-cover-<mode>.png` and `<version>-art<n>-<mode>.png`, listed in the entry's `cover` and `shots`. An app on a relay too old to hand them out shows the thumbnail instead.

- `pack.json` is a v2 manifest ([theme-pack-spec.md](theme-pack-spec.md)). Its `id` is the folder's name. Anything it would carry as base64 or text can be a file in a folder beside it instead, and publishing puts it in (so the pack's signature covers it):
  - an asset, a PNG: `"head": {"file": "assets/head.png"}`
  - a sound, a WAV: `{"name": "Drip", "file": "sounds/drip.wav"}`
  - an icon picture in `icons` or `codes` (and their `on`, `dark`, `onDark` and `sizes`), an SVG or PNG: `"chat": {"path": "icons/chat.svg", "keep": false}`
  - an icon font: `"glyphs": {"path": "fonts/glyphs.ttf", "map": {…}}`
  - component override code: `"overrides": {"style": {"path": "overrides/theme.css"}, "script": {"path": "overrides/theme.tsx"}}`

  A file's path is inside the folder, below a folder of its own (`icons/chat.svg`, not `chat.svg`).
- `card.json` is a card's plain JSON ([cards.md](cards.md)); `card.isocard` a card file exported from Card Studio.
- `listing.json`: `version` (whole number, goes up with every change), `by` (author), `licence` (an SPDX id such as `CC-BY-4.0` or `CC0-1.0`), and optionally `desc` (at most 200 characters), `tags` (at most 8, `[a-z0-9-]`), `minApp` (`0.25.0`), `needs` (the lowest version still allowed: older copies update even with updating by themselves off, and an app that asks for something new can't open until it's updated), and for a theme pack `cover` (an illustration slot) and `illustrations` (up to six slots).

Only `official/` folders may be by "Isolink"; CODEOWNERS keeps them, the tools and the workflows to maintainers.

## What is published

The `store` branch holds `index.json`, `themes/<id>/<version>.isotheme`, `cards/<id>/<version>.isocard`, the thumbnails, covers and illustrations. A file for a version never changes: building again leaves unchanged entries byte for byte the same, and a pack whose content changed without its version going up is refused. A folder that is refused or can't be built (a bad pack, or a community pack with no reviewer key) is skipped and listed as a warning at the end of the run: the rest are published, and the store keeps the version it already had of the skipped one.

`index.json`:

```json
{"v": 1, "seq": 42, "at": 1793000000000,
 "items": [
   {"id": "froggy-pond", "version": 3, "name": "Froggy Pond", "by": "Isolink",
    "kind": "pack", "parts": ["app", "mine", "theirs", "background"], "official": true,
    "size": 1843200, "sha256": "…", "file": "themes/froggy-pond/3.isotheme",
    "thumbs": {"light": "themes/froggy-pond/3-light.png"}, "at": 1793000000000, "src": "…"},
   {"type": "card", "id": "split-bill", "version": 1, "name": "Split the bill", "by": "Isolink",
    "kind": "scripted", "sites": [], "official": true, "size": 18322, "sha256": "…",
    "file": "cards/split-bill/1.isocard", "at": 1793000000000, "src": "…"}],
 "dev": {…}, "sig": "…"}
```

`StoreIndex` and `StoreEntry` in `packages/isolink_core/lib/src/engine/theme_store.dart` read and check it.

## Through the relay

A relay's `theme_stores` setting names the stores it pulls (see "The Isolink store" in [server/RELAY.md](../server/RELAY.md)). Members ask it with two requests:

- `{"t": "storeIndex"}` → `{"stores": [{"store": "<key>", "official": true, "index": {…}}]}`, with `"account": "<signer>"` in place of `official` for a store its owner added. `<key>` is the relay's name for the store.
  The answer carries an `"etag"`; asked again with `{"t": "storeIndex", "etag": "<it>"}`, a relay that has nothing new answers `{"same": true, "etag": "<it>"}`. The relay itself checks each store's `index.json`, `translations.json` and `extras.json` every 5 minutes with `If-None-Match`, so a publish shows within minutes; Refresh store in Manage relay checks straight away.
- `{"t": "storeFile", "store": "<key>", "path": "<a file the index lists>", "from": <byte>}` → `{"size": <whole file>, "from": <byte>, "data": "<base64, at most 256 KB>"}`.
  Past `store_files_per_minute` pieces in a minute it fails with `"why": "busy"` and `"wait": <seconds>`, until the oldest piece leaves the minute.

In the app, `StoreClient` (`packages/isolink_core/lib/src/engine/store_client.dart`) asks for these, checks each shelf with `StoreShelf.check` and keeps the highest `seq` seen per store. The Store asks again each time it opens (at most every 20 seconds), sending the `etag` it has. `StoreCatalog` (`app/lib/state/store_catalog.dart`) keeps what's been got and auto-updates: a pack becomes an ordinary library item and a card goes in My cards. Auto-update replaces only copies that haven't been changed since they came; a changed one waits for an Update tap, which keeps the changed copy.

### Other languages

A folder may hold `translations.json`: its name, `desc` and, for a card, its lines (each as written in the card, to the translation) in any of the app's other languages, `es`, `fr`, `de`, `pt`, `zh` and `ar`:

```json
{"de": {"name": "Zusage", "desc": "Ja, nein oder vielleicht, mit einer Notiz", "lines": {"Are you coming?": "Kommst du?"}}}
```

The build gathers every folder's into the store's own `translations.json`, signed by the index's account, and leaves it byte for byte as it was when nothing in it changed. Relays fetch it with the index and hand it out as the store file `translations.json`; the index itself doesn't change, so older apps and relays carry on as before, in English. The app shows an entry's name and line about it in the person's language when the store gives them, and a template's lines from the store's translation before its own. `StoreTranslations` (`packages/isolink_core/lib/src/engine/store_translations.dart`) reads and checks it.

### Extras: effect packs and story styles

Kinds of item that came after `index.json` was set go in a second signed list, `extras.json` (`StoreExtras`, `packages/isolink_core/lib/src/engine/store_extras.dart`), so apps and relays that don't know them carry on as before. Its shape is the index's, signed with the context `isolink-store-extras-v1`, and every item has a `type`: `effects` or `story`. An app skips a type it doesn't know without failing the list. Relays fetch it with the index and hand it out as the store file `extras.json`, with the files it lists.

An **effect pack** (`effects/<official|community>/<id>/effects.json`) is a face or photo effect library, published as `effects/<id>/<version>.isoeffects`:

```json
{"v": 1, "id": "party", "name": "Party", "by": "Isolink",
 "assets": {"hat": {"file": "assets/hat.png"}},
 "effects": [
   {"id": "hat", "name": "Party hat", "icon": "🥳", "layers": [
     {"type": "sticker", "asset": "hat", "anchor": "top", "size": 1.6, "y": -0.3, "motion": "bob"},
     {"type": "particles", "emoji": "🎉", "count": 14, "motion": "fall", "anchor": "screen", "size": 0.06}]}]}
```

Layers are `sticker` (an asset or `emoji`), `warp` (`bulge`, `pinch` or `swirl`), `filter` (a 4 x 5 colour `matrix`), `particles` and `frame`. Anchors are places on a face measured in eye distances from between the eyes (`face`, `eyes`, `leftEye`, `rightEye`, `nose`, `mouth`, `chin`, `top`), or `screen`. A pack with any face anchor is listed with kind `face`, otherwise `photo`. In the app, got packs join the camera's effect ring and the photo editor's effects (`EffectLibrary`); the camera tracks the face with MediaPipe's face models on the device.

### Card templates

New card's templates come from the store too, so they can change without an app release. An official card tagged `template` (with one of `plans`, `polls`, `lists`, `games`, `tools` for its group) is a template: `storeTemplates` gets them through the relay and keeps them on the device, downloading only the ones whose hash changed, and New card shows each in place of the built-in template with the same id (the folder name). The app still has every template built in, for when there is no relay or the store can't be reached yet; Browse leaves templates out.

Templates are kept in English. When one is picked the app puts it in the person's language: the title, a block card's lines and a scripted card's `const words = { … }` values and `card({ name })` all go through the app's translations, so a line the app doesn't know yet stays English until it does. A Countdown's `const target = 0;` becomes a week from now, and a Weather card's `const fixed = null;` the device's rough place. `STORE_DIR=<isolink-store> flutter test test/store_card_templates_test.dart` writes the built-in templates out as `cards/official/` folders, raising `version` for any that changed.

## The tools

```
cd packages/isolink_core
dart run isolink_core:theme_store check <repo>
dart run isolink_core:theme_store build <repo> <out> --key publisher.json --review-key reviewer.json [--previous index.json]
dart run isolink_core:theme_store verify <out>
```

## Setting it up

1. Run **Sign official theme packs** with `keygen-store` once, and pin the account id it prints in `IsolinkStore.reviewerIds`.
2. Add the `ISOLINK_STORE_TOKEN` secret to this repo: a fine-grained token with Contents read and write on orimovo/isolink-store only.
3. Run **Publish theme store**.

## Privacy

Apps fetch the index and packs through the relay connection they already have, so GitHub sees relays, never people. Nothing is fetched until someone opens Browse, or auto-update finds an outdated pack. Search and filters run on the device over the whole index.
