From 190948336a51f7ce2c5ed2a52aa373673d1cfcec Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Mon, 2 Dec 2013 13:28:46 -0500 Subject: [PATCH] closes #590 --- public/language/en/global.json | 2 ++ public/src/forum/favourites.js | 4 ---- public/src/templates.js | 14 +++++++------- public/templates/500.tpl | 5 +++++ public/templates/favourites.tpl | 26 ++++++++++++++++++++------ src/posts.js | 30 +++++++++++++++++++++--------- src/routes/api.js | 14 +++++++++----- src/routes/user.js | 8 +++++--- src/webserver.js | 8 ++++---- 9 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 public/templates/500.tpl diff --git a/public/language/en/global.json b/public/language/en/global.json index f1615549d3..00576a18be 100644 --- a/public/language/en/global.json +++ b/public/language/en/global.json @@ -6,6 +6,8 @@ "403.message": "You seem to have stumbled upon a page that you do not have access to. Perhaps you should try logging in?", "404.title": "Not Found", "404.message": "You seem to have stumbled upon a page that does not exist. Return to the home page.", + "500.title": "Internal error.", + "500.message": "Ooops! Looks like something went wrong!", "logout": "Logout", "logout.title": "You are now logged out.", "logout.message": "You have successfully logged out of NodeBB", diff --git a/public/src/forum/favourites.js b/public/src/forum/favourites.js index 20b77b4d41..e23572e1b2 100644 --- a/public/src/forum/favourites.js +++ b/public/src/forum/favourites.js @@ -3,10 +3,6 @@ define(['forum/accountheader'], function(header) { AccountHeader.init = function() { header.init(); - - $('.user-favourite-posts .topic-row').on('click', function() { - ajaxify.go($(this).attr('topic-url')); - }); }; return AccountHeader; diff --git a/public/src/templates.js b/public/src/templates.js index d185b7c16e..6772f998b7 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -172,7 +172,7 @@ } else if (data && data.status === 403) { return ajaxify.go('403'); } else { - app.alertError("Can't load template data!"); + app.alertError(data.responseJSON.error); } }); @@ -319,18 +319,18 @@ if (conditionalBlock[1]) { // there is an else statement - if (!value) { - template = template.replace(matches[i], conditionalBlock[1]); + if (!value) { + template = template.replace(matches[i], conditionalBlock[1]); } else { - template = template.replace(matches[i], conditionalBlock[0]); + template = template.replace(matches[i], conditionalBlock[0]); } } else { - // regular if statement + // regular if statement if (!value) { template = template.replace(matches[i], ''); } } - } + } } } @@ -341,7 +341,7 @@ checkConditional('@first', blockInfo.iterator === 0); checkConditional('@last', blockInfo.iterator === blockInfo.total); } - + template = replace(namespace + d, data[d], template); } } diff --git a/public/templates/500.tpl b/public/templates/500.tpl new file mode 100644 index 0000000000..e3c110a50d --- /dev/null +++ b/public/templates/500.tpl @@ -0,0 +1,5 @@ +
+ [[global:500.title]] +

[[global:500.message]]

+

{errorMessage}

+
\ No newline at end of file diff --git a/public/templates/favourites.tpl b/public/templates/favourites.tpl index 19e8726f2f..4bd4c442c7 100644 --- a/public/templates/favourites.tpl +++ b/public/templates/favourites.tpl @@ -6,17 +6,31 @@ -
You don't have any favourites, favourite some posts to see them here!
+ +
You don't have any favourites, favourite some posts to see them here!
+
-
- {posts.username} : - {posts.category_name} >> {posts.title} -
{posts.content}
+
diff --git a/src/posts.js b/src/posts.js index 3fd6205b57..646f38dded 100644 --- a/src/posts.js +++ b/src/posts.js @@ -250,8 +250,9 @@ var RDB = require('./redis'), async.waterfall([ function(next) { Posts.getPostFields(pid, ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'], function(err, postData) { - if (postData.deleted === '1') return callback(null); - else { + if (postData.deleted === '1') { + return callback(null); + } else { postData.relativeTime = new Date(parseInt(postData.timestamp || 0, 10)).toISOString(); next(null, postData); } @@ -264,10 +265,15 @@ var RDB = require('./redis'), }, function(postData, next) { topics.getTopicFields(postData.tid, ['title', 'cid', 'slug', 'deleted'], function(err, topicData) { - if (err) return callback(err); - else if (topicData.deleted === '1') return callback(null); - categories.getCategoryField(topicData.cid, 'name', function(err, categoryData) { - postData.category_name = categoryData; + if (err) { + return callback(err); + } else if (topicData.deleted === '1') { + return callback(null); + } + categories.getCategoryFields(topicData.cid, ['name', 'icon', 'slug'], function(err, categoryData) { + postData.categoryName = categoryData.name; + postData.categoryIcon = categoryData.icon; + postData.categorySlug = categoryData.slug; postData.title = validator.sanitize(topicData.title).escape(); postData.topicSlug = topicData.slug; next(null, postData); @@ -277,13 +283,19 @@ var RDB = require('./redis'), function(postData, next) { if (postData.content) { postTools.parse(postData.content, function(err, content) { - if (!err) postData.content = utils.strip_tags(content); + if (!err) { + postData.content = utils.strip_tags(content); + } next(err, postData); }); - } else next(null, postData); + } else { + next(null, postData); + } } ], function(err, postData) { - if (!err) posts.push(postData); + if (!err) { + posts.push(postData); + } callback(err); }); } diff --git a/src/routes/api.js b/src/routes/api.js index 746243f420..389763bac5 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -143,14 +143,14 @@ var path = require('path'), }); }); - app.get('/recent/:term?', function (req, res) { + app.get('/recent/:term?', function (req, res, next) { var uid = (req.user) ? req.user.uid : 0; topics.getLatestTopics(uid, 0, 19, req.params.term, function (err, data) { - if (!err) { - res.json(data); - } else { - res.send(500); + if(err) { + return next(err); } + + res.json(data); }); }); @@ -295,6 +295,10 @@ var path = require('path'), app.get('/403', function (req, res) { res.json({}); }); + + app.get('/500', function(req, res) { + res.json({errorMessage: 'testing'}); + }) }); } }(exports)); \ No newline at end of file diff --git a/src/routes/user.js b/src/routes/user.js index 9a3364071c..b0de83ebcb 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -351,15 +351,17 @@ var fs = require('fs'), } user.getUserFields(uid, ['username', 'userslug'], function (err, userData) { - if (err) + if (err) { return next(err); + } if (userData) { posts.getFavourites(uid, function (err, posts) { - if (err) + if (err) { return next(err); + } userData.posts = posts; - userData.show_nofavourites = posts.length ? 'hide' : 'show'; + userData.show_nofavourites = posts.length === 0; res.json(userData); }); } else { diff --git a/src/webserver.js b/src/webserver.js index 624f08258d..716bd5dcfe 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -301,10 +301,10 @@ var path = require('path'), // here and next(err) appropriately, or if // we possibly recovered from the error, simply next(). console.error(err.stack); + var status = err.status || 500; + res.status(status); - res.status(err.status || 500); - - res.json('500', { + res.json(status, { error: err.message }); }); @@ -360,7 +360,7 @@ var path = require('path'), // Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section) (function () { - var routes = ['login', 'register', 'account', 'recent', '403', '404'], + var routes = ['login', 'register', 'account', 'recent', '403', '404', '500'], loginRequired = ['unread', 'search', 'notifications']; async.each(routes.concat(loginRequired), function(route, next) {