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:
Davide Scaini
2026-04-24 15:07:52 +02:00
parent 44b2878b14
commit 02726034c7
3 changed files with 25 additions and 12 deletions
+15 -4
View File
@@ -537,13 +537,24 @@ async def get_token(login_req: LoginRequest, request: Request) -> JSONResponse:
@app.get("/api/feed")
async def get_feed(user: User = Depends(_require_auth)) -> JSONResponse:
"""Return the authenticated user's activity summaries (mobile feed sync)."""
"""Return the authenticated user's activity summaries (mobile feed sync).
_merged/index.json is a shard manifest (activities: []) when the user has
more than FEED_PAGE_SIZE activities. Collect from all shard files.
"""
dd = _get_data_dir()
user_dir = dd / user.handle
for index_path in (user_dir / "_merged" / "index.json", user_dir / "index.json"):
if index_path.exists():
index = json.loads(index_path.read_text())
return JSONResponse({"activities": index.get("activities", [])})
if not index_path.exists():
continue
index = json.loads(index_path.read_text())
activities: list[dict] = index.get("activities", [])
for shard in index.get("shards", []):
shard_path = index_path.parent / shard["url"]
if shard_path.exists():
shard_doc = json.loads(shard_path.read_text())
activities.extend(shard_doc.get("activities", []))
return JSONResponse({"activities": activities})
return JSONResponse({"activities": []})