diff --git a/docs-developers/bridge/audible.md b/docs-developers/bridge/audible.md index 849cae87..ee1feb18 100644 --- a/docs-developers/bridge/audible.md +++ b/docs-developers/bridge/audible.md @@ -1,6 +1,6 @@ --- title: Audible -description: "Audible accounts on the Bridge (the paste-back PKCE sign-in, device registration and deregistration, the vault), the audible.poll job and its matching rules, preorders from the orders and the catalog (remembered, Bridge 0012), the audible.import backup (params, steps, result and codes, resume and temp files, the hardened transfer, enrichment, secrets), the server's accounts and titles (0063, 0064, 0065), its queueing (priorities, policy_since, preorders, the done fold) and the audible module, the console, and the build-tagged Audible fake; the current device profile (Bridge 0010), redacted account ids, contests settled by series number and planned destinations; position sync both ways (audible.sync, the push loop, furthest only, the two Bridge routes, server 0066 and 0085, and Bridge 0011)." +description: "Audible accounts on the Bridge (the paste-back PKCE sign-in, device registration and deregistration, the vault), the audible.poll job and its matching rules, preorders from the orders and the catalog (remembered, Bridge 0012), the audible.import backup (params, steps, result and codes, resume and temp files, the hardened transfer, enrichment, secrets), the server's accounts and titles (0063, 0064, 0065, and 0093's backup refusals), its queueing (priorities, policy_since, preorders, the done fold) and the audible module, the console, and the build-tagged Audible fake; the current device profile (Bridge 0010), redacted account ids, owed account removals (Bridge 0022), contests settled by series number and planned destinations; position sync both ways (audible.sync, the push loop, furthest only, the two Bridge routes, server 0066 and 0085, and Bridge 0011)." --- The Bridge does three things with Audible accounts: @@ -30,7 +30,7 @@ probe and OpenAudible sources were not imported. | Where | What | |---|---| | The Bridge's vault | each account's sign-in, sealed as `audible_` (`secrets.AudibleName`): the access, refresh and adp tokens, the device's RSA key, its serial, the customer id and marketplace (`audible.Credentials.Marshal`). **Full Amazon account access**, so never anywhere else | -| The Bridge's `bridge.db` | `audible_accounts` (migration `0009`): `id`, `label`, `marketplace`, `customer_hash` (sha256 of the customer id, never the id), `server_id` (the server the mapping was last recorded on), `user_id`/`user_name`, `library_id`, `policy`, `sync` (sent to the server, which ignores it), `created_at`, `last_ok_at`, `last_poll_at`, `last_titles`, `last_matched`, `refused_at`, `last_error`, `device_profile` (migration `0010`, [below](#the-device-profile)), and the push and sync health columns and `refusal_kind` with the `audible_push` table (migration `0011`, [below](#bridge-migration-0011_audible_push)). The remembered pending preorders: `audible_preorders` (migration `0012`, [below](#preorders)). Unique on `(customer_hash, marketplace)` where the hash isn't empty. No credential | +| The Bridge's `bridge.db` | `audible_accounts` (migration `0009`): `id`, `label`, `marketplace`, `customer_hash` (sha256 of the customer id, never the id), `server_id` (the server the mapping was last recorded on), `user_id`/`user_name`, `library_id`, `policy`, `sync` (sent to the server, which ignores it), `created_at`, `last_ok_at`, `last_poll_at`, `last_titles`, `last_matched`, `refused_at`, `last_error`, `device_profile` (migration `0010`, [below](#the-device-profile)), and the push and sync health columns and `refusal_kind` with the `audible_push` table (migration `0011`, [below](#bridge-migration-0011_audible_push)). The remembered pending preorders: `audible_preorders` (migration `0012`, [below](#preorders)). The owed server removals: `audible_removals` (migration `0022`, under Remove below). Unique on `(customer_hash, marketplace)` where the hash isn't empty. No credential | | The server | the mapping and the titles (`0063`, `0064`, `0065`, `0066`, below), the poll and sync schedules, what to back up and when, and whether an account's places sync. It never talks to Audible and never holds a credential or a book's key | No credential reaches the store, job params or results, logs, the heartbeat or the @@ -66,10 +66,10 @@ app's own flow, and the admin pastes back the address where it ends: - seals the credentials as `audible_` and stores the row. If any step after the registration fails, the Bridge deregisters the new device - and puts back what it changed, best effort. A new account is DELETEd on the - server. An existing one's earlier sign-in goes back in the vault, and its - earlier mapping is PUT again. A failed sign-in leaves no device and the account - as it was. + and puts back what it changed, best effort. If it already asked the server + to map a new account, it records a pending removal and sends the DELETE. + For an existing account, it restores the earlier sign-in and sends the + earlier mapping again if that mapping was on the same server. The device is named `"'s AudioSilo"` in the Amazon account's device list (`audible.DeviceName`, decisions 14/16: "AudioSilo", not "Bridge"). The @@ -92,22 +92,44 @@ If the server omits it, the Bridge keeps its local value. The Accounts card labels this value **Last known to the Bridge**. A console edit reaches the card on the next mapping save. -**Remove** (page `DELETE /api/accounts/audible/{id}`, answering `{audible, -warning}`) runs to the end even if the page goes away. In order, it: +**Remove** (page `DELETE /api/accounts/audible/{id}`, answering `{audible, warning}`) +finishes even if the page closes. The Bridge takes these steps in order. + +1. Under `finishMu`, it forgets the sign-in in the vault and deletes the local + account row. For an account on the paired server, the same SQLite transaction + records the pending DELETE in `audible_removals` (migration `0022`). +2. It releases the lock and sends `DELETE /bridge/audible/accounts/{account}` + on that pairing. +3. It calls `audible.Deregister`, which refreshes an access token within two + minutes of expiry and asks Amazon to remove this device + (`POST /auth/deregister` with `deregister_all_existing_accounts: false`). + If the page warns about the device, the admin removes it in Amazon's device list. + +Each pending removal stores the old account ID, the pairing ID, the number of +failed attempts and the next retry time. After a successful heartbeat, the +Bridge drops rows from ended pairings and retries at most one due DELETE in the +background. Unpairing and pairing again gives a new pairing ID, even on the +same server. Pairing again with the same server after its token was refused, +with no unpair between, keeps the pairing ID and its pending removals. Adding +the same Audible account again gives it a fresh account ID, so a late DELETE +can only remove the old entry. + +| DELETE outcome | What the Bridge does | +|---|---| +| Any `2xx` or `404` | Clears the pending removal. The server answers `204` whether or not it knew the account ([the Bridge DELETE](../server/api/reference.md#delete-apiv1bridgeaudibleaccountsaccount)). | +| Network failure, `401`, `408`, `429` or `5xx` | Keeps the removal for retry. A refused token still belongs to the same pairing. | +| Other `4xx`, such as `403` | Logs the refusal and drops the pending removal. The server may still list the account. | -1. forgets locally **first**: the vault, then the row (it takes `finishMu` - meanwhile); -2. DELETEs on the server; -3. deregisters (`audible.Deregister`: `POST /auth/deregister` with - `deregister_all_existing_accounts: false`, this device only). It first - refreshes an access token within 2 minutes of expiry. +The retry delay starts at one minute and doubles up to one hour. A longer +`Retry-After` takes precedence, up to 24 hours. On the 168th failed attempt, +the Bridge logs a warning and drops the removal. That takes about a week of +hourly failures. Failed heartbeats do not count toward the limit. -A server or Amazon failure is a `warning` for the page, never the account kept. -When the server's DELETE failed, the server keeps an orphaned account until an -admin removes it -([`DELETE /admin/audible/accounts/{account}`](../server/api/reference.md#delete-apiv1adminaudibleaccountsaccount)). -The admin does that from the console with **Remove** (see -[The console](#the-console) for which card lists it). +After a temporary failure, the page says the Bridge will try again. If the +Bridge is unpaired, paired to a different known server, or receives a permanent +refusal, the page says the server may still list the account. An admin can +remove an old entry with **Remove...** in the console. +See [the admin account DELETE](../server/api/reference.md#delete-apiv1adminaudibleaccountsaccount). **Refused.** A `401`/`403` on a signed call (`audible.IsRefused`) marks the account refused (`refused_at`, `last_error`; the page's **Sign in again**). The exception @@ -1054,16 +1076,21 @@ request. Where the Bridge finds preorders is in [Preorders](#preorders). `FoldAudibleImport` runs from the jobs' `OnEnded` hook for a done `audible.import`: 1. The result must name the job's library (and the params' library) and a clean - book path, else `ErrImportNotIndexed`. + book path, else `ErrImportNotIndexed` (stored as `invalid_result`, as is an + unreadable result). 2. `importOwnBook` checks the book. It must be indexed in a books library, else - `ErrImportNotIndexed`. It must be the **title's own**, else `ErrImportNotOwn` - (logged). The title's own book carries the title's ASIN, or holds **only** the - files the backup placed (a single-file book is its file). A file the scan joined + `ErrImportNotIndexed` (`not_indexed`). It must be the **title's own**, else + `ErrImportNotOwn` (logged; `not_own_book`, or `rejected_match` for the book + marked Not this book). The title's own book carries the title's ASIN, or holds + **only** the files the backup placed (a single-file book is its file). A file the scan joined into a folder book already there never makes that book (nor another title's) this title's match. Nor does it ever match the book the admin marked Not this book for the title. -3. It sets the match (`match_how = 'import'`) and clears `rejected` and - `import_pending`, only while `import_job_id` is still this job. +3. If the checks pass, it sets the match (`match_how = 'import'`) and clears + `rejected`, `import_pending` and any previous refusal. If it refuses the + result, it records the reason and time on the title and keeps the existing + match. An older job cannot overwrite a newer backup's outcome. + See the [admin title fields](../server/api/reference.md#get-apiv1adminaudibletitles). A later poll keeps an `import` match while its book is indexed and the poll names no other match for the title (`importMatches`). The poll may have listed the library @@ -1565,7 +1592,8 @@ The server keeps two tables. The columns are in the `last_queued_at`, `template` and `policy_since`. - **`audible_titles`**: one row per account and ASIN, with its state and match, plus the console's and an import's columns: `rejected`, `ignored`, `dest_path`, - `import_job_id` and `import_pending`, which a titles PUT never overwrites. + `import_job_id`, `import_pending` and the refusal pair `fold_refusal_reason` and + `fold_refused_at` (`0093`), which a titles PUT never overwrites. `ignored` has no route that sets it yet. Back up everything, the policy pass and the preorder clock skip an ignored title. Migration `0066` adds `sync_queued_at`, @@ -1635,6 +1663,8 @@ The code is in `features/audible/`. and **Destination...** (`title-actions.tsx`); - the import's state and code (`importStatus`). The loaded titles are fetched again every 5 s while a backup is queued or running (`backupRunning`); +- a done backup's refusal (`fold_refusal_reason`, `FoldNote`) in place of + **Backed up**; - **Backed up here** for an `import` match; - **Don't sync** / **Sync again**, with a **Not synced** badge. diff --git a/docs-developers/bridge/development.md b/docs-developers/bridge/development.md index 80fe6b96..5b644d19 100644 --- a/docs-developers/bridge/development.md +++ b/docs-developers/bridge/development.md @@ -110,7 +110,8 @@ internal/ store/ SQLite, append-only migrations (0001 the engine, 0002 server_jobs, 0003 generate_books, server_jobs.server_id, books.contrib_mode, 0004 workers, 0005 books.goal, 0006 accounts, 0007-0008 podcasts, - 0009-0012 and 0014 Audible, 0013 idx_books_unfinished: a partial + 0009-0012, 0014, 0017-0018, 0020 and 0022 Audible, + 0013 idx_books_unfinished: a partial index whose WHERE is unfinishedBook's word for word, so the periodic passes scan only the books the engine may act on) -- Phase 2b -- diff --git a/docs-developers/server/api/reference.md b/docs-developers/server/api/reference.md index b2c975a4..c1d738b2 100644 --- a/docs-developers/server/api/reference.md +++ b/docs-developers/server/api/reference.md @@ -12489,10 +12489,32 @@ queued until the destination is corrected. A failed backup with `code: "exists"` | `dest_path` | the console's destination for the title's backup, `""` for the account's placement | | `import_job_id` | the title's latest backup job, `null` for none | | `import` | the title's backup: `{job_id, state, code}` with `state` the job's ([job states](#the-job-object)) and `code` a failed or parked job's code (`not_released`, `no_rights`, `audible_refused`, `not_writable`, `exists`, `disk_full`, `offline_url_expired`, `unknown_account`, `drm_free` (Audible offers the title only as a DRM-free MP3, which the Bridge doesn't back up yet), or `""`); `{job_id: null, state: "pending"}` for a Back up on a preorder waiting for its release; `null` for none | +| `fold_refusal_reason` | Optional string. The code below explains why the server could not use a completed backup as this title's match. | +| `fold_refused_at` | Optional string. The UTC time the server recorded the refusal, in RFC 3339 format with up to nine fractional digits. | | `needs_attention` | a preorder still not available 14 days after its `release_date`: the account is no longer polled hourly for it | | `planned` | where a backup would go, as the Bridge planned it at its last poll (the object the [titles PUT](#put-apiv1bridgeaudibleaccountsaccounttitles) takes); `null` when matched, not planned, or the account is no longer mapped to the plan's library (paused or mapped again: none until its next poll) | | `contested` | for a title left unmatched as `contested`, `{library_id, path, with: [{asin, title}]}`: the book it tied for and the account's other titles (not `gone`) contested over that book or matched to it, by title, at most 10; `null` otherwise, or while the account isn't mapped to that library | +Both refusal fields are omitted when the title has no recorded refusal, in the +list and in the [title PATCH](#patch-apiv1adminaudibletitlesaccountasin)'s +answer. The backup job itself can still be `done`. + +| `fold_refusal_reason` | Why the server refused the match | +|---|---| +| `invalid_result` | The server could not read the backup result, or it named the wrong library or lacked a usable book path. | +| `not_indexed` | The server found no indexed book at that path in the backup's books library. | +| `not_own_book` | The indexed book's ASIN did not match the title's, and the server could not verify that the book contained only files placed by this backup. | +| `rejected_match` | The book at that path was marked **Not this book** for this title. | + +The server clears both fields when it queues the title's next backup or +successfully matches a completed import. A poll that adds or changes the +accepted match also clears them, as does undoing **Not this book** on an +existing match. A poll that repeats the same match and a move of the matched +book both preserve the refusal. + +Removing the finished job alone preserves the refusal on the title. Deleting a +library clears refusals whose backup jobs belong to that library. + | Status | Meaning | |---|---| | `200` | `{ titles, next }` | @@ -12578,7 +12600,8 @@ out is left as it is: ``` - `rejected` `true` marks the title "Not this book" (and drops a pending preorder - request); `false` clears it. A done backup clears it too. + request). `false` clears the mark. A completed backup clears it when the + server accepts the book as the title's match. - `dest_path` is where in the account's library its backup goes, `""` for the account's placement: a file path, or a folder (a last segment without an audio extension) the title's file goes in. At most 1024 bytes, clean (no `..`, no @@ -12838,8 +12861,8 @@ clock too, so the Bridge counts its baseline's recent window back from ### `DELETE /api/v1/admin/audible/accounts/{account}` *Admin*, **not** behind the module's toggle. Removes an account the Bridge can no -longer remove (one from a Bridge since unpaired or replaced, or whose removal on the -Bridge couldn't reach the server), exactly as the Bridge's delete does: its titles +longer remove (one from a Bridge since unpaired or replaced, or whose removal the +Bridge [gave up on](../../bridge/audible.md#the-sign-in-internalaudibleacct)), exactly as the Bridge's delete does: its titles go, its open poll and sync are cancelled, and so are its open backups. It signs nothing out of Amazon: that is the Bridge's. `204`, known or not; audited as `bridge.audible.remove` when there was one, its target the account's name (its label, else "person diff --git a/docs-developers/server/data-model.md b/docs-developers/server/data-model.md index 4025051a..d4f6f4db 100644 --- a/docs-developers/server/data-model.md +++ b/docs-developers/server/data-model.md @@ -1691,8 +1691,8 @@ Pairing a new Bridge clears the status, and unpairing deletes the row. Migration `0063` adds the Bridge's Audible accounts ([Audible](../bridge/audible.md)). The credentials stay in the Bridge's vault. -These tables hold only what the Bridge reports. Later migrations added -columns, listed after the tables. +The server keeps the account mapping, the titles the Bridge reports and the +outcomes of backups and matches. **`audible_accounts`** *(0063; `template` and `policy_since` in 0064; `sync_queued_at`, `last_sync_at` and `refused_at` in 0066; `refusal_ended_at` @@ -1726,7 +1726,8 @@ Mapping the account to another person turns `sync` off and clears `sync_queued_at` and `last_sync_at`. **`audible_titles`** *(0063; `import_pending` in 0064; `planned`, -`contest_library_id` and `contest_path` in 0065; `no_sync` in 0066)*: +`contest_library_id` and `contest_path` in 0065; `no_sync` in 0066; +`fold_refusal_reason` and `fold_refused_at` in 0093)*: - `account` (FK `ON DELETE CASCADE`) + `asin`. - `title`, `authors` and `series` (JSON), `release_date`, `runtime_s`. @@ -1752,6 +1753,10 @@ Mapping the account to another person turns `sync` off and clears a stored match clears them. - `no_sync`: the console's "Don't sync this book". A poll keeps it, like `rejected` and `ignored`. Only the sync reads it; backups never do. +- `fold_refusal_reason TEXT NOT NULL DEFAULT ''` and `fold_refused_at TEXT` + store the code and UTC timestamp when the server refuses a completed backup + as the title's match. Existing rows start with an empty reason and a NULL + timestamp. See the [admin title fields and codes](api/reference.md#get-apiv1adminaudibletitles). The titles are indexed by match, by title, and by account then title (the console's orders). @@ -1779,6 +1784,9 @@ What each later migration did: trigger. A `preorder` title then takes no part in its account's sync, like a `gone` one, and a preorder becoming available offers its book's place to Audible again. It adds `refusal_ended_at`. +- `0093_audible_fold_refusal` adds the refusal columns and a trigger that + clears them on a newly accepted match. Every case that clears or keeps them + is under the [admin title fields](api/reference.md#get-apiv1adminaudibletitles). ### Podcasts @@ -2573,6 +2581,7 @@ The migration history so far: | 0090 | `chapter_basis_match` | Includes the recorded work and recording in the basis for a community chapter check. | | 0091 | `mp3_rev2_tag_read_failed` | Advances current revision 1 rows for books without an MP3 to revision 2. The next scan [re-indexes MP3 books once](scanner.md#the-first-scan-after-updating). Adds the nullable `books.tag_read_failed_at` timestamp. | | 0092 | `health_ignores` | Stores duplicate decisions per comparison and split-disc ignores per folder. Carries forward [existing Health ignores](#health-ignores). | +| 0093 | `audible_fold_refusal` | Records why and when the server refused to match a completed backup on the [Audible title](#audible-accounts-and-titles). | `server_state.progress_feed` identifies the Audible progress feed. A restore renews this ID before clients can page it. The separate data-folder mark detects a database restored behind the last diff --git a/docs-developers/server/web-ui.md b/docs-developers/server/web-ui.md index f21e3bad..521b0d3e 100644 --- a/docs-developers/server/web-ui.md +++ b/docs-developers/server/web-ui.md @@ -1697,7 +1697,7 @@ While the `audible` module is on, an **Audible accounts** card (`features/audible/audible-accounts-card.tsx`) sits under Libraries, in the paired view only (`Paired` in `bridge-topic.tsx`). With no Bridge paired, **Remove** on Server > Settings > Audible is the console's way to clear an -orphaned account. +account the Bridge can no longer remove. The card runs on [`GET /admin/audible/accounts`](api/reference.md#get-apiv1adminaudibleaccounts), @@ -1738,7 +1738,10 @@ one), state, and on this server. The legal line sits under the filters. - **Matched by title** for a `fuzzy` match, **Backed up here** for an `import` one, **Not this book** for a rejected title, and **Not synced** for a `no_sync` one; -- the backup's state and code (`importStatus`, `audible.code.`). +- the backup's state and code (`importStatus`, `audible.code.`); +- for a done backup the server refused as the title's match, the stored + reason in place of **Backed up** (`FoldNote`, `foldReasonKey`: + `audible.fold.`, `audible.fold.other` for a code it doesn't know). **Per-title actions** (`title-actions.tsx`): diff --git a/docs-users/admin/audible.md b/docs-users/admin/audible.md index 4bd8a883..03007879 100644 --- a/docs-users/admin/audible.md +++ b/docs-users/admin/audible.md @@ -122,8 +122,8 @@ it matched. **Back up everything** and **Back up by itself** skip it. Then: 3. Press **Back up**. The Bridge downloads the book, removes its copy protection and saves it in your -library. Once the server indexes it, the title shows **On this server** with -**Backed up here**. +library. Once the server matches the saved book to the title, the title shows +**On this server** with **Backed up here**. The Bridge backs up one book at a time. A download cut off part way picks up where it stopped. Once the copy protection is removed, the Bridge keeps @@ -135,6 +135,22 @@ This removes the temporary copy. A failed backup keeps its copy in the Bridge's data folder until you back the title up again. When it starts, the Bridge also clears away copies left over for seven days or more. +### When the server can't match a finished backup + +The Bridge can finish saving the files even if the server can't match them +to the Audible title. The message under the title explains why. + +- The server couldn't use the backup details to match this title to a book in its library. +- The server couldn't find the backed-up book in the library index. +- The server couldn't confirm that the book at the backup destination belongs to this title. +- The backup went into the book marked “Not this book” for this title, so the server didn't match it. + +The message clears when you back the title up again, when a backup or a check +matches the title to a book, or when you delete the library the backup went +into. If you marked the right book by mistake, **Undo Not this book** accepts +that match and clears the message. A check that finds the same match, or moving +the matched book, keeps it. + ### When a backup fails The reason under the title says what to do. After fixing the problem, press @@ -314,13 +330,14 @@ titles update once the check finishes. ## Removing an account -Remove an account on the Bridge's Accounts tab (see +Remove the account on the Bridge's **Accounts** tab (see [Bridge accounts](../getting-started/bridge-accounts.md#removing-an-account)). That -removes it everywhere, including from your Amazon device list. +removes it from the Bridge, this server and your Amazon device list, and says +what to do if one of them can't be reached. **Remove...** in the console is for an account the Bridge can no longer remove, such as one from a Bridge you replaced. It removes the account from **this server -only**, so also remove the device in your Amazon account. Any check, backup or +only**. If the device is still listed on Amazon, remove it there too. Any check, backup or sync that hasn't finished is cancelled. Books already on your server stay. If your Bridge still has the account, its checks fail until you map it again on the Bridge. diff --git a/docs-users/getting-started/bridge-accounts.md b/docs-users/getting-started/bridge-accounts.md index 82b17a92..e74d4ca3 100644 --- a/docs-users/getting-started/bridge-accounts.md +++ b/docs-users/getting-started/bridge-accounts.md @@ -144,9 +144,18 @@ off. You can turn it on in the console. Changing the person turns it off again. ### Removing an account -**Remove**, then **Remove the account**, removes it everywhere: from the Bridge, -from the server and from your Amazon device list. Books already in your library -stay. If the server or Amazon can't be reached, the card says what didn't happen. +On the Audible card, choose **Remove**, then **Remove the account**. The Bridge +forgets the sign-in immediately. Books already in your library stay. + +If the paired server is offline, the card says the Bridge will try again. +Leave the Bridge paired until it does. If you unpair first, or the server +still hasn't accepted the removal after about a week, the Bridge stops trying. +If the card says the server may still list the account, or the console still +lists it after that, remove it with **Remove...** in the console (see +[Removing an account](../admin/audible.md#removing-an-account)). + +The Bridge also asks Amazon to remove the AudioSilo device. If the card warns +about the device, remove it yourself in Amazon's device list. Backing up is for a personal backup of books you bought. Read the note at the top of [Back up your Audible books](../admin/audible.md) first.