store group member count in group hash

yet another upgrade script
v1.18.x
barisusakli 10 years ago
parent 607e88b3c3
commit dc2a2aa98e

@ -299,7 +299,7 @@ var async = require('async'),
};
Groups.getMemberCount = function(groupName, callback) {
db.sortedSetCard('group:' + groupName + ':members', callback);
db.getObjectField('group:' + groupName, 'memberCount', callback);
};
Groups.isMemberOfGroupList = function(uid, groupListKey, callback) {
@ -611,46 +611,56 @@ var async = require('async'),
};
Groups.join = function(groupName, uid, callback) {
callback = callback || function() {};
Groups.exists(groupName, function(err, exists) {
if (exists) {
var tasks = [
async.apply(db.sortedSetAdd, 'group:' + groupName + ':members', Date.now(), uid)
];
function join() {
var tasks = [
async.apply(db.sortedSetAdd, 'group:' + groupName + ':members', Date.now(), uid),
async.apply(db.incrObjectField, 'group:' + groupName, 'memberCount')
];
user.isAdministrator(uid, function(err, isAdmin) {
async.waterfall([
function(next) {
user.isAdministrator(uid, next);
},
function(isAdmin, next) {
if (isAdmin) {
tasks.push(async.apply(db.setAdd, 'group:' + groupName + ':owners', uid));
}
async.parallel(tasks, next);
}
], function(err, results) {
if (err) {
return callback(err);
}
plugins.fireHook('action:group.join', {
groupName: groupName,
uid: uid
});
callback();
});
}
async.parallel(tasks, function(err) {
plugins.fireHook('action:group.join', {
groupName: groupName,
uid: uid
});
callback = callback || function() {};
callback();
});
});
} else {
Groups.create({
name: groupName,
description: '',
hidden: 1
}, function(err) {
if (err && err.message !== '[[error:group-already-exists]]') {
winston.error('[groups.join] Could not create new hidden group: ' + err.message);
return callback(err);
}
Groups.exists(groupName, function(err, exists) {
if (err) {
return callback(err);
}
db.sortedSetAdd('group:' + groupName + ':members', Date.now(), uid, callback);
plugins.fireHook('action:group.join', {
groupName: groupName,
uid: uid
});
});
if (exists) {
return join();
}
Groups.create({
name: groupName,
description: '',
hidden: 1
}, function(err) {
if (err && err.message !== '[[error:group-already-exists]]') {
winston.error('[groups.join] Could not create new hidden group: ' + err.message);
return callback(err);
}
join();
});
});
};
@ -692,9 +702,10 @@ var async = require('async'),
callback = callback || function() {};
var tasks = [
async.apply(db.sortedSetRemove, 'group:' + groupName + ':members', uid),
async.apply(db.setRemove, 'group:' + groupName + ':owners', uid)
];
async.apply(db.sortedSetRemove, 'group:' + groupName + ':members', uid),
async.apply(db.setRemove, 'group:' + groupName + ':owners', uid),
async.apply(db.decrObjectField, 'group:' + groupName, 'memberCount')
];
async.parallel(tasks, function(err) {
if (err) {
@ -715,7 +726,7 @@ var async = require('async'),
if (group.hidden && group.memberCount === 0) {
Groups.destroy(groupName, callback);
} else {
return callback();
callback();
}
});
});
@ -781,51 +792,36 @@ var async = require('async'),
groupData = groupData.filter(function(group) {
return parseInt(group.hidden, 10) !== 1 && !!group.userTitle;
}).map(function(group) {
group.createtimeISO = utils.toISOString(group.createtime);
return group;
});
async.map(groupData, function(groupObj, next) {
Groups.getMemberCount(groupObj.name, function(err, memberCount) {
if (err) { return next(err); }
var groupSets = groupData.map(function(group) {
group.labelColor = group.labelColor || '#000000';
group.createtimeISO = utils.toISOString(group.createtime);
groupObj.memberCount = memberCount;
next(err, groupObj);
});
}, function(err, groupData) {
if (err) {
return callback(err);
if (!group['cover:url']) {
group['cover:url'] = nconf.get('relative_path') + '/images/cover-default.png';
group['cover:position'] = '50% 50%';
}
var groupSets = groupData.map(function(group) {
group.labelColor = group.labelColor || '#000000';
return 'group:' + group.name + ':members';
});
if (!group['cover:url']) {
group['cover:url'] = nconf.get('relative_path') + '/images/cover-default.png';
group['cover:position'] = '50% 50%';
async.map(uids, function(uid, next) {
db.isMemberOfSortedSets(groupSets, uid, function(err, isMembers) {
if (err) {
return next(err);
}
return 'group:' + group.name + ':members';
});
async.map(uids, function(uid, next) {
db.isMemberOfSortedSets(groupSets, uid, function(err, isMembers) {
if (err) {
return next(err);
var memberOf = [];
isMembers.forEach(function(isMember, index) {
if (isMember) {
memberOf.push(groupData[index]);
}
var memberOf = [];
isMembers.forEach(function(isMember, index) {
if (isMember) {
memberOf.push(groupData[index]);
}
});
next(null, memberOf);
});
}, callback);
});
next(null, memberOf);
});
}, callback);
});
});
};

@ -21,7 +21,7 @@ var db = require('./database'),
schemaDate, thisSchemaDate,
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
latestSchema = Date.UTC(2015, 0, 21);
latestSchema = Date.UTC(2015, 0, 30);
Upgrade.check = function(callback) {
db.get('schemaDate', function(err, value) {
@ -773,6 +773,41 @@ Upgrade.upgrade = function(callback) {
winston.info('[2015/01/21] Upgrading groups to sorted set skipped');
next();
}
},
function(next) {
thisSchemaDate = Date.UTC(2015, 0, 30);
if (schemaDate < thisSchemaDate) {
updatesMade = true;
winston.info('[2015/01/30] Adding group member counts');
db.getSortedSetRange('groups:createtime', 0, -1, function(err, groupNames) {
if (err) {
return next(err);
}
var now = Date.now();
async.each(groupNames, function(groupName, next) {
db.sortedSetCard('group:' + groupName + ':members', function(err, memberCount) {
console.log(groupName, memberCount);
if (err) {
return next(err);
}
if (parseInt(memberCount, 10)) {
db.setObjectField('group:' + groupName, 'memberCount', memberCount, next);
} else {
next();
}
});
}, function(err) {
winston.info('[2015/01/30] Adding group member counts done');
Upgrade.update(thisSchemaDate, next);
});
});
} else {
winston.info('[2015/01/30] Adding group member counts skipped');
next();
}
}
// Add new schema updates here

Loading…
Cancel
Save