diet-guard/app/lib/services/sync_body.dart
Krzysztof kuhy Rudnicki 617e1ec879
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
E2E-encrypt diet_guard sync and export keys
Every sync payload (food log, banks, budget, catering credential,
Body, export keyring) now lives on diet-guard-sync/devices-e2e, sealed
with the Argon2id key from the shared crdt-sync passphrase; record
ids, tombstones and clocks stay clear. Old plaintext nodes are copied
once and never written or deleted. Locked = no pull, no push.

- PC: _sync_e2e.open_e2e over crdt-sync 0.11.0, key and copy memo in
  ~/.local/share/diet_guard/e2e; undecryptable peers are skipped.
- Phone/desktop: sync_e2e.dart, crdt_sync_flutter 0.8.1, lock icon
  in Settings; pullExportKeys goes through the same open.
- wake-alarm weigh-ins read from wake-alarm-sync/devices-e2e (own
  header), on both sides.

Todo: fda71ed1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 10:31:44 +01:00

44 lines
1.6 KiB
Dart

/// The Body document's part of a sync tick.
///
/// Its own file because `sync_service.dart` is near the repo's 250-line cap.
/// First ingests the wake-alarm weigh-ins (never throws), then pulls peers'
/// `body.json`, merges last-writer-wins per field, applies the result locally
/// and pushes. Mirrors `diet_guard/_sync_body.py`.
library;
import 'package:crdt_sync/crdt_sync.dart';
import 'package:diet_guard_app/services/body_service.dart';
import 'package:diet_guard_app/services/sync_device_id.dart';
import 'package:diet_guard_app/services/sync_merge_body.dart';
import 'package:diet_guard_app/services/wake_alarm_weights.dart';
/// Runs the Body sync against [client], under [devicesDir].
///
/// [wakeAlarm] is wake-alarm's own encrypted store; null (locked, or not
/// opened) skips the weigh-in ingest and nothing else.
///
/// No-ops when [BodyService] is uninitialised (a widget test, or an isolate
/// that never loaded it), so it can neither publish nor clobber anything.
Future<void> syncBody(
RemoteStore client, {
required String devicesDir,
RemoteStore? wakeAlarm,
}) async {
if (!BodyService.isInitialized) return;
if (wakeAlarm != null) await ingestWakeAlarmWeights(wakeAlarm);
final merged = await syncLog(
client: client,
deviceId: currentSyncDeviceId,
legacyDeviceId: legacySyncDeviceId,
pathPrefix: devicesDir,
revsPath: syncRevsDir,
localLog: bodyToLog(BodyService.instance.document),
encode: encodeBodyForPush,
decode: parseRemoteBody,
filename: 'body.json',
commitMessage: 'diet_guard_app sync',
);
if (merged.isEmpty) return;
await BodyService.instance.applyMerged(logToBody(merged));
}