diff --git a/src/install.js b/src/install.js index 6d9a7afd3b..2841fe2c2e 100644 --- a/src/install.js +++ b/src/install.js @@ -95,17 +95,23 @@ var async = require('async'), }, function(next) { // Applying default database configs - var meta = require('./meta'); - - meta.configs.set('postDelay', 10000); - meta.configs.set('minimumPostLength', 8); - meta.configs.set('minimumTitleLength', 3); - meta.configs.set('minimumUsernameLength', 2); - meta.configs.set('maximumUsernameLength', 16); - meta.configs.set('minimumPasswordLength', 6); - meta.configs.set('imgurClientID', ''); + winston.info('Populating database with default configs, if not already set...') + var meta = require('./meta'), + defaults = [ + { field: 'postDelay', value: 10000 }, + { field: 'minimumPostLength', value: 8 }, + { field: 'minimumTitleLength', value: 3 }, + { field: 'minimumUsernameLength', value: 2 }, + { field: 'maximumUsernameLength', value: 16 }, + { field: 'minimumPasswordLength', value: 6 }, + { field: 'imgurClientID', value: '' } + ]; - meta.configs.init(next); + async.each(defaults, function(configObj, next) { + meta.configs.setOnEmpty(configObj.field, configObj.value, next); + }, function(err) { + meta.configs.init(next); + }); }, function(next) { // Check if an administrator needs to be created diff --git a/src/meta.js b/src/meta.js index 5248a68d6b..25c8d49f4b 100644 --- a/src/meta.js +++ b/src/meta.js @@ -2,30 +2,34 @@ var utils = require('./../public/src/utils.js'), RDB = require('./redis.js'), async = require('async'), path = require('path'), - fs = require('fs'); + fs = require('fs'), + winston = require('winston'); (function(Meta) { Meta.configs = { init: function(callback) { - Meta.configs.get(function(config) { - Meta.config = config; - callback(); + Meta.configs.list(function(err, config) { + if (!err) { + Meta.config = config; + callback(); + } else winston.error(err); }); }, - get: function(callback) { + list: function(callback) { RDB.hgetall('config', function(err, config) { if (!err) { config = config || {}; config.status = 'ok'; - callback(config); + callback(err, config); } else { - callback({ - status: 'error' - }); + callback(new Error('could-not-read-config')); } }); }, + get: function(field, callback) { + RDB.hget('config', field, callback); + }, getFields: function(fields, callback) { RDB.hmgetObject('config', fields, callback); }, @@ -35,6 +39,12 @@ var utils = require('./../public/src/utils.js'), callback(err, res); }); }, + setOnEmpty: function(field, value, callback) { + this.get(field, function(err, curValue) { + if (!curValue) Meta.configs.set(field, value, callback); + else callback(); + }); + }, remove: function(field) { RDB.hdel('config', field); } diff --git a/src/websockets.js b/src/websockets.js index 54f1356a90..67161692dc 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -569,8 +569,8 @@ var SocketIO = require('socket.io').listen(global.server, { }); socket.on('api:config.get', function(data) { - meta.configs.get(function(config) { - socket.emit('api:config.get', config); + meta.configs.list(function(err, config) { + if (!err) socket.emit('api:config.get', config); }); });