dont load uid 0, -1 from db

v1.18.x
Barış Soner Uşaklı
parent d5af39ca5d
commit 9d28b935fd

@ -82,25 +82,10 @@ module.exports = function (middleware) {
privileges.global.get(req.uid, next); privileges.global.get(req.uid, next);
}, },
user: function (next) { user: function (next) {
var userData = { user.getUserData(req.uid, next);
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);
}
}, },
isEmailConfirmSent: function (next) { isEmailConfirmSent: function (next) {
if (!meta.config.requireEmailConfirmation || !req.uid) { if (!meta.config.requireEmailConfirmation || req.uid <= 0) {
return next(null, false); return next(null, false);
} }
db.get('uid:' + req.uid + ':confirm:email:sent', next); db.get('uid:' + req.uid + ':confirm:email:sent', next);

@ -33,12 +33,26 @@ module.exports = function (User) {
'cover:position', 'groupTitle', '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) { User.getUsersFields = function (uids, fields, callback) {
if (!Array.isArray(uids) || !uids.length) { if (!Array.isArray(uids) || !uids.length) {
return setImmediate(callback, null, []); return setImmediate(callback, null, []);
} }
uids = uids.map(uid => (isNaN(uid) ? 0 : uid)); uids = uids.map(uid => (isNaN(uid) ? 0 : parseInt(uid, 10)));
var fieldsToRemove = []; var fieldsToRemove = [];
function addField(field) { function addField(field) {
@ -60,7 +74,7 @@ module.exports = function (User) {
addField('lastonline'); addField('lastonline');
} }
var uniqueUids = _.uniq(uids); var uniqueUids = _.uniq(uids).filter(uid => uid > 0);
async.waterfall([ async.waterfall([
function (next) { function (next) {
@ -114,12 +128,12 @@ module.exports = function (User) {
}; };
function uidsToUsers(uids, uniqueUids, usersData) { function uidsToUsers(uids, uniqueUids, usersData) {
var uidToUser = uniqueUids.reduce(function (memo, cur, idx) { var uidToUser = uniqueUids.reduce(function (memo, uid, idx) {
memo[cur] = usersData[idx]; memo[uid] = usersData[idx];
return memo; return memo;
}, {}); }, {});
var users = uids.map(function (uid) { var users = uids.map(function (uid) {
const returnPayload = uidToUser[uid]; const returnPayload = uidToUser[uid] || _.clone(User.guestData);
if (uid > 0 && !returnPayload.uid) { if (uid > 0 && !returnPayload.uid) {
returnPayload.oldUid = parseInt(uid, 10); returnPayload.oldUid = parseInt(uid, 10);
} }

@ -614,6 +614,16 @@ describe('User', function () {
done(); 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 () { describe('not logged in', function () {

Loading…
Cancel
Save