From 086013829bb091ffd0f11e88bcac3ab8bcbc48f5 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Thu, 15 May 2014 03:06:47 -0400 Subject: [PATCH 1/8] added 'filter:register.build' hook to augment register template data --- src/controllers/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/controllers/index.js b/src/controllers/index.js index 31ea691136..4bb46ed943 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -209,7 +209,13 @@ Controllers.register = function(req, res, next) { data.minimumPasswordLength = meta.config.minimumPasswordLength; data.termsOfUse = meta.config.termsOfUse; - res.render('register', data); + data.captcha = null; + plugins.fireHook('filter:register.build', req, res, data, function(err, data) { + if (err && process.env === 'development') { + winston.warn(JSON.stringify(err)); + } + res.render('register', data); + }); }; From 61a5d4768a0fc4887cc17bfec252d8bb0deaaf81 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Thu, 15 May 2014 03:07:38 -0400 Subject: [PATCH 2/8] global var --- src/plugins.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins.js b/src/plugins.js index c72420def7..e3c3121a6f 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -295,14 +295,14 @@ var fs = require('fs'), }; Plugins.fireHook = function(hook) { - var callback = typeof arguments[arguments.length-1] === "function" ? arguments[arguments.length-1] : null, + var callback = typeof arguments[arguments.length-1] === 'function' ? arguments[arguments.length-1] : null, args = arguments.length ? Array.prototype.slice.call(arguments, 1) : []; if (callback) { args.pop(); } - hookList = Plugins.loadedHooks[hook]; + var hookList = Plugins.loadedHooks[hook]; if (hookList && Array.isArray(hookList)) { // if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\''); From aff3c9558a01ef5bdb2a0d7ff08c6db30383dd87 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Thu, 15 May 2014 03:08:24 -0400 Subject: [PATCH 3/8] hook error print to console in dev env --- src/plugins.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins.js b/src/plugins.js index e3c3121a6f..a1c715711b 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -334,7 +334,7 @@ var fs = require('fs'), }, function(err, values) { if (err) { if (global.env === 'development') { - winston.info('[plugins] Problem executing hook: ' + hook); + winston.info('[plugins] Problem executing hook: ' + hook + ' err: ' + JSON.stringify(err)); } } From bab88893f195e6a686073dcefbe51e3b80ccb3b3 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Thu, 15 May 2014 03:10:15 -0400 Subject: [PATCH 4/8] changing 'filter:register.check' hook signature to provide req, res and data --- src/routes/authentication.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/routes/authentication.js b/src/routes/authentication.js index b203952b99..c008dd2b80 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -73,13 +73,12 @@ var userData = { username: req.body.username, password: req.body.password, - email: req.body.email, - ip: req.ip + email: req.body.email }; - plugins.fireHook('filter:register.check', userData, function(err, userData) { + plugins.fireHook('filter:register.check', req, res, userData, function(err, userData) { if (err) { - return res.redirect(nconf.get('relative_path') + '/register'); + return res.redirect(nconf.get('relative_path') + '/register' + (err.message ? '?error=' + err.message : '')); } user.create(userData, function(err, uid) { From 611fd380eb55197cf1c8fccc14d058143ce65751 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Fri, 16 May 2014 01:38:20 -0400 Subject: [PATCH 5/8] fixes topic/1503 --- public/src/app.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/src/app.js b/public/src/app.js index edf22a0c50..3484b54205 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -512,6 +512,7 @@ var socket, app.load = function() { $('document').ready(function () { var url = window.location.pathname.slice(1), + search = window.location.search, tpl_url = ajaxify.getTemplateMapping(url); url = url.replace(/\/$/, ""); @@ -551,7 +552,7 @@ var socket, if (window.history && window.history.replaceState) { var hash = window.location.hash ? window.location.hash : ''; window.history.replaceState({ - url: url + hash + url: url + search + hash }, url, RELATIVE_PATH + '/' + url + hash); } From c7734937fb0c30c7bdeea58acdc01ebc8d9e958f Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Fri, 16 May 2014 01:38:47 -0400 Subject: [PATCH 6/8] added a bunch of useful utils functions --- public/src/utils.js | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/public/src/utils.js b/public/src/utils.js index 534ef59273..944929e486 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -272,6 +272,87 @@ return env; } } + }, + + // get all the url params in a single key/value hash + params: function(options) { + var a, hash = {}, params; + + options = options || {}; + options.skipToType = options.skipToType || {}; + + if (options.url) { + a = utils.urlToLocation(options.url); + } + params = (a ? a.search : window.location.search).substring(1).split("&"); + + params.forEach(function(param) { + var val = param.split('='), + key = decodeURI(val[0]), + value = options.skipToType[key] ? decodeURI(val[1]) : utils.toType(decodeURI(val[1])); + + if (key) + hash[key] = value; + }); + return hash; + }, + + param: function(key) { + return this.params()[key]; + }, + + urlToLocation: function(url) { + var a = document.createElement('a'); + a.href = url; + return a; + }, + + // return boolean if string 'true' or string 'false', or if a parsable string which is a number + // also supports JSON object and/or arrays parsing + toType: function(str) { + var type = typeof str; + if (type !== 'string') { + return str; + } else { + var nb = parseFloat(str); + if (!isNaN(nb) && isFinite(str)) + return nb; + if (str === 'false') + return false; + if (str === 'true') + return true; + + try { + str = JSON.parse(str); + } catch (e) {} + + return str; + } + }, + + // Safely get/set chained properties on an object + // set example: utils.props(A, 'a.b.c.d', 10) // sets A to {a: {b: {c: {d: 10}}}}, and returns 10 + // get example: utils.props(A, 'a.b.c') // returns {d: 10} + // get example: utils.props(A, 'a.b.c.foo.bar') // returns undefined without throwing a TypeError + // credits to github.com/gkindel + props: function(obj, props, value) { + if(obj === undefined) + obj = window; + if(props == null) + return undefined; + var i = props.indexOf('.'); + if( i == -1 ) { + if(value !== undefined) + obj[props] = value; + return obj[props]; + } + var prop = props.slice(0, i), + newProps = props.slice(i + 1); + + if(props !== undefined && !(obj[prop] instanceof Object) ) + obj[prop] = {}; + + return util.props(obj[prop], newProps, value); } }; From 230b4a0952e71236414801336bee61d363034c91 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Fri, 16 May 2014 01:55:13 -0400 Subject: [PATCH 7/8] now really fix topic/1503 --- public/src/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/src/app.js b/public/src/app.js index 3484b54205..270f731d70 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -553,7 +553,7 @@ var socket, var hash = window.location.hash ? window.location.hash : ''; window.history.replaceState({ url: url + search + hash - }, url, RELATIVE_PATH + '/' + url + hash); + }, url, RELATIVE_PATH + '/' + url + search + hash); } ajaxify.loadScript(tpl_url, function() { From 0e0f66ba9e881652be8708f03a3bdd631f2e017b Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Fri, 16 May 2014 09:39:46 -0400 Subject: [PATCH 8/8] minor cleanups. --- public/src/app.js | 17 +++++++++-------- src/controllers/index.js | 1 - 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 270f731d70..9ae50d4d87 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -24,7 +24,7 @@ var socket, reconnecting = false; // Rejoin room that was left when we disconnected - var url_parts = document.location.pathname.slice(RELATIVE_PATH.length).split('/').slice(1); + var url_parts = window.location.pathname.slice(RELATIVE_PATH.length).split('/').slice(1); var room; switch(url_parts[0]) { @@ -513,7 +513,9 @@ var socket, $('document').ready(function () { var url = window.location.pathname.slice(1), search = window.location.search, - tpl_url = ajaxify.getTemplateMapping(url); + hash = window.location.hash, + tpl_url = ajaxify.getTemplateMapping(url), + $window = $(window); url = url.replace(/\/$/, ""); @@ -521,7 +523,7 @@ var socket, url = url.slice(RELATIVE_PATH.length); } - $(window).trigger('action:ajaxify.start', { + $window.trigger('action:ajaxify.start', { url: url }); @@ -533,11 +535,11 @@ var socket, $('#logout-link').on('click', app.logout); - $(window).blur(function(){ + $window.blur(function(){ app.isFocused = false; }); - $(window).focus(function(){ + $window.focus(function(){ app.isFocused = true; app.alternatingTitle(''); }); @@ -550,21 +552,20 @@ var socket, ajaxify.widgets.render(tpl_url, url); if (window.history && window.history.replaceState) { - var hash = window.location.hash ? window.location.hash : ''; window.history.replaceState({ url: url + search + hash }, url, RELATIVE_PATH + '/' + url + search + hash); } ajaxify.loadScript(tpl_url, function() { - $(window).trigger('action:ajaxify.end', { + $window.trigger('action:ajaxify.end', { url: url }); }); }); }; - showWelcomeMessage = location.href.indexOf('loggedin') !== -1; + showWelcomeMessage = window.location.href.indexOf('loggedin') !== -1; app.loadConfig(); app.alternatingTitle(''); diff --git a/src/controllers/index.js b/src/controllers/index.js index 9d3254fca1..cf58f4955c 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -207,7 +207,6 @@ Controllers.register = function(req, res, next) { data.minimumPasswordLength = meta.config.minimumPasswordLength; data.termsOfUse = meta.config.termsOfUse; - data.captcha = null; plugins.fireHook('filter:register.build', req, res, data, function(err, data) { if (err && process.env === 'development') { winston.warn(JSON.stringify(err));