Skip to content

Commit

Permalink
Refactored rooms.io code from complexity 16 to 15
Browse files Browse the repository at this point in the history
  • Loading branch information
alicekang1 committed Sep 18, 2024
1 parent 576fb84 commit 7e47494
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/socket.io/admin/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,39 @@ SocketRooms.getAll = async function () {
};
const userRooms = {};
const topicData = {};

Check failure on line 34 in src/socket.io/admin/rooms.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Trailing spaces not allowed
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 = [];
Expand Down

0 comments on commit 7e47494

Please sign in to comment.