From 42d77080f36f4b8bd0b857b8a2e5884c37f873de Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 30 Dec 2013 16:19:59 -0500 Subject: [PATCH 1/4] plugins: filter:server.create_routes - allow plugins to add custom templates and/or modify individual blocks withiin a template --- src/webserver.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/webserver.js b/src/webserver.js index 0ac8e00fe7..3606113cc8 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -807,7 +807,8 @@ var path = require('path'), var custom_routes = { 'routes': [], - 'api': [] + 'api': [], + 'templates': [] }; plugins.ready(function() { @@ -843,6 +844,17 @@ var path = require('path'), } } + var templateRoutes = custom_routes.templates; + for (var route in templateRoutes) { + if (templateRoutes.hasOwnProperty(route)) { + (function(route) { + app.get('/templates/' + templateRoutes[route].template, function(req, res) { + res.send(templateRoutes[route].content); + }); + }(route)); + } + } + }); }); From 6dcd06b63c76e15225518e8bc2b7034bd77993ab Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 30 Dec 2013 16:26:30 -0500 Subject: [PATCH 2/4] removed unused parameter in templates.prepare --- public/src/templates.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/src/templates.js b/public/src/templates.js index 5829ac5195..87c86b3052 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -48,7 +48,7 @@ } }; - templates.prepare = function (raw_tpl, data) { + templates.prepare = function (raw_tpl) { var template = {}; template.html = raw_tpl; template.parse = parse; From 69e0aa338d060370b59e3a7445900598ee4ae0f4 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 30 Dec 2013 17:11:59 -0500 Subject: [PATCH 3/4] resolved some todo that's been in there for many months --- app.js | 18 +++++++----------- public/src/templates.js | 4 +++- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index e1201f1bd1..c27163ffa4 100644 --- a/app.js +++ b/app.js @@ -27,8 +27,9 @@ async = require('async'), semver = require('semver'), winston = require('winston'), - pkg = require('./package.json'), path = require('path'), + pkg = require('./package.json'), + utils = require('./public/src/utils.js'), meta; // Runtime environment @@ -108,16 +109,11 @@ var customTemplates = meta.config['theme:templates'] ? path.join(__dirname, 'node_modules', meta.config['theme:id'], meta.config['theme:templates']) : false; - // todo: replace below with read directory code, derp. - templates.init([ - 'header', 'footer', 'logout', 'outgoing', 'admin/header', 'admin/footer', 'admin/index', - 'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext', - 'emails/header', 'emails/footer', - - 'noscript/header', 'noscript/home', 'noscript/category', 'noscript/topic' - ], customTemplates); - - + + utils.walk(path.join(__dirname, 'public/templates'), function (err, tplsToLoad) { + templates.init(tplsToLoad, customTemplates); + }); + plugins.ready(function() { templates.ready(webserver.init); }); diff --git a/public/src/templates.js b/public/src/templates.js index 87c86b3052..0c90900d9c 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -78,7 +78,9 @@ global.templates[file] = new template; loaded--; - if (loaded == 0) templates.ready(); + if (loaded === 0) { + templates.ready(); + } }); }(templatesToLoad[t])); } From 50323c3d23f52874b77028ae57057b0664e60555 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Mon, 30 Dec 2013 19:40:57 -0500 Subject: [PATCH 4/4] guarding against Errors on Socket Disconnect Saw this ``` TypeError: Cannot call method 'indexOf' of undefined at Socket. (/home/admin/NodeBB/src/websockets.js:108:33) at Socket.EventEmitter.emit [as $emit] (events.js:95:17) at Socket.onDisconnect (/home/admin/NodeBB/node_modules/socket.io/lib/socket.js:153:10) at SocketNamespace.handleDisconnect (/home/admin/NodeBB/node_modules/socket.io/lib/namespace.js:229:46) ``` and this ``` /home/admin/NodeBB/src/websockets.js:113 if (userSockets[uid].length === 0) { ^ TypeError: Cannot read property 'length' of undefined at Socket. (/home/admin/NodeBB/src/websockets.js:113:24) ``` in my logs, when users are disconnecting --- src/websockets.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/websockets.js b/src/websockets.js index 8d16a66c08..310af2fbc9 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -106,12 +106,12 @@ websockets.init = function(io) { socket.on('disconnect', function() { - var index = userSockets[uid].indexOf(socket); + var index = (userSockets[uid] || []).indexOf(socket); if (index !== -1) { userSockets[uid].splice(index, 1); } - if (userSockets[uid].length === 0) { + if (userSockets[uid] && userSockets[uid].length === 0) { delete users[sessionID]; delete userSockets[uid]; if (uid) {