Files
endurain/backend/app/websocket/utils.py
João Vitória Silva b7bfe21fcf Add activity hidden flag, push notifications & websocket support
[backend] add notification on create activity based if is hidden or not
[backend] add websocket logic to create activity in order to send new notification to frontend
[backend] add logic to get activity based on start time
[backend] on followers logic the get activities logic will not return hidden or Strava originated activities
[backend] add new is_hidden column to activities table
[backend] moved logic to notify frontend with websockets to notifications.utils
[backend] add validation to validate notification id
[backend] add new route to get notification by id
[backend] fixed encode JWT
[backend] bump dependencies
[frontend] bump dependencies
[frontend] add is_hidden field to EditActivityModalComponent
[frontend] add push notifications to NavbarNotificationsComponent
[frontend] add logic to listen to backend notifications via websocket
[frontend] add NewActivityDuplicateStartTimeNotificationComponent
[frontend] moved to composition API in some files
[frontend] add i18n to new logic
[frontend] add border and pill to activity on home view if it is hidden
[frontend] add alert to activity page if activity is hidden
2025-07-14 16:02:33 +01:00

31 lines
1.1 KiB
Python

from fastapi import HTTPException, status
import websocket.schema as websocket_schema
async def notify_frontend(
user_id: int, websocket_manager: websocket_schema.WebSocketManager, json_data: dict
):
"""
Sends a JSON message to the frontend via an active WebSocket connection for a specific user.
Args:
user_id (int): The ID of the user to notify.
websocket_manager (websocket_schema.WebSocketManager): The manager handling WebSocket connections.
json_data (dict): The JSON-serializable data to send to the frontend.
Raises:
HTTPException: If there is no active WebSocket connection for the specified user.
"""
# Check if the user has an active WebSocket connection
websocket = websocket_manager.get_connection(user_id)
if websocket:
await websocket.send_json(json_data)
else:
if json_data.get("message") == "MFA_REQUIRED":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"No active WebSocket connection for user {user_id}",
)