From 964fbfe2bb24d26c74cd25dad1119aa7bb9f5e85 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 24 Sep 2013 13:30:54 -0400 Subject: [PATCH 1/3] fixes to get nodebb to at least load on IE --- public/src/ajaxify.js | 2 +- public/src/templates.js | 76 ++++++++++++++++++------------------ public/templates/config.json | 46 +++++++++++----------- 3 files changed, 62 insertions(+), 62 deletions(-) diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index 20198d7151..aca3e9405a 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -158,7 +158,7 @@ var ajaxify = {}; var scripts = [], script, - children_nodes = body_el.childNodes, + children_nodes = $(body_el).children(), child, i; diff --git a/public/src/templates.js b/public/src/templates.js index af6918935e..41c5bbd4d7 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -1,4 +1,4 @@ -(function(module) { +(function (module) { var config = {}, templates, @@ -12,11 +12,11 @@ fs = require('fs'); } catch (e) {} - templates.force_refresh = function(tpl) { + templates.force_refresh = function (tpl) { return !!config.force_refresh[tpl]; } - templates.get_custom_map = function(tpl) { + templates.get_custom_map = function (tpl) { if (config['custom_mapping'] && tpl) { for (var pattern in config['custom_mapping']) { if (tpl.match(pattern)) { @@ -28,11 +28,11 @@ return false; } - templates.is_available = function(tpl) { + templates.is_available = function (tpl) { return jQuery.inArray(tpl, available_templates) !== -1; }; - templates.ready = function(callback) { + templates.ready = function (callback) { if (callback == null) { if (this.ready_callback) { this.ready_callback(); @@ -48,7 +48,7 @@ } }; - templates.prepare = function(raw_tpl, data) { + templates.prepare = function (raw_tpl, data) { var template = {}; template.html = raw_tpl; template.parse = parse; @@ -62,10 +62,10 @@ var loaded = templatesToLoad.length; for (var t in templatesToLoad) { - (function(file) { - fs.readFile(__dirname + '/../templates/' + file + '.tpl', function(err, html) { - var template = function() { - this.toString = function() { + (function (file) { + fs.readFile(__dirname + '/../templates/' + file + '.tpl', function (err, html) { + var template = function () { + this.toString = function () { return this.html; }; } @@ -84,7 +84,7 @@ } function loadClient() { - jQuery.when(jQuery.getJSON(RELATIVE_PATH + '/templates/config.json'), jQuery.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function(config_data, templates_data) { + jQuery.when(jQuery.getJSON(RELATIVE_PATH + '/templates/config.json'), jQuery.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function (config_data, templates_data) { config = config_data[0]; available_templates = templates_data[0]; templates.ready(); @@ -96,11 +96,11 @@ } - templates.init = function(templates_to_load) { + templates.init = function (templates_to_load) { loadTemplates(templates_to_load || []); } - templates.getTemplateNameFromUrl = function(url) { + templates.getTemplateNameFromUrl = function (url) { var parts = url.split('?')[0].split('/'); for (var i = 0; i < parts.length; ++i) { @@ -112,7 +112,7 @@ } - templates.load_template = function(callback, url, template) { + templates.load_template = function (callback, url, template) { var location = document.location || window.location, rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''); @@ -129,13 +129,13 @@ var template_data = null; - (function() { + (function () { var timestamp = new Date().getTime(); //debug if (!templates[tpl_url]) { - jQuery.get(RELATIVE_PATH + '/templates/' + tpl_url + '.tpl?v=' + timestamp, function(html) { - var template = function() { - this.toString = function() { + jQuery.get(RELATIVE_PATH + '/templates/' + tpl_url + '.tpl?v=' + timestamp, function (html) { + var template = function () { + this.toString = function () { return this.html; }; } @@ -154,9 +154,9 @@ }()); - (function() { + (function () { - jQuery.get(API_URL + api_url, function(data) { + jQuery.get(API_URL + api_url, function (data) { if (!data) { ajaxify.go('404'); @@ -165,7 +165,7 @@ template_data = data; parse_template(); - }).fail(function(data) { + }).fail(function (data) { template_data = {}; parse_template(); }); @@ -182,20 +182,20 @@ document.getElementById('content').innerHTML = templates[tpl_url].parse(template_data); - jQuery('#content [template-variable]').each(function(index, element) { + jQuery('#content [template-variable]').each(function (index, element) { var value = null; switch (element.getAttribute('template-type')) { - case 'boolean': - value = (element.value === 'true' || element.value === '1') ? true : false; - break; - case 'int': // Intentional fall-through - case 'integer': - value = parseInt(element.value); - break; - default: - value = element.value; - break; + case 'boolean': + value = (element.value === 'true' || element.value === '1') ? true : false; + break; + case 'int': // Intentional fall-through + case 'integer': + value = parseInt(element.value); + break; + default: + value = element.value; + break; } templates.set(element.getAttribute('template-variable'), value); @@ -208,20 +208,20 @@ } - templates.flush = function() { + templates.flush = function () { parsed_variables = {}; } - templates.get = function(key) { + templates.get = function (key) { return parsed_variables[key]; } - templates.set = function(key, value) { + templates.set = function (key, value) { parsed_variables[key] = value; } //modified from https://github.com/psychobunny/dcp.templates - var parse = function(data) { + var parse = function (data) { var self = this; function replace(key, value, template) { @@ -230,7 +230,7 @@ } function makeRegex(block) { - return new RegExp("[^]*", 'g'); + return new RegExp("[\\s\\S]*", 'g'); } function getBlock(regex, block, template) { @@ -299,7 +299,7 @@ } if (namespace) { - var regex = new RegExp("{" + namespace + "[^]*?}", 'g'); + var regex = new RegExp("{" + namespace + "[\\s\\S]*?}", 'g'); template = template.replace(regex, ''); } diff --git a/public/templates/config.json b/public/templates/config.json index c478149352..f97cd5ce26 100644 --- a/public/templates/config.json +++ b/public/templates/config.json @@ -1,40 +1,40 @@ { "custom_mapping": { - "admin/testing/categories[^]*": "admin/testing/categories", - "admin/topics[^]*": "admin/topics", - "admin/categories[^]*": "admin/categories", - "admin/users[^]*": "admin/users", - "admin/redis[^]*": "admin/redis", - "admin/index[^]*": "admin/index", - "admin/themes[^]*": "admin/themes", - "admin/plugins[^]*": "admin/plugins", - "^admin/settings[^]*": "admin/settings", - "admin/twitter[^]*": "admin/twitter", - "admin/facebook[^]*": "admin/facebook", - "admin/gplus[^]*": "admin/gplus", + "admin/testing/categories.*": "admin/testing/categories", + "admin/topics.*": "admin/topics", + "admin/categories.*": "admin/categories", + "admin/users.*": "admin/users", + "admin/redis.*": "admin/redis", + "admin/index.*": "admin/index", + "admin/themes.*": "admin/themes", + "admin/plugins.*": "admin/plugins", + "^admin/settings.*": "admin/settings", + "admin/twitter.*": "admin/twitter", + "admin/facebook.*": "admin/facebook", + "admin/gplus.*": "admin/gplus", "admin/motd/?$": "admin/motd", "admin/groups/?$": "admin/groups", "install/?$": "install/mail", "install/mail/?": "install/mail", "install/social/?": "install/social", "install/privileges/?": "install/privileges", - "users/sort-posts": "users", - "users/latest": "users", - "users/sort-reputation": "users", - "users/search": "users", - "user[^]*edit": "accountedit", - "user[^]*following": "following", - "user[^]*followers": "followers", - "user[^]*settings": "accountsettings", - "user[^]*favourites": "favourites", - "user/[^]*": "account", + "users/sort-posts": "users", + "users/latest": "users", + "users/sort-reputation": "users", + "users/search": "users", + "user.*edit": "accountedit", + "user.*following": "following", + "user.*followers": "followers", + "user.*settings": "accountsettings", + "user.*favourites": "favourites", + "user/.*": "account", "recent": "recent", "unread": "unread", "popular": "category", "active": "category", "search": "search", - "reset/[^]*": "reset_code", + "reset/.*": "reset_code", "reset": "reset" }, From b6ee89a6d8857eae63d8089426171c8c739ac804 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 24 Sep 2013 14:12:26 -0400 Subject: [PATCH 2/3] fixed typo in plugins page --- public/templates/admin/plugins.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/templates/admin/plugins.tpl b/public/templates/admin/plugins.tpl index b14d19a9a5..8c4943c32c 100644 --- a/public/templates/admin/plugins.tpl +++ b/public/templates/admin/plugins.tpl @@ -15,7 +15,7 @@

- Interesed in writing plugins for NodeBB? + Interested in writing plugins for NodeBB?

Full documentation regarding plugin authoring can be found in the NodeBB Wiki. From c80e2552b2c1868bdf1b3ac0ad4bc3eef98dc620 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 24 Sep 2013 14:18:41 -0400 Subject: [PATCH 3/3] express-namespaceing routes --- package.json | 2 +- src/routes/admin.js | 260 ++++++++++++++-------------- src/routes/api.js | 400 ++++++++++++++++++++++---------------------- src/routes/user.js | 312 +++++++++++++++++----------------- 4 files changed, 492 insertions(+), 482 deletions(-) diff --git a/package.json b/package.json index ea38ec8fec..b82a2747be 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "socket.io": "~0.9.16", "redis": "0.8.3", "express": "3.2.0", - "express-namespace": "0.1.1", + "express-namespace": "~0.1.1", "emailjs": "0.3.4", "cookie": "0.0.6", "connect-redis": "1.4.5", diff --git a/src/routes/admin.js b/src/routes/admin.js index 4a8f1a13b4..43896e3857 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -8,24 +8,24 @@ var user = require('./../user.js'), winston = require('winston'), nconf = require('nconf'); -(function(Admin) { - Admin.isAdmin = function(req, res, next) { - user.isAdministrator((req.user && req.user.uid) ? req.user.uid : 0, function(isAdmin) { +(function (Admin) { + Admin.isAdmin = function (req, res, next) { + user.isAdministrator((req.user && req.user.uid) ? req.user.uid : 0, function (isAdmin) { if (!isAdmin) res.redirect('/403'); else next(); }); } - Admin.build_header = function(res) { + Admin.build_header = function (res) { return templates['admin/header'].parse({ csrf: res.locals.csrf_token, relative_path: nconf.get('relative_path') }); } - Admin.create_routes = function(app) { + Admin.create_routes = function (app) { - (function() { + (function () { var routes = [ 'categories/active', 'categories/disabled', 'users', 'topics', 'settings', 'themes', 'twitter', 'facebook', 'gplus', 'redis', 'motd', 'groups', @@ -34,8 +34,8 @@ var user = require('./../user.js'), ]; for (var i = 0, ii = routes.length; i < ii; i++) { - (function(route) { - app.get('/admin/' + route, Admin.isAdmin, function(req, res) { + (function (route) { + app.get('/admin/' + route, Admin.isAdmin, function (req, res) { res.send(Admin.build_header(res) + app.create_route('admin/' + route) + templates['admin/footer']); }); }(routes[i])); @@ -44,8 +44,8 @@ var user = require('./../user.js'), var unit_tests = ['categories']; for (var i = 0, ii = unit_tests.length; i < ii; i++) { - (function(route) { - app.get('/admin/testing/' + route, Admin.isAdmin, function(req, res) { + (function (route) { + app.get('/admin/testing/' + route, Admin.isAdmin, function (req, res) { res.send(Admin.build_header(res) + app.create_route('admin/testing/' + route) + templates['admin/footer']); }); }(unit_tests[i])); @@ -53,174 +53,178 @@ var user = require('./../user.js'), }()); - app.get('/admin', Admin.isAdmin, function(req, res) { - res.send(Admin.build_header(res) + app.create_route('admin/index') + templates['admin/footer']); - }); - - app.get('/admin/index', Admin.isAdmin, function(req, res) { - res.send(Admin.build_header(res) + app.create_route('admin/index') + templates['admin/footer']); - }); - - app.get('/api/admin/index', function(req, res) { - res.json({ - version: pkg.version + app.namespace('/admin', function () { + app.get('/', Admin.isAdmin, function (req, res) { + res.send(Admin.build_header(res) + app.create_route('admin/index') + templates['admin/footer']); }); - }); - app.get('/api/admin/users/search', function(req, res) { - res.json({ - search_display: 'block', - loadmore_display: 'none', - users: [] + app.get('/index', Admin.isAdmin, function (req, res) { + res.send(Admin.build_header(res) + app.create_route('admin/index') + templates['admin/footer']); }); }); - app.get('/api/admin/users/latest', function(req, res) { - user.getUsers('users:joindate', 0, 49, function(err, data) { + app.namespace('/api/admin', function () { + app.get('/index', function (req, res) { res.json({ - search_display: 'none', - loadmore_display: 'block', - users: data, - yourid: req.user.uid + version: pkg.version }); }); - }); - app.get('/api/admin/users/sort-posts', function(req, res) { - user.getUsers('users:postcount', 0, 49, function(err, data) { + app.get('/users/search', function (req, res) { res.json({ - search_display: 'none', - loadmore_display: 'block', - users: data, - yourid: req.user.uid + search_display: 'block', + loadmore_display: 'none', + users: [] }); }); - }); - app.get('/api/admin/users/sort-reputation', function(req, res) { - user.getUsers('users:reputation', 0, 49, function(err, data) { - res.json({ - search_display: 'none', - loadmore_display: 'block', - users: data, - yourid: req.user.uid + app.get('/users/latest', function (req, res) { + user.getUsers('users:joindate', 0, 49, function (err, data) { + res.json({ + search_display: 'none', + loadmore_display: 'block', + users: data, + yourid: req.user.uid + }); }); }); - }); - app.get('/api/admin/users', function(req, res) { - user.getUsers('users:joindate', 0, 49, function(err, data) { - res.json({ - search_display: 'none', - users: data, - yourid: req.user.uid + app.get('/users/sort-posts', function (req, res) { + user.getUsers('users:postcount', 0, 49, function (err, data) { + res.json({ + search_display: 'none', + loadmore_display: 'block', + users: data, + yourid: req.user.uid + }); }); }); - }); - app.get('/api/admin/categories', function(req, res) { - categories.getAllCategories(function(data) { - res.json(data); + app.get('/users/sort-reputation', function (req, res) { + user.getUsers('users:reputation', 0, 49, function (err, data) { + res.json({ + search_display: 'none', + loadmore_display: 'block', + users: data, + yourid: req.user.uid + }); + }); }); - }); - app.get('/api/admin/categories/active', function(req, res) { - categories.getAllCategories(function(data) { - data.categories = data.categories.filter(function(category) { - return (!category.disabled || category.disabled === "0"); + app.get('/users', function (req, res) { + user.getUsers('users:joindate', 0, 49, function (err, data) { + res.json({ + search_display: 'none', + users: data, + yourid: req.user.uid + }); }); - res.json(data); }); - }); - app.get('/api/admin/categories/disabled', function(req, res) { - categories.getAllCategories(function(data) { - data.categories = data.categories.filter(function(category) { - return category.disabled === "1"; + app.get('/categories', function (req, res) { + categories.getAllCategories(function (data) { + res.json(data); }); - res.json(data); }); - }); - app.get('/api/admin/topics', function(req, res) { - topics.getAllTopics(10, null, function(topics) { - res.json({ - topics: topics + app.get('/categories/active', function (req, res) { + categories.getAllCategories(function (data) { + data.categories = data.categories.filter(function (category) { + return (!category.disabled || category.disabled === "0"); + }); + res.json(data); }); }); - }); - app.get('/api/admin/redis', function(req, res) { - RDB.info(function(err, data) { - data = data.split("\r\n"); - var finalData = {}; + app.get('/categories/disabled', function (req, res) { + categories.getAllCategories(function (data) { + data.categories = data.categories.filter(function (category) { + return category.disabled === "1"; + }); + res.json(data); + }); + }); - for (var i in data) { + app.get('/topics', function (req, res) { + topics.getAllTopics(10, null, function (topics) { + res.json({ + topics: topics + }); + }); + }); - if (data[i].indexOf(':') == -1 || !data[i]) - continue; + app.get('/redis', function (req, res) { + RDB.info(function (err, data) { + data = data.split("\r\n"); + var finalData = {}; - try { - data[i] = data[i].replace(/:/, "\":\""); - var json = "{\"" + data[i] + "\"}"; + for (var i in data) { - var jsonObject = JSON.parse(json); - for (var key in jsonObject) { - finalData[key] = jsonObject[key]; + if (data[i].indexOf(':') == -1 || !data[i]) + continue; + + try { + data[i] = data[i].replace(/:/, "\":\""); + var json = "{\"" + data[i] + "\"}"; + + var jsonObject = JSON.parse(json); + for (var key in jsonObject) { + finalData[key] = jsonObject[key]; + } + } catch (err) { + winston.warn('can\'t parse redis status variable, ignoring', i, data[i], err); } - } catch (err) { - winston.warn('can\'t parse redis status variable, ignoring', i, data[i], err); } - } - res.json(finalData); + res.json(finalData); + }); }); - }); - app.get('/api/admin/plugins', function(req, res) { - plugins.showInstalled(function(err, plugins) { - if (err || !Array.isArray(plugins)) plugins = []; + app.get('/plugins', function (req, res) { + plugins.showInstalled(function (err, plugins) { + if (err || !Array.isArray(plugins)) plugins = []; - res.json(200, { - plugins: plugins + res.json(200, { + plugins: plugins + }); }); }); - }); - app.get('/api/admin/settings', function(req, res) { - res.json(200, {}); - }); + app.get('/settings', function (req, res) { + res.json(200, {}); + }); - app.get('/api/admin/motd', function(req, res) { - res.json(200, {}); - }); + app.get('/motd', function (req, res) { + res.json(200, {}); + }); - app.get('/api/admin/themes', function(req, res) { - res.json(200, {}); - }); + app.get('/themes', function (req, res) { + res.json(200, {}); + }); - app.get('/api/admin/twitter', function(req, res) { - res.json(200, {}); - }); + app.get('/twitter', function (req, res) { + res.json(200, {}); + }); - app.get('/api/admin/facebook', function(req, res) { - res.json(200, {}); - }); + app.get('/facebook', function (req, res) { + res.json(200, {}); + }); - app.get('/api/admin/gplus', function(req, res) { - res.json(200, {}); - }); + app.get('/gplus', function (req, res) { + res.json(200, {}); + }); - app.get('/api/admin/testing/categories', function(req, res) { - res.json(200, {}); - }); + app.get('/testing/categories', function (req, res) { + res.json(200, {}); + }); - app.get('/api/admin/groups', function(req, res) { - Groups.list({ - expand: true - }, function(err, groups) { - res.json(200, { - groups: groups + app.get('/groups', function (req, res) { + Groups.list({ + expand: true + }, function (err, groups) { + res.json(200, { + groups: groups + }); }); }); }); diff --git a/src/routes/api.js b/src/routes/api.js index b09378c8ad..2feaf45c7e 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -9,257 +9,259 @@ var user = require('./../user.js'), nconf = require('nconf'); -(function(Api) { - Api.create_routes = function(app) { - app.get('/api/get_templates_listing', function(req, res) { - utils.walk(path.join(__dirname, '../../', 'public/templates'), function(err, data) { - res.json(data); +(function (Api) { + Api.create_routes = function (app) { + app.namespace('/api', function () { + app.get('/get_templates_listing', function (req, res) { + utils.walk(path.join(__dirname, '../../', 'public/templates'), function (err, data) { + res.json(data); + }); }); - }); - app.get('/api/config', function(req, res, next) { - var config = require('../../public/config.json'); + app.get('/config', function (req, res, next) { + var config = require('../../public/config.json'); - config['postDelay'] = meta.config['postDelay']; - config['minimumTitleLength'] = meta.config['minimumTitleLength']; - config['minimumPostLength'] = meta.config['minimumPostLength']; - config['imgurClientIDSet'] = !! meta.config['imgurClientID']; - config['minimumUsernameLength'] = meta.config['minimumUsernameLength']; - config['maximumUsernameLength'] = meta.config['maximumUsernameLength']; - config['minimumPasswordLength'] = meta.config['minimumPasswordLength']; + config.postDelay = meta.config.postDelay; + config.minimumTitleLength = meta.config.minimumTitleLength; + config.minimumPostLength = meta.config.minimumPostLength; + config.imgurClientIDSet = !! meta.config.imgurClientID; + config.minimumUsernameLength = meta.config.minimumUsernameLength; + config.maximumUsernameLength = meta.config.maximumUsernameLength; + config.minimumPasswordLength = meta.config.minimumPasswordLength; - res.json(200, config); - }); + res.json(200, config); + }); - app.get('/api/home', function(req, res) { - var uid = (req.user) ? req.user.uid : 0; - categories.getAllCategories(function(data) { - data.categories = data.categories.filter(function(category) { - return (!category.disabled || category.disabled === "0"); - }); + app.get('/home', function (req, res) { + var uid = (req.user) ? req.user.uid : 0; + categories.getAllCategories(function (data) { + data.categories = data.categories.filter(function (category) { + return (!category.disabled || category.disabled === "0"); + }); - function iterator(category, callback) { - categories.getRecentReplies(category.cid, 2, function(posts) { - category["posts"] = posts; - category["post_count"] = posts.length > 2 ? 2 : posts.length; - callback(null); + function iterator(category, callback) { + categories.getRecentReplies(category.cid, 2, function (posts) { + category.posts = posts; + category.post_count = posts.length > 2 ? 2 : posts.length; + callback(null); + }); + } + + require('async').each(data.categories, iterator, function (err) { + data.motd_class = (meta.config.show_motd === '1' || meta.config.show_motd === undefined) ? '' : 'none'; + data.motd = require('marked')(meta.config.motd || "# NodeBB v " + pkg.version + "\nWelcome to NodeBB, the discussion platform of the future.\n\n

"); + res.json(data); }); - } - require('async').each(data.categories, iterator, function(err) { - data.motd_class = (meta.config.show_motd === '1' || meta.config.show_motd === undefined) ? '' : 'none'; - data.motd = require('marked')(meta.config.motd || "# NodeBB v " + pkg.version + "\nWelcome to NodeBB, the discussion platform of the future.\n\n"); - res.json(data); - }); + }, uid); + }); - }, uid); - }); + app.get('/login', function (req, res) { + var data = {}, + login_strategies = auth.get_login_strategies(), + num_strategies = login_strategies.length; - app.get('/api/login', function(req, res) { - var data = {}, - login_strategies = auth.get_login_strategies(), - num_strategies = login_strategies.length; - - if (num_strategies == 0) { - data = { - 'login_window:spansize': 'col-md-12', - 'alternate_logins:display': 'none' - }; - } else { - data = { - 'login_window:spansize': 'col-md-6', - 'alternate_logins:display': 'block' - } - for (var i = 0, ii = num_strategies; i < ii; i++) { - data[login_strategies[i] + ':display'] = 'active'; + if (num_strategies == 0) { + data = { + 'login_window:spansize': 'col-md-12', + 'alternate_logins:display': 'none' + }; + } else { + data = { + 'login_window:spansize': 'col-md-6', + 'alternate_logins:display': 'block' + } + for (var i = 0, ii = num_strategies; i < ii; i++) { + data[login_strategies[i] + ':display'] = 'active'; + } } - } - data.token = res.locals.csrf_token; + data.token = res.locals.csrf_token; - res.json(data); - }); + res.json(data); + }); - app.get('/api/register', function(req, res) { - var data = {}, - login_strategies = auth.get_login_strategies(), - num_strategies = login_strategies.length; - - if (num_strategies == 0) { - data = { - 'register_window:spansize': 'col-md-12', - 'alternate_logins:display': 'none' - }; - } else { - data = { - 'register_window:spansize': 'col-md-6', - 'alternate_logins:display': 'block' - } - for (var i = 0, ii = num_strategies; i < ii; i++) { - data[login_strategies[i] + ':display'] = 'active'; + app.get('/register', function (req, res) { + var data = {}, + login_strategies = auth.get_login_strategies(), + num_strategies = login_strategies.length; + + if (num_strategies == 0) { + data = { + 'register_window:spansize': 'col-md-12', + 'alternate_logins:display': 'none' + }; + } else { + data = { + 'register_window:spansize': 'col-md-6', + 'alternate_logins:display': 'block' + } + for (var i = 0, ii = num_strategies; i < ii; i++) { + data[login_strategies[i] + ':display'] = 'active'; + } } - } - data.token = res.locals.csrf_token; - data.minimumUsernameLength = meta.config['minimumUsernameLength']; - data.maximumUsernameLength = meta.config['maximumUsernameLength']; - data.minimumPasswordLength = meta.config['minimumPasswordLength']; - res.json(data); - }); + data.token = res.locals.csrf_token; + data.minimumUsernameLength = meta.config['minimumUsernameLength']; + data.maximumUsernameLength = meta.config['maximumUsernameLength']; + data.minimumPasswordLength = meta.config['minimumPasswordLength']; + res.json(data); + }); - app.get('/api/topic/:id/:slug?', function(req, res, next) { - var uid = (req.user) ? req.user.uid : 0; - topics.getTopicWithPosts(req.params.id, uid, 0, 10, function(err, data) { - if (!err) { - if (data.deleted === '1' && data.expose_tools === 0) { - return res.json(404, {}); - } - res.json(data); - } else next(); + app.get('/topic/:id/:slug?', function (req, res, next) { + var uid = (req.user) ? req.user.uid : 0; + topics.getTopicWithPosts(req.params.id, uid, 0, 10, function (err, data) { + if (!err) { + if (data.deleted === '1' && data.expose_tools === 0) { + return res.json(404, {}); + } + res.json(data); + } else next(); + }); + }); + + app.get('/category/:id/:slug?', function (req, res, next) { + var uid = (req.user) ? req.user.uid : 0; + categories.getCategoryById(req.params.id, uid, function (err, data) { + if (!err) + res.json(data); + else + next(); + }, req.params.id, uid); }); - }); - app.get('/api/category/:id/:slug?', function(req, res, next) { - var uid = (req.user) ? req.user.uid : 0; - categories.getCategoryById(req.params.id, uid, function(err, data) { - if (!err) + app.get('/recent', function (req, res) { + var uid = (req.user) ? req.user.uid : 0; + topics.getLatestTopics(uid, 0, 19, function (data) { res.json(data); - else - next(); - }, req.params.id, uid); - }); + }); + }); - app.get('/api/recent', function(req, res) { - var uid = (req.user) ? req.user.uid : 0; - topics.getLatestTopics(uid, 0, 19, function(data) { - res.json(data); + app.get('/unread', function (req, res) { + var uid = (req.user) ? req.user.uid : 0; + topics.getUnreadTopics(uid, 0, 19, function (data) { + res.json(data); + }); }); - }); - app.get('/api/unread', function(req, res) { - var uid = (req.user) ? req.user.uid : 0; - topics.getUnreadTopics(uid, 0, 19, function(data) { - res.json(data); + app.get('/unread/total', function (req, res) { + var uid = (req.user) ? req.user.uid : 0; + topics.getTotalUnread(uid, function (data) { + res.json(data); + }); }); - }); - app.get('/api/unread/total', function(req, res) { - var uid = (req.user) ? req.user.uid : 0; - topics.getTotalUnread(uid, function(data) { - res.json(data); + app.get('/confirm/:id', function (req, res) { + user.email.confirm(req.params.id, function (data) { + if (data.status === 'ok') { + res.json({ + 'alert-class': 'alert-success', + title: 'Email Confirmed', + text: 'Thank you for vaidating your email. Your account is now fully activated.' + }); + } else { + res.json({ + 'alert-class': 'alert-error', + title: 'An error occurred...', + text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.' + }); + } + }); }); - }); - app.get('/api/confirm/:id', function(req, res) { - user.email.confirm(req.params.id, function(data) { - if (data.status === 'ok') { + app.get('/outgoing', function (req, res) { + var url = req.query.url; + + if (url) { res.json({ - 'alert-class': 'alert-success', - title: 'Email Confirmed', - text: 'Thank you for vaidating your email. Your account is now fully activated.' + url: url, + home: nconf.get('url') }); } else { - res.json({ - 'alert-class': 'alert-error', - title: 'An error occurred...', - text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.' - }); + res.status(404); + res.redirect(nconf.get('relative_path') + '/404'); } }); - }); - app.get('/api/outgoing', function(req, res) { - var url = req.query.url; - - if (url) { - res.json({ - url: url, - home: nconf.get('url') + app.get('/search', function (req, res) { + return res.json({ + show_no_topics: 'hide', + show_no_posts: 'hide', + show_results: 'hide', + search_query: '', + posts: [], + topics: [] }); - } else { - res.status(404); - res.redirect(nconf.get('relative_path') + '/404'); - } - }); - - app.get('/api/search', function(req, res) { - return res.json({ - show_no_topics: 'hide', - show_no_posts: 'hide', - show_results: 'hide', - search_query: '', - posts: [], - topics: [] }); - }); - app.get('/api/search/:term', function(req, res, next) { + app.get('/search/:term', function (req, res, next) { - var reds = require('reds'); - var postSearch = reds.createSearch('nodebbpostsearch'); - var topicSearch = reds.createSearch('nodebbtopicsearch'); + var reds = require('reds'); + var postSearch = reds.createSearch('nodebbpostsearch'); + var topicSearch = reds.createSearch('nodebbtopicsearch'); - function search(searchObj, callback) { - searchObj - .query(query = req.params.term).type('or') - .end(callback); - } + function search(searchObj, callback) { + searchObj + .query(query = req.params.term).type('or') + .end(callback); + } - function searchPosts(callback) { - search(postSearch, function(err, pids) { - if (err) - return callback(err, null); + function searchPosts(callback) { + search(postSearch, function (err, pids) { + if (err) + return callback(err, null); + + posts.getPostSummaryByPids(pids, function (err, posts) { + if (err) + return callback(err, null); + callback(null, posts); + }); + }) + } - posts.getPostSummaryByPids(pids, function(err, posts) { + function searchTopics(callback) { + search(topicSearch, function (err, tids) { if (err) return callback(err, null); - callback(null, posts); + + topics.getTopicsByTids(tids, 0, function (topics) { + callback(null, topics); + }, 0); }); - }) - } + } - function searchTopics(callback) { - search(topicSearch, function(err, tids) { + async.parallel([searchPosts, searchTopics], function (err, results) { if (err) - return callback(err, null); - - topics.getTopicsByTids(tids, 0, function(topics) { - callback(null, topics); - }, 0); + return next(); + + return res.json({ + show_no_topics: results[1].length ? 'hide' : '', + show_no_posts: results[0].length ? 'hide' : '', + show_results: '', + search_query: req.params.term, + posts: results[0], + topics: results[1] + }); }); - } + }); - async.parallel([searchPosts, searchTopics], function(err, results) { - if (err) - return next(); + app.get('/reset', function (req, res) { + res.json({}); + }); - return res.json({ - show_no_topics: results[1].length ? 'hide' : '', - show_no_posts: results[0].length ? 'hide' : '', - show_results: '', - search_query: req.params.term, - posts: results[0], - topics: results[1] + app.get('/reset/:code', function (req, res) { + res.json({ + reset_code: req.params.code }); }); - }); - - app.get('/api/reset', function(req, res) { - res.json({}); - }); - app.get('/api/reset/:code', function(req, res) { - res.json({ - reset_code: req.params.code + app.get('/404', function (req, res) { + res.json({}); }); - }); - app.get('/api/404', function(req, res) { - res.json({}); - }); - - app.get('/api/403', function(req, res) { - res.json({}); + app.get('/403', function (req, res) { + res.json({}); + }); }); } -}(exports)); +}(exports)); \ No newline at end of file diff --git a/src/routes/user.js b/src/routes/user.js index 29b8fcacae..f7461886d7 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -8,15 +8,15 @@ var user = require('./../user.js'), nconf = require('nconf'), meta = require('./../meta'); -(function(User) { - User.create_routes = function(app) { +(function (User) { + User.create_routes = function (app) { - app.get('/uid/:uid', function(req, res) { + app.get('/uid/:uid', function (req, res) { if (!req.params.uid) return res.redirect('/404'); - user.getUserData(req.params.uid, function(err, data) { + user.getUserData(req.params.uid, function (err, data) { if (data) { res.send(data); } else { @@ -27,156 +27,160 @@ var user = require('./../user.js'), }); }); - app.get('/users', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route("users", "users") + templates['footer']); - }); - }); - - app.get('/users/latest', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route("users/latest", "users") + templates['footer']); + app.namespace('/users', function () { + app.get('', function (req, res) { + app.build_header({ + req: req, + res: res + }, function (err, header) { + res.send(header + app.create_route("users", "users") + templates['footer']); + }); }); - }); - app.get('/users/sort-posts', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route("users/sort-posts", "users") + templates['footer']); + app.get('/latest', function (req, res) { + app.build_header({ + req: req, + res: res + }, function (err, header) { + res.send(header + app.create_route("users/latest", "users") + templates['footer']); + }); }); - }); - app.get('/users/sort-reputation', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route("users/sort-reputation", "users") + templates['footer']); + app.get('/sort-posts', function (req, res) { + app.build_header({ + req: req, + res: res + }, function (err, header) { + res.send(header + app.create_route("users/sort-posts", "users") + templates['footer']); + }); }); - }); - app.get('/users/online', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route("users/online", "users") + templates['footer']); + app.get('/sort-reputation', function (req, res) { + app.build_header({ + req: req, + res: res + }, function (err, header) { + res.send(header + app.create_route("users/sort-reputation", "users") + templates['footer']); + }); }); - }); - app.get('/users/search', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route("users/search", "users") + templates['footer']); + app.get('/online', function (req, res) { + app.build_header({ + req: req, + res: res + }, function (err, header) { + res.send(header + app.create_route("users/online", "users") + templates['footer']); + }); }); - }); - - app.get('/user/:userslug', function(req, res, next) { - - if (!req.params.userslug) { - next(); - return; - } - - user.get_uid_by_userslug(req.params.userslug, function(err, uid) { - if (!uid) { - return next(); - } + app.get('/search', function (req, res) { app.build_header({ req: req, res: res - }, function(err, header) { - res.send(header + app.create_route('user/' + req.params.userslug, 'account') + templates['footer']); + }, function (err, header) { + res.send(header + app.create_route("users/search", "users") + templates['footer']); }); - }); }); - app.get('/user/:userslug/edit', function(req, res) { + app.namespace('/user', function () { + app.get('/:userslug', function (req, res, next) { - if (!req.user) - return res.redirect('/403'); + if (!req.params.userslug) { + next(); + return; + } + + user.get_uid_by_userslug(req.params.userslug, function (err, uid) { + if (!uid) { + return next(); + } - user.getUserField(req.user.uid, 'userslug', function(err, userslug) { - if (req.params.userslug && userslug === req.params.userslug) { app.build_header({ req: req, res: res - }, function(err, header) { - res.send(header + app.create_route('user/' + req.params.userslug + '/edit', 'accountedit') + templates['footer']); + }, function (err, header) { + res.send(header + app.create_route('user/' + req.params.userslug, 'account') + templates['footer']); }); - } else { - return res.redirect('/404'); - } + + }); }); - }); - app.get('/user/:userslug/settings', function(req, res) { + app.get('/:userslug/edit', function (req, res) { - if (!req.user) - return res.redirect('/403'); + if (!req.user) + return res.redirect('/403'); - user.getUserField(req.user.uid, 'userslug', function(err, userslug) { - if (req.params.userslug && userslug === req.params.userslug) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route('user/' + req.params.userslug + '/settings', 'accountsettings') + templates['footer']); - }) - } else { - return res.redirect('/404'); - } + user.getUserField(req.user.uid, 'userslug', function (err, userslug) { + if (req.params.userslug && userslug === req.params.userslug) { + app.build_header({ + req: req, + res: res + }, function (err, header) { + res.send(header + app.create_route('user/' + req.params.userslug + '/edit', 'accountedit') + templates['footer']); + }); + } else { + return res.redirect('/404'); + } + }); }); - }); - app.post('/user/uploadpicture', function(req, res) { - if (!req.user) - return res.redirect('/403'); + app.get('/:userslug/settings', function (req, res) { - var uploadSize = meta.config.maximumProfileImageSize || 256; + if (!req.user) + return res.redirect('/403'); - if (req.files.userPhoto.size > uploadSize * 1024) { - res.send({ - error: 'Images must be smaller than ' + uploadSize + ' kb!' + user.getUserField(req.user.uid, 'userslug', function (err, userslug) { + if (req.params.userslug && userslug === req.params.userslug) { + app.build_header({ + req: req, + res: res + }, function (err, header) { + res.send(header + app.create_route('user/' + req.params.userslug + '/settings', 'accountsettings') + templates['footer']); + }) + } else { + return res.redirect('/404'); + } }); - return; - } + }); - var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']; + app.post('/uploadpicture', function (req, res) { + if (!req.user) + return res.redirect('/403'); - if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) { - res.send({ - error: 'Allowed image types are png, jpg and gif!' - }); - return; - } + var uploadSize = meta.config.maximumProfileImageSize || 256; - user.getUserField(req.user.uid, 'uploadedpicture', function(err, oldpicture) { - if (!oldpicture) { - uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); + if (req.files.userPhoto.size > uploadSize * 1024) { + res.send({ + error: 'Images must be smaller than ' + uploadSize + ' kb!' + }); return; } - var absolutePath = path.join(process.cwd(), nconf.get('upload_path'), path.basename(oldpicture)); + var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']; - fs.unlink(absolutePath, function(err) { - if (err) { - winston.err(err); + if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) { + res.send({ + error: 'Allowed image types are png, jpg and gif!' + }); + return; + } + + user.getUserField(req.user.uid, 'uploadedpicture', function (err, oldpicture) { + if (!oldpicture) { + uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); + return; } - uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); + var absolutePath = path.join(process.cwd(), nconf.get('upload_path'), path.basename(oldpicture)); + + fs.unlink(absolutePath, function (err) { + if (err) { + winston.err(err); + } + + uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); + }); }); }); }); @@ -197,7 +201,7 @@ var user = require('./../user.js'), var is = fs.createReadStream(tempPath); var os = fs.createWriteStream(uploadPath); - is.on('end', function() { + is.on('end', function () { fs.unlinkSync(tempPath); var imageUrl = nconf.get('upload_url') + filename; @@ -210,7 +214,7 @@ var user = require('./../user.js'), dstPath: uploadPath, width: 128, height: 128 - }, function(err, stdout, stderr) { + }, function (err, stdout, stderr) { if (err) { winston.err(err); } @@ -221,7 +225,7 @@ var user = require('./../user.js'), }); }); - os.on('error', function(err) { + os.on('error', function (err) { fs.unlinkSync(tempPath); winston.err(err); }); @@ -229,12 +233,12 @@ var user = require('./../user.js'), is.pipe(os); } - app.get('/user/:userslug/following', function(req, res) { + app.get('/user/:userslug/following', function (req, res) { if (!req.user) return res.redirect('/403'); - user.get_uid_by_userslug(req.params.userslug, function(err, uid) { + user.get_uid_by_userslug(req.params.userslug, function (err, uid) { if (!uid) { res.redirect('/404'); return; @@ -243,18 +247,18 @@ var user = require('./../user.js'), app.build_header({ req: req, res: res - }, function(err, header) { + }, function (err, header) { res.send(header + app.create_route('user/' + req.params.userslug + '/following', 'following') + templates['footer']); }); }); }); - app.get('/user/:userslug/followers', function(req, res) { + app.get('/user/:userslug/followers', function (req, res) { if (!req.user) return res.redirect('/403'); - user.get_uid_by_userslug(req.params.userslug, function(err, uid) { + user.get_uid_by_userslug(req.params.userslug, function (err, uid) { if (!uid) { res.redirect('/404'); return; @@ -262,18 +266,18 @@ var user = require('./../user.js'), app.build_header({ req: req, res: res - }, function(err, header) { + }, function (err, header) { res.send(header + app.create_route('user/' + req.params.userslug + '/followers', 'followers') + templates['footer']); }); }); }); - app.get('/user/:userslug/favourites', function(req, res) { + app.get('/user/:userslug/favourites', function (req, res) { if (!req.user) return res.redirect('/403'); - user.get_uid_by_userslug(req.params.userslug, function(err, uid) { + user.get_uid_by_userslug(req.params.userslug, function (err, uid) { if (!uid) { res.redirect('/404'); return; @@ -281,18 +285,18 @@ var user = require('./../user.js'), app.build_header({ req: req, res: res - }, function(err, header) { + }, function (err, header) { res.send(header + app.create_route('user/' + req.params.userslug + '/favourites', 'favourites') + templates['footer']); }); }); }); - app.get('/api/user/:userslug/following', function(req, res) { + app.get('/api/user/:userslug/following', function (req, res) { var callerUID = req.user ? req.user.uid : '0'; - getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) { + getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) { if (userData) { - user.getFollowing(userData.uid, function(followingData) { + user.getFollowing(userData.uid, function (followingData) { userData.following = followingData; userData.followingCount = followingData.length; res.json(userData); @@ -306,12 +310,12 @@ var user = require('./../user.js'), }); }); - app.get('/api/user/:userslug/followers', function(req, res) { + app.get('/api/user/:userslug/followers', function (req, res) { var callerUID = req.user ? req.user.uid : '0'; - getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) { + getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) { if (userData) { - user.getFollowers(userData.uid, function(followersData) { + user.getFollowers(userData.uid, function (followersData) { userData.followers = followersData; userData.followersCount = followersData.length; res.json(userData); @@ -324,18 +328,18 @@ var user = require('./../user.js'), }); }); - app.get('/api/user/:userslug/edit', function(req, res) { + app.get('/api/user/:userslug/edit', function (req, res) { var callerUID = req.user ? req.user.uid : '0'; - getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) { + getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) { res.json(userData); }); }); - app.get('/api/user/:userslug/settings', function(req, res, next) { + app.get('/api/user/:userslug/settings', function (req, res, next) { var callerUID = req.user ? req.user.uid : '0'; - user.get_uid_by_userslug(req.params.userslug, function(err, uid) { + user.get_uid_by_userslug(req.params.userslug, function (err, uid) { if (!uid) { res.json(404, { error: 'User not found!' @@ -351,7 +355,7 @@ var user = require('./../user.js'), } - user.getUserFields(uid, ['username', 'userslug', 'showemail'], function(err, userData) { + user.getUserFields(uid, ['username', 'userslug', 'showemail'], function (err, userData) { if (err) return next(err); @@ -370,10 +374,10 @@ var user = require('./../user.js'), }); }); - app.get('/api/user/:userslug/favourites', function(req, res, next) { + app.get('/api/user/:userslug/favourites', function (req, res, next) { var callerUID = req.user ? req.user.uid : '0'; - user.get_uid_by_userslug(req.params.userslug, function(err, uid) { + user.get_uid_by_userslug(req.params.userslug, function (err, uid) { if (!uid) { res.json(404, { error: 'User not found!' @@ -388,12 +392,12 @@ var user = require('./../user.js'), return; } - user.getUserFields(uid, ['username', 'userslug'], function(err, userData) { + user.getUserFields(uid, ['username', 'userslug'], function (err, userData) { if (err) return next(err); if (userData) { - posts.getFavourites(uid, function(err, posts) { + posts.getFavourites(uid, function (err, posts) { if (err) return next(err); userData.posts = posts; @@ -409,15 +413,15 @@ var user = require('./../user.js'), }); }); - app.get('/api/user/:userslug', function(req, res) { + app.get('/api/user/:userslug', function (req, res) { var callerUID = req.user ? req.user.uid : '0'; - getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) { + getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) { if (userData) { - user.isFollowing(callerUID, userData.theirid, function(isFollowing) { - posts.getPostsByUid(userData.theirid, 0, 9, function(posts) { + user.isFollowing(callerUID, userData.theirid, function (isFollowing) { + posts.getPostsByUid(userData.theirid, 0, 9, function (posts) { - userData.posts = posts.filter(function(p) { + userData.posts = posts.filter(function (p) { return p.deleted !== "1"; }); userData.isFollowing = isFollowing; @@ -426,7 +430,7 @@ var user = require('./../user.js'), if (callerUID !== userData.uid) user.incrementUserFieldBy(userData.uid, 'profileviews', 1); - postTools.parse(userData.signature, function(err, signature) { + postTools.parse(userData.signature, function (err, signature) { userData.signature = signature; res.json(userData); }); @@ -449,7 +453,7 @@ var user = require('./../user.js'), function getUsersSortedByJoinDate(req, res) { - user.getUsers('users:joindate', 0, 49, function(err, data) { + user.getUsers('users:joindate', 0, 49, function (err, data) { res.json({ search_display: 'none', loadmore_display: 'block', @@ -459,7 +463,7 @@ var user = require('./../user.js'), } function getUsersSortedByPosts(req, res) { - user.getUsers('users:postcount', 0, 49, function(err, data) { + user.getUsers('users:postcount', 0, 49, function (err, data) { res.json({ search_display: 'none', loadmore_display: 'block', @@ -469,7 +473,7 @@ var user = require('./../user.js'), } function getUsersSortedByReputation(req, res) { - user.getUsers('users:reputation', 0, 49, function(err, data) { + user.getUsers('users:reputation', 0, 49, function (err, data) { res.json({ search_display: 'none', loadmore_display: 'block', @@ -479,7 +483,7 @@ var user = require('./../user.js'), } function getOnlineUsers(req, res) { - user.getUsers('users:online', 0, 49, function(err, data) { + user.getUsers('users:online', 0, 49, function (err, data) { res.json({ search_display: 'none', loadmore_display: 'block', @@ -497,14 +501,14 @@ var user = require('./../user.js'), } function getUserDataByUserSlug(userslug, callerUID, callback) { - user.get_uid_by_userslug(userslug, function(err, uid) { + user.get_uid_by_userslug(userslug, function (err, uid) { if (uid === null) { callback(null); return; } - user.getUserData(uid, function(err, data) { + user.getUserData(uid, function (err, data) { if (data) { data.joindate = new Date(parseInt(data.joindate, 10)).toISOString(); @@ -532,8 +536,8 @@ var user = require('./../user.js'), data.yourid = callerUID; data.theirid = uid; - user.getFollowingCount(uid, function(followingCount) { - user.getFollowerCount(uid, function(followerCount) { + user.getFollowingCount(uid, function (followingCount) { + user.getFollowerCount(uid, function (followerCount) { data.followingCount = followingCount; data.followerCount = followerCount; callback(data);