Support unmarking notifications as read
This commit is contained in:
parent
82e82b9317
commit
1a24a779d9
|
@ -103,6 +103,9 @@ async def read_notification(notification_id: PydanticObjectId, user: CurrentUser
|
||||||
if cast(User, notification.user).id != user.id:
|
if cast(User, notification.user).id != user.id:
|
||||||
raise HTTPException(status.HTTP_403_FORBIDDEN, "You can only access your own notifications.")
|
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 = True
|
||||||
notification.read_at = datetime.now(UTC)
|
notification.read_at = datetime.now(UTC)
|
||||||
notification = await notification.replace()
|
notification = await notification.replace()
|
||||||
|
@ -110,6 +113,29 @@ async def read_notification(notification_id: PydanticObjectId, user: CurrentUser
|
||||||
return NotificationData.from_notification(notification)
|
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 = 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