Platform v2 step 1.5 (APPS_PLATFORM D-7, D-17, §3, §6.1).
- user_permissions(user_id, key); revision 0005 copies existing v1 flags into
keys (frozen mapping) and drops the perm_* columns; can_cancel_orders gone
- core/access: check() = module effective + key held (superadmin implicit,
override keys wired for 1.9); holds()/can(); markers require/require_any/
public/require_device; role templates from manifests; queries.holders_filter
replaces User.perm_x == True; legacy.py projects/accepts the v1 booleans
for the current manager/waiter UIs only (§3.3, temporary)
- routers.deps: require_manager/... are now aliases of require("<key>");
history endpoints use history.view; superadmin-only = core.system.admin;
get_current_user carries the "signed_in" marker
- every inline perm_* read in routers/services converted to holds()
- staff API: permission catalogue by module with role templates, get/set a
user's keys, reset to role template; legacy flag toggles still work; a
non-superadmin can only grant keys they hold (access.cannot_grant)
- setup wizard creates the first account as owner (v1: store_manager, who
couldn't change settings); demo/seed manager is owner
- roles.py keeps only the role list and labels
- guards: every route has an access marker (19 open routes marked public()
on purpose; /waiters/roles now needs sign-in), no perm_* attribute reads
- tests: check matrix, templates, projection, staff permission API, grant
guard, 0005 data copy; pack rule CS-14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
"""
|
|
SSE stream endpoint — one long-lived GET per connected phone.
|
|
|
|
Authentication: token passed as query param ?token=<jwt>
|
|
(EventSource API in browsers cannot set custom headers, so query param is the standard pattern.)
|
|
|
|
The client receives a stream of JSON lines:
|
|
data: {"type": "...", "data": {...}}\n\n
|
|
|
|
A keepalive comment (": ping") is sent every 25 seconds to prevent proxy timeouts.
|
|
"""
|
|
|
|
from core.access.deps import public
|
|
import asyncio
|
|
from fastapi import Depends, APIRouter, Query
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
from routers.deps import decode_token
|
|
from services.sse_bus import subscribe, unsubscribe
|
|
|
|
router = APIRouter()
|
|
|
|
KEEPALIVE_INTERVAL = 25 # seconds
|
|
|
|
|
|
async def _event_stream(user_id: int):
|
|
q = await subscribe(user_id)
|
|
try:
|
|
while True:
|
|
try:
|
|
payload = await asyncio.wait_for(q.get(), timeout=KEEPALIVE_INTERVAL)
|
|
yield f"data: {payload}\n\n"
|
|
except asyncio.TimeoutError:
|
|
# keepalive — prevents nginx/proxies from closing idle connections
|
|
yield ": ping\n\n"
|
|
except asyncio.CancelledError:
|
|
pass
|
|
finally:
|
|
await unsubscribe(user_id, q)
|
|
|
|
|
|
@router.get("/stream", dependencies=[Depends(public())])
|
|
async def sse_stream(token: str = Query(...)):
|
|
"""
|
|
Open an SSE stream for the authenticated user.
|
|
The phone connects once on login and stays connected.
|
|
On reconnect (after network drop) it does a full GET first, then reconnects here.
|
|
"""
|
|
# decode_token raises HTTPException on invalid/expired — no manual check needed
|
|
payload = decode_token(token)
|
|
user_id: int = int(payload["sub"])
|
|
|
|
return StreamingResponse(
|
|
_event_stream(user_id),
|
|
media_type="text/event-stream",
|
|
headers={
|
|
"Cache-Control": "no-cache",
|
|
"X-Accel-Buffering": "no", # disable nginx buffering
|
|
"Connection": "keep-alive",
|
|
},
|
|
)
|