You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/user.js

248 lines
6.6 KiB
JavaScript

11 years ago
'use strict';
10 years ago
var async = require('async'),
11 years ago
plugins = require('./plugins'),
11 years ago
db = require('./database'),
10 years ago
topics = require('./topics'),
9 years ago
privileges = require('./privileges'),
utils = require('../public/src/utils');
11 years ago
(function(User) {
11 years ago
User.email = require('./user/email');
User.notifications = require('./user/notifications');
User.reset = require('./user/reset');
10 years ago
User.digest = require('./user/digest');
9 years ago
require('./user/data')(User);
11 years ago
require('./user/auth')(User);
require('./user/create')(User);
require('./user/posts')(User);
9 years ago
require('./user/categories')(User);
require('./user/follow')(User);
require('./user/profile')(User);
require('./user/admin')(User);
require('./user/delete')(User);
require('./user/settings')(User);
require('./user/search')(User);
require('./user/jobs')(User);
10 years ago
require('./user/picture')(User);
10 years ago
require('./user/approval')(User);
10 years ago
require('./user/invite')(User);
9 years ago
require('./user/password')(User);
User.updateLastOnlineTime = function(uid, callback) {
11 years ago
callback = callback || function() {};
10 years ago
User.getUserFields(uid, ['status', 'lastonline'], function(err, userData) {
var now = Date.now();
if (err || userData.status === 'offline' || now - parseInt(userData.lastonline, 10) < 300000) {
11 years ago
return callback(err);
}
User.setUserField(uid, 'lastonline', now, callback);
});
};
User.updateOnlineUsers = function(uid, callback) {
callback = callback || function() {};
10 years ago
var now = Date.now();
async.waterfall([
function(next) {
db.sortedSetScore('users:online', uid, next);
},
function(userOnlineTime, next) {
if (now - parseInt(userOnlineTime, 10) < 300000) {
return callback();
}
db.sortedSetAdd('users:online', now, uid, next);
10 years ago
},
function(next) {
topics.pushUnreadCount(uid);
plugins.fireHook('action:user.online', {uid: uid, timestamp: now});
10 years ago
next();
}
], callback);
10 years ago
};
User.getUidsFromSet = function(set, start, stop, callback) {
if (set === 'users:online') {
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
var now = Date.now();
10 years ago
db.getSortedSetRevRangeByScore(set, start, count, now, now - 300000, callback);
} else {
10 years ago
db.getSortedSetRevRange(set, start, stop, callback);
}
};
User.getUsersFromSet = function(set, uid, start, stop, callback) {
11 years ago
async.waterfall([
function(next) {
User.getUidsFromSet(set, start, stop, next);
11 years ago
},
function(uids, next) {
User.getUsers(uids, uid, next);
11 years ago
}
], callback);
};
User.getUsers = function(uids, uid, callback) {
var fields = ['uid', 'username', 'userslug', 'picture', 'status', 'banned', 'joindate', 'postcount', 'reputation', 'email:confirmed', 'lastonline'];
9 years ago
async.waterfall([
function (next) {
plugins.fireHook('filter:users.addFields', {fields: fields}, next);
},
function (data, next) {
data.fields = data.fields.filter(function(field, index, array) {
return array.indexOf(field) === index;
});
9 years ago
async.parallel({
userData: function(next) {
User.getUsersFields(uids, data.fields, next);
},
isAdmin: function(next) {
User.isAdministrator(uids, next);
}
}, next);
},
function (results, next) {
results.userData.forEach(function(user, index) {
if (user) {
user.status = User.getStatus(user);
user.joindateISO = utils.toISOString(user.joindate);
user.administrator = results.isAdmin[index];
user.banned = parseInt(user.banned, 10) === 1;
user['email:confirmed'] = parseInt(user['email:confirmed'], 10) === 1;
user.lastonlineISO = utils.toISOString(user.lastonline);
}
});
9 years ago
plugins.fireHook('filter:userlist.get', {users: results.userData, uid: uid}, next);
},
function (data, next) {
next(null, data.users);
}
], callback);
};
User.getStatus = function(userData) {
var isOnline = Date.now() - parseInt(userData.lastonline, 10) < 300000;
return isOnline ? (userData.status || 'online') : 'offline';
};
User.isOnline = function(uid, callback) {
if (Array.isArray(uid)) {
db.sortedSetScores('users:online', uid, function(err, lastonline) {
if (err) {
return callback(err);
}
var now = Date.now();
var isOnline = uid.map(function(uid, index) {
return now - lastonline[index] < 300000;
});
callback(null, isOnline);
});
} else {
db.sortedSetScore('users:online', uid, function(err, lastonline) {
if (err) {
return callback(err);
}
var isOnline = Date.now() - parseInt(lastonline, 10) < 300000;
callback(null, isOnline);
});
}
};
User.exists = function(uid, callback) {
db.isSortedSetMember('users:joindate', uid, callback);
};
User.existsBySlug = function(userslug, callback) {
User.getUidByUserslug(userslug, function(err, exists) {
11 years ago
callback(err, !! exists);
});
9 years ago
};
User.getUidByUsername = function(username, callback) {
if (!username) {
10 years ago
return callback(null, 0);
}
db.sortedSetScore('username:uid', username, callback);
};
User.getUidsByUsernames = function(usernames, callback) {
db.sortedSetScores('username:uid', usernames, callback);
};
User.getUidByUserslug = function(userslug, callback) {
if (!userslug) {
10 years ago
return callback(null, 0);
}
db.sortedSetScore('userslug:uid', userslug, callback);
};
User.getUsernamesByUids = function(uids, callback) {
9 years ago
User.getUsersFields(uids, ['username'], function(err, users) {
if (err) {
return callback(err);
}
users = users.map(function(user) {
return user.username;
});
callback(null, users);
});
};
User.getUsernameByUserslug = function(slug, callback) {
async.waterfall([
function(next) {
User.getUidByUserslug(slug, next);
},
function(uid, next) {
User.getUserField(uid, 'username', next);
}
], callback);
};
User.getUidByEmail = function(email, callback) {
db.sortedSetScore('email:uid', email.toLowerCase(), callback);
};
11 years ago
User.getUsernameByEmail = function(email, callback) {
db.sortedSetScore('email:uid', email.toLowerCase(), function(err, uid) {
if (err) {
return callback(err);
}
11 years ago
User.getUserField(uid, 'username', callback);
});
};
User.isModerator = function(uid, cid, callback) {
9 years ago
privileges.users.isModerator(uid, cid, callback);
};
User.isAdministrator = function(uid, callback) {
9 years ago
privileges.users.isAdministrator(uid, callback);
11 years ago
};
9 years ago
User.isAdminOrSelf = function(callerUid, uid, callback) {
if (parseInt(callerUid, 10) === parseInt(uid, 10)) {
return callback();
}
User.isAdministrator(callerUid, function(err, isAdmin) {
if (err || !isAdmin) {
return callback(err || new Error('[[error:no-privileges]]'));
}
callback();
});
};
11 years ago
}(exports));
10 years ago