Spaces:
Running on T4
Running on T4
Download backend/app/modules/threads/controller.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/threads/controller.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/app/modules/threads/controller.py
-
curl -L -o controller.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/threads/controller.py
3.41 kB
| """N10-N15: conversations between players and coaches.""" | |
| from typing import Annotated | |
| from uuid import UUID | |
| from fastapi import APIRouter, Depends, Header, Query, Response, status | |
| from app.common.academy_access import CurrentStaff | |
| from app.common.schemas import Page | |
| from app.core.dependencies import CurrentAccount, Db, Paging, Storage | |
| from app.integrations.push import PushClient, get_push_client | |
| from app.modules.idempotency.service import run_idempotent | |
| from app.modules.threads.schemas import EnsureThreadIn, MessageIn, ReadIn, SendResult, ThreadEnvelope, ThreadOut | |
| from app.modules.threads.service import ThreadService | |
| router = APIRouter(prefix="/v1", tags=["threads"]) | |
| def get_thread_service(db: Db, storage: Storage, | |
| push: Annotated[PushClient, Depends(get_push_client)]) -> ThreadService: | |
| return ThreadService(db, storage, push) | |
| Service = Annotated[ThreadService, Depends(get_thread_service)] | |
| async def list_threads(account: CurrentAccount, service: Service, paging: Paging, mode: Annotated[ | |
| str | None, Query(pattern="^[hp]$")] = None, player_id: UUID | None = None, | |
| unread: bool | None = None) -> Page[ThreadOut]: | |
| return await service.list_threads(account, paging, mode=mode, player_id=player_id, unread=unread) | |
| async def get_thread(thread_id: UUID, account: CurrentAccount, service: Service, | |
| message_cursor: str | None = None, | |
| message_limit: Annotated[int, Query(ge=1, le=100)] = 50) -> ThreadEnvelope: | |
| return ThreadEnvelope(thread=await service.get(account, thread_id, message_cursor, message_limit)) | |
| async def session_thread(session_id: UUID, account: CurrentAccount, service: Service) -> ThreadEnvelope: | |
| return ThreadEnvelope(thread=await service.for_session(account, session_id)) | |
| async def mark_read(thread_id: UUID, body: ReadIn, account: CurrentAccount, service: Service) -> Response: | |
| await service.mark_read(account, thread_id, body.last_message_id) | |
| return Response(status_code=status.HTTP_204_NO_CONTENT) | |
| async def send_message(thread_id: UUID, body: MessageIn, account: CurrentAccount, db: Db, service: Service, | |
| idempotency_key: Annotated[str | None, Header()] = None): | |
| # The key is scoped to the account, so the thread id is part of what must match on replay. | |
| return await run_idempotent(db, account.id, idempotency_key, body, lambda: service.send(account, thread_id, body), | |
| status_code=status.HTTP_201_CREATED, scope=thread_id) | |
| async def ensure_thread(player_id: UUID, body: EnsureThreadIn, staff: CurrentStaff, response: Response, | |
| service: Service) -> ThreadEnvelope: | |
| thread, created = await service.ensure_general(staff, player_id, body) | |
| if not created: | |
| response.status_code = status.HTTP_200_OK | |
| return ThreadEnvelope(thread=thread) | |