From 1a24a779d962e1e9fd13a469cb5ed260529fe470 Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Sat, 4 Jan 2025 18:11:21 +0100 Subject: [PATCH] Support unmarking notifications as read --- src/api/notifications.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/api/notifications.py b/src/api/notifications.py index e166133..0ccdaad 100644 --- a/src/api/notifications.py +++ b/src/api/notifications.py @@ -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)