Bind dashboard ingestion to current stream
This commit is contained in:
82
app/main.py
82
app/main.py
@@ -29,7 +29,7 @@ app = FastAPI(
|
||||
class DashboardRequest(BaseModel):
|
||||
"""Request body for saving a stream dashboard."""
|
||||
|
||||
session_id: str
|
||||
session_id: str | None = None
|
||||
raw_markdown: str
|
||||
stream_title: str | None = None
|
||||
game: str | None = None
|
||||
@@ -311,17 +311,30 @@ def dashboard_status(dashboard: dict | None) -> dict:
|
||||
}
|
||||
|
||||
|
||||
@app.post("/admin/session/dashboard", dependencies=[Depends(require_admin)])
|
||||
async def save_session_dashboard(request: DashboardRequest) -> dict:
|
||||
"""Create or update the approved dashboard for a stream session."""
|
||||
def get_current_stream_session_id() -> str | None:
|
||||
"""Resolve the session currently bound to Twitch or the only active session."""
|
||||
if not orchestrator:
|
||||
return None
|
||||
if twitch_session_id and twitch_session_id in orchestrator.active_sessions:
|
||||
return twitch_session_id
|
||||
if len(orchestrator.active_sessions) == 1:
|
||||
return next(iter(orchestrator.active_sessions))
|
||||
return None
|
||||
|
||||
|
||||
async def save_dashboard_for_session(
|
||||
session_id: str,
|
||||
request: DashboardRequest,
|
||||
) -> dict:
|
||||
"""Persist a dashboard and refresh live orchestrator context."""
|
||||
async for db_session in get_db_session():
|
||||
repo = Repository(db_session)
|
||||
stream_session = await repo.get_session(request.session_id)
|
||||
stream_session = await repo.get_session(session_id)
|
||||
if not stream_session:
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
|
||||
dashboard = await repo.upsert_dashboard(
|
||||
session_id=request.session_id,
|
||||
session_id=session_id,
|
||||
raw_markdown=request.raw_markdown,
|
||||
stream_title=request.stream_title,
|
||||
game=request.game,
|
||||
@@ -332,17 +345,42 @@ async def save_session_dashboard(request: DashboardRequest) -> dict:
|
||||
content_angle=request.content_angle,
|
||||
)
|
||||
|
||||
if orchestrator and request.session_id in orchestrator.active_sessions:
|
||||
orchestrator.active_sessions[request.session_id]["dashboard"] = (
|
||||
serialize_dashboard(dashboard)
|
||||
)
|
||||
orchestrator.active_sessions[request.session_id]["theme"] = (
|
||||
dashboard_data = serialize_dashboard(dashboard)
|
||||
if orchestrator and session_id in orchestrator.active_sessions:
|
||||
orchestrator.active_sessions[session_id]["dashboard"] = dashboard_data
|
||||
orchestrator.active_sessions[session_id]["theme"] = (
|
||||
request.content_angle or request.stream_title
|
||||
)
|
||||
|
||||
return dashboard_data
|
||||
|
||||
|
||||
@app.post("/admin/session/dashboard", dependencies=[Depends(require_admin)])
|
||||
async def save_session_dashboard(request: DashboardRequest) -> dict:
|
||||
"""Create or update the approved dashboard for a stream session."""
|
||||
if not request.session_id:
|
||||
raise HTTPException(status_code=400, detail="session_id is required")
|
||||
|
||||
dashboard = await save_dashboard_for_session(request.session_id, request)
|
||||
return {
|
||||
"status": "dashboard_saved",
|
||||
"dashboard": serialize_dashboard(dashboard),
|
||||
"dashboard": dashboard,
|
||||
"timestamp": datetime.utcnow().isoformat(),
|
||||
}
|
||||
|
||||
|
||||
@app.post("/admin/stream/dashboard", dependencies=[Depends(require_admin)])
|
||||
async def save_current_stream_dashboard(request: DashboardRequest) -> dict:
|
||||
"""Create or update the approved dashboard for the current stream session."""
|
||||
session_id = get_current_stream_session_id()
|
||||
if not session_id:
|
||||
raise HTTPException(status_code=404, detail="No active stream session found")
|
||||
|
||||
dashboard = await save_dashboard_for_session(session_id, request)
|
||||
return {
|
||||
"status": "dashboard_saved",
|
||||
"session_id": session_id,
|
||||
"dashboard": dashboard,
|
||||
"timestamp": datetime.utcnow().isoformat(),
|
||||
}
|
||||
|
||||
@@ -362,6 +400,26 @@ async def get_session_dashboard(session_id: str) -> dict:
|
||||
}
|
||||
|
||||
|
||||
@app.get("/admin/stream/dashboard", dependencies=[Depends(require_admin)])
|
||||
async def get_current_stream_dashboard() -> dict:
|
||||
"""Get the approved dashboard for the current stream session."""
|
||||
session_id = get_current_stream_session_id()
|
||||
if not session_id:
|
||||
raise HTTPException(status_code=404, detail="No active stream session found")
|
||||
|
||||
async for db_session in get_db_session():
|
||||
repo = Repository(db_session)
|
||||
dashboard = await repo.get_dashboard(session_id)
|
||||
if not dashboard:
|
||||
raise HTTPException(status_code=404, detail="Dashboard not found")
|
||||
|
||||
return {
|
||||
"session_id": session_id,
|
||||
"dashboard": serialize_dashboard(dashboard),
|
||||
"timestamp": datetime.utcnow().isoformat(),
|
||||
}
|
||||
|
||||
|
||||
@app.get("/admin/ledger", dependencies=[Depends(require_admin)])
|
||||
async def get_ledger(session_id: str) -> dict:
|
||||
"""Get the markdown ledger for a session."""
|
||||
|
||||
Reference in New Issue
Block a user