Spaces:
Running
Running
Download app/api/v1/endpoints/notifications.py from MonteeqOrg/backend: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/spaces/MonteeqOrg/backend/resolve/main/app/api/v1/endpoints/notifications.py
- Command line
-
hf download hf://spaces/MonteeqOrg/backend/app/api/v1/endpoints/notifications.py
-
curl -L -o notifications.py https://huggingface.co/spaces/MonteeqOrg/backend/resolve/main/app/api/v1/endpoints/notifications.py
2.93 kB
| from typing import List | |
| from fastapi import APIRouter, Depends, HTTPException | |
| from sqlalchemy.orm import Session | |
| from app.core import dependencies | |
| from app.db.session import get_db | |
| from app.crud import notification as crud_notification | |
| from app.models.models import User | |
| from app.schemas import notification as schemas | |
| from app.schemas import push as push_schemas | |
| import os | |
| router = APIRouter() | |
| def get_vapid_public_key(): | |
| """Return the VAPID public key so the frontend can subscribe to push notifications.""" | |
| key = os.getenv("VAPID_PUBLIC_KEY", "") | |
| if not key: | |
| raise HTTPException(status_code=503, detail="Push notifications not configured") | |
| return {"public_key": key} | |
| def get_unread_notifications( | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(dependencies.get_current_user) | |
| ): | |
| return crud_notification.get_unread_notifications(db, user_id=current_user.id) | |
| def mark_notification_read( | |
| notification_id: int, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(dependencies.get_current_user) | |
| ): | |
| notification = crud_notification.mark_notification_read(db, notification_id=notification_id, user_id=current_user.id) | |
| if not notification: | |
| raise HTTPException(status_code=404, detail="Notification not found") | |
| return notification | |
| def mark_all_notifications_read( | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(dependencies.get_current_user) | |
| ): | |
| crud_notification.mark_all_notifications_read(db, user_id=current_user.id) | |
| return {"message": "All notifications marked as read"} | |
| def read_notifications( | |
| skip: int = 0, | |
| limit: int = 50, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(dependencies.get_current_user) | |
| ): | |
| return crud_notification.get_notifications(db, user_id=current_user.id, skip=skip, limit=limit) | |
| def subscribe_push( | |
| subscription: push_schemas.PushSubscriptionCreate, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(dependencies.get_current_user) | |
| ): | |
| return crud_notification.create_push_subscription(db, subscription=subscription, user_id=current_user.id) | |
| def unsubscribe_push( | |
| endpoint: str, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(dependencies.get_current_user) | |
| ): | |
| success = crud_notification.delete_push_subscription(db, endpoint=endpoint, user_id=current_user.id) | |
| if not success: | |
| raise HTTPException(status_code=404, detail="Subscription not found") | |
| return {"message": "Successfully unsubscribed"} | |