From 300c04cef9e60e1912cdd40390c50894c41a7b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 10 Feb 2020 13:47:15 -0500 Subject: [PATCH] fix: #8154, move start/stop every iteration --- src/groups/index.js | 15 +++++++++++---- src/routes/debug.js | 21 +++++++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/groups/index.js b/src/groups/index.js index c6db18e99f..bf2a93cf97 100644 --- a/src/groups/index.js +++ b/src/groups/index.js @@ -154,12 +154,17 @@ Groups.getOwnersAndMembers = async function (groupName, uid, start, stop) { } }); + let done = false; let returnUsers = owners; - const memberStart = Math.max(0, start - ownerUids.length + 1); - const memberStop = memberStart + countToReturn; - + let memberStart = start - ownerUids.length + 1; + let memberStop = memberStart + countToReturn - 1; + memberStart = Math.max(0, memberStart); + memberStop = Math.max(0, memberStop); async function addMembers(start, stop) { let batch = await user.getUsersFromSet('group:' + groupName + ':members', uid, start, stop); + if (!batch.length) { + done = true; + } batch = batch.filter(user => user && user.uid && !ownerUids.includes(user.uid.toString())); returnUsers = returnUsers.concat(batch); } @@ -167,9 +172,11 @@ Groups.getOwnersAndMembers = async function (groupName, uid, start, stop) { if (stop === -1) { await addMembers(memberStart, -1); } else { - while (returnUsers.length < countToReturn) { + while (returnUsers.length < countToReturn && !done) { /* eslint-disable no-await-in-loop */ await addMembers(memberStart, memberStop); + memberStart += countToReturn; + memberStop += countToReturn; } } diff --git a/src/routes/debug.js b/src/routes/debug.js index 460534fcdc..0370a30a26 100644 --- a/src/routes/debug.js +++ b/src/routes/debug.js @@ -6,8 +6,25 @@ var nconf = require('nconf'); module.exports = function (app) { var router = express.Router(); - router.get('/test', function (req, res) { - res.redirect(404); + router.get('/test', async function (req, res) { + // res.redirect(404); + const groups = require('../groups'); + + // const d1 = await groups.getOwnersAndMembers('GNU/Linux User', 1, 0, 9); + // const d2 = await groups.getOwnersAndMembers('GNU/Linux User', 1, 10, 19); + // const d3 = await groups.getOwnersAndMembers('GNU/Linux User', 1, 0, 19); + const d4 = await groups.getOwnersAndMembers('GNU/Linux User', 1, 45, 51); + + + // const uids1 = d1.map(d => d.uid); + // const uids2 = d2.map(d => d.uid); + res.json({ + // uids1: uids1, + // uids2: uids2, + // uids3: d3.map(d => d.uid), + uids4: d4.map(d => d.uid), + len4: d4.length, + }); }); app.use(nconf.get('relative_path') + '/debug', router);