From c9308efbec6877963101177ce9548942737affed Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Mon, 2 Dec 2013 17:10:26 -0500 Subject: [PATCH] more work --- app.js | 33 +++++++++++++++++---------------- src/database/redis.js | 17 +++++++++++++++-- src/user.js | 39 ++++++++++++++++++++++----------------- src/webserver.js | 35 +++++++++++++++++++++++------------ 4 files changed, 77 insertions(+), 47 deletions(-) diff --git a/app.js b/app.js index 7507ee4ac1..b1e27a6c9b 100644 --- a/app.js +++ b/app.js @@ -74,25 +74,26 @@ } meta.configs.init(function () { - // Initial setup for Redis & Reds - var reds = require('reds'), - RDB = require('./src/redis.js'); - reds.createClient = function () { - return reds.client || (reds.client = RDB); - }; - - var templates = require('./public/src/templates.js'), - translator = require('./public/src/translator.js'), - webserver = require('./src/webserver.js'), + // + // TODO : figure out reds search after dbal is complete + // + //var reds = require('reds'), + // db = require('./src/database'); + /*reds.createClient = function () { + return reds.client || (reds.client = db); + };*/ + + var templates = require('./public/src/templates'), + translator = require('./public/src/translator'), + webserver = require('./src/webserver'), SocketIO = require('socket.io').listen(global.server, { log: false, transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket'], 'browser client minification': true}), - websockets = require('./src/websockets.js'), - posts = require('./src/posts.js'), + websockets = require('./src/websockets'), plugins = require('./src/plugins'), // Don't remove this - plugins initializes itself - Notifications = require('./src/notifications'), - Upgrade = require('./src/upgrade'); + notifications = require('./src/notifications'), + upgrade = require('./src/upgrade'); - Upgrade.check(function(schema_ok) { + upgrade.check(function(schema_ok) { if (schema_ok || nconf.get('check-schema') === false) { websockets.init(SocketIO); @@ -117,7 +118,7 @@ templates.ready(webserver.init); }); - Notifications.init(); + notifications.init(); } else { winston.warn('Your NodeBB schema is out-of-date. Please run the following command to bring your dataset up to spec:'); winston.warn(' node app --upgrade'); diff --git a/src/database/redis.js b/src/database/redis.js index 827491966f..0f7fa840c4 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -111,10 +111,13 @@ redisClient.hdel(key, field, callback); } - module.incrObjectField = function(key, field, value, callback) { - redisClient.hincrby(key, field, value, callback); + module.incrObjectField = function(key, field, callback) { + redisClient.hincrby(key, field, 1, callback); } + module.incrObjectFieldBy = function(key, field, value, callback) { + redisClient.hincrby(key, field, value, callback); + } module.setAdd = function(key, value, callback) { redisClient.sadd(key, value, callback); @@ -132,6 +135,16 @@ redisClient.smembers(key, callback); } + module.sortedSetAdd = function(key, score, value, callback) { + redisClient.zadd(key, score, value, callback); + } + + module.sortedSetRemove = function(key, value, callback) { + redisClient.zrem(key, value, callback); + } + + + }(exports)); diff --git a/src/user.js b/src/user.js index bfb312db91..92ba8ac42e 100644 --- a/src/user.js +++ b/src/user.js @@ -10,7 +10,7 @@ var bcrypt = require('bcrypt'), utils = require('./../public/src/utils'), plugins = require('./plugins'), - RDB = require('./redis'), + db = require('./database'), meta = require('./meta'), emailjsServer = emailjs.server.connect(meta.config['email:smtp:host'] || '127.0.0.1'), Groups = require('./groups'), @@ -65,16 +65,18 @@ var bcrypt = require('bcrypt'), } ], function(err, results) { if (err) { - return callback(err, null); + return callback(err); } - RDB.incr('global:next_user_id', function(err, uid) { - RDB.handle(err); + db.incrObjectField('global', 'nextUserId', function(err, uid) { + if(err) { + return callback(err); + } var gravatar = User.createGravatarURLFromEmail(email); var timestamp = Date.now(); - RDB.hmset('user:' + uid, { + db.setObject('user:' + uid, { 'uid': uid, 'username': username, 'userslug': userslug, @@ -96,20 +98,20 @@ var bcrypt = require('bcrypt'), 'showemail': 0 }); - RDB.hset('username:uid', username, uid); - RDB.hset('userslug:uid', userslug, uid); + db.setObjectField('username:uid', username, uid); + db.setObjectField('userslug:uid', userslug, uid); if (email !== undefined) { - RDB.hset('email:uid', email, uid); + db.setObjectField('email:uid', email, uid); User.sendConfirmationEmail(email); } plugins.fireHook('action:user.create', {uid: uid, username: username, email: email, picture: gravatar, timestamp: timestamp}); - RDB.incr('usercount'); + db.incrObjectField('global', 'usercount'); - RDB.zadd('users:joindate', timestamp, uid); - RDB.zadd('users:postcount', 0, uid); - RDB.zadd('users:reputation', 0, uid); + db.sortedSetAdd('users:joindate', timestamp, uid); + db.sortedSetAdd('users:postcount', 0, uid); + db.sortedSetAdd('users:reputation', 0, uid); userSearch.index(username, uid); @@ -134,11 +136,11 @@ var bcrypt = require('bcrypt'), }; User.getUserField = function(uid, field, callback) { - RDB.hget('user:' + uid, field, callback); + db.getObjectField('user:' + uid, field, callback); }; User.getUserFields = function(uid, fields, callback) { - RDB.hmgetObject('user:' + uid, fields, callback); + db.getObjectFields('user:' + uid, fields, callback); }; User.getMultipleUserFields = function(uids, fields, callback) { @@ -168,7 +170,10 @@ var bcrypt = require('bcrypt'), }; User.getUserData = function(uid, callback) { - RDB.hgetall('user:' + uid, function(err, data) { + db.getObject('user:' + uid, function(err, data) { + if(err) { + return callback(err); + } if (data && data.password) { delete data.password; @@ -253,8 +258,8 @@ var bcrypt = require('bcrypt'), return next(err); } - RDB.hdel('email:uid', userData.email); - RDB.hset('email:uid', data.email, uid); + db.deleteObjectField('email:uid', userData.email); + db.setObjectField('email:uid', data.email, uid); User.setUserField(uid, field, data[field]); if (userData.picture !== userData.uploadedpicture) { returnData.picture = gravatarpicture; diff --git a/src/webserver.js b/src/webserver.js index 716bd5dcfe..60c5e87e62 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -14,7 +14,7 @@ var path = require('path'), pkg = require('../package.json'), utils = require('../public/src/utils'), - RDB = require('./redis'), + db = require('./database'), user = require('./user'), categories = require('./categories'), posts = require('./posts'), @@ -140,7 +140,9 @@ var path = require('path'), })); app.use(express.bodyParser()); // Puts POST vars in request.body app.use(express.cookieParser()); // If you want to parse cookies (res.cookies) - app.use(express.session({ + + // TODO : this uses redis + /*app.use(express.session({ store: new RedisStore({ client: RDB, ttl: 60 * 60 * 24 * 30 @@ -150,7 +152,16 @@ var path = require('path'), cookie: { maxAge: 60 * 60 * 24 * 30 * 1000 // 30 days } + }));*/ + + app.use(express.cookieSession({ + secret: nconf.get('secret'), + key: 'express.sid', + cookie: { + maxAge: 60 * 60 * 24 * 30 * 1000 // 30 days + } })); + app.use(express.csrf()); // Local vars, other assorted setup @@ -172,33 +183,33 @@ var path = require('path'), function(next) { async.parallel([ function(next) { - // Theme configuration - RDB.hmget('config', 'theme:type', 'theme:id', 'theme:staticDir', 'theme:templates', function(err, themeData) { - var themeId = (themeData[1] || 'nodebb-theme-vanilla'); + + db.getObjectFields('config', ['theme:type', 'theme:id', 'theme:staticDir', 'theme:templates'], function(err, themeData) { + var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'); // Detect if a theme has been selected, and handle appropriately - if (!themeData[0] || themeData[0] === 'local') { + if (!themeData['theme:type'] || themeData['theme:type'] === 'local') { // Local theme if (process.env.NODE_ENV === 'development') { winston.info('[themes] Using theme ' + themeId); } // Theme's static directory - if (themeData[2]) { - app.use('/css/assets', express.static(path.join(__dirname, '../node_modules', themeData[1], themeData[2]), { + if (themeData['theme:staticDir']) { + app.use('/css/assets', express.static(path.join(__dirname, '../node_modules', themeData['theme:id'], themeData['theme:staticDir']), { maxAge: app.enabled('cache') ? 5184000000 : 0 })); if (process.env.NODE_ENV === 'development') { - winston.info('Static directory routed for theme: ' + themeData[1]); + winston.info('Static directory routed for theme: ' + themeData['theme:id']); } } - if (themeData[3]) { - app.use('/templates', express.static(path.join(__dirname, '../node_modules', themeData[1], themeData[3]), { + if (themeData['theme:templates']) { + app.use('/templates', express.static(path.join(__dirname, '../node_modules', themeData['theme:id'], themeData['theme:templates']), { maxAge: app.enabled('cache') ? 5184000000 : 0 })); if (process.env.NODE_ENV === 'development') { - winston.info('Custom templates directory routed for theme: ' + themeData[1]); + winston.info('Custom templates directory routed for theme: ' + themeData['theme:id']); } }