diff --git a/src/groups.js b/src/groups.js index fe832b4e83..2564ddd7dd 100644 --- a/src/groups.js +++ b/src/groups.js @@ -1,7 +1,6 @@ 'use strict'; var async = require('async'); -var validator = require('validator'); var user = require('./user'); var db = require('./database'); @@ -114,7 +113,7 @@ Groups.get = function (groupName, options, callback) { function (next) { async.parallel({ base: function (next) { - db.getObject('group:' + groupName, next); + Groups.getGroupData(groupName, next); }, members: function (next) { if (options.truncateUserList) { @@ -144,29 +143,17 @@ Groups.get = function (groupName, options, callback) { }, function (descriptionParsed, next) { var groupData = results.base; - Groups.escapeGroupData(groupData); groupData.descriptionParsed = descriptionParsed; - groupData.userTitleEnabled = groupData.userTitleEnabled ? parseInt(groupData.userTitleEnabled, 10) === 1 : true; - groupData.createtimeISO = utils.toISOString(groupData.createtime); groupData.members = results.members; groupData.membersNextStart = stop + 1; groupData.pending = results.pending.filter(Boolean); groupData.invited = results.invited.filter(Boolean); - groupData.deleted = !!parseInt(groupData.deleted, 10); - groupData.hidden = !!parseInt(groupData.hidden, 10); - groupData.system = !!parseInt(groupData.system, 10); - groupData.memberCount = parseInt(groupData.memberCount, 10); - groupData.private = (groupData.private === null || groupData.private === undefined) ? true : !!parseInt(groupData.private, 10); - groupData.disableJoinRequests = parseInt(groupData.disableJoinRequests, 10) === 1; + groupData.isMember = results.isMember; groupData.isPending = results.isPending; groupData.isInvited = results.isInvited; groupData.isOwner = results.isOwner; - groupData['cover:url'] = groupData['cover:url'] || require('./coverPhoto').getDefaultGroupCover(groupName); - groupData['cover:position'] = validator.escape(String(groupData['cover:position'] || '50% 50%')); - groupData.labelColor = validator.escape(String(groupData.labelColor || '#000000')); - groupData.icon = validator.escape(String(groupData.icon || '')); plugins.fireHook('filter:group.get', { group: groupData }, next); }, @@ -218,15 +205,6 @@ Groups.getOwnersAndMembers = function (groupName, uid, start, stop, callback) { ], callback); }; -Groups.escapeGroupData = function (group) { - if (group) { - group.nameEncoded = encodeURIComponent(group.name); - group.displayName = validator.escape(String(group.name)); - group.description = validator.escape(String(group.description || '')); - group.userTitle = validator.escape(String(group.userTitle || '')) || group.displayName; - } -}; - Groups.getByGroupslug = function (slug, options, callback) { async.waterfall([ function (next) { diff --git a/src/groups/create.js b/src/groups/create.js index 3645ea4b2f..a5b60e68b5 100644 --- a/src/groups/create.js +++ b/src/groups/create.js @@ -36,9 +36,9 @@ module.exports = function (Groups) { slug: slug, createtime: timestamp, userTitle: data.userTitle || data.name, + userTitleEnabled: parseInt(data.userTitleEnabled, 10) === 1 ? 1 : 0, description: data.description || '', memberCount: memberCount, - deleted: 0, hidden: isHidden ? 1 : 0, system: isSystem ? 1 : 0, private: isPrivate, diff --git a/src/groups/data.js b/src/groups/data.js index bd15bd6b72..2bf0418891 100644 --- a/src/groups/data.js +++ b/src/groups/data.js @@ -7,16 +7,17 @@ var db = require('../database'); var plugins = require('../plugins'); var utils = require('../utils'); +const intFields = [ + 'createtime', 'memberCount', 'hidden', 'system', 'private', + 'userTitleEnabled', 'disableJoinRequests', +]; + module.exports = function (Groups) { - Groups.getGroupsData = function (groupNames, callback) { + Groups.getGroupsFields = function (groupNames, fields, callback) { if (!Array.isArray(groupNames) || !groupNames.length) { return callback(null, []); } - var keys = groupNames.map(function (groupName) { - return 'group:' + groupName; - }); - var ephemeralIdx = groupNames.reduce(function (memo, cur, idx) { if (Groups.ephemeralGroups.includes(cur)) { memo.push(idx); @@ -26,7 +27,12 @@ module.exports = function (Groups) { async.waterfall([ function (next) { - db.getObjects(keys, next); + const keys = groupNames.map(groupName => 'group:' + groupName); + if (fields.length) { + db.getObjectsFields(keys, fields, callback); + } else { + db.getObjects(keys, next); + } }, function (groupData, next) { if (ephemeralIdx.length) { @@ -35,23 +41,7 @@ module.exports = function (Groups) { }); } - groupData.forEach(function (group) { - if (group) { - Groups.escapeGroupData(group); - group.userTitleEnabled = group.userTitleEnabled ? parseInt(group.userTitleEnabled, 10) === 1 : true; - group.labelColor = validator.escape(String(group.labelColor || '#000000')); - group.icon = validator.escape(String(group.icon || '')); - group.createtimeISO = utils.toISOString(group.createtime); - group.hidden = parseInt(group.hidden, 10) === 1; - group.system = parseInt(group.system, 10) === 1; - group.private = (group.private === null || group.private === undefined) ? true : !!parseInt(group.private, 10); - group.disableJoinRequests = parseInt(group.disableJoinRequests, 10) === 1; - - group['cover:url'] = group['cover:url'] || require('../coverPhoto').getDefaultGroupCover(group.name); - group['cover:thumb:url'] = group['cover:thumb:url'] || group['cover:url']; - group['cover:position'] = validator.escape(String(group['cover:position'] || '50% 50%')); - } - }); + groupData.forEach(modifyGroup); plugins.fireHook('filter:groups.get', { groups: groupData }, next); }, @@ -61,6 +51,10 @@ module.exports = function (Groups) { ], callback); }; + Groups.getGroupsData = function (groupNames, callback) { + Groups.getGroupsFields(groupNames, [], callback); + }; + Groups.getGroupData = function (groupName, callback) { Groups.getGroupsData([groupName], function (err, groupsData) { callback(err, Array.isArray(groupsData) && groupsData[0] ? groupsData[0] : null); @@ -73,12 +67,6 @@ module.exports = function (Groups) { }); }; - Groups.getGroupsFields = function (groupNames, fields, callback) { - db.getObjectsFields(groupNames.map(function (group) { - return 'group:' + group; - }), fields, callback); - }; - Groups.setGroupField = function (groupName, field, value, callback) { async.waterfall([ function (next) { @@ -91,3 +79,28 @@ module.exports = function (Groups) { ], callback); }; }; + +function modifyGroup(group) { + if (group) { + intFields.forEach(field => db.parseIntField(group, field)); + escapeGroupData(group); + group.userTitleEnabled = ([null, undefined].includes(group.userTitleEnabled)) ? 1 : group.userTitleEnabled; + group.labelColor = validator.escape(String(group.labelColor || '#000000')); + group.icon = validator.escape(String(group.icon || '')); + group.createtimeISO = utils.toISOString(group.createtime); + group.private = ([null, undefined].includes(group.private)) ? 1 : group.private; + + group['cover:url'] = group['cover:url'] || require('../coverPhoto').getDefaultGroupCover(group.name); + group['cover:thumb:url'] = group['cover:thumb:url'] || group['cover:url']; + group['cover:position'] = validator.escape(String(group['cover:position'] || '50% 50%')); + } +} + +function escapeGroupData(group) { + if (group) { + group.nameEncoded = encodeURIComponent(group.name); + group.displayName = validator.escape(String(group.name)); + group.description = validator.escape(String(group.description || '')); + group.userTitle = validator.escape(String(group.userTitle || '')) || group.displayName; + } +} diff --git a/test/groups.js b/test/groups.js index 4799b45f91..fe630d9d65 100644 --- a/test/groups.js +++ b/test/groups.js @@ -333,6 +333,20 @@ describe('Groups', function () { done(); }); }); + + it('should return falsy for userTitleEnabled', function (done) { + Groups.create({ name: 'userTitleEnabledGroup' }, function (err) { + assert.ifError(err); + Groups.setGroupField('userTitleEnabledGroup', 'userTitleEnabled', 0, function (err) { + assert.ifError(err); + Groups.getGroupData('userTitleEnabledGroup', function (err, data) { + assert.ifError(err); + assert.strictEqual(data.userTitleEnabled, 0); + done(); + }); + }); + }); + }); }); describe('.hide()', function () { @@ -342,7 +356,7 @@ describe('Groups', function () { Groups.get('foo', {}, function (err, groupObj) { assert.ifError(err); - assert.strictEqual(true, groupObj.hidden); + assert.strictEqual(1, groupObj.hidden); done(); }); });