fix: read activity shards in GET /api/feed; improve sync feedback
_merged/index.json is a shard manifest with activities:[] when the user
has >FEED_PAGE_SIZE activities. The endpoint now collects from all
index-{year}.json shard files before returning.
SyncResult gains a `total` field (activities received from server) so the
feed screen can distinguish "server returned nothing" from "all already
stored locally". Messages: "No activities on instance" / "Up to date (N)"
/ "X of N activities synced".
This commit is contained in:
+6
-6
@@ -1,14 +1,14 @@
|
||||
import type { SQLiteDatabase } from 'expo-sqlite';
|
||||
import { getSetting, upsertRemoteActivity } from './queries';
|
||||
|
||||
export type SyncResult = { synced: number; error?: string };
|
||||
export type SyncResult = { synced: number; total: number; error?: string };
|
||||
|
||||
export async function syncFeed(db: SQLiteDatabase): Promise<SyncResult> {
|
||||
const instanceUrl = (await getSetting(db, 'instance_url'))?.replace(/\/$/, '');
|
||||
const token = await getSetting(db, 'api_token');
|
||||
|
||||
if (!instanceUrl || !token) {
|
||||
return { synced: 0, error: 'No instance configured — add one in Settings.' };
|
||||
return { synced: 0, total: 0, error: 'No instance configured — add one in Settings.' };
|
||||
}
|
||||
|
||||
let resp: Response;
|
||||
@@ -17,14 +17,14 @@ export async function syncFeed(db: SQLiteDatabase): Promise<SyncResult> {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
} catch {
|
||||
return { synced: 0, error: 'Could not reach instance — check your connection.' };
|
||||
return { synced: 0, total: 0, error: 'Could not reach instance — check your connection.' };
|
||||
}
|
||||
|
||||
if (resp.status === 401) {
|
||||
return { synced: 0, error: 'Session expired — reconnect in Settings.' };
|
||||
return { synced: 0, total: 0, error: 'Session expired — reconnect in Settings.' };
|
||||
}
|
||||
if (!resp.ok) {
|
||||
return { synced: 0, error: `Server error (${resp.status})` };
|
||||
return { synced: 0, total: 0, error: `Server error (${resp.status})` };
|
||||
}
|
||||
|
||||
const data: { activities?: RemoteSummary[] } = await resp.json();
|
||||
@@ -48,7 +48,7 @@ export async function syncFeed(db: SQLiteDatabase): Promise<SyncResult> {
|
||||
if (changed) synced++;
|
||||
}
|
||||
|
||||
return { synced };
|
||||
return { synced, total: activities.length };
|
||||
}
|
||||
|
||||
type RemoteSummary = {
|
||||
|
||||
Reference in New Issue
Block a user