Add endpoint to mark notifications as read
This commit is contained in:
parent
acd280a243
commit
e0fb934a7e
|
@ -1,4 +1,4 @@
|
||||||
from datetime import datetime
|
from datetime import UTC, datetime
|
||||||
from typing import Any, Literal, cast, final
|
from typing import Any, Literal, cast, final
|
||||||
|
|
||||||
from beanie import Link, PydanticObjectId
|
from beanie import Link, PydanticObjectId
|
||||||
|
@ -89,6 +89,27 @@ async def get_notification(notification_id: PydanticObjectId, user: CurrentUserD
|
||||||
return NotificationData.from_notification(notification)
|
return NotificationData.from_notification(notification)
|
||||||
|
|
||||||
|
|
||||||
|
@notifications_router.get("{notification_id}/read")
|
||||||
|
async def read_notification(notification_id: PydanticObjectId, user: CurrentUserDep) -> NotificationData:
|
||||||
|
"""Mark a notification as read."""
|
||||||
|
notification = await Notification.get(notification_id, fetch_links=True)
|
||||||
|
|
||||||
|
if notification is None:
|
||||||
|
raise HTTPException(status.HTTP_404_NOT_FOUND, "Notification not found.")
|
||||||
|
|
||||||
|
if user.id is None:
|
||||||
|
raise MissingIdError(user)
|
||||||
|
|
||||||
|
if cast(User, notification.user).id != user.id:
|
||||||
|
raise HTTPException(status.HTTP_403_FORBIDDEN, "You can only access your own notifications.")
|
||||||
|
|
||||||
|
notification.read = True
|
||||||
|
notification.read_at = datetime.now(UTC)
|
||||||
|
notification = await notification.replace()
|
||||||
|
|
||||||
|
return NotificationData.from_notification(notification)
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
router.include_router(base_router)
|
router.include_router(base_router)
|
||||||
router.include_router(notifications_router)
|
router.include_router(notifications_router)
|
||||||
|
|
Loading…
Reference in a new issue