Add user ping system and room deletion functionality
Backend changes: - Fix track deletion foreign key constraint (tracks.py) * Clear current_track_id from rooms before deleting track * Prevent deletion errors when track is currently playing - Implement user ping/keepalive system (sync.py, websocket.py, ping_task.py, main.py) * Track last pong timestamp for each user * Background task sends ping every 30s, disconnects users after 60s timeout * Auto-pause playback when room becomes empty * Remove disconnected users from room_participants - Enhance room deletion (rooms.py) * Broadcast room_deleted event to all connected users * Close all WebSocket connections before deletion * Cascade delete participants, queue, and messages Frontend changes: - Add ping/pong WebSocket handling (activeRoom.js) * Auto-respond to server pings * Handle room_deleted event with redirect to home - Add room deletion UI (RoomView.vue, HomeView.vue, RoomCard.vue) * Delete button visible only to room owner * Confirmation dialog with warning * Delete button on room cards (shows on hover) * Redirect to home page after deletion 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -63,6 +63,11 @@ async def room_websocket(websocket: WebSocket, room_id: UUID):
|
||||
await websocket.send_json({"type": "pong"})
|
||||
continue
|
||||
|
||||
# Handle pong response (from server ping)
|
||||
if message.get("type") == "pong":
|
||||
manager.update_pong(room_id, user.id)
|
||||
continue
|
||||
|
||||
async with async_session() as db:
|
||||
if message["type"] == "player_action":
|
||||
await handle_player_action(db, room_id, user, message)
|
||||
@@ -72,11 +77,50 @@ async def room_websocket(websocket: WebSocket, room_id: UUID):
|
||||
await handle_sync_request(db, room_id, websocket)
|
||||
|
||||
except WebSocketDisconnect:
|
||||
manager.disconnect(websocket, room_id, user.id)
|
||||
await manager.broadcast_to_room(
|
||||
room_id,
|
||||
{"type": "user_left", "user_id": str(user.id)},
|
||||
await handle_user_disconnect(websocket, room_id, user.id)
|
||||
|
||||
|
||||
async def handle_user_disconnect(websocket: WebSocket, room_id: UUID, user_id: UUID):
|
||||
"""Обработка отключения пользователя от комнаты"""
|
||||
manager.disconnect(websocket, room_id, user_id)
|
||||
|
||||
# Удаляем пользователя из participants в БД
|
||||
async with async_session() as db:
|
||||
await db.execute(
|
||||
select(RoomParticipant)
|
||||
.where(RoomParticipant.room_id == room_id)
|
||||
.where(RoomParticipant.user_id == user_id)
|
||||
)
|
||||
await db.execute(
|
||||
RoomParticipant.__table__.delete().where(
|
||||
RoomParticipant.room_id == room_id,
|
||||
RoomParticipant.user_id == user_id
|
||||
)
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
# Проверяем, остались ли участники в комнате
|
||||
room_user_count = manager.get_room_user_count(room_id)
|
||||
|
||||
# Если комната пустая - ставим трек на паузу
|
||||
if room_user_count == 0:
|
||||
result = await db.execute(select(Room).where(Room.id == room_id))
|
||||
room = result.scalar_one_or_none()
|
||||
if room and room.is_playing:
|
||||
# Сохраняем текущую позицию
|
||||
if room.playback_started_at:
|
||||
elapsed = (datetime.utcnow() - room.playback_started_at).total_seconds() * 1000
|
||||
room.playback_position = int((room.playback_position or 0) + elapsed)
|
||||
|
||||
room.is_playing = False
|
||||
room.playback_started_at = None
|
||||
await db.commit()
|
||||
|
||||
# Уведомляем остальных участников
|
||||
await manager.broadcast_to_room(
|
||||
room_id,
|
||||
{"type": "user_left", "user_id": str(user_id)},
|
||||
)
|
||||
|
||||
|
||||
async def handle_player_action(db: AsyncSession, room_id: UUID, user: User, message: dict):
|
||||
|
||||
Reference in New Issue
Block a user