Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Sync: a photo added to a workout on one device survives the other device's later edit of it
Duarte Santos05f02b72c267
diff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.jsindex 33a13069d..0af4d7d27 100644--- a/frontend/src/lib/sync-merge.js+++ b/frontend/src/lib/sync-merge.js@@ -15,7 +15,10 @@* - workouts: union by id; of an id that both have, the version edited last by its own `_ts`* (stampWorkout — a workout changed after it was logged: its sets edited, moved to another* day, its length or note corrected), the newer copy's on a tie; sorted by day and start- * like every other writer+ * like every other writer. Its `media` list is the exception: the union by hash of both+ * copies' lists (the kept version's in its order, then the other's extras), so a photo added+ * on one device survives the other's edit of the same workout — or its own photo+ * (mergeWorkoutMedia)* - routines: union by id in the newer copy's order; of an id that both have, the version* edited last by its own `_ts` (stampRoutines), the newer copy's on a tie* - bodyweight: union by day, the later-edited (`t`) entry of a day that both have@@ -63,6 +66,36 @@ export function unionById(newer = [], older = [], key = x => x?.id) {}const workoutKey = w => (w?.id != null ? w.id : `${w?.d}|${w?.start}`)++/**+ * The photos and videos of a workout both copies have: `kept`'s list as it stands (its order,+ * its refs), then every ref of `other`'s list whose hash `kept` lacks. Whole-record merging lost a+ * photo added on one device whenever the other edited that workout's note, date or length, or+ * added a photo of its own, later — the other copy's version was kept, and with it its list.+ *+ * The price is the file's known limit, one field in: with no record of what was removed, a photo+ * taken off on one device inside the conflict window comes back from the other copy that still+ * lists it — a resurrected photo is one tap to remove again, a lost one is gone. A removal the+ * other device has already pulled sticks, since neither copy lists it any more. Refs are carried+ * as they are; readers normalise and cap (workoutMediaOf). Mutates and returns `kept`.+ */+export function mergeWorkoutMedia(kept, other) {+ if (!kept || typeof kept !== 'object') return kept+ const mine = list(kept.media)+ const theirs = list(other?.media)+ if (!theirs.length) return kept+ const hashOf = m => (m && typeof m === 'object' && typeof m.hash === 'string' ? m.hash : null)+ const seen = new Set(mine.map(hashOf).filter(Boolean))+ const extra = []+ for (const m of theirs) {+ const h = hashOf(m)+ if (!h || seen.has(h)) continue+ seen.add(h)+ extra.push(clone(m))+ }+ if (extra.length) kept.media = [...(Array.isArray(kept.media) ? kept.media : []), ...extra]+ return kept+}const byDayStart = (a, b) => (a.d === b.d ? (a.start || 0) - (b.start || 0) : a.d < b.d ? -1 : 1)/** One entry per day; where both have a day, the one edited later (`t`); sorted by day. */@@ -168,6 +201,18 @@ export function mergeStates(a, b, { prefer } = {}) {return kept})}+ // Of a workout both copies have, the kept version's media list gains the other's extras — with+ // `prefer` too: signing in must not drop a photo added while signed out any more than a+ // conflict may.+ {+ const nBy = new Map(list(n.workouts).map(w => [workoutKey(w), w]))+ const oBy = new Map(list(o.workouts).map(w => [workoutKey(w), w]))+ for (const w of out.workouts) {+ const key = workoutKey(w)+ const x = nBy.get(key), y = oBy.get(key)+ if (x && y) { mergeWorkoutMedia(w, x); mergeWorkoutMedia(w, y) }+ }+ }out.workouts.sort(byDayStart)for (const f of ['routines', 'customEx', 'equipProfiles', 'gymCards']) {if (list(n[f]).length || list(o[f]).length) out[f] = unionById(n[f], o[f]).map(clone)diff --git a/frontend/src/lib/workout-media.test.js b/frontend/src/lib/workout-media.test.jsindex 4b5e46257..fd419f55d 100644--- a/frontend/src/lib/workout-media.test.js+++ b/frontend/src/lib/workout-media.test.js@@ -99,14 +99,60 @@ describe('the sync keeps a media edit', () => {}})- it('a removal on one device beats the older copy that still shows it', () => {+ it('a photo added on one device survives the other device\'s later note edit of the same workout', () => {+ const base = state(workout({ _ts: 10 }))+ const phone = structuredClone(base)+ addWorkoutMedia(phone, { id: 'w1' }, photo(H(1)), 200)+ phone._ts = 200+ const desktop = structuredClone(base)+ desktop.workouts[0].note = 'Felt strong'+ desktop.workouts[0]._ts = 300 // stampWorkout: the note edit is the later edit of this workout+ desktop._ts = 300+ for (const merged of [mergeStates(desktop, phone), mergeStates(phone, desktop)]) {+ expect(merged.workouts[0].note).toBe('Felt strong')+ expect(merged.workouts[0].media.map(m => m.hash)).toEqual([H(1)])+ expect(merged.workouts[0]._ts).toBe(300)+ }+ })++ it('a photo on each device: both are kept, the later-edited copy\'s first', () => {+ const base = state(workout({ _ts: 10, media: [photo(H(1))] }))+ const phone = structuredClone(base)+ addWorkoutMedia(phone, { id: 'w1' }, photo(H(2)), 200)+ phone._ts = 200+ const desktop = structuredClone(base)+ addWorkoutMedia(desktop, { id: 'w1' }, clip(H(3), H(4)), 300)+ desktop._ts = 300+ for (const merged of [mergeStates(desktop, phone), mergeStates(phone, desktop)]) {+ expect(merged.workouts[0].media.map(m => m.hash)).toEqual([H(1), H(3), H(2)])+ expect(merged.workouts[0].media[1]).toEqual(clip(H(3), H(4)))+ }+ })++ it('signing in (prefer) keeps the preferred version but not at the cost of the other\'s photo', () => {+ const base = state(workout({ _ts: 10 }))+ const device = structuredClone(base)+ addWorkoutMedia(device, { id: 'w1' }, photo(H(1)), 200)+ const server = structuredClone(base)+ server.workouts[0].note = 'server'+ const merged = mergeStates(server, device, { prefer: 'a' })+ expect(merged.workouts[0].note).toBe('server')+ expect(merged.workouts[0].media.map(m => m.hash)).toEqual([H(1)])+ })++ it('a removal inside the conflict window comes back from the copy that still lists it (resurrected beats lost); one already pulled sticks', () => {const base = state(workout({ _ts: 10, media: [photo(H(1)), photo(H(2))] }))const a = structuredClone(base)removeWorkoutMedia(a, { id: 'w1' }, H(1), 200)a._ts = 200const b = structuredClone(base)b._ts = 400- expect(mergeStates(b, a).workouts[0].media.map(m => m.hash)).toEqual([H(2)])+ expect(mergeStates(b, a).workouts[0].media.map(m => m.hash)).toEqual([H(2), H(1)])+ // Once the other device has the removal, neither copy lists it and it stays gone.+ const pulled = structuredClone(a)+ pulled.bodyweight = [{ d: '2026-09-21', w: 80, t: 500 }]+ pulled._ts = 500+ expect(mergeStates(pulled, a).workouts[0].media.map(m => m.hash)).toEqual([H(2)])})it('a media edit does not count as an edit of the sets (the kept loads are left alone)', () => {