'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'), util = require('util'), 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 || group.isMember || options.isAdmin || group.isInvited)) || (!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) { return { 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; }, isPrivilegeGroup: /^cid:\d+:privileges:[\w:]+$/ }; Groups.list = function(options, callback) { db.getSortedSetRevRange('groups:createtime', 0, -1, function (err, groupNames) { if (err) { return callback(err); } groupNames = groupNames.concat(ephemeralGroups); async.parallel({ groups: async.apply(async.map, groupNames, function (groupName, next) { Groups.get(groupName, options, next); }), isAdmin: function(next) { if (!options.uid || parseInt(options.uid, 10) === 0) { return next(null, false); } user.isAdministrator(parseInt(options.uid, 10), next); } }, function (err, data) { options.isAdmin = options.isAdmin || data.isAdmin; callback(err, internals.filterGroups(data.groups, options)); }); }); }; Groups.getGroups = function(start, end, callback) { db.getSortedSetRevRange('groups:createtime', start, end, callback); }; 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 { next(null, internals.getEphemeralGroup(groupName)); } }, users: function (next) { db.getSortedSetRevRange('group:' + groupName + ':members', 0, -1, function (err, uids) { if (err) { return next(err); } if (options.truncateUserList) { var userListCount = parseInt(options.userListCount, 10) || 4; if (uids.length > userListCount) { numUsers = uids.length; uids.length = userListCount; 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); }); }, function(err, users) { if (err) { return next(); } next(null, users.sort(function(a, b) { if (a.isOwner === b.isOwner) { return 0; } else { return a.isOwner && !b.isOwner ? -1 : 1; } })); }); } ], next); } else { next(err, uids); } }); }, pending: function (next) { db.getSetMembers('group:' + groupName + ':pending', function (err, uids) { if (err) { return next(err); } if (options.expand && uids.length) { 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); }, isInvited: async.apply(Groups.isInvited, options.uid, groupName), 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) { return callback(err); } else if (!results.base) { return callback(new Error('[[error:no-group]]')); } // 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%'; } plugins.fireHook('filter:parse.raw', results.base.description, function(err, descriptionParsed) { if (err) { return callback(err); } results.base.name = !options.unescape ? validator.escape(results.base.name) : results.base.name; results.base.description = options.unescape ? validator.escape(results.base.description) : results.base.description; results.base.descriptionParsed = descriptionParsed; results.base.userTitle = options.unescape ? validator.escape(results.base.userTitle) : results.base.userTitle; results.base.userTitleEnabled = results.base.userTitleEnabled ? !!parseInt(results.base.userTitleEnabled, 10) : true; results.base.createtimeISO = utils.toISOString(results.base.createtime); 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.isInvited = results.isInvited; results.base.isOwner = results.isOwner; plugins.fireHook('filter:group.get', {group: results.base}, function(err, data) { callback(err, data ? data.group : null); }); }); }); }; Groups.getByGroupslug = function(slug, options, callback) { db.getObjectField('groupslug:groupname', slug, function(err, groupName) { if (err) { return callback(err); } else if (!groupName) { return callback(new Error('[[error:no-group]]')); } Groups.get.call(Groups, groupName, options, callback); }); }; Groups.getGroupNameByGroupSlug = function(slug, callback) { db.getObjectField('groupslug:groupname', slug, callback); }; 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.isHidden = function(groupName, callback) { Groups.getGroupFields(groupName, ['hidden'], function(err, values) { if (err) { winston.warn('[groups.isHidden] Could not determine group hidden state (group: ' + groupName + ')'); return callback(null, true); // Default true } callback(null, parseInt(values.hidden, 10)); }); }; Groups.getMembers = function(groupName, start, end, callback) { db.getSortedSetRevRange('group:' + groupName + ':members', start, end, callback); }; Groups.isMember = function(uid, groupName, callback) { if (!uid || parseInt(uid, 10) <= 0) { return callback(null, false); } db.isSortedSetMember('group:' + groupName + ':members', uid, callback); }; Groups.isMembers = function(uids, groupName, callback) { db.isSortedSetMembers('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.isMemberOfSortedSets(groups, uid, callback); }; Groups.getMemberCount = function(groupName, callback) { db.getObjectField('group:' + groupName, 'memberCount', callback); }; Groups.isMemberOfGroupList = function(uid, groupListKey, callback) { db.getSortedSetRange('group:' + groupListKey + ':members', 0, -1, 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.getSortedSetsMembers(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