diff --git a/src/middleware/header.js b/src/middleware/header.js index be2e100044..9ccb085169 100644 --- a/src/middleware/header.js +++ b/src/middleware/header.js @@ -82,25 +82,10 @@ module.exports = function (middleware) { privileges.global.get(req.uid, next); }, user: function (next) { - var userData = { - uid: req.uid, - username: '[[global:guest]]', - userslug: '', - fullname: '[[global:guest]]', - email: '', - picture: user.getDefaultAvatar(), - status: 'offline', - reputation: 0, - 'email:confirmed': 0, - }; - if (req.loggedIn) { - user.getUserFields(req.uid, Object.keys(userData), next); - } else { - next(null, userData); - } + user.getUserData(req.uid, next); }, isEmailConfirmSent: function (next) { - if (!meta.config.requireEmailConfirmation || !req.uid) { + if (!meta.config.requireEmailConfirmation || req.uid <= 0) { return next(null, false); } db.get('uid:' + req.uid + ':confirm:email:sent', next); diff --git a/src/user/data.js b/src/user/data.js index ec3a51000f..22fb8e8df7 100644 --- a/src/user/data.js +++ b/src/user/data.js @@ -33,12 +33,26 @@ module.exports = function (User) { 'cover:position', 'groupTitle', ]; + User.guestData = { + uid: 0, + username: '[[global:guest]]', + userslug: '', + fullname: '[[global:guest]]', + email: '', + 'icon:text': '?', + 'icon:bgColor': '#aaa', + groupTitle: '', + status: 'offline', + reputation: 0, + 'email:confirmed': 0, + }; + User.getUsersFields = function (uids, fields, callback) { if (!Array.isArray(uids) || !uids.length) { return setImmediate(callback, null, []); } - uids = uids.map(uid => (isNaN(uid) ? 0 : uid)); + uids = uids.map(uid => (isNaN(uid) ? 0 : parseInt(uid, 10))); var fieldsToRemove = []; function addField(field) { @@ -60,7 +74,7 @@ module.exports = function (User) { addField('lastonline'); } - var uniqueUids = _.uniq(uids); + var uniqueUids = _.uniq(uids).filter(uid => uid > 0); async.waterfall([ function (next) { @@ -114,12 +128,12 @@ module.exports = function (User) { }; function uidsToUsers(uids, uniqueUids, usersData) { - var uidToUser = uniqueUids.reduce(function (memo, cur, idx) { - memo[cur] = usersData[idx]; + var uidToUser = uniqueUids.reduce(function (memo, uid, idx) { + memo[uid] = usersData[idx]; return memo; }, {}); var users = uids.map(function (uid) { - const returnPayload = uidToUser[uid]; + const returnPayload = uidToUser[uid] || _.clone(User.guestData); if (uid > 0 && !returnPayload.uid) { returnPayload.oldUid = parseInt(uid, 10); } diff --git a/test/user.js b/test/user.js index 387b038ba2..2137d6aec4 100644 --- a/test/user.js +++ b/test/user.js @@ -614,6 +614,16 @@ describe('User', function () { done(); }); }); + + it('should load guest data', function (done) { + User.getUsersData([1, 0], function (err, data) { + assert.ifError(err); + assert.strictEqual(data[1].username, '[[global:guest]]'); + assert.strictEqual(data[1].userslug, ''); + assert.strictEqual(data[1].uid, 0); + done(); + }); + }); }); describe('not logged in', function () {