From e8e33a8a23cdbc1d44d975a549fb3d2aa3277a53 Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Sun, 4 Dec 2016 17:41:27 +0100 Subject: [PATCH 01/16] ZSET scores are float: parseInt => parseFloat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In Redis, scores of sorted sets can be floats – so we should use `parseFloat` instead of `parseInt` when converting from string to number. Should not lead to #4939 again, as `new Date()` works regardless of whether it's being passed a float or integer. --- src/database/redis/sorted.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js index 2230f0498a..9e50c158c7 100644 --- a/src/database/redis/sorted.js +++ b/src/database/redis/sorted.js @@ -115,7 +115,7 @@ module.exports = function (redisClient, module) { } var objects = []; for(var i = 0; i < data.length; i += 2) { - objects.push({value: data[i], score: parseInt(data[i + 1], 10)}); + objects.push({value: data[i], score: parseFloat(data[i + 1])}); } callback(null, objects); }); @@ -144,7 +144,7 @@ module.exports = function (redisClient, module) { } var objects = []; for(var i = 0; i < data.length; i += 2) { - objects.push({value: data[i], score: parseInt(data[i + 1], 10)}); + objects.push({value: data[i], score: parseFloat(data[i + 1])}); } callback(null, objects); }); @@ -195,7 +195,7 @@ module.exports = function (redisClient, module) { module.sortedSetScore = function (key, value, callback) { redisClient.zscore(key, value, function (err, score) { - callback(err, !err ? parseInt(score, 10) : undefined); + callback(err, !err ? parseFloat(score) : undefined); }); }; @@ -289,7 +289,7 @@ module.exports = function (redisClient, module) { results = results[1] || []; var objects = []; for(var i = 0; i < results.length; i += 2) { - objects.push({value: results[i], score: parseInt(results[i + 1], 10)}); + objects.push({value: results[i], score: parseFloat(results[i + 1])}); } callback(null, objects); }); @@ -297,7 +297,7 @@ module.exports = function (redisClient, module) { module.sortedSetIncrBy = function (key, increment, value, callback) { redisClient.zincrby(key, increment, value, function (err, newValue) { - callback(err, !err ? parseInt(newValue, 10) : undefined); + callback(err, !err ? parseFloat(newValue) : undefined); }); }; @@ -419,4 +419,4 @@ module.exports = function (redisClient, module) { callback(null, objects); }); } -}; \ No newline at end of file +}; From 7c697759e9da678cee23561ce413da0d75f7bc64 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 1 Dec 2016 17:06:53 -0700 Subject: [PATCH 02/16] Escape and ignore `%` and `\,` in translator args --- public/src/modules/translator.js | 5 +++-- test/translator.js | 7 +++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js index a876dba44c..e60385ed79 100644 --- a/public/src/modules/translator.js +++ b/public/src/modules/translator.js @@ -105,7 +105,7 @@ } else if (text.slice(i, i + 2) === ']]') { level -= 1; i += 1; - } else if (level === 0 && text[i] === ',') { + } else if (level === 0 && text[i] === ',' && text[i - 1] !== '\\') { arr.push(text.slice(brk, i).trim()); i += 1; brk = i; @@ -260,7 +260,8 @@ } var out = translated; translatedArgs.forEach(function (arg, i) { - out = out.replace(new RegExp('%' + (i + 1), 'g'), arg); + var escaped = arg.replace(/%/g, '%').replace(/\\,/g, ','); + out = out.replace(new RegExp('%' + (i + 1), 'g'), escaped); }); return out; }); diff --git a/test/translator.js b/test/translator.js index 91fbf8f696..f52fd7c79c 100644 --- a/test/translator.js +++ b/test/translator.js @@ -127,14 +127,13 @@ describe('new Translator(language)', function () { }); }); - it('should properly escape % and ,', function (done) { + it('should properly escape and ignore % and \\, in arguments', function (done) { var translator = Translator.create('en-GB'); - var title = 'Test 1, 2, 3 % salmon'; - title = title.replace(/%/g, '%').replace(/,/g, ','); + var title = 'Test 1\\, 2\\, 3 % salmon'; var key = "[[topic:composer.replying_to, " + title + "]]"; translator.translate(key).then(function (translated) { - assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon'); + assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon'); done(); }); }); From 375af7ad53a21514d289acffbe561665731bcce9 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 1 Dec 2016 17:09:09 -0700 Subject: [PATCH 03/16] Restructure and rename translator tests --- test/translator.js | 100 ++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 60 deletions(-) diff --git a/test/translator.js b/test/translator.js index f52fd7c79c..60fb589e0d 100644 --- a/test/translator.js +++ b/test/translator.js @@ -7,7 +7,7 @@ var Translator = shim.Translator; require('../src/languages').init(function () {}); -describe('translator shim', function () { +describe('Translator shim', function () { describe('.translate()', function () { it('should translate correctly', function (done) { shim.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]', function (translated) { @@ -15,6 +15,13 @@ describe('translator shim', function () { done(); }); }); + + it('should accept a language parameter and adjust accordingly', function (done) { + shim.translate('[[global:home]]', 'de', function (translated) { + assert.strictEqual(translated, 'Übersicht'); + done(); + }); + }); }); }); @@ -27,138 +34,115 @@ describe('new Translator(language)', function () { }); describe('.translate()', function () { - it('should handle basic translations', function (done) { + it('should handle basic translations', function () { var translator = Translator.create('en-GB'); - translator.translate('[[global:home]]').then(function (translated) { + return translator.translate('[[global:home]]').then(function (translated) { assert.strictEqual(translated, 'Home'); - done(); }); }); - it('should handle language keys in regular text', function (done) { + it('should handle language keys in regular text', function () { var translator = Translator.create('en-GB'); - translator.translate('Let\'s go [[global:home]]').then(function (translated) { + return translator.translate('Let\'s go [[global:home]]').then(function (translated) { assert.strictEqual(translated, 'Let\'s go Home'); - done(); - }); - }); - - it('should accept a language parameter and adjust accordingly', function (done) { - var translator = Translator.create('de'); - - translator.translate('[[global:home]]').then(function (translated) { - assert.strictEqual(translated, 'Übersicht'); - done(); }); }); - it('should handle language keys in regular text with another language specified', function (done) { + it('should handle language keys in regular text with another language specified', function () { var translator = Translator.create('de'); - translator.translate('[[global:home]] test').then(function (translated) { + return translator.translate('[[global:home]] test').then(function (translated) { assert.strictEqual(translated, 'Übersicht test'); - done(); }); }); - it('should handle language keys with parameters', function (done) { + it('should handle language keys with parameters', function () { var translator = Translator.create('en-GB'); - translator.translate('[[global:pagination.out_of, 1, 5]]').then(function (translated) { + return translator.translate('[[global:pagination.out_of, 1, 5]]').then(function (translated) { assert.strictEqual(translated, '1 out of 5'); - done(); }); }); - it('should handle language keys inside language keys', function (done) { + it('should handle language keys inside language keys', function () { var translator = Translator.create('en-GB'); - translator.translate('[[notifications:outgoing_link_message, [[global:guest]]]]').then(function (translated) { + return translator.translate('[[notifications:outgoing_link_message, [[global:guest]]]]').then(function (translated) { assert.strictEqual(translated, 'You are now leaving Guest'); - done(); }); }); - it('should handle language keys inside language keys with multiple parameters', function (done) { + it('should handle language keys inside language keys with multiple parameters', function () { var translator = Translator.create('en-GB'); - translator.translate('[[notifications:user_posted_to, [[global:guest]], My Topic]]').then(function (translated) { + return translator.translate('[[notifications:user_posted_to, [[global:guest]], My Topic]]').then(function (translated) { assert.strictEqual(translated, 'Guest has posted a reply to: My Topic'); - done(); }); }); - it('should handle language keys inside language keys with all parameters as language keys', function (done) { + it('should handle language keys inside language keys with all parameters as language keys', function () { var translator = Translator.create('en-GB'); - translator.translate('[[notifications:user_posted_to, [[global:guest]], [[global:guest]]]]').then(function (translated) { + return translator.translate('[[notifications:user_posted_to, [[global:guest]], [[global:guest]]]]').then(function (translated) { assert.strictEqual(translated, 'Guest has posted a reply to: Guest'); - done(); }); }); - it('should properly handle parameters that contain square brackets', function (done) { + it('should properly handle parameters that contain square brackets', function () { var translator = Translator.create('en-GB'); - translator.translate('[[global:pagination.out_of, [guest], [[global:home]]]]').then(function (translated) { + return translator.translate('[[global:pagination.out_of, [guest], [[global:home]]]]').then(function (translated) { assert.strictEqual(translated, '[guest] out of Home'); - done(); }); }); - it('should properly handle parameters that contain parentheses', function (done) { + it('should properly handle parameters that contain parentheses', function () { var translator = Translator.create('en-GB'); - translator.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]').then(function (translated) { + return translator.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]').then(function (translated) { assert.strictEqual(translated, '(foobar) out of Home'); - done(); }); }); - it('should not translate language key parameters with HTML in them', function (done) { + it('should escape language key parameters with HTML in them', function () { var translator = Translator.create('en-GB'); var key = '[[global:403.login, test]]'; - translator.translate(key).then(function (translated) { + return translator.translate(key).then(function (translated) { assert.strictEqual(translated, 'Perhaps you should try logging in?'); - done(); }); }); - it('should properly escape and ignore % and \\, in arguments', function (done) { + it('should properly escape and ignore % and \\, in arguments', function () { var translator = Translator.create('en-GB'); var title = 'Test 1\\, 2\\, 3 % salmon'; var key = "[[topic:composer.replying_to, " + title + "]]"; - translator.translate(key).then(function (translated) { + return translator.translate(key).then(function (translated) { assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon'); - done(); }); }); - it('should not translate [[derp] some text', function (done) { + it('should not translate [[derp] some text', function () { var translator = Translator.create('en-GB'); - translator.translate('[[derp] some text').then(function (translated) { + return translator.translate('[[derp] some text').then(function (translated) { assert.strictEqual('[[derp] some text', translated); - done(); }); }); - it('should not translate [[derp:xyz] some text', function (done) { + it('should not translate [[derp:xyz] some text', function () { var translator = Translator.create('en-GB'); - translator.translate('[[derp:xyz] some text').then(function (translated) { + return translator.translate('[[derp:xyz] some text').then(function (translated) { assert.strictEqual('[[derp:xyz] some text', translated); - done(); }); }); - it('should translate [[pages:users/latest]] properly', function (done) { + it('should translate keys with slashes properly', function () { var translator = Translator.create('en-GB'); - translator.translate('[[pages:users/latest]]').then(function (translated) { + return translator.translate('[[pages:users/latest]]').then(function (translated) { assert.strictEqual(translated, 'Latest Users'); - done(); }); }); }); @@ -187,7 +171,7 @@ describe('Translator.create()', function () { }); describe('Translator modules', function () { - it('should work before registered', function (done) { + it('should work before registered', function () { var translator = Translator.create(); Translator.registerModule('test-custom-integer-format', function (lang) { @@ -206,20 +190,16 @@ describe('Translator modules', function () { }; }); - translator.translate('[[test-custom-integer-format:octal, 24]]') - .then(function (translation) { + return translator.translate('[[test-custom-integer-format:octal, 24]]').then(function (translation) { assert.strictEqual(translation, '30'); - done(); }); }); - it('should work after registered', function (done) { + it('should work after registered', function () { var translator = Translator.create('de'); - translator.translate('[[test-custom-integer-format:octal, 23]]') - .then(function (translation) { + return translator.translate('[[test-custom-integer-format:octal, 23]]').then(function (translation) { assert.strictEqual(translation, '27'); - done(); }); }); From a81aad61ab6e7c47f1460e20583cae7c469c68e5 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 1 Dec 2016 17:23:06 -0700 Subject: [PATCH 04/16] Add tests for translator static methods --- public/src/modules/translator.js | 6 ++++-- test/translator.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js index e60385ed79..d975e16e1d 100644 --- a/public/src/modules/translator.js +++ b/public/src/modules/translator.js @@ -395,11 +395,13 @@ /** * Construct a translator pattern + * @param {string} name - Translation name + * @param {...string} arg - Optional argument for the pattern */ Translator.compile = function compile() { - var args = Array.prototype.slice.call(arguments, 0); + var args = Array.prototype.slice.call(arguments, 0); - return '[[' + args.join(', ') + ']]'; + return '[[' + args.join(', ') + ']]'; }; return Translator; diff --git a/test/translator.js b/test/translator.js index 60fb589e0d..a89681adef 100644 --- a/test/translator.js +++ b/test/translator.js @@ -223,4 +223,33 @@ describe('Translator static methods', function () { done(); }); }); + describe('.escape', function () { + it('should escape translation patterns within text', function (done) { + assert.strictEqual( + Translator.escape('some nice text [[global:home]] here'), + 'some nice text \\[\\[global:home\\]\\] here' + ); + done(); + }); + }); + + describe('.unescape', function () { + it('should unescape escaped translation patterns within text', function (done) { + assert.strictEqual( + Translator.unescape('some nice text \\[\\[global:home\\]\\] here'), + 'some nice text [[global:home]] here' + ); + done(); + }); + }); + + describe('.compile', function () { + it('should create a translator pattern from a key and list of arguments', function (done) { + assert.strictEqual( + Translator.compile('amazing:cool', 'awesome', 'great'), + '[[amazing:cool, awesome, great]]' + ); + done(); + }); + }); }); From bb5fe0cc8396bcecd2bb519a148d365ebc6f3a40 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Sat, 10 Dec 2016 20:41:49 -0700 Subject: [PATCH 05/16] Escape arguments in `Translator.compile` --- public/src/modules/translator.js | 7 +++++-- test/translator.js | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js index d975e16e1d..30b67ef3a8 100644 --- a/public/src/modules/translator.js +++ b/public/src/modules/translator.js @@ -399,9 +399,12 @@ * @param {...string} arg - Optional argument for the pattern */ Translator.compile = function compile() { - var args = Array.prototype.slice.call(arguments, 0); + var args = Array.prototype.slice.call(arguments, 0).map(function (text) { + // escape commas and percent signs in arguments + return text.replace(/%/g, '%').replace(/,/g, ','); + }); - return '[[' + args.join(', ') + ']]'; + return '[[' + args.join(', ') + ']]'; }; return Translator; diff --git a/test/translator.js b/test/translator.js index a89681adef..8198814164 100644 --- a/test/translator.js +++ b/test/translator.js @@ -251,5 +251,13 @@ describe('Translator static methods', function () { ); done(); }); + + it('should escape `%` and `,` in arguments', function (done) { + assert.strictEqual( + Translator.compile('amazing:cool', '100% awesome!', 'one, two, and three'), + '[[amazing:cool, 100% awesome!, one, two, and three]]' + ); + done(); + }); }); }); From db1fdb897fbebdeb1ae334d562bf190c3f549614 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Sat, 10 Dec 2016 20:57:34 -0700 Subject: [PATCH 06/16] Fixes for dev-ing on windows - Change `nodebb.bat` to simply run `node ./nodebb` with same arguments - Fix `npm test` for windows --- nodebb.bat | 123 +-------------------------------------------------- package.json | 5 +-- 2 files changed, 3 insertions(+), 125 deletions(-) diff --git a/nodebb.bat b/nodebb.bat index d432dd3143..daaf09224f 100644 --- a/nodebb.bat +++ b/nodebb.bat @@ -1,122 +1 @@ -@echo off - -rem %1 action -rem %2 subaction - -setlocal enabledelayedexpansion -2>nul call :CASE_%1 -if ERRORLEVEL 1 call :DEFAULT_CASE - -exit /B - -:CASE_start - echo Starting NodeBB - echo "nodebb.bat stop" to stop the NodeBB server - echo "nodebb.bat log" to view server output - - rem Start the loader daemon - node loader %* - - goto END_CASE - -:CASE_stop - call :pidexists - if %_result%==0 ( - echo NodeBB is already stopped. - ) else ( - echo Stopping NodeBB. Goodbye! - - rem Doing this forcefully is probably not the best method - taskkill /PID !_pid! /f>nul - ) - - goto END_CASE - -:CASE_restart - echo Unsupported - - goto END_CASE - -:CASE_reload - echo Unsupported - - goto END_CASE - -:CASE_status - call :pidexists - if %_result%==0 ( - echo NodeBB is not running - echo "nodebb.bat start" to launch the NodeBB server - ) else ( - echo NodeBB Running ^(pid !_pid!^) - echo "nodebb.bat stop" to stop the NodeBB server - echo "nodebb.bat log" to view server output - echo "nodebb.bat restart" to restart NodeBB - ) - - goto END_CASE - -:CASE_log - cls - type .\logs\output.log - - goto END_CASE - -:CASE_upgrade - call npm install - call npm i nodebb-theme-vanilla nodebb-theme-lavender nodebb-widget-essentials - node app --upgrade - copy /b package.json +,,>nul - - goto END_CASE - -:CASE_setup - node app --setup %* - - goto END_CASE - -:CASE_reset - node app --reset --%2 - - goto END_CASE - -:CASE_dev - echo Launching NodeBB in "development" mode. - echo To run the production build of NodeBB, please use "forever". - echo More Information: https://docs.nodebb.org/en/latest/running/index.html - set NODE_ENV=development - node loader --no-daemon %* - - goto END_CASE - -:CASE_watch - echo Not supported - - goto END_CASE - -:DEFAULT_CASE - echo Welcome to NodeBB - echo Usage: nodebb.bat ^{start^|stop^|reload^|restart^|log^|setup^|reset^|upgrade^|dev^|watch^} - - goto END_CASE - -:END_CASE - endlocal - VER > NUL - goto :EOF - -:pidexists -if exist %~dp0pidfile ( - set /p _pid= Date: Sun, 11 Dec 2016 15:58:57 +0100 Subject: [PATCH 07/16] mongo: set scores as float instead of int --- src/database/mongo/sorted.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index 11d4676b95..583794a380 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -17,7 +17,7 @@ module.exports = function (db, module) { value = helpers.valueToString(value); - db.collection('objects').update({_key: key, value: value}, {$set: {score: parseInt(score, 10)}}, {upsert:true, w: 1}, function (err) { + db.collection('objects').update({_key: key, value: value}, {$set: {score: parseFloat(score)}}, {upsert:true, w: 1}, function (err) { if (err && err.message.startsWith('E11000 duplicate key error')) { return process.nextTick(module.sortedSetAdd, key, score, value, callback); } @@ -38,7 +38,7 @@ module.exports = function (db, module) { var bulk = db.collection('objects').initializeUnorderedBulkOp(); for(var i = 0; i < scores.length; ++i) { - bulk.find({_key: key, value: values[i]}).upsert().updateOne({$set: {score: parseInt(scores[i], 10)}}); + bulk.find({_key: key, value: values[i]}).upsert().updateOne({$set: {score: parseFloat(scores[i])}}); } bulk.execute(function (err) { @@ -56,7 +56,7 @@ module.exports = function (db, module) { var bulk = db.collection('objects').initializeUnorderedBulkOp(); for(var i = 0; i < keys.length; ++i) { - bulk.find({_key: keys[i], value: value}).upsert().updateOne({$set: {score: parseInt(score, 10)}}); + bulk.find({_key: keys[i], value: value}).upsert().updateOne({$set: {score: parseFloat(score)}}); } bulk.execute(function (err) { @@ -564,7 +564,7 @@ module.exports = function (db, module) { } var data = {}; value = helpers.valueToString(value); - data.score = parseInt(increment, 10); + data.score = parseFloat(increment); db.collection('objects').findAndModify({_key: key, value: value}, {}, {$inc: data}, {new: true, upsert: true}, function (err, result) { // if there is duplicate key error retry the upsert From ec5d13188bf8939abc0b171b3e67f58b787a3ebb Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Sun, 11 Dec 2016 15:59:28 +0100 Subject: [PATCH 08/16] sneakily adjust database/sorted tests to include float scores --- test/database/sorted.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/database/sorted.js b/test/database/sorted.js index dc57c8e0e0..ac95fcbe09 100644 --- a/test/database/sorted.js +++ b/test/database/sorted.js @@ -10,7 +10,7 @@ describe('Sorted Set methods', function () { before(function (done) { async.parallel([ function (next) { - db.sortedSetAdd('sortedSetTest1', [1, 2, 3], ['value1', 'value2', 'value3'], next); + db.sortedSetAdd('sortedSetTest1', [1.1, 1.2, 1.3], ['value1', 'value2', 'value3'], next); }, function (next) { db.sortedSetAdd('sortedSetTest2', [1, 4], ['value1', 'value4'], next); @@ -97,7 +97,7 @@ describe('Sorted Set methods', function () { db.getSortedSetRangeWithScores('sortedSetTest1', 0, -1, function (err, values) { assert.equal(err, null); assert.equal(arguments.length, 2); - assert.deepEqual(values, [{value: 'value1', score: 1}, {value: 'value2', score: 2}, {value: 'value3', score: 3}]); + assert.deepEqual(values, [{value: 'value1', score: 1.1}, {value: 'value2', score: 1.2}, {value: 'value3', score: 1.3}]); done(); }); }); @@ -108,7 +108,7 @@ describe('Sorted Set methods', function () { db.getSortedSetRevRangeWithScores('sortedSetTest1', 0, -1, function (err, values) { assert.equal(err, null); assert.equal(arguments.length, 2); - assert.deepEqual(values, [{value: 'value3', score: 3}, {value: 'value2', score: 2}, {value: 'value1', score: 1}]); + assert.deepEqual(values, [{value: 'value3', score: 1.3}, {value: 'value2', score: 1.2}, {value: 'value1', score: 1.1}]); done(); }); }); @@ -116,7 +116,7 @@ describe('Sorted Set methods', function () { describe('getSortedSetRangeByScore()', function () { it('should get count elements with score between min max sorted by score lowest to highest', function (done) { - db.getSortedSetRangeByScore('sortedSetTest1', 0, -1, '-inf', 2, function (err, values) { + db.getSortedSetRangeByScore('sortedSetTest1', 0, -1, '-inf', 1.2, function (err, values) { assert.equal(err, null); assert.equal(arguments.length, 2); assert.deepEqual(values, ['value1', 'value2']); @@ -127,7 +127,7 @@ describe('Sorted Set methods', function () { describe('getSortedSetRevRangeByScore()', function () { it('should get count elements with score between max min sorted by score highest to lowest', function (done) { - db.getSortedSetRevRangeByScore('sortedSetTest1', 0, -1, '+inf', 2, function (err, values) { + db.getSortedSetRevRangeByScore('sortedSetTest1', 0, -1, '+inf', 1.2, function (err, values) { assert.equal(err, null); assert.equal(arguments.length, 2); assert.deepEqual(values, ['value3', 'value2']); @@ -138,10 +138,10 @@ describe('Sorted Set methods', function () { describe('getSortedSetRangeByScoreWithScores()', function () { it('should get count elements with score between min max sorted by score lowest to highest with scores', function (done) { - db.getSortedSetRangeByScoreWithScores('sortedSetTest1', 0, -1, '-inf', 2, function (err, values) { + db.getSortedSetRangeByScoreWithScores('sortedSetTest1', 0, -1, '-inf', 1.2, function (err, values) { assert.equal(err, null); assert.equal(arguments.length, 2); - assert.deepEqual(values, [{value: 'value1', score: 1}, {value: 'value2', score: 2}]); + assert.deepEqual(values, [{value: 'value1', score: 1.1}, {value: 'value2', score: 1.2}]); done(); }); }); @@ -149,10 +149,10 @@ describe('Sorted Set methods', function () { describe('getSortedSetRevRangeByScoreWithScores()', function () { it('should get count elements with score between max min sorted by score highest to lowest', function (done) { - db.getSortedSetRevRangeByScoreWithScores('sortedSetTest1', 0, -1, '+inf', 2, function (err, values) { + db.getSortedSetRevRangeByScoreWithScores('sortedSetTest1', 0, -1, '+inf', 1.2, function (err, values) { assert.equal(err, null); assert.equal(arguments.length, 2); - assert.deepEqual(values, [{value: 'value3', score: 3}, {value: 'value2', score: 2}]); + assert.deepEqual(values, [{value: 'value3', score: 1.3}, {value: 'value2', score: 1.2}]); done(); }); }); @@ -169,7 +169,7 @@ describe('Sorted Set methods', function () { }); it('should return number of elements between scores min max inclusive', function (done) { - db.sortedSetCount('sortedSetTest1', '-inf', 2, function (err, count) { + db.sortedSetCount('sortedSetTest1', '-inf', 1.2, function (err, count) { assert.equal(err, null); assert.equal(arguments.length, 2); assert.equal(count, 2); @@ -321,7 +321,7 @@ describe('Sorted Set methods', function () { db.sortedSetScore('sortedSetTest1', 'value2', function (err, score) { assert.equal(err, null); assert.equal(arguments.length, 2); - assert.equal(score, 2); + assert.equal(score, 1.2); done(); }); }); @@ -332,7 +332,7 @@ describe('Sorted Set methods', function () { db.sortedSetsScore(['sortedSetTest1', 'sortedSetTest2', 'doesnotexist'], 'value1', function (err, scores) { assert.equal(err, null); assert.equal(arguments.length, 2); - assert.deepEqual(scores, [1, 1, null]); + assert.deepEqual(scores, [1.1, 1, null]); done(); }); }); @@ -355,7 +355,7 @@ describe('Sorted Set methods', function () { db.sortedSetScores('sortedSetTest1', ['value2', 'value1', 'doesnotexist'], function (err, scores) { assert.equal(err, null); assert.equal(arguments.length, 2); - assert.deepEqual(scores, [2, 1, null]); + assert.deepEqual(scores, [1.2, 1.1, null]); done(); }); }); From 0a4e45c0da7efde86367c39ec80051737aff2336 Mon Sep 17 00:00:00 2001 From: pichalite Date: Tue, 13 Dec 2016 01:18:42 +0000 Subject: [PATCH 09/16] Check password length on setup --- src/install.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/install.js b/src/install.js index acf7aadc97..910fd8c46b 100644 --- a/src/install.js +++ b/src/install.js @@ -220,9 +220,10 @@ function createAdministrator(next) { } function createAdmin(callback) { - var User = require('./user'), - Groups = require('./groups'), - password; + var User = require('./user'); + var Groups = require('./groups'); + var password; + var meta = require('./meta'); winston.warn('No administrators have been detected, running initial user setup\n'); @@ -262,6 +263,12 @@ function createAdmin(callback) { winston.warn("Passwords did not match, please try again"); return retryPassword(results); } + + if (results.password.length < meta.config.minimumPasswordLength) { + winston.warn("Password too short, please try again"); + return retryPassword(results); + } + var adminUid; async.waterfall([ function (next) { From aea08d58b1a75b07c4de5aada230dc1efe6d6337 Mon Sep 17 00:00:00 2001 From: Anil Mandepudi Date: Mon, 12 Dec 2016 17:19:59 -0800 Subject: [PATCH 10/16] Fix tabs --- src/install.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/install.js b/src/install.js index 910fd8c46b..8c14302d80 100644 --- a/src/install.js +++ b/src/install.js @@ -221,8 +221,8 @@ function createAdministrator(next) { function createAdmin(callback) { var User = require('./user'); - var Groups = require('./groups'); - var password; + var Groups = require('./groups'); + var password; var meta = require('./meta'); winston.warn('No administrators have been detected, running initial user setup\n'); From 35a15f37ece9d110e3c1bc7d39fec91868434b9a Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 13 Dec 2016 14:01:07 +0300 Subject: [PATCH 11/16] closes #5276 --- src/user/email.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/user/email.js b/src/user/email.js index 5eb93b71e0..99d2d9693d 100644 --- a/src/user/email.js +++ b/src/user/email.js @@ -100,6 +100,9 @@ var emailer = require('../emailer'); async.apply(db.delete, 'uid:' + confirmObj.uid + ':confirm:email:sent'), function (next) { db.sortedSetRemove('users:notvalidated', confirmObj.uid, next); + }, + function (next) { + plugins.fireHook('action:user.email.confirmed', {uid: confirmObj.uid, email: confirmObj.email}, next); } ], function (err) { callback(err ? new Error('[[error:email-confirm-failed]]') : null); From e86708cb0c7632e09528a470c6ec64b4464d1e04 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 13 Dec 2016 14:11:56 +0300 Subject: [PATCH 12/16] add cid to widgets.render init date pickers in widgets ACP --- public/src/admin/extend/widgets.js | 11 +++++++++++ public/src/widgets.js | 1 + src/controllers/api.js | 17 ++++++----------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/public/src/admin/extend/widgets.js b/public/src/admin/extend/widgets.js index 761a57f423..5b7c3a13e1 100644 --- a/public/src/admin/extend/widgets.js +++ b/public/src/admin/extend/widgets.js @@ -54,6 +54,7 @@ define('admin/extend/widgets', ['jqueryui'], function (jqueryui) { $('#widgets .widget-area').sortable({ update: function (event, ui) { + createDatePicker(ui.item); appendToggle(ui.item); }, connectWith: "div" @@ -154,6 +155,15 @@ define('admin/extend/widgets', ['jqueryui'], function (jqueryui) { }); } + function createDatePicker(el) { + var currentYear = new Date().getFullYear(); + el.find('.date-selector').datepicker({ + changeMonth: true, + changeYear: true, + yearRange: currentYear + ':' + (currentYear + 100) + }); + } + function appendToggle(el) { if (!el.hasClass('block')) { el.addClass('block').css('width', '').css('height', '') @@ -209,6 +219,7 @@ define('admin/extend/widgets', ['jqueryui'], function (jqueryui) { widgetArea.append(populateWidget(widgetEl, widgetData.data)); appendToggle(widgetEl); + createDatePicker(widgetEl); } } diff --git a/public/src/widgets.js b/public/src/widgets.js index 8d4eb10e1f..46023fad15 100644 --- a/public/src/widgets.js +++ b/public/src/widgets.js @@ -33,6 +33,7 @@ locations: widgetLocations, template: template + '.tpl', url: url, + cid: ajaxify.data.cid, isMobile: utils.isMobile() }, function (renderedAreas) { for (var x = 0; x < renderedAreas.length; ++x) { diff --git a/src/controllers/api.js b/src/controllers/api.js index f9e317cee7..1bbf3962c8 100644 --- a/src/controllers/api.js +++ b/src/controllers/api.js @@ -109,22 +109,17 @@ apiController.getConfig = function (req, res, next) { apiController.renderWidgets = function (req, res, next) { - var areas = { - template: req.query.template, - locations: req.query.locations, - url: req.query.url - }; - - if (!areas.template || !areas.locations) { + if (!req.query.template || !req.query.locations) { return res.status(200).json({}); } widgets.render(req.uid, { - template: areas.template, - url: areas.url, - locations: areas.locations, - isMobile: req.query.isMobile === 'true' + template: req.query.template, + url: req.query.url, + locations: req.query.locations, + isMobile: req.query.isMobile === 'true', + cid: req.query.cid }, req, res, From 97c0b33cf67b97365aecd8f1ef8d829d816ad421 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 13 Dec 2016 14:22:37 +0300 Subject: [PATCH 13/16] filter:middleware.render --- src/middleware/render.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/middleware/render.js b/src/middleware/render.js index a15ee121e6..6453d09632 100644 --- a/src/middleware/render.js +++ b/src/middleware/render.js @@ -34,11 +34,6 @@ module.exports = function (middleware) { var ajaxifyData; async.waterfall([ function (next) { - plugins.fireHook('filter:' + template + '.build', {req: req, res: res, templateData: options}, next); - }, - function (data, next) { - options = data.templateData; - options.loggedIn = !!req.uid; options.relative_path = nconf.get('relative_path'); options.template = {name: template}; @@ -46,6 +41,14 @@ module.exports = function (middleware) { options.url = (req.baseUrl + req.path).replace(/^\/api/, ''); options.bodyClass = buildBodyClass(req); + plugins.fireHook('filter:' + template + '.build', {req: req, res: res, templateData: options}, next); + }, + function (data, next) { + plugins.fireHook('filter.middleware.render', {req: res, res: res, templateData: data. templateData}, next); + }, + function (data, next) { + options = data.templateData; + res.locals.template = template; options._locals = undefined; From 53fe432d8a4c171b54196e4c969ba3a42597e6b3 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 13 Dec 2016 14:27:54 +0300 Subject: [PATCH 14/16] fix hook --- src/middleware/render.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/middleware/render.js b/src/middleware/render.js index 6453d09632..59348aba34 100644 --- a/src/middleware/render.js +++ b/src/middleware/render.js @@ -44,7 +44,7 @@ module.exports = function (middleware) { plugins.fireHook('filter:' + template + '.build', {req: req, res: res, templateData: options}, next); }, function (data, next) { - plugins.fireHook('filter.middleware.render', {req: res, res: res, templateData: data. templateData}, next); + plugins.fireHook('filter:middleware.render', {req: res, res: res, templateData: data.templateData}, next); }, function (data, next) { options = data.templateData; From c32e6aaabb1da60edeeeb5531ae47f7481fed720 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 13 Dec 2016 15:43:20 +0300 Subject: [PATCH 15/16] use build module instead of forking --- app.js | 14 +++++++------- build.js | 8 +++++++- public/src/admin/appearance/themes.js | 5 +++-- src/meta/css.js | 17 +++++------------ src/socket.io/admin.js | 11 ++++------- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app.js b/app.js index 85abee4ab2..265df71850 100644 --- a/app.js +++ b/app.js @@ -75,7 +75,7 @@ if (nconf.get('setup') || nconf.get('install')) { } else if (nconf.get('reset')) { async.waterfall([ async.apply(require('./src/reset').reset), - async.apply(require('./build').build, true) + async.apply(require('./build').buildAll) ], function (err) { process.exit(err ? 1 : 0); }); @@ -180,12 +180,12 @@ function start() { }); async.waterfall([ - async.apply(db.init), + async.apply(db.init), function (next) { var meta = require('./src/meta'); async.parallel([ async.apply(db.checkCompatibility), - async.apply(meta.configs.init), + async.apply(meta.configs.init), function (next) { if (nconf.get('dep-check') === undefined || nconf.get('dep-check') !== false) { meta.dependencies.check(next); @@ -195,8 +195,8 @@ function start() { } }, function (next) { - require('./src/upgrade').check(next); - } + require('./src/upgrade').check(next); + } ], function (err) { next(err); }); @@ -257,7 +257,7 @@ function setup() { async.series([ async.apply(install.setup), async.apply(loadConfig), - async.apply(build.build, true) + async.apply(build.buildAll) ], function (err, data) { // Disregard build step data data = data[0]; @@ -302,7 +302,7 @@ function upgrade() { async.apply(db.init), async.apply(meta.configs.init), async.apply(upgrade.upgrade), - async.apply(build.build, true) + async.apply(build.buildAll) ], function (err) { if (err) { winston.error(err.stack); diff --git a/build.js b/build.js index 4ae2d34c77..a5174d2c70 100644 --- a/build.js +++ b/build.js @@ -5,13 +5,19 @@ var winston = require('winston'); var buildStart; +var valid = ['js', 'clientCSS', 'acpCSS', 'tpl']; + +exports.buildAll = function (callback) { + exports.build(valid.join(','), callback); +}; + exports.build = function build(targets, callback) { buildStart = Date.now(); var db = require('./src/database'); var meta = require('./src/meta'); var plugins = require('./src/plugins'); - var valid = ['js', 'clientCSS', 'acpCSS', 'tpl']; + targets = (targets === true ? valid : targets.split(',').filter(function (target) { return valid.indexOf(target) !== -1; diff --git a/public/src/admin/appearance/themes.js b/public/src/admin/appearance/themes.js index 0c71baa4c4..10daae4430 100644 --- a/public/src/admin/appearance/themes.js +++ b/public/src/admin/appearance/themes.js @@ -3,7 +3,7 @@ define('admin/appearance/themes', ['translator'], function (translator) { var Themes = {}; - + Themes.init = function () { $('#installed_themes').on('click', function (e) { var target = $(e.target), @@ -23,6 +23,7 @@ define('admin/appearance/themes', ['translator'], function (translator) { if (err) { return app.alertError(err.message); } + config['theme:id'] = themeId; highlightSelectedTheme(themeId); app.alert({ @@ -38,7 +39,7 @@ define('admin/appearance/themes', ['translator'], function (translator) { }); } }); - + translator.translate('[[admin/appearance/themes:revert-confirm]]', function (revert) { $('#revert_theme').on('click', function () { bootbox.confirm(revert, function (confirm) { diff --git a/src/meta/css.js b/src/meta/css.js index b9bc1787b6..0d63e09d6d 100644 --- a/src/meta/css.js +++ b/src/meta/css.js @@ -153,15 +153,13 @@ module.exports = function (Meta) { }; function minify(source, paths, destination, callback) { + callback = callback || function () {}; less.render(source, { paths: paths }, function (err, lessOutput) { if (err) { winston.error('[meta/css] Could not minify LESS/CSS: ' + err.message); - if (typeof callback === 'function') { - callback(err); - } - return; + return callback(err); } postcss([ autoprefixer, clean() ]).process(lessOutput.css).then(function (result) { @@ -171,19 +169,14 @@ module.exports = function (Meta) { Meta.css[destination] = result.css; // Save the compiled CSS in public/ so things like nginx can serve it - if (nconf.get('isPrimary') === 'true' && (nconf.get('local-assets') === undefined || nconf.get('local-assets') !== false)) { + if (nconf.get('local-assets') === undefined || nconf.get('local-assets') !== false) { return Meta.css.commitToFile(destination, function () { - if (typeof callback === 'function') { - callback(null, result.css); - } + callback(null, result.css); }); } - if (typeof callback === 'function') { - callback(null, result.css); - } + callback(null, result.css); }); - }); } diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 141d567f11..f1b4e402c1 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -62,14 +62,11 @@ SocketAdmin.reload = function (socket, data, callback) { }; SocketAdmin.restart = function (socket, data, callback) { - // Rebuild assets and reload NodeBB - var child_process = require('child_process'); - var build_worker = child_process.fork('app.js', ['--build'], { - cwd: path.join(__dirname, '../../'), - stdio: 'pipe' - }); + require('../../build').buildAll(function (err) { + if (err) { + return callback(err) + } - build_worker.on('exit', function () { events.log({ type: 'build', uid: socket.uid, From fba28791f5d9ec7bfa1d94325efb7f7c4d5e551f Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 13 Dec 2016 16:08:54 +0300 Subject: [PATCH 16/16] missing ; --- src/socket.io/admin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index f1b4e402c1..d9312cc871 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -64,7 +64,7 @@ SocketAdmin.reload = function (socket, data, callback) { SocketAdmin.restart = function (socket, data, callback) { require('../../build').buildAll(function (err) { if (err) { - return callback(err) + return callback(err); } events.log({