feat: add sync mode setting — summaries only vs full data download

This commit is contained in:
Davide Scaini
2026-04-24 21:52:25 +02:00
parent bb44b80e97
commit a306682b52
3 changed files with 89 additions and 8 deletions
+43 -2
View File
@@ -1,7 +1,7 @@
import type { SQLiteDatabase } from 'expo-sqlite';
import { getSetting, upsertRemoteActivity } from './queries';
export type SyncResult = { synced: number; total: number; error?: string };
export type SyncResult = { synced: number; total: number; fetched?: number; error?: string };
export async function syncFeed(db: SQLiteDatabase): Promise<SyncResult> {
const instanceUrl = (await getSetting(db, 'instance_url'))?.replace(/\/$/, '');
@@ -30,6 +30,8 @@ export async function syncFeed(db: SQLiteDatabase): Promise<SyncResult> {
const data: { activities?: RemoteSummary[] } = await resp.json();
const activities = data.activities ?? [];
const syncMode = (await getSetting(db, 'sync_mode')) ?? 'summaries';
let synced = 0;
for (const a of activities) {
const detailJson = JSON.stringify({
@@ -48,7 +50,46 @@ export async function syncFeed(db: SQLiteDatabase): Promise<SyncResult> {
if (changed) synced++;
}
return { synced, total: activities.length };
if (syncMode !== 'full') {
return { synced, total: activities.length };
}
// Full mode: fetch geojson + timeseries for any activity missing them
const headers = { Authorization: `Bearer ${token}` };
let fetched = 0;
for (const a of activities) {
const row = db.getFirstSync<{ g: number; t: number }>(
'SELECT (geojson IS NOT NULL) as g, (timeseries_json IS NOT NULL) as t FROM activities WHERE id = ?',
[a.id],
);
if (row?.g && row?.t) continue;
let gj: string | null = null;
let ts: string | null = null;
try {
if (!row?.g) {
const r = await fetch(`${instanceUrl}/api/activity/${a.id}/geojson`, { headers });
if (r.ok) gj = await r.text();
}
if (!row?.t) {
const r = await fetch(`${instanceUrl}/api/activity/${a.id}/timeseries`, { headers });
if (r.ok) ts = await r.text();
}
} catch {}
if (gj !== null || ts !== null) {
await db.runAsync(
`UPDATE activities SET
geojson = COALESCE(geojson, ?),
timeseries_json = COALESCE(timeseries_json, ?)
WHERE id = ? AND origin = 'remote'`,
[gj, ts, a.id],
);
fetched++;
}
}
return { synced, total: activities.length, fetched };
}
type RemoteSummary = {