api: /api/data carries a server revision — GET hands it out, a PUT with a stale baseRev answers 409 with the current document instead of overwriting another device's write; clients without baseRev keep overwriting as before

This commit is contained in:
Duarte Santos committed 2026-09-12 13:15:48 +02:00
1 parent d58a98e768
commit 5276f9714a
3 files changed
+177 -9

No files matched your search

+46 -4
View File
@@ -474,11 +474,13 @@ paths:
summary: Pull the account's app state
description: >-
Returns the caller's entire app state as one JSON blob, or `{"state": null}`
if nothing has been synced yet. Sync conflict resolution happens client-side
(last-writer-wins by the client-set `_ts` timestamp).
if nothing has been synced yet, together with `rev`, the server's revision of
that document (0 when there is none). A client keeps the `rev` it last read or
wrote and sends it back as `baseRev` on the next push, which lets the server
refuse a write over a document the client never saw (see the 409 on PUT).
responses:
'200':
description: The stored state, or null.
description: The stored state, or null, and its revision.
content:
application/json:
schema:
@@ -488,6 +490,9 @@ paths:
oneOf:
- $ref: '#/components/schemas/State'
- type: 'null'
rev:
type: integer
description: Server revision of the stored document; 0 when there is none.
'401': { $ref: '#/components/responses/Unauthorized' }
put:
tags: [data]
@@ -497,6 +502,14 @@ paths:
Replaces the stored state wholesale (atomic write; there is no merge on the
server). The `active` field — an in-progress workout — is stripped before
saving: a running session belongs to the device running it. Body limit 5 MiB.
With `baseRev` the write is conditional: it goes through only when `baseRev`
equals the document's current revision, and answers 409 with the current
document otherwise — the client merges the two and pushes again with the
revision it was given. Without `baseRev` the write is unconditional (clients
from before revisions, and deliberate replaces such as a backup import). The
server stamps the new revision into the document as `_rev`; a `_rev` sent by
the client is ignored.
requestBody:
required: true
content:
@@ -506,6 +519,11 @@ paths:
required: [state]
properties:
state: { $ref: '#/components/schemas/State' }
baseRev:
oneOf: [{ type: integer }, { type: 'null' }]
description: >-
The revision this client last read or wrote. Omit or null for an
unconditional overwrite.
responses:
'200':
description: Saved.
@@ -518,6 +536,24 @@ paths:
ts:
oneOf: [{ type: number }, { type: 'null' }]
description: Echo of the pushed state's `_ts`, if present.
rev:
type: integer
description: The revision the write produced.
'409':
description: >-
`baseRev` is not the current revision — another client wrote since this one
last read. Carries the current document so the client can merge and retry.
content:
application/json:
schema:
type: object
properties:
error: { type: string, const: conflict }
rev: { type: integer }
state:
oneOf:
- $ref: '#/components/schemas/State'
- type: 'null'
'400':
description: >-
`state` missing or not an object, or `workouts`/`routines` set to anything
@@ -1142,11 +1178,17 @@ components:
strips `active` on save and reads a few fields for reminders and the admin
dashboard) — the schema below is **representative, not enforced** (except
`workouts` and `routines`, which must be arrays (or null) when present), and grows
with the app. Conflict resolution is client-side via `_ts`.
with the app. Concurrent writes are caught by the server's revision (`_rev`,
see PUT /api/data); the client merges the two documents and decides by `_ts`
which side's settings win.
properties:
_ts:
type: number
description: Client-set last-write timestamp (ms since epoch); drives sync.
_rev:
type: integer
readOnly: true
description: Server-set revision, incremented on every accepted PUT. Ignored when sent by a client.
unit: { type: string, enum: [kg, lb] }
lang:
type: string