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 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 2x 3x 2x 1x 1x 1x 5x 13x 13x 13x 13x 13x 2x 11x 9x 9x 9x 9x 9x 9x 8x 8x 10x 8x 5x 5x 5x 1x 9x 2x 2x 5x 17x 17x 16x 16x 16x 16x 16x 16x 15x 3x 15x 4x 15x 15x 16x 11x 16x 15x 14x 14x 16x 15x 10x 16x 15x 15x 10x 10x 9x 9x 9x 9x 9x 9x 15x 15x 16x 16x 15x 15x 1x 16x 15x 9x 9x 10x 10x 9x 10x 10x 9x 6x 6x 6x 6x 5x 6x 6x 6x 1x 1x 5x 41x 5x 27x 27x 26x 26x 26x 26x 25x 24x 24x 24x 9x 9x 9x 9x 9x 8x 1x 1x 24x 9x 9x 9x 9x 9x 9x 9x 8x 8x 1x 24x 23x 4x 19x 7x 12x 2x 10x 9x 1x 23x 24x 24x 1x 1x 5x 16x 16x 15x 15x 15x 15x 14x 16x 13x 5x 5x 5x 5x 5x 4x 1x 13x 6x 6x 6x 6x 6x 6x 6x 5x 5x 5x 1x 13x 13x 13x 13x 13x 16x 13x 13x 16x 13x 15x 13x 1x 1x 5x 5x 5x 4x 4x 4x 4x 3x 3x 3x 2x 2x 2x 3x 3x 3x 2x 1x 3x 3x 1x 1x 5x 8x 8x 7x 7x 7x 7x 7x 7x 2x 7x 7x 6x 3x 6x 6x 6x 4x 36x 36x 8x 36x 36x 6x 6x 6x 1x 1x 5x 5x 5x 4x 3x 3x 3x 1x 1x 5x 4x 4x 3x 2x 1x 1x 5x 20x 20x 19x 19x 18x 1x 17x 17x 3x 3x 1x 2x 1x 1x 15x 15x 14x 14x 13x 13x 1x 1x 5x 7x 7x 6x 6x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 1x 1x 5x 9x 9x 9x 8x 2x 6x 6x 5x 5x 5x 4x 1x 3x 3x 2x 2x 2x 1x 1x 5x 7x 7x 7x 7x 7x 1x 7x 7x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 14x 14x 13x 13x 13x 1x 12x 11x 10x 10x 10x 10x 10x 10x 10x 6x 6x 5x 5x 5x 5x 5x 5x 5x 5x 5x 10x 10x 1x 1x 5x 4x 4x 3x 3x 4x 3x 2x 2x 3x 3x 2x 4x 2x 1x 1x 26x 26x 8x 8x 8x 9x 3x 3x 9x 6x 6x 4x 2x 6x 6x 8x 8x 8x 8x 8x 17x 8x 8x 9x 9x 1x 9x 3x 3x 1x 5x | /**
* Report & moderation routes — reports, suspensions, appeals, audit log.
*
* POST /api/reports → Submit a report
* GET /api/reports → List reports (admin, enriched, filterable)
* POST /api/reports/:id/resolve → Resolve a report with full logic (admin)
* POST /api/reports/resolve-all/:userId → Resolve all pending reports for user (admin)
* GET /api/reports/stats → Report statistics (admin)
* GET /api/reports/export → CSV export of resolved reports (admin)
* POST /api/reports/:id/lock → Lock a report (admin)
* DELETE /api/reports/:id/lock → Unlock a report (admin)
* POST /api/admin/users/:uniqueId/suspend → Suspend a user (admin)
* POST /api/admin/users/:uniqueId/unsuspend → Unsuspend a user (admin)
* GET /api/appeals → List appeals (admin)
* PATCH /api/appeals/:id → Review an appeal (admin)
* POST /api/appeals → Submit an appeal
* GET /api/admin/audit-log → Get admin audit log (admin)
*/
const router = require('express').Router();
const { db, rtdb } = require('../utils/firebase');
const { generateId, now } = require('../utils/helpers');
const { requireAdmin, clearSuspensionCache } = require('../middleware/auth');
const { sendSystemPm } = require('../utils/system-pm');
const { computeDisplayScore } = require('../utils/gcs');
const { getDoc, queryDocs } = require('../utils/firestore-helpers');
const { sendFcmToTokens } = require('../utils/fcm');
const log = require('../utils/log');
const { createWarning } = require('./admin-users');
/**
* Remove invalid FCM tokens from admin user docs in Firestore.
* For each invalid token, finds which admin user doc contains it and removes it.
*/
async function cleanupInvalidAdminTokens(invalidTokens, adminUsers) {
if (!invalidTokens || invalidTokens.length === 0) return;
const invalidSet = new Set(invalidTokens);
for (let i = 0; i < adminUsers.length; i += 500) {
const chunk = adminUsers.slice(i, i + 500);
const batch = db.batch();
let hasWrites = false;
for (const u of chunk) {
Iif (!Array.isArray(u.fcmTokens)) continue;
const filtered = u.fcmTokens.filter((t) => !invalidSet.has(t));
if (filtered.length !== u.fcmTokens.length) {
batch.set(db.doc(`users/${u.id}`), { fcmTokens: filtered }, { merge: true });
hasWrites = true;
}
}
Eif (hasWrites) await batch.commit();
}
}
// ─── Route definitions ───────────────────────────────────────────
// ── Submit report ──
router.post('/reports', async (req, res) => {
try {
const body = req.body;
Iif (!body) return res.status(400).json({ error: 'Invalid JSON body' });
const {
reportedUserId,
reportedUserName,
reportedUserUniqueId,
conversationId,
messageId,
messageText,
reason,
description,
evidenceUrls,
} = body;
if (!reportedUserId || !reason) {
return res.status(400).json({ error: 'reportedUserId and reason required' });
}
// Fetch reporter info
const reporter = await getDoc(`users/${req.auth.uniqueId}`);
const reportId = generateId();
const timestamp = now();
await db.doc(`reports/${reportId}`).set(
{
reporterId: req.auth.uniqueId,
reporterName: reporter?.displayName ?? reporter?.display_name ?? null,
reporterUniqueId: reporter?.uniqueId ?? reporter?.unique_id ?? null,
reportedUserId: reportedUserId,
reportedUserName: reportedUserName || null,
reportedUserUniqueId: reportedUserUniqueId || null,
conversationId: conversationId || null,
messageId: messageId || null,
messageText: messageText || null,
reason: reason,
description: description || null,
evidenceUrls: evidenceUrls || [],
status: 'pending',
actionTaken: null,
resolvedAt: null,
resolvedBy: null,
createdAt: timestamp,
},
{ merge: true },
);
// Fire-and-forget: FCM push notification to admin tokens
(async () => {
try {
const adminUsers = await queryDocs(db.collection('users').where('userType', '==', 'ADMIN'));
const tokens = [];
for (const u of adminUsers) {
if (Array.isArray(u.fcmTokens)) tokens.push(...u.fcmTokens);
}
if (tokens.length > 0) {
const data = {
type: 'ADMIN_NEW_REPORT',
reportId,
reason,
reportedUserName: reportedUserName || 'Unknown',
};
const invalid = await sendFcmToTokens(tokens, data);
await cleanupInvalidAdminTokens(invalid, adminUsers);
}
} catch (err) {
log.error('reports', 'Failed to send report notification', { error: err.message });
}
})().catch((err) =>
log.error('reports', 'Report notification fire-and-forget failed', { error: err.message }),
);
res.json({ success: true, reportId });
} catch (err) {
log.error('reports', 'POST /api/reports failed', { error: err.message });
res.status(500).json({ error: 'Internal server error' });
}
});
// ── List reports (admin — enriched, filterable) ──
router.get('/reports', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const statusFilter = req.query.status || 'pending';
const userIdFilter = req.query.userId;
const search = req.query.search?.toLowerCase();
// Build Firestore query — only use status + orderBy to avoid needing a
// composite index. The userId filter is applied client-side afterwards.
const direction = statusFilter === 'pending' ? 'asc' : 'desc';
const query = db
.collection('reports')
.where('status', '==', statusFilter)
.orderBy('createdAt', direction)
.limit(500);
const reports = await queryDocs(query);
// Client-side userId filter (avoids Firestore composite index requirement)
const userFiltered = userIdFilter
? reports.filter((r) => r.reportedUserId === userIdFilter)
: reports;
// Client-side search filter (Firestore doesn't support full-text search)
const filtered = search
? userFiltered.filter(
(r) =>
(r.reportedUserName || '').toLowerCase().includes(search) ||
(r.reporterName || '').toLowerCase().includes(search) ||
(r.reason || '').toLowerCase().includes(search) ||
(r.description || '').toLowerCase().includes(search),
)
: userFiltered;
// Collect all unique user IDs for enrichment
// User documents are keyed by uniqueId (numeric), NOT Firebase Auth UID.
// Build a mapping from reportedUserId → reportedUserUniqueId from report data,
// then use uniqueId to fetch user documents.
const reportedUniqueIdMap = {}; // reportedUserId → reportedUserUniqueId
for (const r of filtered) {
if (
r.reportedUserId &&
r.reportedUserUniqueId !== null &&
r.reportedUserUniqueId !== undefined
) {
reportedUniqueIdMap[r.reportedUserId] = r.reportedUserUniqueId;
}
}
const reportedUserIds = [...new Set(filtered.map((r) => r.reportedUserId).filter(Boolean))];
const reportedUniqueIds = [
...new Set(
reportedUserIds
.map((uid) => reportedUniqueIdMap[uid])
.filter((id) => id !== null && id !== undefined),
),
];
const reporterIds = [...new Set(filtered.map((r) => r.reporterId).filter(Boolean))];
// Parallel-fetch user enrichment data and report locks
const [reportedUserDocs, reporterDocs, locks] = await Promise.all([
Promise.all(reportedUniqueIds.map((uid) => getDoc(`users/${uid}`))),
Promise.all(reporterIds.map((uid) => getDoc(`users/${uid}`))),
queryDocs(db.collection('reportLocks')),
]);
// Build lookup maps — index by reportedUserId for enrichment
const userMap = {};
for (let i = 0; i < reportedUniqueIds.length; i++) {
const reportedUser = reportedUserDocs[i];
if (reportedUser) {
const gcsScore = reportedUser.gcsScore ?? reportedUser.gcs_score ?? 100;
const gcsLastDeduction =
reportedUser.gcsLastDeductionAt ?? reportedUser.gcs_last_deduction_at ?? null;
reportedUser.gcsDisplayScore = computeDisplayScore(gcsScore, gcsLastDeduction);
// Find the reportedUserId(s) that map to this uniqueId and index by them
for (const [ruid, uniqueId] of Object.entries(reportedUniqueIdMap)) {
Eif (String(uniqueId) === String(reportedUniqueIds[i])) {
userMap[ruid] = reportedUser;
}
}
}
}
const reporterMap = {};
for (let i = 0; i < reporterIds.length; i++) {
const reporter = reporterDocs[i];
if (reporter) reporterMap[reporterIds[i]] = reporter;
}
const lockMap = {};
for (const lock of locks) {
// reportLocks documents are keyed by userId (the reported user)
lockMap[lock.id] = lock;
}
// Enrich reports
const enriched = filtered.map((r) => ({
...r,
evidenceUrls: r.evidenceUrls || [],
reportedUser: userMap[r.reportedUserId] || null,
reporter: reporterMap[r.reporterId] || null,
lock: lockMap[r.reportedUserId] || null,
}));
// Group by reported user for pending reports
if (statusFilter === 'pending') {
const grouped = {};
for (const r of enriched) {
const key = r.reportedUserId;
if (!grouped[key]) {
grouped[key] = {
reportedUserId: key,
displayName: r.reportedUser?.displayName ?? r.reportedUser?.display_name ?? null,
profilePhotoUrl:
r.reportedUser?.profilePhotoUrl ?? r.reportedUser?.profile_photo_url ?? null,
uniqueId:
r.reportedUser?.uniqueId ??
r.reportedUser?.unique_id ??
r.reportedUserUniqueId ??
null,
warningCount: r.reportedUser?.warningCount ?? r.reportedUser?.warning_count ?? 0,
isSuspended: r.reportedUser?.isSuspended ?? r.reportedUser?.is_suspended ?? false,
gcsDisplayScore: r.reportedUser?.gcsDisplayScore ?? 100,
lock: r.lock,
reports: [],
reportCount: 0,
};
}
grouped[key].reports.push(r);
grouped[key].reportCount = grouped[key].reports.length;
}
return res.json({ users: Object.values(grouped) });
}
// Resolved/archived: also group by reported user for consistent structure
const grouped = {};
for (const r of enriched) {
const key = r.reportedUserId;
if (!grouped[key]) {
grouped[key] = {
reportedUserId: key,
displayName: r.reportedUser?.displayName ?? r.reportedUser?.display_name ?? null,
profilePhotoUrl:
r.reportedUser?.profilePhotoUrl ?? r.reportedUser?.profile_photo_url ?? null,
uniqueId:
r.reportedUser?.uniqueId ?? r.reportedUser?.unique_id ?? r.reportedUserUniqueId ?? null,
warningCount: r.reportedUser?.warningCount ?? r.reportedUser?.warning_count ?? 0,
isSuspended: r.reportedUser?.isSuspended ?? r.reportedUser?.is_suspended ?? false,
gcsDisplayScore: r.reportedUser?.gcsDisplayScore ?? 100,
lock: r.lock,
reports: [],
reportCount: 0,
};
}
grouped[key].reports.push(r);
grouped[key].reportCount = grouped[key].reports.length;
}
res.json({ users: Object.values(grouped) });
} catch (err) {
log.error('reports', 'GET /api/reports failed', { error: err.message });
res.status(500).json({ error: 'Internal server error' });
}
});
// Normalise short action names sent by the admin panel to the canonical stored values.
// The admin panel sends 'warn' / 'suspend' / 'dismiss'; the backend stores 'warned' / 'suspended' / 'dismissed'.
const ACTION_ALIASES = { warn: 'warned', suspend: 'suspended', dismiss: 'dismissed' };
function normaliseAction(raw) {
return ACTION_ALIASES[raw] || raw || 'dismissed';
}
// ── Resolve report (admin — full logic) ──
router.post('/reports/:id/resolve', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const body = req.body;
const action = normaliseAction(body?.action);
const timestamp = now();
// Fetch the report
const report = await getDoc(`reports/${req.params.id}`);
if (!report) return res.status(404).json({ error: 'Report not found' });
// Resolve the report
await db.doc(`reports/${req.params.id}`).update({
status: 'resolved',
actionTaken: action,
resolvedAt: timestamp,
resolvedBy: req.auth.uid,
});
// Audit log (fire-and-forget)
const auditWrite = db.doc(`adminAuditLog/${generateId()}`).set(
{
adminId: req.auth.uid,
action: 'RESOLVE_REPORT',
targetUserId: report.reportedUserId,
details: `Report ${req.params.id}: ${action}`,
createdAt: timestamp,
},
{ merge: true },
);
// Warning actions: create warning doc (which deducts GCS)
if (action === 'warned' || action === 'warned_severe') {
const severity = body?.severity || (action === 'warned_severe' ? 4 : 2);
const warningReason = body?.reason || report.reason;
// createWarning expects uniqueId (user doc key), not Firebase Auth UID
const warnUniqueId = report.reportedUserUniqueId ?? report.reportedUserId;
try {
await createWarning(warnUniqueId, {
reason: warningReason,
severity,
adminNote: body?.adminNote || null,
source: 'report',
linkedReportId: req.params.id,
adminUid: req.auth.uid,
adminUniqueId: req.auth.uniqueId,
});
// Send warning PM (fire-and-forget)
sendSystemPm(
report.reportedUserId,
`\u26a0\ufe0f You have received a warning.\n\nReason: ${warningReason}\n\nRepeated violations may result in suspension.`,
).catch((err) =>
log.error('reports', 'Failed to send warning PM', {
userId: report.reportedUserId,
error: err.message,
}),
);
} catch (warnErr) {
log.error('reports', 'Failed to create warning from report', {
reportId: req.params.id,
error: warnErr.message,
});
}
}
// Suspension action: suspend the reported user
if (action === 'suspended') {
const suspensionDays = body?.suspensionDays ? Number(body.suspensionDays) : 0;
const canAppeal = body?.canAppeal ?? false;
const endTimestamp = suspensionDays > 0 ? Date.now() + suspensionDays * 86400000 : null;
// User documents are keyed by uniqueId, not Firebase Auth UID
const reportedUniqueId = report.reportedUserUniqueId ?? report.reportedUserId;
const reportedUser = await getDoc(`users/${reportedUniqueId}`);
try {
await db.doc(`users/${reportedUniqueId}`).update({
isSuspended: true,
suspensionReason: report.reason || 'Moderation action',
suspensionStartDate: timestamp,
suspensionEndDate: endTimestamp,
suspensionCanAppeal: canAppeal,
suspendedBy: req.auth.uid,
preSuspensionDisplayName: reportedUser?.displayName ?? reportedUser?.display_name ?? null,
preSuspensionProfilePhotoUrl:
reportedUser?.profilePhotoUrl ?? reportedUser?.profile_photo_url ?? null,
preSuspensionCoverPhotoUrl:
reportedUser?.coverPhotoUrl ?? reportedUser?.cover_photo_url ?? null,
displayName: 'Suspended Account',
profilePhotoUrl: null,
coverPhotoUrl: null,
avatarUrl: null,
description: null,
currentRoomId: null,
});
// Evict from rooms (fire-and-forget)
evictSuspendedUser(reportedUniqueId).catch((err) =>
log.error('reports', 'Failed to evict suspended user from resolve', {
userId: reportedUniqueId,
error: err.message,
}),
);
// Send suspension PM (fire-and-forget)
sendSystemPm(
report.reportedUserId,
`Your account has been suspended.\n\nReason: ${report.reason || 'Moderation action'}${canAppeal ? '\n\nYou may submit an appeal.' : ''}`,
).catch(() => {});
} catch (susErr) {
log.error('reports', 'Failed to suspend user from resolve', {
reportId: req.params.id,
error: susErr.message,
});
}
}
// Resolution PM to reporter (fire-and-forget)
if (report.reporterId) {
let actionText;
if (action === 'dismissed') {
actionText = 'reviewed and dismissed';
} else if (action === 'warned') {
actionText = 'reviewed and a warning was issued';
} else if (action === 'warned_severe') {
actionText = 'reviewed and a severe warning was issued';
} else if (action === 'suspended') {
actionText = 'reviewed and the user has been suspended';
} else {
actionText = 'reviewed';
}
sendSystemPm(
report.reporterId,
`Your report has been ${actionText}. Thank you for helping keep ShyTalk safe.`,
).catch((err) =>
log.error('reports', 'Failed to send reporter PM', {
reporterId: report.reporterId,
error: err.message,
}),
);
}
// Release lock and write audit log in parallel
await Promise.all([auditWrite, db.doc(`reportLocks/${req.params.id}`).delete()]);
res.json({ success: true });
} catch (err) {
log.error('reports', 'POST /api/reports/:id/resolve failed', {
reportId: req.params.id,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
// ── Resolve all pending reports for a user ──
router.post('/reports/resolve-all/:userId', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const body = req.body;
const action = normaliseAction(body?.action);
const timestamp = now();
// Fetch all pending reports for this user
const reports = await queryDocs(
db
.collection('reports')
.where('reportedUserId', '==', req.params.userId)
.where('status', '==', 'pending'),
);
if (reports.length === 0) return res.json({ success: true, resolved: 0 });
// Build batch writes for resolving all reports
const allWrites = reports.map((r) => ({
path: `reports/${r.id}`,
data: {
status: 'resolved',
actionTaken: action,
resolvedAt: timestamp,
resolvedBy: req.auth.uid,
},
}));
// Apply warning if applicable (uses createWarning to write warning doc + update user)
if (action === 'warned' || action === 'warned_severe') {
const severity = body?.severity || (action === 'warned_severe' ? 4 : 2);
const warningReason = body?.reason || 'Multiple reports';
// createWarning expects uniqueId (user doc key), not Firebase Auth UID
const warnUniqueId = reports[0]?.reportedUserUniqueId ?? req.params.userId;
try {
await createWarning(warnUniqueId, {
reason: warningReason,
severity,
adminNote: body?.adminNote || null,
source: 'report',
linkedReportId: null,
adminUid: req.auth.uid,
adminUniqueId: req.auth.uniqueId,
});
// Send warning PM (fire-and-forget)
sendSystemPm(
req.params.userId,
`\u26a0\ufe0f You have received a warning based on multiple reports.\n\nReason: ${warningReason}\n\nRepeated violations may result in suspension.`,
).catch((err) =>
log.error('reports', 'Failed to send warning PM', {
userId: req.params.userId,
error: err.message,
}),
);
} catch (warnErr) {
log.error('reports', 'Failed to create warning from bulk resolve', {
userId: req.params.userId,
error: warnErr.message,
});
}
}
// Suspension action: suspend the reported user
if (action === 'suspended') {
const suspensionDays = body?.suspensionDays ? Number(body.suspensionDays) : 0;
const canAppeal = body?.canAppeal ?? false;
const endTimestamp = suspensionDays > 0 ? Date.now() + suspensionDays * 86400000 : null;
// User docs are keyed by uniqueId; get it from the report data
const reportedUniqueId = reports[0]?.reportedUserUniqueId ?? req.params.userId;
const reportedUser = await getDoc(`users/${reportedUniqueId}`);
try {
await db.doc(`users/${reportedUniqueId}`).update({
isSuspended: true,
suspensionReason: 'Multiple reports',
suspensionStartDate: timestamp,
suspensionEndDate: endTimestamp,
suspensionCanAppeal: canAppeal,
suspendedBy: req.auth.uid,
preSuspensionDisplayName: reportedUser?.displayName ?? reportedUser?.display_name ?? null,
preSuspensionProfilePhotoUrl:
reportedUser?.profilePhotoUrl ?? reportedUser?.profile_photo_url ?? null,
preSuspensionCoverPhotoUrl:
reportedUser?.coverPhotoUrl ?? reportedUser?.cover_photo_url ?? null,
displayName: 'Suspended Account',
profilePhotoUrl: null,
coverPhotoUrl: null,
avatarUrl: null,
description: null,
currentRoomId: null,
});
evictSuspendedUser(reportedUniqueId).catch((err) =>
log.error('reports', 'Failed to evict suspended user from bulk resolve', {
userId: reportedUniqueId,
error: err.message,
}),
);
// Send suspension PM (fire-and-forget)
sendSystemPm(
reports[0]?.reportedUserId ?? req.params.userId,
`Your account has been suspended.\n\nReason: Multiple reports${canAppeal ? '\n\nYou may submit an appeal.' : ''}`,
).catch((err) =>
log.error('reports', 'Failed to send suspension PM from bulk resolve', {
userId: req.params.userId,
error: err.message,
}),
);
// Audit log for suspension action
db.doc(`adminAuditLog/${generateId()}`)
.set(
{
adminId: req.auth.uid,
action: 'SUSPEND',
targetUserId: reportedUniqueId,
details: `Suspended via bulk resolve (${reports.length} reports)`,
createdAt: timestamp,
},
{ merge: true },
)
.catch((err) =>
log.error('reports', 'Failed to write suspension audit log from bulk resolve', {
userId: reportedUniqueId,
error: err.message,
}),
);
} catch (susErr) {
log.error('reports', 'Failed to suspend user from bulk resolve', {
userId: req.params.userId,
error: susErr.message,
});
}
}
// Execute batch writes in chunks of 500
const combinedWrites = allWrites;
for (let i = 0; i < combinedWrites.length; i += 500) {
const chunk = combinedWrites.slice(i, i + 500);
const batch = db.batch();
for (const w of chunk) {
batch.set(db.doc(w.path), w.data, { merge: true });
}
await batch.commit();
}
// Audit log and lock release in parallel
await Promise.all([
db.doc(`adminAuditLog/${generateId()}`).set(
{
adminId: req.auth.uid,
action: 'RESOLVE_ALL_REPORTS',
targetUserId: req.params.userId,
details: `Resolved ${reports.length} reports: ${action}`,
createdAt: timestamp,
},
{ merge: true },
),
db.doc(`reportLocks/${req.params.userId}`).delete(),
]);
// Resolution PMs to all unique reporters (fire-and-forget)
const uniqueReporters = [...new Set(reports.map((r) => r.reporterId).filter(Boolean))];
for (const reporterId of uniqueReporters) {
sendSystemPm(
reporterId,
'Your report has been reviewed. Thank you for helping keep ShyTalk safe.',
).catch((err) =>
log.error('reports', 'Failed to send reporter PM (resolve-all)', {
reporterId,
error: err.message,
}),
);
}
res.json({ success: true, resolved: reports.length });
} catch (err) {
log.error('reports', 'POST /api/reports/resolve-all/:userId failed', {
userId: req.params.userId,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
// ── Report statistics ──
router.get('/reports/stats', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const todayStart = new Date();
todayStart.setHours(0, 0, 0, 0);
const todayMs = todayStart.getTime();
// Fetch pending and resolved-today in parallel
const [pendingReports, resolvedTodayReports, allResolved] = await Promise.all([
queryDocs(db.collection('reports').where('status', '==', 'pending').limit(1000)),
queryDocs(
db
.collection('reports')
.where('status', '==', 'resolved')
.where('resolvedAt', '>=', todayMs)
.limit(1000),
),
queryDocs(
db
.collection('reports')
.where('status', '==', 'resolved')
.where('resolvedAt', '>', 0)
.limit(5000),
),
]);
// Compute average response time from all resolved reports
let totalMs = 0;
let countWithTimes = 0;
for (const r of allResolved) {
Eif (r.resolvedAt && r.createdAt) {
totalMs += r.resolvedAt - r.createdAt;
countWithTimes++;
}
}
const avgResponseHours =
countWithTimes > 0 ? Math.round((totalMs / countWithTimes / (60 * 60 * 1000)) * 10) / 10 : 0;
// Active reviewers today
const reviewerCounts = {};
for (const r of resolvedTodayReports) {
if (r.resolvedBy) {
reviewerCounts[r.resolvedBy] = (reviewerCounts[r.resolvedBy] || 0) + 1;
}
}
const activeReviewers = Object.entries(reviewerCounts).map(([resolvedBy, count]) => ({
resolvedBy,
count,
}));
res.json({
pendingCount: pendingReports.length,
resolvedToday: resolvedTodayReports.length,
avgResponseHours,
activeReviewers,
});
} catch (err) {
log.error('reports', 'GET /api/reports/stats failed', { error: err.message });
res.status(500).json({ error: 'Internal server error' });
}
});
// ── CSV export ──
router.get('/reports/export', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const from = req.query.from;
const to = req.query.to;
const fromMs = from ? new Date(from).getTime() : null;
const toMs = to ? new Date(to + 'T23:59:59.999Z').getTime() : null;
// Build Firestore query
let query = db.collection('reports').where('status', '==', 'resolved');
if (fromMs && !Number.isNaN(fromMs)) {
query = query.where('resolvedAt', '>=', fromMs);
}
query = query.orderBy('resolvedAt', 'desc').limit(5000);
const results = await queryDocs(query);
// Client-side upper bound filter (Firestore requires composite index for two range filters)
const rows =
toMs && !Number.isNaN(toMs)
? results.filter((r) => r.resolvedAt && r.resolvedAt <= toMs)
: results;
// Build CSV
const headers = [
'id',
'reporterName',
'reportedUserName',
'reason',
'description',
'actionTaken',
'resolvedAt',
'resolvedBy',
'createdAt',
];
const csvRows = [headers.join(',')];
for (const r of rows) {
csvRows.push(
headers
.map((h) => {
let val = r[h] ?? '';
if (h === 'resolvedAt' || h === 'createdAt') {
val = val ? new Date(val).toISOString() : '';
}
val = String(val).replaceAll('"', '""');
return `"${val}"`;
})
.join(','),
);
}
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename="reports-export.csv"');
res.send(csvRows.join('\n'));
} catch (err) {
log.error('reports', 'GET /api/reports/export failed', { error: err.message });
res.status(500).json({ error: 'Internal server error' });
}
});
// ── Lock report (admin) ──
router.post('/reports/:id/lock', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const admin = await getDoc(`users/${req.auth.uniqueId}`);
const displayName = admin?.displayName ?? admin?.display_name ?? null;
// reportLocks is keyed by the reported userId (same as report ID here)
await db.doc(`reportLocks/${req.params.id}`).set(
{
lockedBy: req.auth.uid,
lockedAt: now(),
displayName: displayName,
},
{ merge: true },
);
res.json({ success: true });
} catch (err) {
log.error('reports', 'POST /api/reports/:id/lock failed', {
reportId: req.params.id,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
// ── Unlock report (admin) ──
router.delete('/reports/:id/lock', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
await db.doc(`reportLocks/${req.params.id}`).delete();
res.json({ success: true });
} catch (err) {
log.error('reports', 'DELETE /api/reports/:id/lock failed', {
reportId: req.params.id,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
// ══════════════════════════════════════════════════════════════
// SUSPENSIONS (admin — canonical routes)
// ══════════════════════════════════════════════════════════════
// ── Suspend user ──
router.post('/admin/users/:uniqueId/suspend', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const body = req.body;
if (!body?.reason) return res.status(400).json({ error: 'reason is required' });
if (typeof body.canAppeal !== 'boolean')
return res.status(400).json({ error: 'canAppeal must be a boolean' });
let endTimestamp = null;
if (body.endDate) {
const endDate = new Date(body.endDate);
if (Number.isNaN(endDate.getTime()))
return res.status(400).json({ error: 'endDate must be a valid ISO-8601 date' });
if (endDate.getTime() <= Date.now())
return res.status(400).json({ error: 'endDate must be in the future' });
endTimestamp = endDate.getTime();
}
const user = await getDoc(`users/${req.params.uniqueId}`);
if (!user) return res.status(404).json({ error: 'User not found' });
const timestamp = now();
await Promise.all([
db.doc(`users/${req.params.uniqueId}`).update({
isSuspended: true,
suspensionReason: body.reason.trim(),
suspensionStartDate: timestamp,
suspensionEndDate: endTimestamp,
suspensionCanAppeal: body.canAppeal,
suspendedBy: req.auth.uid,
preSuspensionDisplayName: user.displayName ?? user.display_name ?? null,
preSuspensionProfilePhotoUrl: user.profilePhotoUrl ?? user.profile_photo_url ?? null,
preSuspensionCoverPhotoUrl: user.coverPhotoUrl ?? user.cover_photo_url ?? null,
displayName: 'Suspended Account',
profilePhotoUrl: null,
coverPhotoUrl: null,
avatarUrl: null,
description: null,
currentRoomId: null,
}),
db.doc(`adminAuditLog/${generateId()}`).set(
{
adminId: req.auth.uid,
action: 'SUSPEND',
targetUserId: req.params.uniqueId,
details: body.reason.trim(),
createdAt: timestamp,
},
{ merge: true },
),
]);
// Evict from rooms (fire-and-forget)
evictSuspendedUser(req.params.uniqueId).catch((err) =>
log.error('reports', 'Failed to evict suspended user', {
userId: req.params.uniqueId,
error: err.message,
}),
);
res.json({ success: true });
} catch (err) {
log.error('reports', 'POST /api/admin/users/:uniqueId/suspend failed', {
userId: req.params.uniqueId,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
// ── Unsuspend user ──
router.post('/admin/users/:uniqueId/unsuspend', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const user = await getDoc(`users/${req.params.uniqueId}`);
if (!user) return res.status(404).json({ error: 'User not found' });
const preName = user.preSuspensionDisplayName ?? user.pre_suspension_display_name ?? null;
const prePhoto =
user.preSuspensionProfilePhotoUrl ?? user.pre_suspension_profile_photo_url ?? null;
const preCover = user.preSuspensionCoverPhotoUrl ?? user.pre_suspension_cover_photo_url ?? null;
const restore = {};
if (preName) restore.displayName = preName;
if (prePhoto) restore.profilePhotoUrl = prePhoto;
if (preCover) restore.coverPhotoUrl = preCover;
await Promise.all([
db.doc(`users/${req.params.uniqueId}`).update({
isSuspended: false,
suspensionReason: null,
suspensionStartDate: null,
suspensionEndDate: null,
suspensionCanAppeal: null,
suspendedBy: null,
preSuspensionDisplayName: null,
preSuspensionProfilePhotoUrl: null,
preSuspensionCoverPhotoUrl: null,
...restore,
}),
db.doc(`adminAuditLog/${generateId()}`).set(
{
adminId: req.auth.uid,
action: 'UNSUSPEND',
targetUserId: req.params.uniqueId,
details: null,
createdAt: now(),
},
{ merge: true },
),
]);
clearSuspensionCache(Number(req.params.uniqueId));
res.json({ success: true });
} catch (err) {
log.error('reports', 'POST /api/admin/users/:uniqueId/unsuspend failed', {
userId: req.params.uniqueId,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
// ══════════════════════════════════════════════════════════════
// APPEALS
// ══════════════════════════════════════════════════════════════
// ── Submit appeal ──
router.post('/appeals', async (req, res) => {
try {
const body = req.body;
if (!body?.appealText) return res.status(400).json({ error: 'appealText is required' });
if (typeof body.appealText !== 'string' || body.appealText.length > 500) {
return res
.status(400)
.json({ error: 'appealText must be a string of at most 500 characters' });
}
const uniqueId = req.auth.uniqueId;
const user = await getDoc(`users/${uniqueId}`);
const isSuspended = user?.isSuspended ?? user?.is_suspended ?? false;
const canAppeal = user?.suspensionCanAppeal ?? user?.suspension_can_appeal ?? false;
if (!isSuspended) return res.status(400).json({ error: 'User is not suspended' });
if (!canAppeal)
return res.status(403).json({ error: 'Appeals are not allowed for this suspension' });
// Check for existing pending appeal
const existing = await queryDocs(
db
.collection('suspensionAppeals')
.where('userId', '==', uniqueId)
.where('status', '==', 'pending')
.limit(1),
);
if (existing.length > 0) return res.status(409).json({ error: 'An appeal is already pending' });
const appealId = generateId();
await db.doc(`suspensionAppeals/${appealId}`).set(
{
userId: uniqueId,
appealText: body.appealText,
status: 'pending',
reviewedBy: null,
reviewedAt: null,
createdAt: now(),
},
{ merge: true },
);
res.json({ success: true, appealId });
} catch (err) {
log.error('reports', 'POST /api/appeals failed', { error: err.message });
res.status(500).json({ error: 'Internal server error' });
}
});
// ── List appeals (admin) ──
router.get('/appeals', async (req, res) => {
try {
Iif (requireAdmin(req, res)) return;
const statusFilter = req.query.status;
let query = db.collection('suspensionAppeals');
if (statusFilter) {
query = query.where('status', '==', statusFilter);
}
query = query.limit(100);
const appeals = await queryDocs(query);
appeals.sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0));
// Enrich with user data (display name, uniqueId, suspension info)
const enriched = await Promise.all(
appeals.map(async (a) => {
const uid = a.userId ?? a.user_id;
const userData = uid ? await getDoc(`users/${uid}`) : null;
const userUniqueId = userData?.uniqueId ?? userData?.unique_id ?? null;
return {
...a,
userUniqueId,
uniqueId: userUniqueId,
userDisplayName: userData?.displayName ?? userData?.display_name ?? null,
displayName: userData?.displayName ?? userData?.display_name ?? null,
userInfo: {
uniqueId: userUniqueId,
displayName: userData?.displayName ?? userData?.display_name ?? null,
profilePhotoUrl: userData?.profilePhotoUrl ?? userData?.profile_photo_url ?? null,
suspensionReason: userData?.suspensionReason ?? userData?.suspension_reason ?? null,
suspensionStartDate:
userData?.suspensionStartDate ?? userData?.suspension_start_date ?? null,
suspensionEndDate: userData?.suspensionEndDate ?? userData?.suspension_end_date ?? null,
},
suspensionReason: userData?.suspensionReason ?? userData?.suspension_reason ?? null,
suspensionEndDate: userData?.suspensionEndDate ?? userData?.suspension_end_date ?? null,
};
}),
);
res.json(enriched);
} catch (err) {
log.error('reports', 'GET /api/appeals failed', { error: err.message });
res.status(500).json({ error: 'Internal server error' });
}
});
// ── Review appeal (admin) ──
router.patch('/appeals/:id', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const body = req.body;
const status = body?.status;
if (!status || !['approved', 'denied'].includes(status)) {
return res.status(400).json({ error: 'status must be "approved" or "denied"' });
}
const appeal = await getDoc(`suspensionAppeals/${req.params.id}`);
if (!appeal) return res.status(404).json({ error: 'Appeal not found' });
const timestamp = now();
const userId = appeal.userId ?? appeal.user_id;
// Update the appeal document
const appealUpdate = {
status: status,
reviewedBy: req.auth.uid,
reviewedAt: timestamp,
};
if (body.adminNote !== undefined) appealUpdate.adminNote = body.adminNote;
await db.doc(`suspensionAppeals/${req.params.id}`).update(appealUpdate);
// Update user's appeal status on the user document
await db.doc(`users/${userId}`).update({
suspensionAppealStatus: status, // 'approved' or 'denied'
...(status === 'denied' ? { suspensionCanAppeal: false } : {}),
});
// If approved, unsuspend the user
if (status === 'approved') {
const user = await getDoc(`users/${userId}`);
if (user) {
const preName = user.preSuspensionDisplayName ?? user.pre_suspension_display_name ?? null;
const prePhoto =
user.preSuspensionProfilePhotoUrl ?? user.pre_suspension_profile_photo_url ?? null;
const preCover =
user.preSuspensionCoverPhotoUrl ?? user.pre_suspension_cover_photo_url ?? null;
const restore = {};
if (preName) restore.displayName = preName;
if (prePhoto) restore.profilePhotoUrl = prePhoto;
if (preCover) restore.coverPhotoUrl = preCover;
await db.doc(`users/${userId}`).update({
isSuspended: false,
suspensionReason: null,
suspensionStartDate: null,
suspensionEndDate: null,
suspensionCanAppeal: null,
suspendedBy: null,
preSuspensionDisplayName: null,
preSuspensionProfilePhotoUrl: null,
preSuspensionCoverPhotoUrl: null,
...restore,
});
clearSuspensionCache(userId);
}
}
// Audit log
await db.doc(`adminAuditLog/${generateId()}`).set(
{
adminId: req.auth.uid,
action: status === 'approved' ? 'APPEAL_APPROVED' : 'APPEAL_DENIED',
targetUserId: userId,
details: null,
createdAt: timestamp,
},
{ merge: true },
);
res.json({ success: true });
} catch (err) {
log.error('reports', 'PATCH /api/appeals/:id failed', {
appealId: req.params.id,
error: err.message,
});
res.status(500).json({ error: 'Internal server error' });
}
});
// ══════════════════════════════════════════════════════════════
// AUDIT LOG (admin)
// ══════════════════════════════════════════════════════════════
router.get('/admin/audit-log', async (req, res) => {
try {
if (requireAdmin(req, res)) return;
const limit = Math.min(Number.parseInt(req.query.limit, 10) || 50, 200);
const entries = await queryDocs(
db.collection('adminAuditLog').orderBy('createdAt', 'desc').limit(limit),
);
// Enrich with admin display name
const adminIds = [...new Set(entries.map((e) => e.adminId).filter(Boolean))];
const adminDocs = await Promise.all(adminIds.map((id) => getDoc(`users/${id}`)));
const adminNameMap = {};
for (let i = 0; i < adminIds.length; i++) {
const doc = adminDocs[i];
if (doc) {
adminNameMap[adminIds[i]] = doc.displayName ?? doc.display_name ?? null;
}
}
const enriched = entries.map((e) => ({
...e,
adminName: adminNameMap[e.adminId] || null,
}));
res.json(enriched);
} catch (err) {
log.error('reports', 'GET /api/admin/audit-log failed', { error: err.message });
res.status(500).json({ error: 'Internal server error' });
}
});
// ══════════════════════════════════════════════════════════════
// HELPERS
// ══════════════════════════════════════════════════════════════
/**
* Evict a suspended user from all rooms they're in.
*
* Queries rooms where the user appears in participantIds, removes them from
* the participant list, clears their seat, and clears their currentRoomId.
* If the user is the owner, the room is closed and an RTDB close event is fired.
*/
async function evictSuspendedUser(userId) {
const rooms = await queryDocs(
db.collection('rooms').where('participantIds', 'array-contains', userId),
);
if (rooms.length === 0) return;
const batchOps = [];
const rtdbEvents = []; // Collect RTDB writes to fire AFTER Firestore batch
for (const room of rooms) {
if (room.ownerId === userId) {
// Owner suspended — close the room
batchOps.push({
path: `rooms/${room.id}`,
data: { state: 'CLOSED', closedAt: now() },
});
rtdbEvents.push({ roomId: room.id, type: 'room_closed', remove: true });
} else {
// Regular participant — remove from participants and clear their seat
const participantIds = (room.participantIds || []).filter((id) => id !== userId);
const seats = room.seats ? { ...room.seats } : {};
for (const [index, seat] of Object.entries(seats)) {
if (seat && (seat.userId === userId || seat.user_id === userId)) {
seats[index] = { userId: null, state: 'EMPTY', isMuted: false };
}
}
batchOps.push({
path: `rooms/${room.id}`,
data: { participantIds, seats },
});
rtdbEvents.push({ roomId: room.id, type: 'room_updated', remove: false });
}
}
// Clear user's currentRoomId
batchOps.push({
path: `users/${userId}`,
data: { currentRoomId: null },
});
// Commit Firestore batch first
for (let i = 0; i < batchOps.length; i += 500) {
const chunk = batchOps.slice(i, i + 500);
const batch = db.batch();
for (const op of chunk) {
batch.set(db.doc(op.path), op.data, { merge: true });
}
await batch.commit();
}
// Then fire RTDB events (after Firestore is committed)
for (const evt of rtdbEvents) {
try {
await rtdb.ref(`rooms/${evt.roomId}/events/lastEvent`).set({
type: evt.type,
ts: Date.now(),
});
} catch (err) {
log.warn('reports', `Failed to write ${evt.type} RTDB event`, {
roomId: evt.roomId,
error: err.message,
});
}
if (evt.remove) {
try {
await rtdb.ref(`rooms/${evt.roomId}`).remove();
} catch (err) {
log.warn('reports', 'Failed to remove RTDB room node', {
roomId: evt.roomId,
error: err.message,
});
}
}
}
}
module.exports = router;
|