'use strict'; var async = require('async'), winston = require('winston'), _ = require('underscore'), user = require('./user'), meta = require('./meta'), db = require('./database'), plugins = require('./plugins'), posts = require('./posts'), utils = require('../public/src/utils'); (function(Groups) { var ephemeralGroups = ['guests'], internals = { filterGroups: function(groups, options) { // Remove system, hidden, or deleted groups from this list if (groups && !options.showAllGroups) { return groups.filter(function (group) { if (!group) { return false; } if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) { return false; } else if (options.removeEphemeralGroups && ephemeralGroups.indexOf(group.name) !== -1) { return false; } else { return true; } }); } else { return groups; } }, getEphemeralGroup: function(groupName, options, callback) { Groups.exists(groupName, function(err, exists) { if (!err && exists) { Groups.get.apply(null, arguments); } else { callback(null, { name: groupName, description: '', deleted: '0', hidden: '0', system: '1' }); } }); }, removeEphemeralGroups: function(groups) { var x = groups.length; while(x--) { if (ephemeralGroups.indexOf(groups[x]) !== -1) { groups.splice(x, 1); } } return groups; } }; Groups.list = function(options, callback) { db.getSetMembers('groups', function (err, groupNames) { if (err) { return callback(err); } groupNames = groupNames.concat(ephemeralGroups); async.map(groupNames, function (groupName, next) { Groups.get(groupName, options, next); }, function (err, groups) { callback(err, internals.filterGroups(groups, options)); }); }); }; Groups.get = function(groupName, options, callback) { var truncated = false, numUsers; async.parallel({ base: function (next) { if (ephemeralGroups.indexOf(groupName) === -1) { db.getObject('group:' + groupName, function(err, groupObj) { if (err) { next(err); } else if (!groupObj) { next('group-not-found'); } else { next(err, groupObj); } }); } else { internals.getEphemeralGroup(groupName, options, next); } }, users: function (next) { db.getSetMembers('group:' + groupName + ':members', function (err, uids) { if (err) { return next(err); } if (options.truncateUserList) { if (uids.length > 4) { numUsers = uids.length; uids.length = 4; truncated = true; } } if (options.expand) { async.map(uids, user.getUserData, next); } else { next(err, uids); } }); } }, function (err, results) { if (err) { return callback(err); } // User counts results.base.count = numUsers || results.users.length; results.base.members = results.users; results.base.memberCount = numUsers || results.users.length; results.base.deleted = !!parseInt(results.base.deleted, 10); results.base.hidden = !!parseInt(results.base.hidden, 10); results.base.system = !!parseInt(results.base.system, 10); results.base.deletable = !results.base.system; results.base.truncated = truncated; callback(err, results.base); }); }; Groups.search = function(query, options, callback) { if (query.length) { db.getSetMembers('groups', function(err, groups) { groups = groups.filter(function(groupName) { return groupName.match(new RegExp(utils.escapeRegexChars(query), 'i')); }); async.map(groups, function(groupName, next) { Groups.get(groupName, options, next); }, function(err, groups) { callback(err, internals.filterGroups(groups, options)); }); }); } else { callback(null, []); } }; Groups.isMember = function(uid, groupName, callback) { db.isSetMember('group:' + groupName + ':members', uid, callback); }; Groups.isMembers = function(uids, groupName, callback) { db.isSetMembers('group:' + groupName + ':members', uids, callback); }; Groups.isMemberOfGroups = function(uid, groups, callback) { groups = groups.map(function(groupName) { return 'group:' + groupName + ':members'; }); db.isMemberOfSets(groups, uid, callback); }; Groups.getMemberCount = function(groupName, callback) { db.setCount('group:' + groupName + ':members', callback); }; Groups.isMemberOfGroupList = function(uid, groupListKey, callback) { db.getSetMembers('group:' + groupListKey + ':members', function(err, groupNames) { if (err) { return callback(err); } groupNames = internals.removeEphemeralGroups(groupNames); if (groupNames.length === 0) { return callback(null, null); } Groups.isMemberOfGroups(uid, groupNames, function(err, isMembers) { if (err) { return callback(err); } callback(null, isMembers.indexOf(true) !== -1); }); }); }; Groups.isMemberOfGroupsList = function(uid, groupListKeys, callback) { var sets = groupListKeys.map(function(groupName) { return 'group:' + groupName + ':members'; }); db.getSetsMembers(sets, function(err, members) { if (err) { return callback(err); } var uniqueGroups = _.unique(_.flatten(members)); uniqueGroups = internals.removeEphemeralGroups(uniqueGroups); Groups.isMemberOfGroups(uid, uniqueGroups, function(err, isMembers) { if (err) { return callback(err); } var map = {}; uniqueGroups.forEach(function(groupName, index) { map[groupName] = isMembers[index]; }); var result = members.map(function(groupNames) { for (var i=0; i