diff --git a/.env.sample b/.env.sample index f0890ed0af..f64cfcc785 100644 --- a/.env.sample +++ b/.env.sample @@ -28,9 +28,6 @@ YJS_DB_LOCAL_URI=mongodb://collaboration-db:27017/yjs-documents HISTORY_DB_CLOUD_URI= HISTORY_DB_LOCAL_URI=mongodb://history-db:27017/history -# Will use cloud MongoDB Atlas database -ENV=PROD - # Broker BROKER_URL=amqp://broker:5672 diff --git a/services/collaboration/src/controllers/roomController.ts b/services/collaboration/src/controllers/roomController.ts index 118b4a02c6..cb877a146a 100644 --- a/services/collaboration/src/controllers/roomController.ts +++ b/services/collaboration/src/controllers/roomController.ts @@ -154,6 +154,7 @@ export const updateUserStatusInRoomController = async (req: Request, res: Respon // Check if all users in the room have forfeited const allUsersForfeited = updatedRoom.users.every(user => user.isForfeit === true); + console.log('All users forfeited check:', allUsersForfeited, updatedRoom.users); if (allUsersForfeited) { // Close the room if both users have forfeited const result = await closeRoomById(roomId); diff --git a/services/collaboration/src/services/mongodbService.ts b/services/collaboration/src/services/mongodbService.ts index ec287f4f31..9c0ed59100 100644 --- a/services/collaboration/src/services/mongodbService.ts +++ b/services/collaboration/src/services/mongodbService.ts @@ -132,9 +132,10 @@ export const createYjsDocument = async (roomId: string) => { */ export const deleteYjsDocument = async (roomId: string) => { try { + console.log(`Attempting to delete Yjs document collection for room: ${roomId}`); const db = await connectToYJSDB(); - await db.collection(roomId).drop(); - console.log(`Yjs document collection for room ${roomId} deleted`); + const result = await db.collection(roomId).drop(); + console.log(`Yjs document collection for room ${roomId} deleted successfully: ${result}`); } catch (error) { console.error(`Failed to delete Yjs document for room ${roomId}:`, error); throw error; @@ -196,6 +197,12 @@ export const closeRoomById = async (roomId: string) => { } }; +/** + * Update the user isForfeit status in a room + * @param roomId + * @param userId + * @param isForfeit + */ export const updateRoomUserStatus = async (roomId: string, userId: string, isForfeit: boolean) => { try { const db = await connectToRoomDB(); diff --git a/services/collaboration/src/services/webSocketService.ts b/services/collaboration/src/services/webSocketService.ts index b79ce42777..66b28f9320 100644 --- a/services/collaboration/src/services/webSocketService.ts +++ b/services/collaboration/src/services/webSocketService.ts @@ -11,12 +11,20 @@ const { setPersistence, setupWSConnection } = require('../utils/utility.js'); const URL_REGEX = /^.*\/([0-9a-f]{24})\?accessToken=([a-zA-Z0-9\-._~%]{1,})$/; -const authorize = async (ws: WebSocket, request: IncomingMessage): Promise => { +/** + * Verifies the user's access to a specific room by validating the JWT token, + * checking the room's status, and ensuring the user has not forfeited. + * Returns `roomId` if the access is authorized, or `null` otherwise. + * @param ws + * @param request + * @returns + */ +const authorize = async (ws: WebSocket, request: IncomingMessage): Promise => { const url = request.url ?? ''; const match = url?.match(URL_REGEX); if (!match) { handleAuthFailed(ws, 'Authorization failed: Invalid format'); - return false; + return null; } const roomId = match[1]; const accessToken = match[2]; @@ -34,27 +42,27 @@ const authorize = async (ws: WebSocket, request: IncomingMessage): Promise u.id === user.id); if (userInRoom?.isForfeit) { handleAuthFailed(ws, 'Authorization failed: User has forfeited'); - return false; + return null; } console.log('WebSocket connection established for room:', roomId); - return true; + return roomId; }; /** @@ -65,13 +73,13 @@ export const startWebSocketServer = (server: Server) => { const wss = new WebSocketServer({ server }); wss.on('connection', async (conn: WebSocket, req: IncomingMessage) => { - const isAuthorized = await authorize(conn, req); - if (!isAuthorized) { + const roomId = await authorize(conn, req); + if (!roomId) { return; } try { - setupWSConnection(conn, req); + setupWSConnection(conn, req, { docName: roomId }); } catch (error) { console.error('Failed to set up WebSocket connection:', error); handleAuthFailed(conn, 'Authorization failed'); @@ -85,7 +93,7 @@ export const startWebSocketServer = (server: Server) => { console.log(`Loaded persisted document for ${docName}`); const newUpdates = Y.encodeStateAsUpdate(ydoc); - mdb.storeUpdate(docName, newUpdates); + await mdb.storeUpdate(docName, newUpdates); Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc));