dont load uid 0, -1 from db

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

@ -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);

@ -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);
}

@ -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 () {

Loading…
Cancel
Save