Spaces:
Paused
Paused
Download api/v1/notifications.py from TeamError404/nexus: direct link, hf CLI and curl.
- Browser
- Download file 2.24 kB
-
https://huggingface.co/spaces/TeamError404/nexus/resolve/main/api/v1/notifications.py
- Command line
-
hf download hf://spaces/TeamError404/nexus/api/v1/notifications.py
-
curl -L -o notifications.py https://huggingface.co/spaces/TeamError404/nexus/resolve/main/api/v1/notifications.py
2.24 kB
| from typing import List, Any | |
| from uuid import UUID | |
| from datetime import datetime | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from sqlalchemy import select, update, desc | |
| from core.deps import get_db, get_current_user | |
| from models.user import User | |
| from models.notification import Notification | |
| from schemas.notification import NotificationResponse | |
| router = APIRouter() | |
| async def list_notifications( | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Fetch notifications for the current user, most recent first.""" | |
| stmt = select(Notification).where( | |
| Notification.user_id == current_user.id, | |
| Notification.workspace_id == current_user.workspace_id, | |
| Notification.deleted_at.is_(None) | |
| ).order_by(desc(Notification.created_at)).limit(50) | |
| result = await db.execute(stmt) | |
| return result.scalars().all() | |
| async def mark_notifications_read( | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Mark all unread notifications as read for current user.""" | |
| stmt = update(Notification).where( | |
| Notification.user_id == current_user.id, | |
| Notification.is_read == False | |
| ).values(is_read=True) | |
| await db.execute(stmt) | |
| await db.commit() | |
| return {"status": "success"} | |
| async def delete_notification( | |
| notification_id: UUID, | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Soft-delete a single notification.""" | |
| stmt = select(Notification).where( | |
| Notification.id == notification_id, | |
| Notification.user_id == current_user.id | |
| ) | |
| result = await db.execute(stmt) | |
| notif = result.scalar_one_or_none() | |
| if not notif: | |
| raise HTTPException(status_code=404, detail="Notification not found") | |
| notif.deleted_at = datetime.utcnow() | |
| db.add(notif) | |
| await db.commit() | |
| return {"status": "deleted"} | |