From 9b0ba6c7a4fb53c5c5f5c0f75519bdcf4389b50f Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sat, 29 Nov 2014 20:50:14 -0500 Subject: [PATCH 1/6] closed #2384 --- src/plugins.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/plugins.js b/src/plugins.js index 4d090d3df8..a4180880f1 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -746,7 +746,22 @@ var fs = require('fs'), }; Plugins.clearRequireCache = function(next) { - async.map(Plugins.libraryPaths, fs.realpath, function(err, paths) { + var cached = Object.keys(require.cache); + async.waterfall([ + async.apply(async.map, Plugins.libraryPaths, fs.realpath), + function(paths, next) { + paths = paths.map(function(pluginLib) { + var parent = path.dirname(pluginLib); + return cached.filter(function(libPath) { + return libPath.indexOf(parent) !== -1; + }); + }).reduce(function(prev, cur) { + return prev.concat(cur); + }); + next(null, paths); + } + ], function(err, paths) { + console.log(paths); for (var x=0,numPaths=paths.length;x Date: Sat, 29 Nov 2014 21:18:02 -0500 Subject: [PATCH 2/6] refactors to sitemap, closed #2254 --- src/sitemap.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/sitemap.js b/src/sitemap.js index dcb3c48f75..6d65719696 100644 --- a/src/sitemap.js +++ b/src/sitemap.js @@ -54,34 +54,34 @@ var path = require('path'), topicUrls: function(next) { var topicUrls = []; - db.getSortedSetRevRange('topics:recent', 0, 49, function(err, tids) { + async.waterfall([ + async.apply(db.getSortedSetRevRange, 'topics:recent', 0, -1), + function(tids, next) { + db.getSortedSetRevRange('topics:recent', 0, -1, next); + }, + function(tids, next) { + privileges.topics.filter('read', tids, 0, next); + }, + function(tids, next) { + topics.getTopicsFields(tids, ['tid', 'title', 'lastposttime'], next); + } + ], function(err, topics) { if (err) { return next(err); } - privileges.topics.filter('read', tids, 0, function(err, tids) { - if (err) { - return next(err); - } - - topics.getTopicsFields(tids, ['tid', 'title', 'lastposttime'], function(err, topics) { - if (err) { - return next(err); - } - topics.forEach(function(topic) { - if (topic) { - topicUrls.push({ - url: '/topic/' + topic.tid + '/' + encodeURIComponent(utils.slugify(topic.title)), - lastmodISO: utils.toISOString(topic.lastposttime), - changefreq: 'daily', - priority: '0.6' - }); - } + topics.forEach(function(topic) { + if (topic) { + topicUrls.push({ + url: '/topic/' + topic.tid + '/' + encodeURIComponent(utils.slugify(topic.title)), + lastmodISO: utils.toISOString(topic.lastposttime), + changefreq: 'daily', + priority: '0.6' }); - - next(null, topicUrls); - }); + } }); + + next(null, topicUrls); }); } }, function(err, data) { From d5e473609cc69768be5272849e622c71d94c28c6 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sat, 29 Nov 2014 21:54:58 -0500 Subject: [PATCH 3/6] first pass, #1984 --- app.js | 10 +++++++++- src/install.js | 31 ++++++++----------------------- src/webserver.js | 2 +- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/app.js b/app.js index 768ac48c64..330ffc455c 100644 --- a/app.js +++ b/app.js @@ -25,6 +25,7 @@ nconf.argv().env(); var fs = require('fs'), os = require('os'), + url = require('url'), async = require('async'), semver = require('semver'), winston = require('winston'), @@ -107,6 +108,10 @@ function loadConfig() { function start() { loadConfig(); + // nconf defaults, if not set in config + if (!nconf.get('upload_path')) nconf.set('upload_path', '/public/uploads'); + if (!nconf.get('bcrypt_rounds')) nconf.set('bcrypt_rounds', 12); + if (!cluster.isWorker || process.env.cluster_setup === 'true') { winston.info('Time: %s', (new Date()).toString()); winston.info('Initializing NodeBB v%s', pkg.version); @@ -140,7 +145,10 @@ function start() { if (schema_ok || nconf.get('check-schema') === false) { sockets.init(webserver.server); - nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path')); + // Parse out the relative_url and other goodies from the configured URL + var urlObject = url.parse(nconf.get('url')); + nconf.set('use_port', !!urlObject.port); + nconf.set('relative_path', urlObject.pathname); async.waterfall([ async.apply(plugins.ready), diff --git a/src/install.js b/src/install.js index 564f06f703..db6bf09673 100644 --- a/src/install.js +++ b/src/install.js @@ -2,7 +2,6 @@ var async = require('async'), fs = require('fs'), - url = require('url'), path = require('path'), prompt = require('prompt'), winston = require('winston'), @@ -24,19 +23,15 @@ var install = {}, questions.main = [ { - name: 'base_url', + name: 'url', description: 'URL used to access this NodeBB', - 'default': nconf.get('base_url') ? (nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '')) : 'http://localhost:4567', + 'default': + nconf.get('url') || + (nconf.get('base_url') ? (nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '')) : null) || // backwards compatibility (remove for v0.7.0) + 'http://localhost:4567', pattern: /^http(?:s)?:\/\//, message: 'Base URL must begin with \'http://\' or \'https://\'', }, - { - name: 'port', - description: 'Port number of your NodeBB', - 'default': nconf.get('port') || 4567, - pattern: /[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]/, - message: 'Please enter a value betweeen 1 and 65535' - }, { name: 'secret', description: 'Please enter a NodeBB secret', @@ -175,6 +170,7 @@ function completeConfigSetup(err, config, next) { if (err) { return next(err); } + // Add CI object if (install.ciVals) { config.test_database = {}; @@ -185,22 +181,12 @@ function completeConfigSetup(err, config, next) { } } - config.bcrypt_rounds = 12; - config.upload_path = '/public/uploads'; - - var urlObject = url.parse(config.base_url), - server_conf = config; - - server_conf.base_url = urlObject.protocol + '//' + urlObject.hostname; - server_conf.use_port = urlObject.port !== null ? true : false; - server_conf.relative_path = (urlObject.pathname && urlObject.pathname.length > 1) ? urlObject.pathname : ''; - - install.save(server_conf, function(err) { + install.save(config, function(err) { if (err) { return next(err); } - setupDatabase(server_conf, next); + setupDatabase(config, next); }); } @@ -329,7 +315,6 @@ function createAdmin(callback) { return retryPassword(results); } - nconf.set('bcrypt_rounds', 12); User.create({username: results.username, password: results.password, email: results.email}, function (err, uid) { if (err) { winston.warn(err.message + ' Please try again.'); diff --git a/src/webserver.js b/src/webserver.js index bf8394cc54..503d49d392 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -34,7 +34,7 @@ if(nconf.get('ssl')) { (function (app) { "use strict"; - var port = nconf.get('PORT') || nconf.get('port'); + var port = nconf.get('port') || nconf.get('PORT') || 4567; logger.init(app); emailer.registerApp(app); From d430ef39832da326177d4215241f88184463d047 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sat, 29 Nov 2014 22:03:49 -0500 Subject: [PATCH 4/6] second pass, #1984 --- app.js | 1 + src/upgrade.js | 1081 +--------------------------------------------- src/webserver.js | 2 +- 3 files changed, 3 insertions(+), 1081 deletions(-) diff --git a/app.js b/app.js index 330ffc455c..73952abd6c 100644 --- a/app.js +++ b/app.js @@ -149,6 +149,7 @@ function start() { var urlObject = url.parse(nconf.get('url')); nconf.set('use_port', !!urlObject.port); nconf.set('relative_path', urlObject.pathname); + nconf.set('port', nconf.get('port') || nconf.get('PORT') || 4567); async.waterfall([ async.apply(plugins.ready), diff --git a/src/upgrade.js b/src/upgrade.js index 2eeb1bb7cf..5986acf066 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -15,7 +15,7 @@ var db = require('./database'), Upgrade = {}, - minSchemaDate = Date.UTC(2014, 1, 14, 21, 50), // This value gets updated every new MINOR version + minSchemaDate = Date.UTC(2014, 9, 22), // This value gets updated every new MINOR version schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema @@ -67,1085 +67,6 @@ Upgrade.upgrade = function(callback) { } }); }, - function(next) { - thisSchemaDate = Date.UTC(2014, 1, 19, 18, 15); - - if (schemaDate < thisSchemaDate) { - db.setObjectField('widgets:home.tpl', 'motd', JSON.stringify([ - { - "widget": "html", - "data": { - "html": Meta.config.motd || "Welcome to NodeBB, if you are an administrator of this forum visit the Themes ACP to modify and add widgets." - } - } - ]), function(err) { - Meta.configs.remove('motd'); - Meta.configs.remove('motd_class'); - Meta.configs.remove('show_motd'); - - winston.info('[2014/2/19] Updated MOTD to use the HTML widget.'); - - if (err) { - next(err); - } else { - Upgrade.update(thisSchemaDate, next); - } - }); - } else { - winston.info('[2014/2/19] Updating MOTD to use the HTML widget - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 1, 20, 15, 30); - - if (schemaDate < thisSchemaDate) { - var container = '
{title}
{body}
'; - - db.setObjectField('widgets:category.tpl', 'sidebar', JSON.stringify([ - { - "widget": "recentreplies", - "data": { - "title": "Recent Replies", - "container": container - } - }, - { - "widget": "activeusers", - "data": { - "title": "Active Users", - "container": container - } - }, - { - "widget": "moderators", - "data": { - "title": "Moderators", - "container": container - } - } - ]), function(err) { - winston.info('[2014/2/20] Adding Recent Replies, Active Users, and Moderator widgets to category sidebar.'); - - if (err) { - next(err); - } else { - Upgrade.update(thisSchemaDate, next); - } - }); - } else { - winston.info('[2014/2/20] Adding Recent Replies, Active Users, and Moderator widgets to category sidebar - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 1, 20, 16, 15); - - if (schemaDate < thisSchemaDate) { - db.setObjectField('widgets:home.tpl', 'footer', JSON.stringify([ - { - "widget": "forumstats", - "data": {} - } - ]), function(err) { - winston.info('[2014/2/20] Adding Forum Stats Widget to the Homepage Footer.'); - - if (err) { - next(err); - } else { - Upgrade.update(thisSchemaDate, next); - } - }); - } else { - winston.info('[2014/2/20] Adding Forum Stats Widget to the Homepage Footer - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 1, 20, 19, 45); - - if (schemaDate < thisSchemaDate) { - var container = '
{title}
{body}
'; - - db.setObjectField('widgets:home.tpl', 'sidebar', JSON.stringify([ - { - "widget": "html", - "data": { - "html": Meta.config.motd || "Welcome to NodeBB, if you are an administrator of this forum visit the Themes ACP to modify and add widgets.", - "container": container, - "title": "MOTD" - } - } - ]), function(err) { - winston.info('[2014/2/20] Updating Lavender MOTD'); - - if (err) { - next(err); - } else { - Upgrade.update(thisSchemaDate, next); - } - }); - } else { - winston.info('[2014/2/20] Updating Lavender MOTD - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 1, 20, 20, 25); - - if (schemaDate < thisSchemaDate) { - db.setAdd('plugins:active', 'nodebb-widget-essentials', function(err) { - winston.info('[2014/2/20] Activating NodeBB Essential Widgets'); - Plugins.reload(function() { - if (err) { - next(err); - } else { - Upgrade.update(thisSchemaDate, next); - } - }); - }); - } else { - winston.info('[2014/2/20] Activating NodeBB Essential Widgets - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 1, 22); - - if (schemaDate < thisSchemaDate) { - db.exists('categories:cid', function(err, exists) { - if(err) { - return next(err); - } - if(!exists) { - winston.info('[2014/2/22] Added categories to sorted set - skipped'); - return next(); - } - - db.getListRange('categories:cid', 0, -1, function(err, cids) { - // Naive type-checking, becaue DBAL does not have .type() support - if(err) { - // Most likely upgraded already. Skip. - winston.info('[2014/2/22] Added categories to sorted set - skipped'); - return Upgrade.update(thisSchemaDate, next); - } - - if(!Array.isArray(cids)) { - winston.info('[2014/2/22] Add categories to sorted set - skipped (cant find any cids)'); - return next(); - } - - db.rename('categories:cid', 'categories:cid:old', function(err) { - if(err) { - return next(err); - } - - async.each(cids, function(cid, next) { - Categories.getCategoryField(cid, 'order', function(err, order) { - if(err) { - return next(err); - } - - // If there was no order present, put it at the end - if (!order) { - order = cids.length; - } - - db.sortedSetAdd('categories:cid', order, cid, next); - }); - }, function(err) { - if(err) { - return next(err); - } - winston.info('[2014/2/22] Added categories to sorted set'); - db.delete('categories:cid:old'); - Upgrade.update(thisSchemaDate, next); - }); - }); - }); - }); - - } else { - winston.info('[2014/2/22] Added categories to sorted set - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 2, 18); - - if (schemaDate < thisSchemaDate) { - db.exists('settings:markdown', function(err, exists) { - if (err || exists) { - winston.info('[2014/3/18] Migrating Markdown settings to new configuration - skipped'); - return next(); - } - - var fields = [ - 'nodebb-plugin-markdown:options:gfm', - 'nodebb-plugin-markdown:options:highlight', - 'nodebb-plugin-markdown:options:tables', - 'nodebb-plugin-markdown:options:breaks', - 'nodebb-plugin-markdown:options:pedantic', - 'nodebb-plugin-markdown:options:sanitize', - 'nodebb-plugin-markdown:options:smartLists', - 'nodebb-plugin-markdown:options:smartypants', - 'nodebb-plugin-markdown:options:langPrefix' - ], - settings = {}, - newFieldName; - - async.series([ - function(next) { - db.getObjectFields('config', fields, function(err, values) { - if (err) { - return next(); - } - - for(var field in values) { - if (values.hasOwnProperty(field)) { - newFieldName = field.slice(31); - settings[newFieldName] = values[field] === '1' ? 'on' : values[field]; - } - } - - next(); - }); - }, - function(next) { - db.setObject('settings:markdown', settings, next); - }, - function(next) { - async.each(fields, function(field, next) { - db.deleteObjectField('config', field, next); - }, next); - } - ], function(err) { - if (err) { - winston.error('[2014/3/18] Problem migrating Markdown settings.'); - next(); - } else { - winston.info('[2014/3/18] Migrated Markdown settings to new configuration'); - Upgrade.update(thisSchemaDate, next); - } - }); - }); - } else { - winston.info('[2014/3/18] Migrating Markdown settings to new configuration - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 2, 21); - - if (schemaDate < thisSchemaDate) { - db.getObject('group:gid', function(err, mapping) { - if (err) { - return next(err); - } - - if (!err && !mapping) { - // Done already, skip - return next(); - } - - var names = Object.keys(mapping), - reverseMapping = {}, - isGroupList = /^cid:[0-9]+:privileges:g\+[rw]$/, - gid; - - for(var groupName in mapping) { - if (mapping.hasOwnProperty(groupName)) { - gid = mapping[groupName]; - if (mapping.hasOwnProperty(groupName) && !reverseMapping.hasOwnProperty(gid)) { - reverseMapping[parseInt(gid, 10)] = groupName; - } - } - } - - async.eachSeries(names, function(name, next) { - async.series([ - function(next) { - // Remove the gid from the hash - db.exists('gid:' + mapping[name], function(err, exists) { - if (exists) { - db.deleteObjectField('gid:' + mapping[name], 'gid', next); - } else { - next(); - } - }); - }, - function(next) { - // Rename gid hash to groupName hash - db.exists('gid:' + mapping[name], function(err, exists) { - if (exists) { - db.rename('gid:' + mapping[name], 'group:' + name, next); - } else { - next(); - } - }); - }, - function(next) { - // Move member lists over - db.exists('gid:' + mapping[name], function(err, dstExists) { - if (err) { - return next(err); - } - - db.exists('gid:' + mapping[name] + ':members', function(err, srcExists) { - if (err) { - return next(err); - } - - if (srcExists && !dstExists) { - db.rename('gid:' + mapping[name] + ':members', 'group:' + name + ':members', next); - } else { - // No members or group memberlist collision: do nothing, they'll be removed later - next(); - } - }); - }); - }, - function(next) { - // Add group to the directory (set) - db.setAdd('groups', name, next); - }, - function(next) { - // If this group contained gids, map the gids to group names - // Also check if the mapping and reverseMapping still work, if not, delete this group - if (isGroupList.test(name) && name === reverseMapping[mapping[name]]) { - db.getSetMembers('group:' + name + ':members', function(err, gids) { - async.each(gids, function(gid, next) { - db.setRemove('group:' + name + ':members', gid); - db.setAdd('group:' + name + ':members', reverseMapping[gid], next); - }, next); - }); - } else if (name !== reverseMapping[mapping[name]]) { - async.parallel([ - function(next) { - db.delete('group:' + name, next); - }, - function(next) { - db.delete('group:' + name + ':members', next); - }, - function(next) { - db.setRemove('groups', name, next); - } - ], next); - } else { - next(); - } - }, - function(next) { - // Fix its' name, if it is wrong for whatever reason - db.getObjectField('group:' + name, 'name', function(err, groupName) { - if (name && groupName && name !== groupName) { - async.series([ - function(cb) { - db.setObjectField('group:' + name, 'name', name, cb); - }, - function(cb) { - db.setRemove('groups', groupName, cb); - }, - function(cb) { - db.setAdd('groups', name, cb); - } - ], next); - } else { - next(); - } - }); - } - ], next); - }, function(err) { - if (err) { - winston.error('[2014/3/21] Problem removing gids and pruning groups.'); - winston.error(err.message); - return next(); - } - - // Clean-up - var isValidHiddenGroup = /^cid:[0-9]+:privileges:(g)?\+[rw]$/; - async.series([ - function(next) { - // Mapping - db.delete('group:gid', next); - }, - function(next) { - // Incrementor - db.deleteObjectField('global', 'nextGid', next); - }, - function(next) { - // Set 'administrators' and 'registered-users' as system groups - async.parallel([ - function(next) { - db.setObject('group:administrators', { - system: '1', - hidden: '0' - }, next); - }, - function(next) { - db.setObject('group:registered-users', { - system: '1', - hidden: '0' - }, next); - } - ], next); - }, - function(next) { - Groups.list({ showAllGroups: true }, function(err, groups) { - async.each(groups, function(group, next) { - // If deleted, (hidden & empty), or invalidly named hidden group, delete - if (group.deleted || (group.hidden && group.memberCount === 0) || (group.hidden && !isValidHiddenGroup.test(group.name))) { - Groups.destroy(group.name, next); - } else { - next(); - } - }, next); - }); - } - ], function(err) { - if (err) { - winston.error('[2014/3/21] Problem removing gids and pruning groups.'); - next(); - } else { - winston.info('[2014/3/21] Removing gids and pruning groups'); - Upgrade.update(thisSchemaDate, next); - } - }); - }); - }); - } else { - winston.info('[2014/3/21] Removing gids and pruning groups - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 3, 31, 12, 30); - - if (schemaDate < thisSchemaDate) { - db.setObjectField('widgets:global', 'footer', "[{\"widget\":\"html\",\"data\":{\"html\":\"\",\"title\":\"\",\"container\":\"\"}}]", function(err) { - if (err) { - winston.error('[2014/3/31] Problem re-adding copyright message into global footer widget'); - next(); - } else { - winston.info('[2014/3/31] Re-added copyright message into global footer widget'); - Upgrade.update(thisSchemaDate, next); - } - }); - } else { - winston.info('[2014/3/31] Re-adding copyright message into global footer widget - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 4, 1); - - if (schemaDate < thisSchemaDate) { - db.getObjectField('widgets:home.tpl', 'sidebar', function(err, widgetData) { - if (err) { - winston.error('[2014/4/1] Error moving home sidebar widgets into draft zone'); - return next(err); - } - - db.setObjectField('widgets:global', 'drafts', widgetData, function(err) { - if (err) { - winston.error('[2014/4/1] Error moving home sidebar widgets into draft zone'); - return next(err); - } - - db.deleteObjectField('widgets:home.tpl', 'sidebar', function(err) { - if (err) { - winston.error('[2014/4/1] Error moving home sidebar widgets into draft zone'); - next(err); - } else { - winston.info('[2014/4/1] Moved home sidebar widgets into draft zone'); - Upgrade.update(thisSchemaDate, next); - } - }); - }); - }); - } else { - winston.info('[2014/4/1] Moved home sidebar widgets into draft zone - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 4, 2); - - if (schemaDate < thisSchemaDate) { - db.getObjectField('widgets:home.tpl', 'footer', function(err, widgetData) { - if (err) { - winston.error('[2014/4/1] Error moving deprecated vanilla footer widgets into draft zone'); - return next(err); - } - - db.setObjectField('widgets:global', 'drafts', widgetData, function(err) { - if (err) { - winston.error('[2014/4/1] Error moving deprecated vanilla footer widgets into draft zone'); - return next(err); - } - - db.deleteObjectField('widgets:home.tpl', 'footer', function(err) { - if (err) { - winston.error('[2014/4/1] Error moving deprecated vanilla footer widgets into draft zone'); - next(err); - } else { - winston.info('[2014/4/1] Moved deprecated vanilla footer widgets into draft zone'); - Upgrade.update(thisSchemaDate, next); - } - }); - }); - }); - } else { - winston.info('[2014/4/2] Moved deprecated vanilla footer widgets into draft zone - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 4, 13); - - if (schemaDate < thisSchemaDate) { - var tasks = []; - db.getSetMembers('groups', function(err, groups) { - var isCidPerm = /^cid:\d+:privileges:g?\+[rw]$/, - privMap = { - "+r": "read", - "+w": "topics:create", - "g+r": "groups:read", - "g+w": "groups:topics:create" - }; - - groups = groups.filter(function(groupName) { - return isCidPerm.test(groupName); - }); - - groups.forEach(function(groupName) { - var split = groupName.split(':'), - privilege = split.pop(), - newPrivilege = privMap[privilege], - newName; - - split.push(newPrivilege); - newName = split.join(':'); - - tasks.push(async.apply(db.rename, 'group:' + groupName, 'group:' + newName)); - tasks.push(async.apply(db.rename, 'group:' + groupName + ':members', 'group:' + newName + ':members')); - tasks.push(async.apply(db.setRemove, 'groups', groupName)); - tasks.push(async.apply(db.setAdd, 'groups', newName)); - }); - - async.parallel(tasks, function(err) { - if (!err) { - winston.info('[2014/4/1] Updating privilege settings'); - Upgrade.update(thisSchemaDate, next); - } else { - winston.error('[2014/4/1] Error encountered while updating privilege settings'); - next(err); - } - }); - }); - } else { - winston.info('[2014/5/13] Updating privilege settings - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 4, 16); - - if (schemaDate < thisSchemaDate) { - var tasks = []; - - db.getObjectField('config', 'allowGuestPosting', function(err, value) { - if (value === '1') { - tasks.push(async.apply(db.deleteObjectField, 'config', 'allowGuestPosting')); - - db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { - async.each(cids, function(cid, next) { - Categories.getCategoryField(cid, 'disabled', function(err, disabled) { - if (!disabled || disabled === '0') { - tasks.push(async.apply(Groups.join, 'cid:' + cid + ':privileges:groups:topics:create', 'guests')); - tasks.push(async.apply(Groups.join, 'cid:' + cid + ':privileges:groups:topics:reply', 'guests')); - next(); - } else { - next(); - } - }); - }, function() { - async.parallel(tasks, function(err) { - if (!err) { - winston.info('[2014/5/16] Removing allowGuestPosting option'); - Upgrade.update(thisSchemaDate, next); - } else { - winston.error('[2014/4/1] Error encountered while removing allowGuestPosting option'); - next(err); - } - }); - }); - }); - } else { - winston.info('[2014/5/16] Removing allowGuestPosting option - skipped'); - next(); - } - }); - } else { - winston.info('[2014/5/16] Removing allowGuestPosting option - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 4, 22); - - if (schemaDate < thisSchemaDate) { - db.exists('tags', function(err, exists) { - if (err || !exists) { - winston.info('[2014/5/22] Skipping tag upgrade'); - return Upgrade.update(thisSchemaDate, next); - } - - db.getSetMembers('tags', function(err, tags) { - if (err) { - return next(err); - } - - async.each(tags, function(tag, next) { - db.sortedSetCard('tag:' + tag + ':topics', function(err, count) { - if (err) { - return next(err); - } - db.sortedSetAdd('tags:topic:count', count, tag, next); - }); - }, function(err) { - if (err) { - winston.error('[2014/5/22] Error encountered while upgrading tags'); - return next(err); - } - - db.delete('tags', function(err) { - if (err) { - winston.error('[2014/5/22] Error encountered while upgrading tags'); - return next(err); - } - winston.info('[2014/5/22] Tags upgraded to sorted set'); - Upgrade.update(thisSchemaDate, next); - }); - }); - }); - }); - } else { - winston.info('[2014/5/16] Tags upgrade - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 5, 6); - - if (schemaDate < thisSchemaDate) { - winston.info('[2014/6/6] Upgrading topics...'); - - db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) { - function upgradeTopic(tid, callback) { - - Topics.getTopicField(tid, 'mainPid', function(err, mainPid) { - if (err) { - return callback(err); - } - - db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, function(err, pids) { - if (err) { - return callback(err); - } - - if (!Array.isArray(pids) || !pids.length) { - return callback(); - } - - if (!parseInt(mainPid, 10)) { - mainPid = pids[0]; - pids.splice(0, 1); - Topics.setTopicField(tid, 'mainPid', mainPid); - db.sortedSetRemove('tid:' + tid + ':posts', mainPid); - db.sortedSetRemove('tid:' + tid + ':posts:votes', mainPid); - } - - if (!pids.length) { - return callback(); - } - - async.eachLimit(pids, 10, function(pid, next) { - Posts.getPostField(pid, 'votes', function(err, votes) { - if (err) { - return next(err); - } - db.sortedSetAdd('tid:' + tid + ':posts:votes', votes ? votes : 0, pid, next); - }); - }, callback); - }); - }); - } - - if (err) { - return next(err); - } - - if (!Array.isArray(tids) || !tids.length) { - winston.info('[2014/6/6] Skipping topic upgrade'); - return Upgrade.update(thisSchemaDate, next); - } - - async.eachLimit(tids, 10, upgradeTopic, function(err) { - if (err) { - winston.error('[2014/6/6] Error encountered while upgrading topics'); - return next(err); - } - winston.info('[2014/6/6] Topics upgraded.'); - Upgrade.update(thisSchemaDate, next); - }); - }); - } else { - winston.info('[2014/6/6] Topic upgrade - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 5, 17); - - if (schemaDate < thisSchemaDate) { - winston.info('[2014/6/17] Upgrading category post counts...'); - - db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { - if (err) { - return next(err); - } - - async.each(cids, function(cid, next) { - db.setObjectField('category:' + cid, 'post_count', 0, next); - }, function(err) { - if (err) { - return next(err); - } - db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) { - function upgradeTopic(tid, callback) { - - Topics.getTopicFields(tid, ['cid', 'postcount', 'deleted'], function(err, topicData) { - if (err || !topicData) { - return callback(err); - } - - if (parseInt(topicData.deleted, 10) === 1) { - return callback(); - } - - db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', topicData.postcount, callback); - }); - } - - if (err) { - return next(err); - } - - if (!Array.isArray(tids) || !tids.length) { - winston.info('[2014/6/17] Skipping category post upgrade'); - return Upgrade.update(thisSchemaDate, next); - } - - async.eachLimit(tids, 10, upgradeTopic, function(err) { - if (err) { - winston.error('[2014/6/17] Error encountered while upgrading category postcounts'); - return next(err); - } - winston.info('[2014/6/17] Category post counts upgraded'); - Upgrade.update(thisSchemaDate, next); - }); - }); - }); - }); - } else { - winston.info('[2014/6/17] Category post count upgrade - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 6, 23); - - if (schemaDate < thisSchemaDate) { - winston.info('[2014/7/23] Upgrading db dependencies...'); - var install = require('./install'); - var config = require('../config.json'); - install.installDbDependencies(config, function(err) { - if (err) { - winston.error('[2014/7/23] Error encountered while upgrading db dependencies'); - return next(err); - } - - winston.info('[2014/7/23] Upgraded db dependencies'); - Upgrade.update(thisSchemaDate, next); - }); - } else { - winston.info('[2014/7/23] Upgrading db dependencies - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 6, 24); - - if (schemaDate < thisSchemaDate) { - winston.info('[2014/7/24] Upgrading chats to sorted set...'); - - db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) { - if (err) { - return next(err); - } - - async.eachLimit(uids, 10, function(uid, next) { - db.getSortedSetRange('uid:' + uid + ':chats', 0, -1, function(err, toUids) { - if (err) { - return next(err); - } - - if (!Array.isArray(toUids) || !toUids.length) { - return next(); - } - - async.eachLimit(toUids, 10, function(toUid, next) { - var uids = [uid, toUid].sort(); - db.getListRange('messages:' + uids[0] + ':' + uids[1], 0, -1, function(err, mids) { - if (err) { - return next(err); - } - - if (!Array.isArray(mids) || !mids.length) { - return next(); - } - - async.eachLimit(mids, 10, function(mid, next) { - db.getObjectField('message:' + mid, 'timestamp', function(err, timestamp) { - if (err || !timestamp) { - return next(err); - } - - db.sortedSetAdd('messages:uid:' + uids[0] + ':to:' + uids[1], timestamp, mid, next); - }); - }, next); - }); - }, next); - }); - }, function(err) { - if (err) { - winston.error('[2014/7/24] Error encountered while updating chats to sorted set'); - return next(err); - } - - async.eachLimit(uids, 10, function(uid, next) { - db.getSortedSetRange('uid:' + uid + ':chats', 0, -1, function(err, toUids) { - if (err) { - return next(err); - } - - if (!Array.isArray(toUids) || !toUids.length) { - return next(); - } - - async.eachLimit(toUids, 10, function(toUid, next) { - var uids = [uid, toUid].sort(); - db.delete('messages:' + uids[0] + ':' + uids[1], next); - }, next); - }); - }, function(err) { - if (err) { - winston.error('[2014/7/24] Error encountered while updating chats to sorted set'); - return next(err); - } - - winston.info('[2014/7/24] Upgraded chats to sorted set'); - Upgrade.update(thisSchemaDate, next); - }); - }); - }); - } else { - winston.info('[2014/7/24] Upgrading chats to sorted set - skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 8, 8); - - if (schemaDate < thisSchemaDate) { - winston.info('[2014/9/8] Deleting old notifications...'); - - async.parallel({ - uids: function(next) { - db.getSortedSetRange('users:joindate', 0, -1, next); - }, - nids: function(next) { - db.getSetMembers('notifications', next); - } - }, function(err, results) { - if (err) { - return next(err); - } - var uidKeys = results.uids.map(function(uid) { - return 'uid:' + uid + ':notifications:uniqueId:nid'; - }); - - var nidKeys = results.nids.filter(Boolean).map(function(nid) { - return 'notifications:' + nid; - }); - - async.series([ - function(next) { - db.deleteAll(nidKeys, next); - }, - function(next) { - db.deleteAll(uidKeys, next); - }, - function(next) { - db.delete('notifications', next); - } - ], function(err, results) { - if (err) { - winston.error('[2014/9/8] Error encountered while deleting notifications'); - return next(err); - } - - winston.info('[2014/9/8] Deleted old notifications'); - Upgrade.update(thisSchemaDate, next); - }); - }); - } else { - winston.info('[2014/9/8] Deleting old notifications skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 8, 27); - if (schemaDate < thisSchemaDate) { - winston.info('[2014/9/27] Deleting tid::read_by_uid...'); - - db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) { - if (err) { - return next(err); - } - tids = tids.filter(Boolean); - var readKeys = tids.map(function(tid) { - return 'tid:' + tid + ':read_by_uid'; - }); - - db.deleteAll(readKeys, function(err, results) { - if (err) { - winston.error('[2014/9/27] Error encountered while deleting tid::read_by_uid'); - return next(err); - } - - winston.info('[2014/9/27] Deleted tid::read_by_uid'); - Upgrade.update(thisSchemaDate, next); - }); - }); - } else { - winston.info('[2014/9/27] Deleting tid::read_by_uid skipped'); - next(); - } - }, - function(next) { - thisSchemaDate = Date.UTC(2014, 9, 7); - if (schemaDate < thisSchemaDate) { - winston.info('[2014/10/7] Banned users sorted set'); - - db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) { - if (err) { - return next(err); - } - - var now = Date.now(); - - async.eachLimit(uids, 50, function(uid, next) { - User.getUserField(uid, 'banned', function(err, banned) { - if (err) { - return next(err); - } - - if (parseInt(banned, 10) !== 1) { - return next(); - } - - db.sortedSetAdd('users:banned', now, uid, next); - }); - }, function(err) { - if (err) { - winston.error('[2014/10/7] Error encountered while updating banned users sorted set'); - return next(err); - } - - winston.info('[2014/10/7] Banned users added to sorted set'); - Upgrade.update(thisSchemaDate, next); - }); - }); - } else { - winston.info('[2014/10/7] Banned users sorted set skipped'); - next(); - } - }, - function(next) { - function updateCategories(next) { - db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { - if (err) { - return next(err); - } - - async.eachLimit(cids, 5, function(cid, next) { - db.sortedSetCard('categories:' + cid + ':tid', function(err, count) { - if (err) { - return next(err); - } - count = parseInt(count, 10) || 0; - db.setObjectField('category:' + cid, 'topic_count', count, next); - }); - }, next); - }); - } - - function updateTopics(next) { - db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) { - if (err) { - return next(err); - } - - async.eachLimit(tids, 50, function(tid, next) { - db.sortedSetCard('tid:' + tid + ':posts', function(err, count) { - if (err) { - return next(err); - } - count = (parseInt(count, 10) || 0) + 1; - winston.info('updating tid ' + tid + ' count ' + count); - db.setObjectField('topic:' + tid, 'postcount', count, next); - }); - }, next); - }); - } - - thisSchemaDate = Date.UTC(2014, 9, 22); - if (schemaDate < thisSchemaDate) { - winston.info('[2014/10/22] Topic post count migration'); - - async.series([ - function(next) { - updateCategories(next); - }, - function(next) { - updateTopics(next); - } - ], function(err) { - if (err) { - winston.error('[2014/10/22] Error encountered while Topic post count migration'); - return next(err); - } - winston.info('[2014/10/22] Topic post count migration done'); - Upgrade.update(thisSchemaDate, next); - }); - } else { - winston.info('[2014/10/22] Topic post count migration skipped'); - next(); - } - }, function(next) { thisSchemaDate = Date.UTC(2014, 9, 31); if (schemaDate < thisSchemaDate) { diff --git a/src/webserver.js b/src/webserver.js index 503d49d392..342e20f7ab 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -34,7 +34,7 @@ if(nconf.get('ssl')) { (function (app) { "use strict"; - var port = nconf.get('port') || nconf.get('PORT') || 4567; + var port = nconf.get('port'); logger.init(app); emailer.registerApp(app); From 03b106b03ac5b3fe612291eb7ee2da846df0e456 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sat, 29 Nov 2014 23:38:36 -0500 Subject: [PATCH 5/6] final pass, #1984 --- app.js | 11 +++++----- src/controllers/api.js | 8 +++---- src/install.js | 5 ----- src/upgrade.js | 50 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index 73952abd6c..5d83a4ad55 100644 --- a/app.js +++ b/app.js @@ -111,6 +111,11 @@ function start() { // nconf defaults, if not set in config if (!nconf.get('upload_path')) nconf.set('upload_path', '/public/uploads'); if (!nconf.get('bcrypt_rounds')) nconf.set('bcrypt_rounds', 12); + // Parse out the relative_url and other goodies from the configured URL + var urlObject = url.parse(nconf.get('url')); + nconf.set('use_port', !!urlObject.port); + nconf.set('relative_path', urlObject.pathname !== '/' ? urlObject.pathname : ''); + nconf.set('port', nconf.get('port') || nconf.get('PORT') || 4567); if (!cluster.isWorker || process.env.cluster_setup === 'true') { winston.info('Time: %s', (new Date()).toString()); @@ -145,12 +150,6 @@ function start() { if (schema_ok || nconf.get('check-schema') === false) { sockets.init(webserver.server); - // Parse out the relative_url and other goodies from the configured URL - var urlObject = url.parse(nconf.get('url')); - nconf.set('use_port', !!urlObject.port); - nconf.set('relative_path', urlObject.pathname); - nconf.set('port', nconf.get('port') || nconf.get('PORT') || 4567); - async.waterfall([ async.apply(plugins.ready), async.apply(meta.templates.compile), diff --git a/src/controllers/api.js b/src/controllers/api.js index 5f96861299..90d8ee412e 100644 --- a/src/controllers/api.js +++ b/src/controllers/api.js @@ -4,15 +4,15 @@ var pkg = require('./../../package.json'), meta = require('./../meta'), user = require('./../user'), plugins = require('./../plugins'), - widgets = require('../widgets'); + widgets = require('../widgets'), + + nconf = require('nconf'); var apiController = {}; apiController.getConfig = function(req, res, next) { - var serverConfig = require('./../../config.json'); - var config = {}; - config.relative_path = serverConfig.relative_path; + config.relative_path = nconf.get('relative_path'); config.version = pkg.version; config.siteTitle = meta.config.title || meta.config.browserTitle || 'NodeBB'; config.showSiteTitle = parseInt(meta.config.showSiteTitle, 10) === 1; diff --git a/src/install.js b/src/install.js index db6bf09673..2104161a67 100644 --- a/src/install.js +++ b/src/install.js @@ -37,11 +37,6 @@ questions.main = [ description: 'Please enter a NodeBB secret', 'default': nconf.get('secret') || utils.generateUUID() }, - { - name: 'bind_address', - description: 'IP or Hostname to bind to', - 'default': nconf.get('bind_address') || '0.0.0.0' - }, { name: 'database', description: 'Which database to use', diff --git a/src/upgrade.js b/src/upgrade.js index 5986acf066..1371af5927 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -3,6 +3,8 @@ var db = require('./database'), async = require('async'), winston = require('winston'), + fs = require('fs'), + path = require('path'), User = require('./user'), Topics = require('./topics'), @@ -19,7 +21,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2014, 10, 17, 13); + latestSchema = Date.UTC(2014, 10, 29, 22); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -270,7 +272,7 @@ Upgrade.upgrade = function(callback) { function(next) { thisSchemaDate = Date.UTC(2014, 10, 17, 13); if (schemaDate < thisSchemaDate) { - winston.info('[2014/11/11] Updating user email digest settings'); + winston.info('[2014/11/17] Updating user email digest settings'); async.waterfall([ async.apply(db.getSortedSetRange, 'users:joindate', 0, -1), @@ -290,14 +292,52 @@ Upgrade.upgrade = function(callback) { } ], function(err) { if (err) { - winston.error('[2014/11/11] Error encountered while updating user email digest settings'); + winston.error('[2014/11/17] Error encountered while updating user email digest settings'); return next(err); } - winston.info('[2014/11/11] Updating user email digest settings done'); + winston.info('[2014/11/17] Updating user email digest settings done'); Upgrade.update(thisSchemaDate, next); }); } else { - winston.info('[2014/11/11] Updating user email digest settings skipped'); + winston.info('[2014/11/17] Updating user email digest settings skipped'); + next(); + } + }, + function(next) { + thisSchemaDate = Date.UTC(2014, 10, 29, 22); + if (schemaDate < thisSchemaDate) { + winston.info('[2014/11/29] Updating config.json to new format'); + var configPath = path.join(__dirname, '../config.json'); + + async.waterfall([ + async.apply(fs.readFile, configPath, { encoding: 'utf-8' }), + function(config, next) { + try { + config = JSON.parse(config); + config.url = config.base_url + (config.use_port ? ':' + config.port : '') + config.relative_path; + if (config.port == '4567') delete config.port; + if (config.bcrypt_rounds == 12) delete config.bcrypt_rounds; + if (config.upload_path === '/public/uploads') delete config.upload_path; + if (config.bind_address === '0.0.0.0') delete config.bind_address; + delete config.base_url; + delete config.use_port; + delete config.relative_path; + + fs.writeFile(configPath, JSON.stringify(config, null, 4), next); + } catch (err) { + return next(err); + } + } + ], function(err) { + if (err) { + winston.error('[2014/11/29] Error encountered while updating config.json to new format'); + return next(err); + } + winston.info('[2014/11/29] Updating config.json to new format done'); + Upgrade.update(thisSchemaDate, next); + }); + } else { + winston.info('[2014/11/29] Updating config.json to new format skipped'); next(); } } From fb3251c2699ab8fb05b99c8abdc7c01595a1731f Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sun, 30 Nov 2014 01:44:46 -0500 Subject: [PATCH 6/6] removed extra db call #2254 --- src/sitemap.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sitemap.js b/src/sitemap.js index 6d65719696..e621fdc713 100644 --- a/src/sitemap.js +++ b/src/sitemap.js @@ -55,7 +55,6 @@ var path = require('path'), var topicUrls = []; async.waterfall([ - async.apply(db.getSortedSetRevRange, 'topics:recent', 0, -1), function(tids, next) { db.getSortedSetRevRange('topics:recent', 0, -1, next); },