Support unmarking notifications as read

This commit is contained in:
Peter Vacho 2025-01-04 18:11:21 +01:00
parent 82e82b9317
commit 1a24a779d9
Signed by: school
GPG key ID: 8CFC3837052871B4

View file

@ -103,6 +103,9 @@ async def read_notification(notification_id: PydanticObjectId, user: CurrentUser
if cast(User, notification.user).id != user.id:
raise HTTPException(status.HTTP_403_FORBIDDEN, "You can only access your own notifications.")
if notification.read:
raise HTTPException(status.HTTP_409_CONFLICT, "Notification is already marked as read.")
notification.read = True
notification.read_at = datetime.now(UTC)
notification = await notification.replace()
@ -110,6 +113,29 @@ async def read_notification(notification_id: PydanticObjectId, user: CurrentUser
return NotificationData.from_notification(notification)
@notifications_router.post("/{notification_id}/unread")
async def unread_notification(notification_id: PydanticObjectId, user: CurrentUserDep) -> NotificationData:
"""Mark a notification as unread."""
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.")
if not notification.read:
raise HTTPException(status.HTTP_409_CONFLICT, "Notification is already marked as unread.")
notification.read = False
notification.read_at = None
notification = await notification.replace()
return NotificationData.from_notification(notification)
router = APIRouter()
router.include_router(base_router)
router.include_router(notifications_router)