make it a method use batch

v1.18.x
Barış Soner Uşaklı 6 years ago
parent f9036e2a6f
commit afa6c71b72

@ -214,27 +214,7 @@ module.exports = function (Groups) {
return callback(new Error('[[error:group-already-exists]]')); return callback(new Error('[[error:group-already-exists]]'));
} }
async.series([ async.series([
// set each member's groupTitle async.apply(updateMemberGroupTitles, oldName, newName),
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(db.setObjectField, 'group:' + oldName, 'name', newName), async.apply(db.setObjectField, 'group:' + oldName, 'name', newName),
async.apply(db.setObjectField, 'group:' + oldName, 'slug', utils.slugify(newName)), async.apply(db.setObjectField, 'group:' + oldName, 'slug', utils.slugify(newName)),
async.apply(db.deleteObjectField, 'groupslug:groupname', group.slug), 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) { function renameGroupMember(group, oldName, newName, callback) {
var score; var score;
async.waterfall([ async.waterfall([

Loading…
Cancel
Save