Signed in, the server's profile is the truth: sign-in and pairing adopt it and ask once about workouts logged while signed out (Add them / Keep profile as is); the app polls the revision every 30 s and on every resume (GET /api/data/rev) and fetches only what moved; offline and unsynced states show a banner, retry on reconnect and confirm with a toast; pagehide flushes a pending change

This commit is contained in:
Duarte Santos committed 2026-09-12 21:51:36 +02:00
1 parent 28a068b8f0
commit 826432a3a2
30 files changed
+541 -37

No files matched your search

+19
View File
@@ -566,6 +566,25 @@ paths:
invalid: { value: { error: "invalid state" } }
'401': { $ref: '#/components/responses/Unauthorized' }
/api/data/rev:
get:
tags: [Data]
summary: Current revision of the caller's profile state
description: |
The `rev` that `GET /api/data` would return, without the document. Signed-in clients poll
this while open and on every return to the foreground and fetch the document only when
the number changed.
responses:
'200':
description: The current revision (0 when the profile has no state yet).
content:
application/json:
schema:
type: object
properties:
rev: { type: integer }
'401': { $ref: '#/components/responses/Unauthorized' }
/api/push/public-key:
get:
tags: [push]
+8
View File
@@ -855,6 +855,14 @@ const routes = {
const state = readState(user.id);
json(res, 200, { state, rev: state?._rev || 0 });
},
// Just the revision: the client asks this every half minute while it is open and on every
// return to the foreground, and fetches the document only when the number moved — a signed-in
// device is meant to show what the server has, and this is what keeps that cheap.
'GET /api/data/rev': async (req, res) => {
const user = readSession(req);
if (!user) return json(res, 401, { error: 'not signed in' });
json(res, 200, { rev: readState(user.id)?._rev || 0 });
},
'PUT /api/data': async (req, res) => {
const user = readSession(req);
+36
View File
@@ -0,0 +1,36 @@
// GET /api/data/rev hands out the revision alone — what a signed-in client polls.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import crypto from 'node:crypto';
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
const API = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const SECRET = 'test-secret-rev-endpoint';
const uid = 'u_rev_1';
const cookie = () => { const p = `${uid}:${Date.now() + 86400000}:0`; return `gymsid=${p}.${crypto.createHmac('sha256', SECRET).update(p).digest('base64url')}`; };
const freePort = () => new Promise(r => { const s = require_net().createServer(); s.listen(0, () => { const p = s.address().port; s.close(() => r(p)); }); });
import net from 'node:net';
function require_net() { return net; }
test('GET /api/data/rev tracks PUT /api/data', async t => {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gym-rev-'));
fs.writeFileSync(path.join(dataDir, 'secret'), SECRET, { mode: 0o600 });
fs.writeFileSync(path.join(dataDir, 'db.json'), JSON.stringify({ users: [{ id: uid, name: 'R', created: new Date().toISOString() }], creds: [], subs: [], invites: [] }));
const port = await freePort();
const child = spawn(process.execPath, ['server.js'], { cwd: API, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, PORT: String(port), DATA_DIR: dataDir, ORIGIN: 'http://localhost:8080', RP_ID: 'localhost' } });
t.after(() => { child.kill('SIGKILL'); fs.rmSync(dataDir, { recursive: true, force: true }); });
const base = `http://127.0.0.1:${port}`;
let up = false;
for (let i = 0; i < 100 && !up; i++) { try { up = (await fetch(`${base}/api/health`)).ok; } catch {} if (!up) await new Promise(r => setTimeout(r, 100)); }
assert.ok(up);
const h = { cookie: cookie(), origin: 'http://localhost:8080', 'content-type': 'application/json' };
assert.equal((await fetch(`${base}/api/data/rev`)).status, 401);
assert.deepEqual(await (await fetch(`${base}/api/data/rev`, { headers: h })).json(), { rev: 0 });
const put = await fetch(`${base}/api/data`, { method: 'PUT', headers: h, body: JSON.stringify({ state: { workouts: [], routines: [] }, baseRev: 0 }) });
assert.equal(put.status, 200);
assert.deepEqual(await (await fetch(`${base}/api/data/rev`, { headers: h })).json(), { rev: 1 });
});