Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes. Co-authored-by: Cursor <cursoragent@cursor.com>
211 lines
7.1 KiB
Python
211 lines
7.1 KiB
Python
"""User data sync: highlights, bookmarks, reading progress.
|
|
|
|
Replaces localStorage-only approach with PocketBase persistence.
|
|
Client sends a device_id (or user token in the future) to identify the user.
|
|
"""
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter, Header
|
|
from fastapi.responses import JSONResponse
|
|
from pydantic import BaseModel
|
|
|
|
from ..db import get_pb
|
|
|
|
router = APIRouter(prefix="/api/userdata", tags=["userdata"])
|
|
logger = logging.getLogger("userdata")
|
|
|
|
|
|
def _get_device_id(x_device_id: str | None = Header(None)) -> str:
|
|
return (x_device_id or "").strip()
|
|
|
|
|
|
# ── Highlights ──────────────────────────────────────────────
|
|
|
|
|
|
class HighlightIn(BaseModel):
|
|
id: str
|
|
slug: str
|
|
text: str
|
|
|
|
|
|
@router.get("/highlights")
|
|
async def get_highlights(x_device_id: str | None = Header(None)):
|
|
did = _get_device_id(x_device_id)
|
|
if not did:
|
|
return JSONResponse({"ok": False, "error": "device_id required"}, 400)
|
|
try:
|
|
pb = get_pb()
|
|
records = pb.collection("highlights").get_list(
|
|
1, 200, {"filter": f'device_id="{did}"', "sort": "-created"}
|
|
)
|
|
items = [{"id": r.id, "highlight_id": r.highlight_id, "slug": r.slug, "text": r.text} for r in records.items]
|
|
return {"ok": True, "items": items}
|
|
except Exception as e:
|
|
logger.error("get highlights: %s", e)
|
|
return JSONResponse({"ok": False}, 500)
|
|
|
|
|
|
@router.post("/highlights")
|
|
async def save_highlight(data: HighlightIn, x_device_id: str | None = Header(None)):
|
|
did = _get_device_id(x_device_id)
|
|
if not did:
|
|
return JSONResponse({"ok": False, "error": "device_id required"}, 400)
|
|
try:
|
|
pb = get_pb()
|
|
pb.collection("highlights").create({
|
|
"device_id": did,
|
|
"highlight_id": data.id,
|
|
"slug": data.slug,
|
|
"text": data.text,
|
|
})
|
|
return {"ok": True}
|
|
except Exception as e:
|
|
logger.error("save highlight: %s", e)
|
|
return JSONResponse({"ok": False}, 500)
|
|
|
|
|
|
@router.delete("/highlights/{highlight_id}")
|
|
async def delete_highlight(highlight_id: str, x_device_id: str | None = Header(None)):
|
|
did = _get_device_id(x_device_id)
|
|
if not did:
|
|
return JSONResponse({"ok": False, "error": "device_id required"}, 400)
|
|
try:
|
|
pb = get_pb()
|
|
records = pb.collection("highlights").get_list(
|
|
1, 1, {"filter": f'device_id="{did}" && highlight_id="{highlight_id}"'}
|
|
)
|
|
if records.items:
|
|
pb.collection("highlights").delete(records.items[0].id)
|
|
return {"ok": True}
|
|
except Exception as e:
|
|
logger.error("delete highlight: %s", e)
|
|
return JSONResponse({"ok": False}, 500)
|
|
|
|
|
|
# ── Bookmarks ───────────────────────────────────────────────
|
|
|
|
|
|
class BookmarkIn(BaseModel):
|
|
slug: str
|
|
scroll_y: float = 0
|
|
title: str = ""
|
|
|
|
|
|
@router.get("/bookmarks")
|
|
async def get_bookmarks(x_device_id: str | None = Header(None)):
|
|
did = _get_device_id(x_device_id)
|
|
if not did:
|
|
return JSONResponse({"ok": False, "error": "device_id required"}, 400)
|
|
try:
|
|
pb = get_pb()
|
|
records = pb.collection("bookmarks").get_list(
|
|
1, 50, {"filter": f'device_id="{did}"', "sort": "-created"}
|
|
)
|
|
items = [{"slug": r.slug, "scroll_y": r.scroll_y, "title": r.title} for r in records.items]
|
|
return {"ok": True, "items": items}
|
|
except Exception as e:
|
|
logger.error("get bookmarks: %s", e)
|
|
return JSONResponse({"ok": False}, 500)
|
|
|
|
|
|
@router.post("/bookmarks")
|
|
async def save_bookmark(data: BookmarkIn, x_device_id: str | None = Header(None)):
|
|
did = _get_device_id(x_device_id)
|
|
if not did:
|
|
return JSONResponse({"ok": False, "error": "device_id required"}, 400)
|
|
try:
|
|
pb = get_pb()
|
|
# Upsert: remove old bookmark for same slug
|
|
existing = pb.collection("bookmarks").get_list(
|
|
1, 1, {"filter": f'device_id="{did}" && slug="{data.slug}"'}
|
|
)
|
|
if existing.items:
|
|
pb.collection("bookmarks").delete(existing.items[0].id)
|
|
pb.collection("bookmarks").create({
|
|
"device_id": did,
|
|
"slug": data.slug,
|
|
"scroll_y": data.scroll_y,
|
|
"title": data.title,
|
|
})
|
|
return {"ok": True}
|
|
except Exception as e:
|
|
logger.error("save bookmark: %s", e)
|
|
return JSONResponse({"ok": False}, 500)
|
|
|
|
|
|
@router.delete("/bookmarks/{slug}")
|
|
async def delete_bookmark(slug: str, x_device_id: str | None = Header(None)):
|
|
did = _get_device_id(x_device_id)
|
|
if not did:
|
|
return JSONResponse({"ok": False, "error": "device_id required"}, 400)
|
|
try:
|
|
pb = get_pb()
|
|
records = pb.collection("bookmarks").get_list(
|
|
1, 1, {"filter": f'device_id="{did}" && slug="{slug}"'}
|
|
)
|
|
if records.items:
|
|
pb.collection("bookmarks").delete(records.items[0].id)
|
|
return {"ok": True}
|
|
except Exception as e:
|
|
logger.error("delete bookmark: %s", e)
|
|
return JSONResponse({"ok": False}, 500)
|
|
|
|
|
|
# ── Reading Progress ────────────────────────────────────────
|
|
|
|
|
|
class ProgressIn(BaseModel):
|
|
slug: str
|
|
scroll_y: float = 0
|
|
read_chapters: list[str] = []
|
|
|
|
|
|
@router.get("/progress")
|
|
async def get_progress(x_device_id: str | None = Header(None)):
|
|
did = _get_device_id(x_device_id)
|
|
if not did:
|
|
return JSONResponse({"ok": False, "error": "device_id required"}, 400)
|
|
try:
|
|
pb = get_pb()
|
|
records = pb.collection("reading_progress").get_list(
|
|
1, 1, {"filter": f'device_id="{did}"', "sort": "-updated"}
|
|
)
|
|
if not records.items:
|
|
return {"ok": True, "progress": None}
|
|
r = records.items[0]
|
|
return {"ok": True, "progress": {
|
|
"slug": r.slug,
|
|
"scroll_y": r.scroll_y,
|
|
"read_chapters": r.read_chapters,
|
|
}}
|
|
except Exception as e:
|
|
logger.error("get progress: %s", e)
|
|
return JSONResponse({"ok": False}, 500)
|
|
|
|
|
|
@router.post("/progress")
|
|
async def save_progress(data: ProgressIn, x_device_id: str | None = Header(None)):
|
|
did = _get_device_id(x_device_id)
|
|
if not did:
|
|
return JSONResponse({"ok": False, "error": "device_id required"}, 400)
|
|
try:
|
|
pb = get_pb()
|
|
existing = pb.collection("reading_progress").get_list(
|
|
1, 1, {"filter": f'device_id="{did}"'}
|
|
)
|
|
record_data = {
|
|
"device_id": did,
|
|
"slug": data.slug,
|
|
"scroll_y": data.scroll_y,
|
|
"read_chapters": data.read_chapters,
|
|
}
|
|
if existing.items:
|
|
pb.collection("reading_progress").update(existing.items[0].id, record_data)
|
|
else:
|
|
pb.collection("reading_progress").create(record_data)
|
|
return {"ok": True}
|
|
except Exception as e:
|
|
logger.error("save progress: %s", e)
|
|
return JSONResponse({"ok": False}, 500)
|