Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | 1x 1x 1x 1x 1x 1x 42x 42x 14x 14x 12x 2x 2x 73x 73x 66x 123x 123x 123x 112x 101x 101x 11x 90x 110x 110x 110x 110x 80x 100x 32x 100x 10x 10x 1x 13x 13x 10x 7x 6x 6x 1x 5x 2x 3x 1x 2x 2x 1x 9x 9x 9x 6x 5x 5x 4x 1x 3x 3x 1x 2x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 2x 2x 1x 7x 7x 7x 7x 6x 6x 3x 3x 2x 2x 2x 1x 1x 1x 2x 2x 1x 3x 3x 3x 3x 2x 1x 1x 1x 8x 8x 8x 1x 7x 7x 7x 6x 6x 5x 3x 2x 2x 1x 2x 2x 1x 5x 5x 4x 4x 1x 3x 1x 2x 1x 1x 1x 3x 3x 3x 1x 2x 2x 1x 12x 12x 9x 1x 8x 5x 2x 3x 2x 2x 1x 9x 2x 7x 7x 4x 1x 3x 2x 2x 1x 7x 4x 1x 3x 2x 1x 1x 1x 12x 12x 12x 12x 12x 12x 11x 11x 1x 10x 10x 10x 10x 9x 9x 9x 9x 8x 4x 4x 3x 3x 9x 9x 1x 1x 1x 13x 13x 13x 13x 13x 13x 10x 4x 6x 5x 5x 5x 40x 5x 5x 12x 5x 5x 4x 4x 12x 4x 1x 5x 12x 1x 1x 1x 19x 19x 18x 18x 15x 1x 14x 11x 10x 7x 3x 3x 3x 3x 3x 1x 9x 6x 5x 1x 4x 4x 1x 7x 4x 4x 3x 3x 1x 7x 4x 4x 4x 1x 3x 3x 2x 2x 2x 3x 3x 1x 12x 12x 12x 11x 11x 11x 11x 11x 10x 10x 1x 9x 8x 8x 8x 7x 7x 7x 7x 5x 2x 2x 2x 2x 2x 2x 2x 2x 7x 2x 2x 1x 2x 7x 1x 1x 1x 6x 3x 2x 2x 1x 1x 1x | /**
* Server-authoritative room mutations (anti-grief + race-safety hardening).
*
* Previously the client RoomRepository wrote the room doc directly, and
* firestore.rules let any same-cohort participant write any field — so all
* role/seat gates were CLIENT-ONLY (a hand-crafted write could self-promote
* to host, kick anyone, or seize a seat), and seat claims were non-atomic
* (`update()` with no precondition => concurrent claims last-write-wins, or a
* user seated twice).
*
* These endpoints enforce the ChatRoom gates SERVER-SIDE via the Admin SDK
* (bypasses rules) with TRANSACTIONAL seat claims: a seat has <=1 occupant and
* a user occupies <=1 seat. firestore.rules is tightened in a later phase to
* forbid direct client room-doc writes.
*
* Phase 1 — the full server-authoritative room-mutation surface: seat lifecycle
* (claim / accept-invite / leave-seat / move), participant lifecycle (join with
* ban enforcement / leave-room / disconnect-eviction / first-join), moderation
* (kick / remove / mute / add+remove host), invite-decline, room settings
* (rename / require-approval) and room lifecycle (owner-away / owner-returned /
* close). firestore.rules is tightened in a later phase to forbid direct client
* room-doc writes.
*/
const router = require('express').Router();
const { db, rtdb, FieldValue } = require('../utils/firebase');
const { cohortFromClaim } = require('../utils/firebase-claims');
const {
canTakeSeatDirectly,
userSeatIndex,
canKickUser,
canRemoveFromSeat,
canForceMute,
canMoveSeat,
canCloseRoom,
canSetOwnerAway,
canRemoveDisconnected,
resolveRole,
MAX_SEATS,
OWNER_SEAT_INDEX,
} = require('../utils/room-auth');
const log = require('../utils/log');
// Mirrors the client room-name input cap (CreateRoomDialog: length <= 50).
const MAX_ROOM_NAME_LENGTH = 50;
/** Supplementary RTDB nudge (the Firestore room-doc listener is the primary propagation). */
async function broadcastRoomUpdated(roomId) {
try {
await rtdb
.ref(`rooms/${roomId}/events/lastEvent`)
.set({ type: 'room_updated', ts: Date.now() });
} catch (err) {
log.error('room-mutations', 'RTDB broadcast failed', { roomId, error: err.message });
}
}
/**
* RTDB presence check for the presence-gated endpoints (owner-away safety net,
* disconnect-user eviction). Reads rooms/<roomId>/presence/<userId>. FAIL-SAFE
* to "present" on any read error so a presence outage can never let a caller
* forge an owner-away transition or evict a still-connected user.
*/
async function isUserPresent(roomId, userId) {
try {
const snap = await rtdb.ref(`rooms/${roomId}/presence/${userId}`).get();
return snap.exists();
} catch (err) {
log.error('room-mutations', 'Presence read failed', {
roomId,
userId,
error: err.message,
});
return true;
}
}
/** Parse + bounds-check a seat index from the path; null if invalid. */
function parseSeatIndex(raw) {
const idx = Number(raw);
if (!Number.isInteger(idx) || idx < 0 || idx >= MAX_SEATS) return null;
return idx;
}
/**
* Load the room + apply the cohort gate inside a transaction, then delegate
* to `mutate(room, t, roomRef) -> { status, body }`. The room is cohort-
* stamped at create; a cohort mismatch is hidden as 404 (OSA existence-hide).
*/
async function inRoomTransaction(req, roomId, mutate) {
const roomRef = db.doc(`rooms/${roomId}`);
return db.runTransaction(async (t) => {
const snap = await t.get(roomRef);
if (!snap.exists) return { status: 404, body: { error: 'Room not found' } };
const room = snap.data();
if (cohortFromClaim(req) !== (room.cohort ?? 'minor')) {
return { status: 404, body: { error: 'Not found' } };
}
return mutate(room, t, roomRef);
});
}
/**
* Standard wrapper for mutation endpoints: routes the request + cohort gate
* through `inRoomTransaction`, broadcasts the RTDB nudge on a real mutation
* (return `{ noop: true }` from the inner `mutate` to opt out — used by
* idempotent set-once / already-in-target-state branches), and converts an
* unexpected throw into a logged 500. Reserved for endpoints whose shape is
* "validate → load+gate→update → respond"; specialised flows (`owner-away`
* pre-reads RTDB presence; `close` does a post-txn batch user-doc clear;
* `disconnect-user` does both) hand-roll their try/catch.
*/
async function executeRoomMutation(req, res, errorContext, mutate) {
const { roomId } = req.params;
const callerId = String(req.auth.uniqueId);
try {
const result = await inRoomTransaction(req, roomId, (room, t, roomRef) =>
mutate({ room, t, roomRef, callerId, req }),
);
if (result.status === 200 && !result.noop) {
await broadcastRoomUpdated(roomId);
}
return res.status(result.status).json(result.body);
} catch (err) {
log.error('room-mutations', `${errorContext} failed`, { roomId, error: err.message });
return res.status(500).json({ error: 'Internal server error' });
}
}
// POST /rooms/:roomId/seats/:seatIndex/claim — caller seats THEMSELVES.
router.post('/rooms/:roomId/seats/:seatIndex/claim', async (req, res) => {
const seatIndex = parseSeatIndex(req.params.seatIndex);
if (seatIndex === null) return res.status(400).json({ error: 'Invalid seat index' });
return executeRoomMutation(req, res, 'Seat claim', ({ room, t, roomRef, callerId }) => {
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
const seat = (room.seats || {})[String(seatIndex)] || {};
if (seat.userId || seat.state === 'OCCUPIED') {
return { status: 409, body: { error: 'Seat is already taken', code: 'SEAT_TAKEN' } };
}
if (!canTakeSeatDirectly(room, callerId, seatIndex)) {
return { status: 403, body: { error: 'Not allowed to take this seat' } };
}
if (userSeatIndex(room, callerId) !== -1) {
return { status: 409, body: { error: 'Already seated', code: 'ALREADY_SEATED' } };
}
t.update(roomRef, {
[`seats.${seatIndex}.userId`]: callerId,
[`seats.${seatIndex}.state`]: 'OCCUPIED',
[`seats.${seatIndex}.isMuted`]: false,
participantIds: FieldValue.arrayUnion(callerId),
allTimeSeatUserIds: FieldValue.arrayUnion(callerId),
});
return { status: 200, body: { success: true } };
});
});
// POST /rooms/:roomId/seats/:seatIndex/accept-invite — invited caller seats SELF.
router.post('/rooms/:roomId/seats/:seatIndex/accept-invite', async (req, res) => {
const seatIndex = parseSeatIndex(req.params.seatIndex);
Iif (seatIndex === null) return res.status(400).json({ error: 'Invalid seat index' });
return executeRoomMutation(req, res, 'Accept invite', ({ room, t, roomRef, callerId }) => {
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
const invited = Object.prototype.hasOwnProperty.call(room.pendingInvites || {}, callerId);
if (!invited) return { status: 403, body: { error: 'No pending invite' } };
if (seatIndex === OWNER_SEAT_INDEX)
return { status: 403, body: { error: 'Seat 0 is owner-only' } };
const seat = (room.seats || {})[String(seatIndex)] || {};
if (seat.userId || seat.state === 'OCCUPIED') {
return { status: 409, body: { error: 'Seat is already taken', code: 'SEAT_TAKEN' } };
}
if (userSeatIndex(room, callerId) !== -1) {
return { status: 409, body: { error: 'Already seated', code: 'ALREADY_SEATED' } };
}
t.update(roomRef, {
[`pendingInvites.${callerId}`]: FieldValue.delete(),
[`seats.${seatIndex}.userId`]: callerId,
[`seats.${seatIndex}.state`]: 'OCCUPIED',
[`seats.${seatIndex}.isMuted`]: false,
participantIds: FieldValue.arrayUnion(callerId),
allTimeSeatUserIds: FieldValue.arrayUnion(callerId),
});
return { status: 200, body: { success: true } };
});
});
// POST /rooms/:roomId/seats/:seatIndex/leave — caller leaves their OWN seat.
router.post('/rooms/:roomId/seats/:seatIndex/leave', async (req, res) => {
const seatIndex = parseSeatIndex(req.params.seatIndex);
Iif (seatIndex === null) return res.status(400).json({ error: 'Invalid seat index' });
return executeRoomMutation(req, res, 'Leave seat', ({ room, t, roomRef, callerId }) => {
const seat = (room.seats || {})[String(seatIndex)] || {};
if (String(seat.userId) !== callerId) {
return { status: 403, body: { error: 'Not your seat' } };
}
t.update(roomRef, {
[`seats.${seatIndex}.userId`]: null,
[`seats.${seatIndex}.state`]: 'EMPTY',
[`seats.${seatIndex}.isMuted`]: false,
});
return { status: 200, body: { success: true } };
});
});
// POST /rooms/:roomId/kick — owner/host bans + removes a target user.
router.post('/rooms/:roomId/kick', async (req, res) => {
const targetId = String(req.body?.userId ?? '') || null;
const reason = typeof req.body?.reason === 'string' ? req.body.reason : '';
const kickerName = typeof req.body?.kickerName === 'string' ? req.body.kickerName : '';
if (!targetId) return res.status(400).json({ error: 'userId required' });
return executeRoomMutation(req, res, 'Kick', ({ room, t, roomRef, callerId }) => {
if (!canKickUser(room, callerId, targetId)) {
return { status: 403, body: { error: 'Not allowed to kick this user' } };
}
// CLOSED gate positioned AFTER the role check (info-hiding): unprivileged
// callers still see 403 regardless of room state, so an attacker cannot
// probe CLOSED state by switching between role-lacking and privileged
// accounts. Kicking a user from a dead room is a state-extending write
// (the ban persists in bannedUserIds) and must be rejected.
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
const update = {
bannedUserIds: FieldValue.arrayUnion(targetId),
participantIds: FieldValue.arrayRemove(targetId),
[`kickInfo.${targetId}`]: { kickerName, reason },
};
const seatIdx = userSeatIndex(room, targetId);
if (seatIdx !== -1) {
update[`seats.${seatIdx}.userId`] = null;
update[`seats.${seatIdx}.state`] = 'EMPTY';
update[`seats.${seatIdx}.isMuted`] = false;
}
t.update(roomRef, update);
return { status: 200, body: { success: true } };
});
});
// POST /rooms/:roomId/seats/:seatIndex/remove — owner/host vacates a seat (no ban).
router.post('/rooms/:roomId/seats/:seatIndex/remove', async (req, res) => {
const seatIndex = parseSeatIndex(req.params.seatIndex);
Iif (seatIndex === null) return res.status(400).json({ error: 'Invalid seat index' });
return executeRoomMutation(req, res, 'Remove from seat', ({ room, t, roomRef, callerId }) => {
if (!canRemoveFromSeat(room, callerId, seatIndex)) {
return { status: 403, body: { error: 'Not allowed to remove this occupant' } };
}
t.update(roomRef, {
[`seats.${seatIndex}.userId`]: null,
[`seats.${seatIndex}.state`]: 'EMPTY',
[`seats.${seatIndex}.isMuted`]: false,
});
return { status: 200, body: { success: true } };
});
});
// PATCH /rooms/:roomId/seats/:seatIndex/mute — force-mute (owner/host) or self-unmute.
router.patch('/rooms/:roomId/seats/:seatIndex/mute', async (req, res) => {
const seatIndex = parseSeatIndex(req.params.seatIndex);
Iif (seatIndex === null) return res.status(400).json({ error: 'Invalid seat index' });
if (typeof req.body?.isMuted !== 'boolean') {
return res.status(400).json({ error: 'isMuted (boolean) required' });
}
const { isMuted } = req.body;
return executeRoomMutation(req, res, 'Mute toggle', ({ room, t, roomRef, callerId }) => {
// CLOSED gate fires FIRST — before the seat-empty probe. Otherwise an
// attacker could compare the seat-empty 409 vs seat-occupied 200/403
// responses to learn the room's seat state after CLOSE. Muting in a dead
// room is also a state-extending write (`seats.{i}.isMuted` persists on
// a room nobody can hear).
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
const seat = (room.seats || {})[String(seatIndex)] || {};
if (!seat.userId) return { status: 409, body: { error: 'Seat is empty' } };
if (isMuted) {
// Force-mute: moderator gate (owner/host, not owner/other-host, not already muted).
if (!canForceMute(room, callerId, seatIndex)) {
return { status: 403, body: { error: 'Not allowed to mute this seat' } };
}
} else if (String(seat.userId) !== callerId) {
// Unmute: only the seat's own occupant may unmute themselves.
return { status: 403, body: { error: 'Only the occupant can unmute' } };
}
t.update(roomRef, { [`seats.${seatIndex}.isMuted`]: isMuted });
return { status: 200, body: { success: true } };
});
});
// POST /rooms/:roomId/hosts — OWNER promotes a participant to host.
router.post('/rooms/:roomId/hosts', async (req, res) => {
const targetId = String(req.body?.userId ?? '') || null;
if (!targetId) return res.status(400).json({ error: 'userId required' });
return executeRoomMutation(req, res, 'Add host', ({ room, t, roomRef, callerId }) => {
if (resolveRole(room, callerId) !== 'OWNER') {
return { status: 403, body: { error: 'Only the owner can add hosts' } };
}
if (String(targetId) === String(room.ownerId)) {
return { status: 400, body: { error: 'Owner is not a host' } };
}
// CLOSED gate positioned AFTER role + input checks (info-hiding for the
// role probe). Promoting a host of a dead room is a state-extending write
// — the host role persists on a room whose lifecycle is over.
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
t.update(roomRef, {
hostIds: FieldValue.arrayUnion(targetId),
allTimeHostIds: FieldValue.arrayUnion(targetId),
});
return { status: 200, body: { success: true } };
});
});
// DELETE /rooms/:roomId/hosts/:userId — OWNER demotes a host.
router.delete('/rooms/:roomId/hosts/:userId', async (req, res) => {
const targetId = String(req.params.userId);
return executeRoomMutation(req, res, 'Remove host', ({ room, t, roomRef, callerId }) => {
if (resolveRole(room, callerId) !== 'OWNER') {
return { status: 403, body: { error: 'Only the owner can remove hosts' } };
}
t.update(roomRef, { hostIds: FieldValue.arrayRemove(targetId) });
return { status: 200, body: { success: true } };
});
});
// PATCH /rooms/:roomId/name — OWNER renames the room.
router.patch('/rooms/:roomId/name', async (req, res) => {
const name = typeof req.body?.name === 'string' ? req.body.name.trim() : '';
if (!name) return res.status(400).json({ error: 'name required' });
if (name.length > MAX_ROOM_NAME_LENGTH) {
return res.status(400).json({ error: 'name too long' });
}
return executeRoomMutation(req, res, 'Rename', ({ room, t, roomRef, callerId }) => {
if (resolveRole(room, callerId) !== 'OWNER') {
return { status: 403, body: { error: 'Only the owner can rename the room' } };
}
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
t.update(roomRef, { name });
return { status: 200, body: { success: true } };
});
});
// PATCH /rooms/:roomId/require-approval — OWNER toggles the seat-approval policy.
router.patch('/rooms/:roomId/require-approval', async (req, res) => {
if (typeof req.body?.requireApproval !== 'boolean') {
return res.status(400).json({ error: 'requireApproval (boolean) required' });
}
const { requireApproval } = req.body;
return executeRoomMutation(req, res, 'Set requireApproval', ({ room, t, roomRef, callerId }) => {
if (resolveRole(room, callerId) !== 'OWNER') {
return { status: 403, body: { error: 'Only the owner can change approval settings' } };
}
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
t.update(roomRef, { requireApproval });
return { status: 200, body: { success: true } };
});
});
// POST /rooms/:roomId/owner-returned — OWNER returns from OWNER_AWAY (idempotent on ACTIVE).
router.post('/rooms/:roomId/owner-returned', async (req, res) =>
executeRoomMutation(req, res, 'Owner returned', ({ room, t, roomRef, callerId }) => {
if (resolveRole(room, callerId) !== 'OWNER') {
return { status: 403, body: { error: 'Only the owner can return to the room' } };
}
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
if (room.state === 'ACTIVE') return { status: 200, body: { success: true }, noop: true };
t.update(roomRef, { state: 'ACTIVE', ownerLeftAt: null });
return { status: 200, body: { success: true } };
}),
);
// POST /rooms/:roomId/owner-away — OWNER (or a participant when the owner is
// verifiably absent) transitions the room to OWNER_AWAY. Presence is read
// BEFORE the transaction (RTDB reads can't run inside a Firestore txn); the
// txn re-validates state/role atomically.
router.post('/rooms/:roomId/owner-away', async (req, res) => {
const { roomId } = req.params;
const callerId = String(req.auth.uniqueId);
try {
const roomRef = db.doc(`rooms/${roomId}`);
const preSnap = await roomRef.get();
if (!preSnap.exists) return res.status(404).json({ error: 'Room not found' });
const preRoom = preSnap.data();
if (cohortFromClaim(req) !== (preRoom.cohort ?? 'minor')) {
return res.status(404).json({ error: 'Not found' });
}
const callerIsOwner = resolveRole(preRoom, callerId) === 'OWNER';
// Presence is read here, immediately before the txn, to keep the window
// between read and commit minimal. A residual TOCTOU window is unavoidable
// (RTDB can't be read inside a Firestore txn): if the owner reconnects
// within it the room may briefly flip to OWNER_AWAY — self-healing via
// owner-returned. This mirrors the client presence monitor and is strictly
// safer than the prior client-only write. (Fully closing it needs a
// Firestore-visible presence token — tracked for the rules-lockdown phase.)
const ownerPresent = callerIsOwner ? false : await isUserPresent(roomId, preRoom.ownerId);
const result = await db.runTransaction(async (t) => {
const snap = await t.get(roomRef);
Iif (!snap.exists) return { status: 404, body: { error: 'Room not found' } };
const room = snap.data();
Iif (cohortFromClaim(req) !== (room.cohort ?? 'minor')) {
return { status: 404, body: { error: 'Not found' } };
}
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
if (!canSetOwnerAway(room, callerId, ownerPresent)) {
return { status: 403, body: { error: 'Not allowed to set owner away' } };
}
// Idempotent: already away (e.g. owner re-triggers). Placed AFTER the auth
// gate so a non-participant can't probe room state via a 200.
if (room.state === 'OWNER_AWAY') return { status: 200, body: { success: true } };
t.update(roomRef, { state: 'OWNER_AWAY', ownerLeftAt: Date.now() });
return { status: 200, body: { success: true }, mutated: true };
});
if (result.status === 200 && result.mutated) await broadcastRoomUpdated(roomId);
return res.status(result.status).json(result.body);
} catch (err) {
log.error('room-mutations', 'Owner away failed', { roomId, error: err.message });
return res.status(500).json({ error: 'Internal server error' });
}
});
// POST /rooms/:roomId/close — OWNER (or a participant under the OWNER_AWAY
// close preconditions) ends the room: empties seats + participants and clears
// every participant's currentRoomId (foreign user-doc writes that only the
// server may perform once firestore.rules is locked down).
router.post('/rooms/:roomId/close', async (req, res) => {
const { roomId } = req.params;
const callerId = String(req.auth.uniqueId);
const now = Date.now();
let participantsToClear = [];
try {
const result = await inRoomTransaction(req, roomId, (room, t, roomRef) => {
if (!canCloseRoom(room, callerId, now)) {
return { status: 403, body: { error: 'Not allowed to close this room' } };
}
if (room.state === 'CLOSED') return { status: 200, body: { success: true } }; // idempotent
participantsToClear = (room.participantIds || []).map(String);
const emptySeats = {};
for (let i = 0; i < MAX_SEATS; i += 1) {
emptySeats[String(i)] = { userId: null, state: 'EMPTY', isMuted: false };
}
t.update(roomRef, {
state: 'CLOSED',
closedAt: now,
ownerLeftAt: null,
seats: emptySeats,
participantIds: [],
});
return { status: 200, body: { success: true }, closed: true };
});
if (result.status === 200 && result.closed) {
// Best-effort: a failure here must NOT undo the already-committed close;
// clients also self-clear their own currentRoomId on observing the close.
try {
if (participantsToClear.length) {
const batch = db.batch();
for (const pid of participantsToClear) {
batch.set(db.doc(`users/${pid}`), { currentRoomId: null }, { merge: true });
}
await batch.commit();
}
} catch (err) {
log.error('room-mutations', 'closeRoom currentRoomId clear failed', {
roomId,
error: err.message,
});
}
await broadcastRoomUpdated(roomId);
}
return res.status(result.status).json(result.body);
} catch (err) {
log.error('room-mutations', 'Close room failed', { roomId, error: err.message });
return res.status(500).json({ error: 'Internal server error' });
}
});
// POST /rooms/:roomId/seats/:seatIndex/move — owner/host moves a seat occupant
// to another seat, swapping with the target seat's contents. seatIndex is the
// SOURCE; the destination is `toIndex` in the body. Mirrors
// ActiveRoomManager.moveSeat: neither seat may be the owner seat, and a host
// may not move the owner or another host.
router.post('/rooms/:roomId/seats/:seatIndex/move', async (req, res) => {
const fromIndex = parseSeatIndex(req.params.seatIndex);
if (fromIndex === null) return res.status(400).json({ error: 'Invalid seat index' });
const toIndex = parseSeatIndex(req.body?.toIndex);
if (toIndex === null) return res.status(400).json({ error: 'Invalid target seat index' });
if (fromIndex === toIndex) {
return res.status(400).json({ error: 'Source and target seats are the same' });
}
return executeRoomMutation(req, res, 'Move seat', ({ room, t, roomRef, callerId }) => {
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
if (!canMoveSeat(room, callerId, fromIndex, toIndex)) {
return { status: 403, body: { error: 'Not allowed to move this seat' } };
}
const seats = room.seats || {};
const fromSeat = seats[String(fromIndex)] || {};
const toSeat = seats[String(toIndex)] || {};
t.update(roomRef, {
[`seats.${fromIndex}.userId`]: toSeat.userId ?? null,
[`seats.${fromIndex}.state`]: toSeat.state ?? 'EMPTY',
[`seats.${fromIndex}.isMuted`]: toSeat.isMuted ?? false,
[`seats.${toIndex}.userId`]: fromSeat.userId ?? null,
[`seats.${toIndex}.state`]: fromSeat.state ?? 'EMPTY',
[`seats.${toIndex}.isMuted`]: fromSeat.isMuted ?? false,
});
return { status: 200, body: { success: true } };
});
});
// POST /rooms/:roomId/join — caller joins the room's participant list. The ban
// list is enforced server-side here. currentRoomId is the caller's own user-doc
// field and remains a client write.
router.post('/rooms/:roomId/join', async (req, res) =>
executeRoomMutation(req, res, 'Join room', ({ room, t, roomRef, callerId }) => {
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
if ((room.bannedUserIds || []).map(String).includes(callerId)) {
return { status: 403, body: { error: 'You are banned from this room', code: 'BANNED' } };
}
t.update(roomRef, { participantIds: FieldValue.arrayUnion(callerId) });
return { status: 200, body: { success: true } };
}),
);
// POST /rooms/:roomId/decline-invite — caller declines THEIR OWN pending invite.
// Self-scoped by auth: a caller can only ever remove their own pendingInvites
// entry. No-op (still 200) when there is no pending invite.
router.post('/rooms/:roomId/decline-invite', async (req, res) =>
executeRoomMutation(req, res, 'Decline invite', ({ room, t, roomRef, callerId }) => {
const hasInvite = Object.prototype.hasOwnProperty.call(room.pendingInvites || {}, callerId);
if (!hasInvite) return { status: 200, body: { success: true }, noop: true };
t.update(roomRef, { [`pendingInvites.${callerId}`]: FieldValue.delete() });
return { status: 200, body: { success: true } };
}),
);
// POST /rooms/:roomId/leave — caller removes THEMSELVES from the room
// (participant list + their own seat, if seated). currentRoomId stays a client
// self-write. Idempotent: a no-op arrayRemove if the caller isn't a member.
router.post('/rooms/:roomId/leave', async (req, res) =>
executeRoomMutation(req, res, 'Leave room', ({ room, t, roomRef, callerId }) => {
const isMember = (room.participantIds || []).map(String).includes(callerId);
const seatIdx = userSeatIndex(room, callerId);
// Idempotent no-op: caller isn't a member and isn't seated. Skip the
// unnecessary t.update + RTDB broadcast (the arrayRemove was a no-op
// anyway). Common on a client retrying after a disconnect.
if (!isMember && seatIdx === -1) {
return { status: 200, body: { success: true }, noop: true };
}
const update = { participantIds: FieldValue.arrayRemove(callerId) };
if (seatIdx !== -1) {
update[`seats.${seatIdx}.userId`] = null;
update[`seats.${seatIdx}.state`] = 'EMPTY';
update[`seats.${seatIdx}.isMuted`] = false;
}
t.update(roomRef, update);
return { status: 200, body: { success: true } };
}),
);
// POST /rooms/:roomId/disconnect-user — remove a DISCONNECTED non-owner
// (presence-timeout eviction triggered by a participant's presence monitor).
// The target's absence is verified against RTDB presence BEFORE the txn
// (fail-safe to present). Also clears the removed user's currentRoomId — a
// foreign user-doc write that only the server may perform once rules lock down.
router.post('/rooms/:roomId/disconnect-user', async (req, res) => {
const { roomId } = req.params;
const targetId = String(req.body?.userId ?? '') || null;
if (!targetId) return res.status(400).json({ error: 'userId required' });
const callerId = String(req.auth.uniqueId);
try {
const roomRef = db.doc(`rooms/${roomId}`);
const preSnap = await roomRef.get();
if (!preSnap.exists) return res.status(404).json({ error: 'Room not found' });
const preRoom = preSnap.data();
if (cohortFromClaim(req) !== (preRoom.cohort ?? 'minor')) {
return res.status(404).json({ error: 'Not found' });
}
// CLOSED gate fires HERE — after the cohort check, before the RTDB
// presence read. Saves a roundtrip on a CLOSED room and avoids leaking
// RTDB error behaviour for a state that no longer matters. Disconnecting
// from a dead room is a state-extending write (clears the target's
// `currentRoomId` on a room nobody can rejoin).
if (preRoom.state === 'CLOSED') return res.status(409).json({ error: 'Room is closed' });
const targetPresent = await isUserPresent(roomId, targetId);
const result = await db.runTransaction(async (t) => {
const snap = await t.get(roomRef);
Iif (!snap.exists) return { status: 404, body: { error: 'Room not found' } };
const room = snap.data();
Iif (cohortFromClaim(req) !== (room.cohort ?? 'minor')) {
return { status: 404, body: { error: 'Not found' } };
}
if (!canRemoveDisconnected(room, callerId, targetId, targetPresent)) {
return { status: 403, body: { error: 'Not allowed to remove this user' } };
}
const update = { participantIds: FieldValue.arrayRemove(targetId) };
const seatIdx = userSeatIndex(room, targetId);
Eif (seatIdx !== -1) {
update[`seats.${seatIdx}.userId`] = null;
update[`seats.${seatIdx}.state`] = 'EMPTY';
update[`seats.${seatIdx}.isMuted`] = false;
}
t.update(roomRef, update);
return { status: 200, body: { success: true } };
});
if (result.status === 200) {
try {
await db.doc(`users/${targetId}`).set({ currentRoomId: null }, { merge: true });
} catch (err) {
log.error('room-mutations', 'disconnect-user currentRoomId clear failed', {
roomId,
targetId,
error: err.message,
});
}
await broadcastRoomUpdated(roomId);
}
return res.status(result.status).json(result.body);
} catch (err) {
log.error('room-mutations', 'Disconnect user failed', { roomId, error: err.message });
return res.status(500).json({ error: 'Internal server error' });
}
});
// POST /rooms/:roomId/first-join — caller records THEIR OWN first-join time.
// Self-scoped + set-once: writes firstJoinTimestamps[caller] only if absent, so
// re-entry never overwrites the original timestamp. Rejects 409 on CLOSED rooms
// — the participation-timestamp record is conceptually a lifecycle-write and
// must follow the same "no writes after CLOSE" invariant as /join, /name, etc.
router.post('/rooms/:roomId/first-join', async (req, res) =>
executeRoomMutation(req, res, 'Record first join', ({ room, t, roomRef, callerId }) => {
if (room.state === 'CLOSED') return { status: 409, body: { error: 'Room is closed' } };
const already = Object.prototype.hasOwnProperty.call(room.firstJoinTimestamps || {}, callerId);
if (already) return { status: 200, body: { success: true }, noop: true };
t.update(roomRef, { [`firstJoinTimestamps.${callerId}`]: Date.now() });
return { status: 200, body: { success: true } };
}),
);
module.exports = router;
|