Files
romm/backend/endpoints/sockets/sync.py
zurdi 00373cee5b refactor: apply the second simplify pass
- A persisted snackbar toasts at once with the caller's options; what this
  tab sends is marked with its tab id, so the socket echo of it adds the row
  without a second toast. The toast no longer depends on which of the
  request and the push arrives first.
- v2 logout disconnects the socket, which otherwise stays in the user's rooms
  until the next login.
- notify_scan_end takes one required outcome, the stats or the error.
- Inboxes are trimmed only for users over the cap, found in one query, and
  pushes go out one after another on one Redis connection.
- The log forwarder and the sync emitters use the shared write manager.
- One makeNotification fixture for the frontend tests; small dead code gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 13:42:12 +02:00

121 lines
3.0 KiB
Python

"""WebSocket events for sync progress notifications.
Emits events:
- sync:started - when a sync session begins
- sync:progress - periodic updates during sync
- sync:completed - when a sync session finishes
- sync:conflict - when a conflict is detected
- sync:error - when a sync operation fails
Uses AsyncRedisManager in write-only mode so these can be called from
RQ background workers (push-pull task, folder watcher) that don't have
access to the main socket server instance.
"""
from handler.socket_handler import socket_handler
async def emit_sync_started(
user_id: int,
device_id: str,
session_id: int,
sync_mode: str,
) -> None:
"""Notify that a sync session has started."""
sm = socket_handler.write_manager()
await sm.emit(
"sync:started",
{
"device_id": device_id,
"session_id": session_id,
"sync_mode": sync_mode,
},
room=f"user:{user_id}",
)
async def emit_sync_progress(
user_id: int,
device_id: str,
session_id: int,
operations_completed: int,
operations_planned: int,
current_file: str | None = None,
) -> None:
"""Notify sync progress update."""
sm = socket_handler.write_manager()
await sm.emit(
"sync:progress",
{
"device_id": device_id,
"session_id": session_id,
"operations_completed": operations_completed,
"operations_planned": operations_planned,
"current_file": current_file,
},
room=f"user:{user_id}",
)
async def emit_sync_completed(
user_id: int,
device_id: str,
session_id: int,
operations_completed: int,
operations_failed: int,
) -> None:
"""Notify that a sync session has completed."""
sm = socket_handler.write_manager()
await sm.emit(
"sync:completed",
{
"device_id": device_id,
"session_id": session_id,
"operations_completed": operations_completed,
"operations_failed": operations_failed,
},
room=f"user:{user_id}",
)
async def emit_sync_conflict(
user_id: int,
device_id: str,
session_id: int,
file_name: str,
rom_id: int,
reason: str,
) -> None:
"""Notify that a sync conflict was detected."""
sm = socket_handler.write_manager()
await sm.emit(
"sync:conflict",
{
"device_id": device_id,
"session_id": session_id,
"file_name": file_name,
"rom_id": rom_id,
"reason": reason,
},
room=f"user:{user_id}",
)
async def emit_sync_error(
user_id: int,
device_id: str,
session_id: int,
error_message: str,
) -> None:
"""Notify that a sync error occurred."""
sm = socket_handler.write_manager()
await sm.emit(
"sync:error",
{
"device_id": device_id,
"session_id": session_id,
"error": error_message,
},
room=f"user:{user_id}",
)