43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
"""In-app notifications / inbox (P4)."""
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from app.auth import CurrentUser
|
|
from app.db import (
|
|
get_user_id_by_username,
|
|
list_notifications,
|
|
mark_all_notifications_read,
|
|
mark_notification_read,
|
|
unread_notification_count,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
async def _uid(user: dict) -> int:
|
|
uid = await get_user_id_by_username(user["username"])
|
|
if uid is None:
|
|
raise HTTPException(404, "用户不存在")
|
|
return uid
|
|
|
|
|
|
@router.get("/notifications")
|
|
async def _list(user: CurrentUser, only_unread: bool = False):
|
|
return await list_notifications(await _uid(user), only_unread)
|
|
|
|
|
|
@router.get("/notifications/unread-count")
|
|
async def _count(user: CurrentUser):
|
|
return {"count": await unread_notification_count(await _uid(user))}
|
|
|
|
|
|
@router.post("/notifications/{notif_id}/read")
|
|
async def _read(notif_id: int, user: CurrentUser):
|
|
await mark_notification_read(notif_id, await _uid(user))
|
|
return {"ok": True}
|
|
|
|
|
|
@router.post("/notifications/read-all")
|
|
async def _read_all(user: CurrentUser):
|
|
n = await mark_all_notifications_read(await _uid(user))
|
|
return {"ok": True, "marked": n}
|