This commit is contained in:
team3
2026-09-22 13:14:06 +02:00
parent a62b587340
commit 2472e89b9c
15 changed files with 461 additions and 28 deletions

View File

@@ -9,4 +9,6 @@ DB_PATH = Path(os.getenv("DB_PATH", DATA_DIR / "desktop.db"))
# Jeder Client schickt diesen Token als "Authorization: Bearer <Token>"
SYNC_TOKEN = os.getenv("SYNC_TOKEN", "")
COLLECTIONS = ("events", "todos", "notes", "folders")
# files: Dateien der PCs (Claude-Sitzungen), kommen nur zurück, wenn der Client sie anfragt
COLLECTIONS = ("events", "todos", "notes", "folders", "files")
BASE_COLLECTIONS = COLLECTIONS[:-1]

View File

@@ -5,7 +5,7 @@ from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel
from . import db
from .config import COLLECTIONS, SYNC_TOKEN
from .config import BASE_COLLECTIONS, COLLECTIONS, SYNC_TOKEN
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("app")
@@ -29,10 +29,18 @@ def check_token(authorization: str):
class SyncIn(BaseModel):
since: int = 0
events: list[dict] = []
todos: list[dict] = []
notes: list[dict] = []
folders: list[dict] = []
events: list[dict] | None = None
todos: list[dict] | None = None
notes: list[dict] | None = None
folders: list[dict] | None = None
files: list[dict] | None = None
# Zurück kommen die Sammlungen der Anfrage. Ohne Sammlungen die vier Basis-Sammlungen,
# Dateien nur auf ausdrückliche Anfrage, sonst zieht das Handy Sitzungen mit.
@property
def requested(self):
named = [c for c in COLLECTIONS if getattr(self, c) is not None]
return named or list(BASE_COLLECTIONS)
@app.get("/api/health")
@@ -53,14 +61,14 @@ def sync(body: SyncIn, authorization: str = Header(default="")):
accepted = 0
rejected = {collection: [] for collection in COLLECTIONS}
for collection in COLLECTIONS:
for item in getattr(body, collection):
for item in getattr(body, collection) or []:
ok, stored = db.merge(conn, collection, item)
if ok:
accepted += 1
else:
rejected[collection].append(stored)
out = {}
for collection in COLLECTIONS:
for collection in body.requested:
changed = db.since(conn, collection, body.since)
ids = {item["id"] for item in changed}
out[collection] = changed + [item for item in rejected[collection] if item["id"] not in ids]