Spaces:
Paused
Paused
Download api/v1/notifications.py from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 3.44 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/api/v1/notifications.py
- Command line
-
hf download hf://spaces/3SCError404/miracle/api/v1/notifications.py
-
curl -L -o notifications.py https://huggingface.co/spaces/3SCError404/miracle/resolve/main/api/v1/notifications.py
3.44 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( | |
| skip: int = 0, | |
| limit: int = 50, | |
| 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)).offset(skip).limit(limit) | |
| 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 mark_notification_read( | |
| notification_id: UUID, | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Mark a single notification as read.""" | |
| stmt = update(Notification).where( | |
| Notification.id == notification_id, | |
| Notification.user_id == current_user.id | |
| ).values(is_read=True) | |
| await db.execute(stmt) | |
| await db.commit() | |
| return {"status": "success"} | |
| async def clear_all_notifications( | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Soft-delete all notifications for the current user.""" | |
| stmt = update(Notification).where( | |
| Notification.user_id == current_user.id, | |
| Notification.workspace_id == current_user.workspace_id, | |
| Notification.deleted_at.is_(None) | |
| ).values(deleted_at=datetime.utcnow()) | |
| await db.execute(stmt) | |
| await db.commit() | |
| return {"status": "success", "detail": "All notifications cleared"} | |
| 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"} | |