var utils = require('./../public/src/utils.js'), RDB = require('./redis.js'), crypto = require('crypto'), emailjs = require('emailjs'), emailjsServer = emailjs.server.connect(config.mailer), bcrypt = require('bcrypt'), marked = require('marked'), notifications = require('./notifications.js'), topics = require('./topics.js'), async = require('async'); (function(User) { User.getUserField = function(uid, field, callback) { RDB.hget('user:' + uid, field, function(err, data) { if(err === null) { callback(data); } else { console.log(err); } }); } User.getUserFields = function(uid, fields, callback) { RDB.hmget('user:' + uid, fields, function(err, data) { if(err === null) { for(var i = 0, returnData = {}, ii=fields.length; i 150) { callback({error:'Signature can\'t be longer than 150 characters!'}); return; } for(var i = 0, key, ii = fields.length; i < ii; ++i) { key = fields[i]; if(data[key] !== undefined) { if(key === 'email') { User.setUserField(uid, 'gravatarpicture', User.createGravatarURLFromEmail(data[key])); RDB.set('email:' + data['email'] +':uid', uid); } else if(key === 'signature') { data[key] = utils.strip_tags(data[key]); } User.setUserField(uid, key, data[key]); } } callback({}); } User.setUserField = function(uid, field, value) { RDB.hset('user:' + uid, field, value); } User.incrementUserFieldBy = function(uid, field, value) { RDB.hincrby('user:' + uid, field, value); } User.getUserList = function(callback) { var data = []; RDB.keys('user:*', function(err, userkeys) { var anonUserIndex = userkeys.indexOf("user:0"); if(anonUserIndex !== -1) { userkeys.splice(anonUserIndex, 1); } for(var i=0,ii=userkeys.length; i= +Date.now()/1000|0) { if (!callback) { socket.emit('user:reset.valid', { valid: true }); } else { callback(true); } } else { // Expired, delete from db RDB.del('reset:' + code + ':uid'); RDB.del('reset:' + code + ':expiry'); if (!callback) { socket.emit('user:reset.valid', { valid: false }); } else { callback(false); } } }); } else { if (!callback) { socket.emit('user:reset.valid', { valid: false }); } else { callback(false); } } }); }, send: function(socket, email) { User.get_uid_by_email(email, function(uid) { if (uid !== null) { // Generate a new reset code var reset_code = utils.generateUUID(); RDB.set('reset:' + reset_code + ':uid', uid); RDB.set('reset:' + reset_code + ':expiry', (60*60)+new Date()/1000|0); // Active for one hour var reset_link = config.url + 'reset/' + reset_code, reset_email = global.templates['emails/reset'].parse({'RESET_LINK': reset_link}), reset_email_plaintext = global.templates['emails/reset_plaintext'].parse({ 'RESET_LINK': reset_link }); var message = emailjs.message.create({ text: reset_email_plaintext, from: config.mailer.from, to: email, subject: 'Password Reset Requested', attachment: [ { data: reset_email, alternative: true } ] }); emailjsServer.send(message, function(err, success) { if (err === null) { socket.emit('user.send_reset', { status: "ok", message: "code-sent", email: email }); } else { socket.emit('user.send_reset', { status: "error", message: "send-failed" }); // @todo handle error properly throw new Error(err); } }); } else { socket.emit('user.send_reset', { status: "error", message: "invalid-email", email: email }); } }); }, commit: function(socket, code, password) { this.validate(code, function(validated) { if (validated) { RDB.get('reset:' + code + ':uid', function(err, uid) { if (err) { RDB.handle(err); } User.setUserField(uid, 'password', password); RDB.del('reset:' + code + ':uid'); RDB.del('reset:' + code + ':expiry'); socket.emit('user:reset.commit', { status: 'ok' }); }); } }); } } User.email = { exists: function(socket, email, callback) { User.get_uid_by_email(email, function(exists) { exists = !!exists; if (typeof callback !== 'function') { socket.emit('user.email.exists', { exists: exists }); } else { callback(exists); } }); }, confirm: function(code, callback) { RDB.get('confirm:' + code + ':email', function(err, email) { if (err) { RDB.handle(err); } if (email !== null) { RDB.set('email:' + email + ':confirm', true); RDB.del('confirm:' + code + ':email'); callback({ status: 'ok' }); } else { callback({ status: 'not_ok' }); } }); } }; User.get_online_users = function(socket, uids) { RDB.sismembers('users:online', uids, function(err, data) { // @todo handle err socket.emit('api:user.get_online_users', data); }); }; User.go_online = function(uid) { RDB.sadd('users:online', uid, function(err) { if (err) { RDB.handle(err); } }); }; User.go_offline = function(uid) { RDB.srem('users:online', uid, function(err) { if (err) { RDB.handle(err); } }); }; User.active = { get_record : function(socket) { RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(err, data) { RDB.handle(err); socket.emit('api:user.active.get_record', { record: data[0], timestamp: data[1] }); }); }, get: function(callback) { function user_record(total) { RDB.get('global:active_user_record', function(err, record) { RDB.handle(err); if (total > record) { RDB.set('global:active_user_record', total); RDB.set('global:active_user_record_date', Date.now()); } }); } RDB.keys('active:*', function(err, active) { RDB.handle(err); var returnObj = { users: 0, anon: 0, uids: [] }, keys = []; if (active.length > 0) { for(var a in active) { keys.push('sess:' + active[a].split(':')[1] + ':uid'); } RDB.mget(keys, function(err, uids) { RDB.handle(err); for(var u in uids) { if (uids[u] !== null) { if (returnObj.uids.indexOf(uids[u]) === -1) { returnObj.users++; returnObj.uids.push(uids[u]); } } else { returnObj.anon++; } } user_record(returnObj.anon + returnObj.users); if (callback === undefined) { io.sockets.emit('api:user.active.get', returnObj) } else { callback(returnObj); } }); } else { io.sockets.emit('api:user.active.get', returnObj) } }); }, register: function(sessionID) { // Active state persists for 10 minutes var active_session = 'active:' + sessionID; RDB.set(active_session, ''); RDB.expire(active_session, 60*10) this.get(); } } User.notifications = { get: function(uid, callback) { async.parallel({ unread: function(next) { RDB.zrevrangebyscore('uid:' + uid + ':notifications:unread', 10, 0, function(err, nids) { // @todo handle err var unread = []; if (nids && nids.length > 0) { async.eachSeries(nids, function(nid, next) { notifications.get(nid, function(notif_data) { unread.push(notif_data); next(); }); }, function(err) { next(null, unread); }); } else { next(null, unread); } }); }, read: function(next) { RDB.zrevrangebyscore('uid:' + uid + ':notifications:read', 10, 0, function(err, nids) { // @todo handle err var read = []; if (nids && nids.length > 0) { async.eachSeries(nids, function(nid, next) { notifications.get(nid, function(notif_data) { read.push(notif_data); next(); }); }, function(err) { next(null, read); }); } else { next(null, read); } }); } }, function(err, notifications) { // While maintaining score sorting, sort by time notifications.read.sort(function(a, b) { if (a.score === b.score) { return (a.datetime - b.datetime) > 0 ? -1 : 1; } }); notifications.unread.sort(function(a, b) { if (a.score === b.score) { return (a.datetime - b.datetime) > 0 ? -1 : 1; } }); callback(notifications); }); }, hasFlag: function(uid, callback) { RDB.get('uid:1:notifications:flag', function(err, flag) { if (err) { RDB.handle(err); } callback(flag === 1); }); }, removeFlag: function(uid) { RDB.del('uid:' + uid + ':notifications:flag', function(err) { if (err) { RDB.handle(err); } }); } } }(exports));