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) { Groups.getMemberCount = function(groupName, callback) {
db.sortedSetCard('group:' + groupName + ':members', callback); db.getObjectField('group:' + groupName, 'memberCount', callback);
}; };
Groups.isMemberOfGroupList = function(uid, groupListKey, callback) { Groups.isMemberOfGroupList = function(uid, groupListKey, callback) {
@ -611,29 +611,45 @@ var async = require('async'),
}; };
Groups.join = function(groupName, uid, callback) { Groups.join = function(groupName, uid, callback) {
callback = callback || function() {}; function join() {
Groups.exists(groupName, function(err, exists) {
if (exists) {
var tasks = [ var tasks = [
async.apply(db.sortedSetAdd, 'group:' + groupName + ':members', Date.now(), uid) 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) { if (isAdmin) {
tasks.push(async.apply(db.setAdd, 'group:' + groupName + ':owners', uid)); tasks.push(async.apply(db.setAdd, 'group:' + groupName + ':owners', uid));
} }
async.parallel(tasks, next);
async.parallel(tasks, function(err) { }
], function(err, results) {
if (err) {
return callback(err);
}
plugins.fireHook('action:group.join', { plugins.fireHook('action:group.join', {
groupName: groupName, groupName: groupName,
uid: uid uid: uid
}); });
callback(); callback();
}); });
}); }
} else {
callback = callback || function() {};
Groups.exists(groupName, function(err, exists) {
if (err) {
return callback(err);
}
if (exists) {
return join();
}
Groups.create({ Groups.create({
name: groupName, name: groupName,
description: '', description: '',
@ -643,14 +659,8 @@ var async = require('async'),
winston.error('[groups.join] Could not create new hidden group: ' + err.message); winston.error('[groups.join] Could not create new hidden group: ' + err.message);
return callback(err); return callback(err);
} }
join();
db.sortedSetAdd('group:' + groupName + ':members', Date.now(), uid, callback);
plugins.fireHook('action:group.join', {
groupName: groupName,
uid: uid
});
}); });
}
}); });
}; };
@ -693,7 +703,8 @@ var async = require('async'),
var tasks = [ var tasks = [
async.apply(db.sortedSetRemove, 'group:' + groupName + ':members', uid), async.apply(db.sortedSetRemove, 'group:' + groupName + ':members', uid),
async.apply(db.setRemove, 'group:' + groupName + ':owners', uid) async.apply(db.setRemove, 'group:' + groupName + ':owners', uid),
async.apply(db.decrObjectField, 'group:' + groupName, 'memberCount')
]; ];
async.parallel(tasks, function(err) { async.parallel(tasks, function(err) {
@ -715,7 +726,7 @@ var async = require('async'),
if (group.hidden && group.memberCount === 0) { if (group.hidden && group.memberCount === 0) {
Groups.destroy(groupName, callback); Groups.destroy(groupName, callback);
} else { } else {
return callback(); callback();
} }
}); });
}); });
@ -781,25 +792,11 @@ var async = require('async'),
groupData = groupData.filter(function(group) { groupData = groupData.filter(function(group) {
return parseInt(group.hidden, 10) !== 1 && !!group.userTitle; 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); }
groupObj.memberCount = memberCount;
next(err, groupObj);
});
}, function(err, groupData) {
if (err) {
return callback(err);
}
var groupSets = groupData.map(function(group) { var groupSets = groupData.map(function(group) {
group.labelColor = group.labelColor || '#000000'; group.labelColor = group.labelColor || '#000000';
group.createtimeISO = utils.toISOString(group.createtime);
if (!group['cover:url']) { if (!group['cover:url']) {
group['cover:url'] = nconf.get('relative_path') + '/images/cover-default.png'; group['cover:url'] = nconf.get('relative_path') + '/images/cover-default.png';
@ -827,7 +824,6 @@ var async = require('async'),
}, callback); }, callback);
}); });
}); });
});
}; };
Groups.updateCoverPosition = function(groupName, position, callback) { Groups.updateCoverPosition = function(groupName, position, callback) {

@ -21,7 +21,7 @@ var db = require('./database'),
schemaDate, thisSchemaDate, schemaDate, thisSchemaDate,
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
latestSchema = Date.UTC(2015, 0, 21); latestSchema = Date.UTC(2015, 0, 30);
Upgrade.check = function(callback) { Upgrade.check = function(callback) {
db.get('schemaDate', function(err, value) { 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'); winston.info('[2015/01/21] Upgrading groups to sorted set skipped');
next(); 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 // Add new schema updates here

Loading…
Cancel
Save