From afa6c71b7249ef51d1a6157831e533d8fb004bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 11 Nov 2018 12:22:07 -0500 Subject: [PATCH] make it a method use batch --- src/groups/update.js | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/groups/update.js b/src/groups/update.js index 982cdec03d..2f1d5dcd65 100644 --- a/src/groups/update.js +++ b/src/groups/update.js @@ -214,27 +214,7 @@ module.exports = function (Groups) { return callback(new Error('[[error:group-already-exists]]')); } async.series([ - // set each member's groupTitle - function (next) { - async.waterfall([ - function (next) { - Groups.getMembers(oldName, 0, -1, next); - }, - function (uids, next) { - async.each(uids, function (uid, next) { - async.waterfall([ - function (next) { - user.getUserData(uid, next); - }, - function (userData, next) { - var newTitleArray = userData.groupTitleArray.map(oldTitle => (oldTitle === oldName ? newName : oldTitle)); - user.setUserField(uid, 'groupTitle', JSON.stringify(newTitleArray), next); - }, - ], next); - }, next); - }, - ], next); - }, + async.apply(updateMemberGroupTitles, oldName, newName), async.apply(db.setObjectField, 'group:' + oldName, 'name', newName), async.apply(db.setObjectField, 'group:' + oldName, 'slug', utils.slugify(newName)), async.apply(db.deleteObjectField, 'groupslug:groupname', group.slug), @@ -275,6 +255,24 @@ module.exports = function (Groups) { }); }; + function updateMemberGroupTitles(oldName, newName, callback) { + const batch = require('../batch'); + batch.processSortedSet('group:' + oldName + ':members', function (uids, next) { + async.waterfall([ + function (next) { + user.getUsersData(uids, next); + }, + function (usersData, next) { + usersData = usersData.filter(userData => userData && userData.groupTitleArray.includes(oldName)); + async.each(usersData, function (userData, next) { + const newTitleArray = userData.groupTitleArray.map(oldTitle => (oldTitle === oldName ? newName : oldTitle)); + user.setUserField(userData.uid, 'groupTitle', JSON.stringify(newTitleArray), next); + }, next); + }, + ], next); + }, callback); + } + function renameGroupMember(group, oldName, newName, callback) { var score; async.waterfall([