diff --git a/src/middleware/header.js b/src/middleware/header.js index d90fcaedb4..a606b0f19a 100644 --- a/src/middleware/header.js +++ b/src/middleware/header.js @@ -1,5 +1,6 @@ 'use strict'; +var path = require('path'); var async = require('async'); var nconf = require('nconf'); var jsesc = require('jsesc'); @@ -14,7 +15,9 @@ var plugins = require('../plugins'); var navigation = require('../navigation'); var translator = require('../translator'); var privileges = require('../privileges'); +var languages = require('../languages'); var utils = require('../utils'); +var file = require('../file'); var controllers = { api: require('../controllers/api'), @@ -223,11 +226,6 @@ module.exports = function (middleware) { ], callback); }; - function addTimeagoLocaleScript(scripts, userLang) { - var languageCode = utils.userLangToTimeagoCode(userLang); - scripts.push({ src: nconf.get('relative_path') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '.js' }); - } - middleware.renderFooter = function renderFooter(req, res, data, callback) { async.waterfall([ function (next) { @@ -240,15 +238,35 @@ module.exports = function (middleware) { function (data, next) { async.parallel({ scripts: async.apply(plugins.fireHook, 'filter:scripts.get', []), + timeagoLocale: (next) => { + const userLang = res.locals.config.userLang; + const pathToLocaleFile = '/vendor/jquery/timeago/locales/jquery.timeago.' + utils.userLangToTimeagoCode(userLang) + '.js'; + + async.waterfall([ + async.apply(languages.list), + (languages, next) => { + if (!languages.some(obj => obj.code === userLang)) { + return next(null, false); + } + + file.exists(path.join(__dirname, '../../public', pathToLocaleFile), next); + }, + (exists, next) => { + next(null, exists ? (nconf.get('relative_path') + '/assets' + pathToLocaleFile) : null); + }, + ], next); + }, }, function (err, results) { next(err, data, results); }); }, function (data, results, next) { + if (results.timeagoLocale) { + results.scripts.push(results.timeagoLocale); + } data.templateValues.scripts = results.scripts.map(function (script) { return { src: script }; }); - addTimeagoLocaleScript(data.templateValues.scripts, res.locals.config.userLang); data.templateValues.useCustomJS = meta.config.useCustomJS; data.templateValues.customJS = data.templateValues.useCustomJS ? meta.config.customJS : ''; diff --git a/src/middleware/index.js b/src/middleware/index.js index 44f2ff346b..896ddbf76c 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -13,7 +13,6 @@ var plugins = require('../plugins'); var meta = require('../meta'); var user = require('../user'); var groups = require('../groups'); -var file = require('../file'); var analytics = require('../analytics'); @@ -174,29 +173,6 @@ middleware.applyBlacklist = function applyBlacklist(req, res, next) { }); }; -middleware.processTimeagoLocales = function processTimeagoLocales(req, res, next) { - var fallback = !req.path.includes('-short') ? 'jquery.timeago.en.js' : 'jquery.timeago.en-short.js'; - var localPath = path.join(__dirname, '../../public/vendor/jquery/timeago/locales', req.path); - - async.waterfall([ - function (next) { - file.exists(localPath, next); - }, - function (exists, next) { - if (exists) { - next(null, localPath); - } else { - next(null, path.join(__dirname, '../../public/vendor/jquery/timeago/locales', fallback)); - } - }, - function (path) { - res.status(200).sendFile(path, { - maxAge: req.app.enabled('cache') ? 5184000000 : 0, - }); - }, - ], next); -}; - middleware.delayLoading = function delayLoading(req, res, next) { // Introduces an artificial delay during load so that brute force attacks are effectively mitigated diff --git a/src/routes/index.js b/src/routes/index.js index 8ac8ee2d43..40abf06e42 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -187,7 +187,6 @@ function addCoreRoutes(app, router, middleware, callback) { res.redirect(relativePath + '/assets/client.css?' + meta.config['cache-buster']); }); - app.use(relativePath + '/assets/vendor/jquery/timeago/locales', middleware.processTimeagoLocales); app.use(controllers['404'].handle404); app.use(controllers.errors.handleURIErrors); app.use(controllers.errors.handleErrors); diff --git a/test/controllers.js b/test/controllers.js index 83a54e50ec..2e101b9d87 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -1807,11 +1807,18 @@ describe('Controllers', function () { }); }); - it('should load timeago locale', function (done) { - request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.404.js', function (err, res, body) { + it('should return not found if NodeBB language exists but timeago locale does not exist', function (done) { + request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.ms.js', function (err, res, body) { assert.ifError(err); - assert.equal(res.statusCode, 200); - assert(body.includes('English')); + assert.equal(res.statusCode, 404); + done(); + }); + }); + + it('should return not found if NodeBB language does not exist', function (done) { + request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.muggle.js', function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 404); done(); }); });