Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions models/discordactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,32 @@ const enrichGroupDataWithMembershipInfo = async (discordId, groups = []) => {
const groupCreatorsDetails = await retrieveUsers({ userIds: Array.from(groupCreatorIds) });
const roleIds = groups.map((group) => group.roleid);
const groupsToUserMappings = await fetchGroupToUserMapping(roleIds);
const roleIdToCountMap = {};

groupsToUserMappings.forEach((groupToUserMapping) => {
// Count how many times roleId comes up in the array.
// This says how many users we have for a given roleId
roleIdToCountMap[groupToUserMapping.roleid] = (roleIdToCountMap[groupToUserMapping.roleid] ?? 0) + 1;
const uniqueDiscordIds = Array.from(new Set(groupsToUserMappings.map((mapping) => mapping.userid).filter(Boolean)));
const discordIdChunks = [];
for (let i = 0; i < uniqueDiscordIds.length; i += BATCH_SIZE_IN_CLAUSE) {
discordIdChunks.push(uniqueDiscordIds.slice(i, i + BATCH_SIZE_IN_CLAUSE));
}

const usersSnapshots = await Promise.all(
discordIdChunks.map(async (chunk) => {
const snap = await userModel.where("discordId", "in", chunk).get();
return snap.docs.map((doc) => doc.data());
})
);
Comment thread
iamitprakash marked this conversation as resolved.

const usersInDiscordSet = new Set(
usersSnapshots
.flat()
.filter((user) => user?.roles?.in_discord === true)
.map((user) => user.discordId)
);
Comment thread
JAHANWEE marked this conversation as resolved.
Outdated

const roleIdToCountMap = {};
Comment thread
JAHANWEE marked this conversation as resolved.
groupsToUserMappings.forEach((mapping) => {
if (usersInDiscordSet.has(mapping.userid)) {
roleIdToCountMap[mapping.roleid] = (roleIdToCountMap[mapping.roleid] ?? 0) + 1;
}
});

const subscribedGroupIds = findSubscribedGroupIds(discordId, groupsToUserMappings);
Expand All @@ -328,8 +348,8 @@ const enrichGroupDataWithMembershipInfo = async (discordId, groups = []) => {
firstName: groupCreator?.first_name,
lastName: groupCreator?.last_name,
image: groupCreator?.picture?.url,
memberCount: roleIdToCountMap[group.roleid] || 0, // Number of users joined this group
isMember: subscribedGroupIds.has(group.roleid), // Is current loggedIn user is a member of this group
memberCount: roleIdToCountMap[group.roleid] || 0,
Comment thread
JAHANWEE marked this conversation as resolved.
Outdated
isMember: subscribedGroupIds.has(group.roleid),
};
});
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/models/discordactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ describe("discordactions", function () {
const result = await enrichGroupDataWithMembershipInfo(userData[0].discordId, newGroupData);
expect(result[0]).to.deep.equal({
...newGroupData[0],
memberCount: 2,
memberCount: 1,
Comment thread
JAHANWEE marked this conversation as resolved.
Comment thread
JAHANWEE marked this conversation as resolved.
firstName: userData[0].first_name,
lastName: userData[0].last_name,
image: userData[0].picture.url,
Expand Down