@@ -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 ) ;
0 commit comments