|
| 1 | +"""API endpoint for querying user activity timelines.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Annotated, Literal, Optional |
| 6 | +from uuid import UUID |
| 7 | + |
| 8 | +from fastapi import APIRouter, Depends, Query |
| 9 | + |
| 10 | +from app.api.v1.dependencies import PaginationParams |
| 11 | +from app.models.common import PaginatedResponse, Pagination |
| 12 | +from app.models.user_activity import UserActivityResponse |
| 13 | +from app.services import user_activity_service |
| 14 | + |
| 15 | +router = APIRouter(tags=["User Activity"]) |
| 16 | + |
| 17 | + |
| 18 | +@router.get( |
| 19 | + "/users/{user_id_path}/activity", |
| 20 | + response_model=PaginatedResponse[UserActivityResponse], |
| 21 | + summary="Get user activity timeline", |
| 22 | +) |
| 23 | +async def get_user_activity( |
| 24 | + user_id_path: UUID, |
| 25 | + pagination: Annotated[PaginationParams, Depends()], |
| 26 | + activity_type: Optional[Literal["view", "comment", "rate"]] = Query( |
| 27 | + None, description="Filter by activity type (view, comment, rate)" |
| 28 | + ), |
| 29 | +): |
| 30 | + """Return a paginated timeline of a user's activity over the last 30 days.""" |
| 31 | + |
| 32 | + activities, total = await user_activity_service.list_user_activity( |
| 33 | + userid=user_id_path, |
| 34 | + page=pagination.page, |
| 35 | + page_size=pagination.pageSize, |
| 36 | + activity_type=activity_type, |
| 37 | + ) |
| 38 | + |
| 39 | + total_pages = (total + pagination.pageSize - 1) // pagination.pageSize |
| 40 | + |
| 41 | + response_items = [UserActivityResponse.model_validate(a) for a in activities] |
| 42 | + |
| 43 | + return PaginatedResponse[UserActivityResponse]( |
| 44 | + data=response_items, |
| 45 | + pagination=Pagination( |
| 46 | + currentPage=pagination.page, |
| 47 | + pageSize=pagination.pageSize, |
| 48 | + totalItems=total, |
| 49 | + totalPages=total_pages, |
| 50 | + ), |
| 51 | + ) |
0 commit comments