From 7e47494740f15be4bd4108f46a746ae94be1fcfd Mon Sep 17 00:00:00 2001 From: Alice Kang Date: Wed, 18 Sep 2024 17:11:33 -0400 Subject: [PATCH] Refactored rooms.io code from complexity 16 to 15 --- src/socket.io/admin/rooms.js | 49 +++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js index a8107edaa7..6c4d4d95f9 100644 --- a/src/socket.io/admin/rooms.js +++ b/src/socket.io/admin/rooms.js @@ -31,30 +31,39 @@ SocketRooms.getAll = async function () { }; const userRooms = {}; const topicData = {}; + + function checkTid(key) { + const tid = key.match(/^topic_(\d+)/); + if (tid) { + totals.users.topics += 1; + topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; + topicData[tid[1]].count += 1; + } + } + function checkKey(key) { + if (key === 'online_guests') { + totals.onlineGuestCount += 1; + } else if (key === 'categories') { + totals.users.categories += 1; + } else if (key === 'recent_topics') { + totals.users.recent += 1; + } else if (key === 'unread_topics') { + totals.users.unread += 1; + } else if (key.startsWith('uid_')) { + userRooms[key] = 1; + } else if (key.startsWith('category_')) { + totals.users.category += 1; + } else { + checkTid(key); + } + } + for (const s of sockets) { for (const key of s.rooms) { - if (key === 'online_guests') { - totals.onlineGuestCount += 1; - } else if (key === 'categories') { - totals.users.categories += 1; - } else if (key === 'recent_topics') { - totals.users.recent += 1; - } else if (key === 'unread_topics') { - totals.users.unread += 1; - } else if (key.startsWith('uid_')) { - userRooms[key] = 1; - } else if (key.startsWith('category_')) { - totals.users.category += 1; - } else { - const tid = key.match(/^topic_(\d+)/); - if (tid) { - totals.users.topics += 1; - topicData[tid[1]] = topicData[tid[1]] || { count: 0 }; - topicData[tid[1]].count += 1; - } - } + checkKey(key); } } + totals.onlineRegisteredCount = Object.keys(userRooms).length; let topTenTopics = [];