File size: 3,414 Bytes
fe40b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae2c68f
 
fe40b87
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""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)]


@router.get("/threads", response_model=Page[ThreadOut])
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)


@router.get("/threads/{thread_id}", response_model=ThreadEnvelope)
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))


@router.get("/sessions/{session_id}/thread", response_model=ThreadEnvelope)
async def session_thread(session_id: UUID, account: CurrentAccount, service: Service) -> ThreadEnvelope:
    return ThreadEnvelope(thread=await service.for_session(account, session_id))


@router.post("/threads/{thread_id}/read", status_code=status.HTTP_204_NO_CONTENT)
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)


@router.post("/threads/{thread_id}/messages", status_code=status.HTTP_201_CREATED, response_model=SendResult)
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)


@router.post("/coach/players/{player_id}/thread", status_code=status.HTTP_201_CREATED,
             response_model=ThreadEnvelope)
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)