aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/api/notifications.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/api/notifications.py')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/api/notifications.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/api/notifications.py b/packages/meshbay-hub/src/meshbay_hub/api/notifications.py
new file mode 100644
index 0000000..e7d36d2
--- /dev/null
+++ b/packages/meshbay-hub/src/meshbay_hub/api/notifications.py
@@ -0,0 +1,93 @@
+"""Notification endpoints — /v1/notifications/*"""
+
+from fastapi import APIRouter, Depends, HTTPException
+from sqlalchemy import select, func, update
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from meshbay_hub.api.deps import get_current_user
+from meshbay_hub.db.engine import get_db
+from meshbay_hub.db.models import Notification, User
+
+router = APIRouter(prefix="/v1/notifications", tags=["notifications"])
+
+
+@router.get("")
+async def list_notifications(
+ current_user: User = Depends(get_current_user),
+ db: AsyncSession = Depends(get_db),
+ limit: int = 50,
+ offset: int = 0,
+ unread_only: bool = False,
+):
+ query = select(Notification).where(Notification.user_id == current_user.id)
+ if unread_only:
+ query = query.where(Notification.read == False) # noqa: E712
+ result = await db.execute(
+ query.order_by(Notification.created_at.desc()).limit(limit).offset(offset)
+ )
+ notifs = result.scalars().all()
+
+ count_q = select(func.count()).select_from(Notification).where(
+ Notification.user_id == current_user.id, Notification.read == False # noqa: E712
+ )
+ unread = (await db.execute(count_q)).scalar() or 0
+
+ return {
+ "notifications": [
+ {
+ "id": n.id,
+ "kind": n.kind,
+ "title": n.title,
+ "detail": n.detail,
+ "link": n.link,
+ "read": n.read,
+ "created_at": n.created_at.isoformat(),
+ }
+ for n in notifs
+ ],
+ "unread_count": unread,
+ }
+
+
+@router.post("/{notification_id}/read")
+async def mark_read(
+ notification_id: int,
+ current_user: User = Depends(get_current_user),
+ db: AsyncSession = Depends(get_db),
+):
+ notif = await db.get(Notification, notification_id)
+ if not notif or notif.user_id != current_user.id:
+ raise HTTPException(status_code=404, detail="Notification not found")
+ notif.read = True
+ await db.commit()
+ return {"status": "ok"}
+
+
+@router.post("/read-all")
+async def mark_all_read(
+ current_user: User = Depends(get_current_user),
+ db: AsyncSession = Depends(get_db),
+):
+ await db.execute(
+ update(Notification)
+ .where(Notification.user_id == current_user.id, Notification.read == False) # noqa: E712
+ .values(read=True)
+ )
+ await db.commit()
+ return {"status": "ok"}
+
+
+async def create_notification(
+ db: AsyncSession,
+ user_id: str,
+ kind: str,
+ title: str,
+ detail: str | None = None,
+ link: str | None = None,
+) -> Notification:
+ notif = Notification(
+ user_id=user_id, kind=kind, title=title, detail=detail, link=link,
+ )
+ db.add(notif)
+ await db.flush()
+ return notif