'use strict'; var async = require('async'), winston = require('winston'), _ = require('underscore'), crypto = require('crypto'), path = require('path'), nconf = require('nconf'), fs = require('fs'), validator = require('validator'), user = require('./user'), meta = require('./meta'), db = require('./database'), plugins = require('./plugins'), posts = require('./posts'), privileges = require('./privileges'), utils = require('../public/src/utils'), uploadsController = require('./controllers/uploads'); (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; }/*, fixImageUrl: function(url) { if (url) { return url.indexOf('http') === -1 ? nconf.get('relative_path') + url : url; } }*/ }; 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, next); } 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.waterfall([ async.apply(async.map, uids, user.getUserData), function(users, next) { // Filter out non-matches users = users.filter(Boolean); async.mapLimit(users, 10, function(userObj, next) { Groups.ownership.isOwner(userObj.uid, groupName, function(err, isOwner) { if (err) { winston.warn('[groups.get] Could not determine ownership in group `' + groupName + '` for uid `' + userObj.uid + '`: ' + err.message); return next(null, userObj); } userObj.isOwner = isOwner; next(null, userObj); }); }, next); } ], next); } else { next(err, uids); } }); }, pending: function (next) { db.getSetMembers('group:' + groupName + ':pending', function (err, uids) { if (err) { return next(err); } if (options.expand) { async.map(uids, user.getUserData, next); } else { next(err, uids); } }); }, isMember: function(next) { // Retrieve group membership state, if uid is passed in if (!options.uid) { return next(); } Groups.isMember(options.uid, groupName, function(err, isMember) { if (err) { winston.warn('[groups.get] Could not determine membership in group `' + groupName + '` for uid `' + options.uid + '`: ' + err.message); return next(); } next(null, isMember); }); }, isPending: function(next) { // Retrieve group membership state, if uid is passed in if (!options.uid) { return next(); } db.isSetMember('group:' + groupName + ':pending', options.uid, next); }, isOwner: function(next) { // Retrieve group ownership state, if uid is passed in if (!options.uid) { return next(); } Groups.ownership.isOwner(options.uid, groupName, function(err, isOwner) { if (err) { winston.warn('[groups.get] Could not determine ownership in group `' + groupName + '` for uid `' + options.uid + '`: ' + err.message); return next(); } next(null, isOwner); }); } }, function (err, results) { if (err || !results.base) { return callback(err); } // Default image if (!results.base['cover:url']) { results.base['cover:url'] = nconf.get('relative_path') + '/images/cover-default.png'; results.base['cover:position'] = '50% 50%'; } results.base.name = validator.escape(results.base.name); results.base.description = validator.escape(results.base.description); results.base.userTitle = validator.escape(results.base.userTitle); results.base.members = results.users.filter(Boolean); results.base.pending = results.pending.filter(Boolean); results.base.count = numUsers || results.base.members.length; results.base.memberCount = numUsers || results.base.members.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.private = results.base.private ? !!parseInt(results.base.private, 10) : true; results.base.deletable = !results.base.system; results.base.truncated = truncated; results.base.isMember = results.isMember; results.base.isPending = results.isPending; results.base.isOwner = results.isOwner; callback(err, results.base); }); }; Groups.getGroupFields = function(groupName, fields, callback) { db.getObjectFields('group:' + groupName, fields, callback); }; Groups.setGroupField = function(groupName, field, value, callback) { plugins.fireHook('action:group.set', {field: field, value: value, type: 'set'}); db.setObjectField('group:' + groupName, field, value, callback); }; Groups.isPrivate = function(groupName, callback) { db.getObjectField('group:' + groupName, 'private', function(err, isPrivate) { isPrivate = isPrivate || isPrivate === null; if (typeof isPrivate === 'string') { isPrivate = (isPrivate === '0' ? false : true); } callback(err, isPrivate); // Private, if not set at all }); }; Groups.getMembers = function(groupName, callback) { db.getSetMembers('group:' + groupName + ':members', callback); }; Groups.search = function(query, options, callback) { if (!query) { return callback(null, []); } db.getSetMembers('groups', function(err, groups) { if (err) { return callback(err); } 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)); }); }); }; Groups.isMember = function(uid, groupName, callback) { if (!uid || parseInt(uid, 10) <= 0) { return callback(null, false); } 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) { if (!uid || parseInt(uid, 10) <= 0) { return callback(null, groups.map(function() {return false;})); } 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