goon/app/api/me.py
jtrzupek c8baa11604 feat(api): device-scope user state (favorites/progress/blacklists)
Public instance has no accounts, so all user state was GLOBAL in DB — new users
saw/overwrote each other's (and Jan's) favorites, watched badges and blacklists
(bug 2026-06-10). Add device_id (VARCHAR 64) to 9 state tables with composite PK
(device_id, entity_id); app sends X-Device-Id header (get_device_id dep). All
favorites/scene-favorites/blacklist/watch + scene&movie list/detail (is_favorite,
watched, blacklist-hide) now filter by device. Existing rows backfilled to
'legacy-shared'; POST /me/adopt-legacy reassigns them to the caller once. Old
clients (no header) map to legacy-shared so they keep working until OTA updates.

Migration 0022: add col, backfill, composite PK. Verified on prod: 967 progress
rows preserved, device isolation holds (new device sees none of legacy state).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 08:58:01 +02:00

68 lines
2.6 KiB
Python

"""Per-device self-service: przejęcie legacy stanu usera.
Po migracji device-scopingu (0022) stare wiersze mają `device_id='legacy-shared'`.
`POST /me/adopt-legacy` przepina WSZYSTKIE legacy wiersze (favorites/progress/blacklisty)
na device wołającego. Robi to JEDEN raz właściciel instancji po update apki — kolejne
wywołania nic nie znajdą (legacy już puste). Jeśli na keep-device istnieje już wiersz
dla tej samej encji, legacy duplikat jest pomijany (ON CONFLICT DO NOTHING → potem
kasujemy resztki legacy).
"""
from __future__ import annotations
from typing import Annotated
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy import text
from sqlalchemy.orm import Session
from app.api.device import LEGACY_DEVICE, get_device_id
from app.auth import require_api_key
from app.db import get_session
router = APIRouter(prefix="/me", tags=["me"], dependencies=[Depends(require_api_key)])
# (tabela, kolumna-encji) — wszystkie tabele device-scoped (migracja 0022).
_TABLES: list[tuple[str, str]] = [
("favorite_performers", "performer_id"),
("favorite_studios", "studio_id"),
("favorite_scenes", "scene_id"),
("favorite_movies", "movie_id"),
("scene_play_progress", "scene_id"),
("movie_play_progress", "movie_id"),
("blacklisted_performers", "performer_id"),
("blacklisted_studios", "studio_id"),
("blacklisted_tags", "tag_id"),
]
class AdoptLegacyOut(BaseModel):
device_id: str
moved: dict[str, int]
@router.post("/adopt-legacy", response_model=AdoptLegacyOut)
def adopt_legacy(
session: Annotated[Session, Depends(get_session)],
device_id: Annotated[str, Depends(get_device_id)],
) -> AdoptLegacyOut:
moved: dict[str, int] = {}
if device_id == LEGACY_DEVICE:
# Wołający bez X-Device-Id == legacy → nie ma czego przepinać.
return AdoptLegacyOut(device_id=device_id, moved={})
for table, entity in _TABLES:
# Przepnij legacy → device dla encji których device JESZCZE nie ma.
res = session.execute(
text(
f"UPDATE {table} SET device_id = :dev "
f"WHERE device_id = :legacy AND {entity} NOT IN "
f"(SELECT {entity} FROM {table} WHERE device_id = :dev)"
).bindparams(dev=device_id, legacy=LEGACY_DEVICE)
)
moved[table] = res.rowcount or 0
# Resztki legacy (encje które device już miał) — skasuj, żeby nie wisiały.
session.execute(
text(f"DELETE FROM {table} WHERE device_id = :legacy").bindparams(legacy=LEGACY_DEVICE)
)
session.commit()
return AdoptLegacyOut(device_id=device_id, moved=moved)