()
| 179 | } |
| 180 | |
| 181 | setupConnectionHandling() { |
| 182 | this.io.on("connection", (socket) => { |
| 183 | // Authentication middleware |
| 184 | socket.on("authenticate", async (data) => { |
| 185 | const { token, teamId } = data; |
| 186 | let user; |
| 187 | |
| 188 | try { |
| 189 | user = await this.authenticateToken(token); |
| 190 | } catch (error) { |
| 191 | socket.emit("authenticated", { success: false, error: error.message || "Unauthorized access" }); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // Remove old connection for this user if exists |
| 196 | const existingSocket = this.activeConnections.get(user.id); |
| 197 | if (existingSocket && existingSocket.id !== socket.id) { |
| 198 | existingSocket.disconnect(true); |
| 199 | } |
| 200 | |
| 201 | this.activeConnections.set(user.id, socket); |
| 202 | // oxlint-disable-next-line no-param-reassign |
| 203 | socket.userId = user.id; |
| 204 | // oxlint-disable-next-line no-param-reassign |
| 205 | socket.teamId = teamId; |
| 206 | |
| 207 | // Join team room for team-wide broadcasts |
| 208 | if (teamId) { |
| 209 | socket.join(`team:${teamId}`); |
| 210 | this.addToRoom(`team:${teamId}`, socket.id); |
| 211 | } |
| 212 | |
| 213 | // Join user room for private messages |
| 214 | socket.join(`user:${user.id}`); |
| 215 | this.addToRoom(`user:${user.id}`, socket.id); |
| 216 | |
| 217 | socket.emit("authenticated", { success: true }); |
| 218 | }); |
| 219 | |
| 220 | // Handle conversation room joining |
| 221 | socket.on("join-conversation", async (data) => { |
| 222 | const { conversationId } = data; |
| 223 | if (!conversationId) { |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | if (!socket.userId) { |
| 228 | // Not authenticated yet, queue this for after authentication |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | const canJoin = await this.canJoinConversation(socket.userId, conversationId, socket.teamId); |
| 233 | if (!canJoin) { |
| 234 | socket.emit("conversation-join-denied", { conversationId }); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | socket.join(`conversation:${conversationId}`); |
no test coverage detected