Skip to content

Commit 02c2548

Browse files
committed
fixed memberCount to show members active in discord
1 parent dadf4d4 commit 02c2548

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

models/discordactions.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,43 @@ const enrichGroupDataWithMembershipInfo = async (discordId, groups = []) => {
311311
const groupCreatorsDetails = await retrieveUsers({ userIds: Array.from(groupCreatorIds) });
312312
const roleIds = groups.map((group) => group.roleid);
313313
const groupsToUserMappings = await fetchGroupToUserMapping(roleIds);
314+
315+
// Extract unique Discord ids from mappings
316+
const uniqueDiscordIds = [...new Set(groupsToUserMappings.map((mapping) => mapping.userid).filter(Boolean))];
317+
318+
// Fetch users by discord ids in batches to check in_discord status
319+
const activeDiscordIdsSet = new Set();
320+
if (uniqueDiscordIds.length > 0) {
321+
const discordIdChunks = [];
322+
for (let i = 0; i < uniqueDiscordIds.length; i += BATCH_SIZE_IN_CLAUSE) {
323+
discordIdChunks.push(uniqueDiscordIds.slice(i, i + BATCH_SIZE_IN_CLAUSE));
324+
}
325+
326+
const userPromises = discordIdChunks.map(async (discordIdChunk) => {
327+
const userSnapshot = await userModel.where("discordId", "in", discordIdChunk).get();
328+
return userSnapshot.docs
329+
.map((doc) => {
330+
const userData = doc.data();
331+
// Only include users who are active in discord (in_discord: true)
332+
if (userData.roles?.in_discord === true) {
333+
return userData.discordId;
334+
}
335+
return null;
336+
})
337+
.filter(Boolean);
338+
});
339+
340+
const activeDiscordIdsArrays = await Promise.all(userPromises);
341+
activeDiscordIdsArrays.flat().forEach((discordId) => activeDiscordIdsSet.add(discordId));
342+
}
343+
314344
const roleIdToCountMap = {};
315345

346+
// Only count users who are active in Discord (in_discord: true)
316347
groupsToUserMappings.forEach((groupToUserMapping) => {
317-
// Count how many times roleId comes up in the array.
318-
// This says how many users we have for a given roleId
319-
roleIdToCountMap[groupToUserMapping.roleid] = (roleIdToCountMap[groupToUserMapping.roleid] ?? 0) + 1;
348+
if (activeDiscordIdsSet.has(groupToUserMapping.userid)) {
349+
roleIdToCountMap[groupToUserMapping.roleid] = (roleIdToCountMap[groupToUserMapping.roleid] ?? 0) + 1;
350+
}
320351
});
321352

322353
const subscribedGroupIds = findSubscribedGroupIds(discordId, groupsToUserMappings);

test/unit/models/discordactions.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,20 @@ describe("discordactions", function () {
432432
let allIds = [];
433433

434434
before(async function () {
435-
const addUsersPromises = userData.map((user) => userModel.add({ ...user }));
435+
// Mark the second user as active in Discord so both users are counted in the test.
436+
const userDataWithInDiscord = userData.map((user, index) => {
437+
if (index === 1) {
438+
return {
439+
...user,
440+
roles: {
441+
...(user.roles || {}),
442+
in_discord: true,
443+
},
444+
};
445+
}
446+
return user;
447+
});
448+
const addUsersPromises = userDataWithInDiscord.map((user) => userModel.add({ ...user }));
436449
const responses = await Promise.all(addUsersPromises);
437450
allIds = responses.map((response) => response.id);
438451
newGroupData = groupData.map((group, index) => {

0 commit comments

Comments
 (0)