got rid of all the res.locals.isAPI checks in all controller methods. now 1000% cleaner :shipit:

v1.18.x
psychobunny 11 years ago
parent faf3de7e27
commit a99be8e7be

@ -18,27 +18,15 @@ var fs = require('fs'),
file = require('./../file'); file = require('./../file');
function userNotFound(res) { function userNotFound(res) {
if (res.locals.isAPI) { return res.render('404', {
return res.json(404, { error: 'User not found!'
error: 'User not found!' });
});
} else {
return res.render('404', {
error: 'User not found!'
});
}
} }
function userNotAllowed(res) { function userNotAllowed(res) {
if (res.locals.isAPI) { return res.render('403', {
return res.json(403, { error: 'Not allowed.'
error: 'Not allowed.' });
});
} else {
return res.render('403', {
error: 'Not allowed.'
});
}
} }
function getUserDataByUserSlug(userslug, callerUID, callback) { function getUserDataByUserSlug(userslug, callerUID, callback) {
@ -165,11 +153,7 @@ accountsController.getAccount = function(req, res, next) {
postTools.parse(userData.signature, function (err, signature) { postTools.parse(userData.signature, function (err, signature) {
userData.signature = signature; userData.signature = signature;
if (res.locals.isAPI) { res.render('account', userData);
res.json(userData);
} else {
res.render('account', userData);
}
}); });
}); });
}); });
@ -192,11 +176,7 @@ accountsController.getFollowing = function(req, res, next) {
userData.following = followingData; userData.following = followingData;
userData.followingCount = followingData.length; userData.followingCount = followingData.length;
if (res.locals.isAPI) { res.render('following', userData);
res.json(userData);
} else {
res.render('following', userData);
}
}); });
} else { } else {
@ -221,11 +201,7 @@ accountsController.getFollowers = function(req, res, next) {
userData.followers = followersData; userData.followers = followersData;
userData.followersCount = followersData.length; userData.followersCount = followersData.length;
if (res.locals.isAPI) { res.render('followers', userData);
res.json(userData);
} else {
res.render('followers', userData);
}
}); });
} else { } else {
return userNotFound(); return userNotFound();
@ -264,11 +240,7 @@ accountsController.getFavourites = function(req, res, next) {
userData.posts = favourites.posts; userData.posts = favourites.posts;
userData.nextStart = favourites.nextStart; userData.nextStart = favourites.nextStart;
if (res.locals.isAPI) { res.render('favourites', userData);
res.json(userData);
} else {
res.render('favourites', userData);
}
}); });
}); });
}); });
@ -301,11 +273,7 @@ accountsController.getPosts = function(req, res, next) {
userData.posts = userPosts.posts; userData.posts = userPosts.posts;
userData.nextStart = userPosts.nextStart; userData.nextStart = userPosts.nextStart;
if (res.locals.isAPI) { res.render('accountposts', userData);
res.json(userData);
} else {
res.render('accountposts', userData);
}
}); });
}); });
}); });
@ -319,11 +287,7 @@ accountsController.accountEdit = function(req, res, next) {
return next(err); return next(err);
} }
if (res.locals.isAPI) { res.render('accountedit', userData);
res.json(userData);
} else {
res.render('accountedit', userData);
}
}); });
}; };
@ -360,11 +324,7 @@ accountsController.accountSettings = function(req, res, next) {
userData.theirid = uid; userData.theirid = uid;
userData.settings = settings; userData.settings = settings;
if (res.locals.isAPI) { res.render('accountsettings', userData);
res.json(userData);
} else {
res.render('accountsettings', userData);
}
}); });
}); });
@ -475,15 +435,9 @@ accountsController.uploadPicture = function (req, res, next) {
accountsController.getNotifications = function(req, res, next) { accountsController.getNotifications = function(req, res, next) {
user.notifications.getAll(req.user.uid, null, null, function(err, notifications) { user.notifications.getAll(req.user.uid, null, null, function(err, notifications) {
if (res.locals.isAPI) { res.render('notifications', {
res.json({ notifications: notifications
notifications: notifications });
});
} else {
res.render('notifications', {
notifications: notifications
});
}
}); });
}; };

@ -28,97 +28,61 @@ var adminController = {
}; };
adminController.home = function(req, res, next) { adminController.home = function(req, res, next) {
var data = { res.render('admin/index', {
version: pkg.version, version: pkg.version,
emailerInstalled: plugins.hasListeners('action:email.send'), emailerInstalled: plugins.hasListeners('action:email.send'),
searchInstalled: plugins.hasListeners('filter:search.query') searchInstalled: plugins.hasListeners('filter:search.query')
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('admin/index', data);
}
}; };
adminController.users.search = function(req, res, next) { adminController.users.search = function(req, res, next) {
var data = { res.render('admin/users', {
search_display: 'block', search_display: 'block',
loadmore_display: 'none', loadmore_display: 'none',
users: [] users: []
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('admin/users', data);
}
}; };
adminController.users.latest = function(req, res, next) { adminController.users.latest = function(req, res, next) {
user.getUsers('users:joindate', 0, 49, function(err, users) { user.getUsers('users:joindate', 0, 49, function(err, users) {
var data = { res.render('admin/users', {
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
users: users, users: users,
yourid: req.user.uid yourid: req.user.uid
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('admin/users', data);
}
}); });
}; };
adminController.users.sortByPosts = function(req, res, next) { adminController.users.sortByPosts = function(req, res, next) {
user.getUsers('users:postcount', 0, 49, function(err, users) { user.getUsers('users:postcount', 0, 49, function(err, users) {
var data = { res.render('admin/users', {
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
users: users, users: users,
yourid: req.user.uid yourid: req.user.uid
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('admin/users', data);
}
}); });
}; };
adminController.users.sortByReputation = function(req, res, next) { adminController.users.sortByReputation = function(req, res, next) {
user.getUsers('users:reputation', 0, 49, function(err, users) { user.getUsers('users:reputation', 0, 49, function(err, users) {
var data = { res.render('admin/users', {
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
users: users, users: users,
yourid: req.user.uid yourid: req.user.uid
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('admin/users', data);
}
}); });
}; };
adminController.users.sortByJoinDate = function(req, res, next) { adminController.users.sortByJoinDate = function(req, res, next) {
user.getUsers('users:joindate', 0, 49, function(err, users) { user.getUsers('users:joindate', 0, 49, function(err, users) {
var data = { res.render('admin/users', {
search_display: 'none', search_display: 'none',
users: users, users: users,
yourid: req.user.uid yourid: req.user.uid
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('admin/users', data);
}
}); });
}; };
@ -127,11 +91,8 @@ adminController.categories.active = function(req, res, next) {
data.categories = data.categories.filter(function (category) { data.categories = data.categories.filter(function (category) {
return !category.disabled; return !category.disabled;
}); });
if (res.locals.isAPI) {
res.json(data); res.render('admin/categories', data);
} else {
res.render('admin/categories', data);
}
}); });
}; };
@ -141,37 +102,23 @@ adminController.categories.disabled = function(req, res, next) {
return category.disabled; return category.disabled;
}); });
if (res.locals.isAPI) { res.render('admin/categories', data);
res.json(data);
} else {
res.render('admin/categories', data);
}
}); });
}; };
adminController.database.get = function(req, res, next) { adminController.database.get = function(req, res, next) {
db.info(function (err, data) { db.info(function (err, data) {
if (res.locals.isAPI) { res.render('admin/database', data);
res.json(data);
} else {
res.render('admin/database', data);
}
}); });
}; };
// todo: deprecate this seemingly useless view // todo: deprecate this seemingly useless view
adminController.topics.get = function(req, res, next) { adminController.topics.get = function(req, res, next) {
topics.getAllTopics(0, 19, function (err, topics) { topics.getAllTopics(0, 19, function (err, topics) {
var data = { res.render('admin/topics', {
topics: topics, topics: topics,
notopics: topics.length === 0 notopics: topics.length === 0
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('admin/topics', data);
}
}); });
}; };
@ -182,12 +129,9 @@ adminController.events.get = function(req, res, next) {
} }
data = data.toString().split('\n').reverse().join('\n'); data = data.toString().split('\n').reverse().join('\n');
res.render('admin/events', {
if (res.locals.isAPI) { eventdata: data
res.json({eventdata: data}); });
} else {
res.render('admin/events', {eventdata: data});
}
}); });
}; };
@ -197,38 +141,26 @@ adminController.plugins.get = function(req, res, next) {
plugins = []; plugins = [];
} }
if (res.locals.isAPI) { res.render('admin/plugins', {
res.json({plugins: plugins}); plugins: plugins
} else { });
res.render('admin/plugins', {plugins: plugins});
}
}); });
}; };
adminController.languages.get = function(req, res, next) { adminController.languages.get = function(req, res, next) {
languages.list(function(err, languages) { languages.list(function(err, languages) {
if (res.locals.isAPI) { res.render('admin/languages', {
res.json({languages: languages}); languages: languages
} else { });
res.render('admin/languages', {languages: languages});
}
}); });
}; };
adminController.settings.get = function(req, res, next) { adminController.settings.get = function(req, res, next) {
if (res.locals.isAPI) { res.render('admin/settings', {});
res.json({});
} else {
res.render('admin/settings', {});
}
}; };
adminController.logger.get = function(req, res, next) { adminController.logger.get = function(req, res, next) {
if (res.locals.isAPI) { res.render('admin/logger', {});
res.json({});
} else {
res.render('admin/logger', {});
}
}; };
adminController.themes.get = function(req, res, next) { adminController.themes.get = function(req, res, next) {
@ -252,16 +184,10 @@ adminController.themes.get = function(req, res, next) {
} }
} }
var data = { res.render('admin/themes', {
areas: widgetData.areas, areas: widgetData.areas,
widgets: widgetData.widgets widgets: widgetData.widgets
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('admin/themes', data);
}
}); });
}); });
}; };
@ -279,17 +205,12 @@ adminController.groups.get = function(req, res, next) {
}, next); }, next);
} }
], function(err, groupData) { ], function(err, groupData) {
var groups = groupData[0].concat(groupData[1]), var groups = groupData[0].concat(groupData[1]);
data = {
groups: groups,
yourid: req.user.uid
};
if (res.locals.isAPI) { res.render('admin/groups', {
res.json(data); groups: groups,
} else { yourid: req.user.uid
res.render('admin/groups', data); });
}
}); });
}; };

@ -16,11 +16,7 @@ categoriesController.recent = function(req, res, next) {
return next(err); return next(err);
} }
if (res.locals.isAPI) { res.render('recent', data);
res.json(data);
} else {
res.render('recent', data);
}
}); });
}; };
@ -36,11 +32,7 @@ categoriesController.popular = function(req, res, next) {
return next(err); return next(err);
} }
if (res.locals.isAPI) { res.render('popular', data);
res.json(data);
} else {
res.render('popular', data);
}
}); });
}; };
@ -52,11 +44,7 @@ categoriesController.unread = function(req, res, next) {
return next(err); return next(err);
} }
if (res.locals.isAPI) { res.render('unread', data);
res.json(data);
} else {
res.render('unread', data);
}
}); });
}; };
@ -68,11 +56,7 @@ categoriesController.unreadTotal = function(req, res, next) {
return next(err); return next(err);
} }
if (res.locals.isAPI) { res.render('unread', data);
res.json(data);
} else {
res.render('unread', data);
}
}); });
}; };
@ -182,11 +166,7 @@ categoriesController.get = function(req, res, next) {
}); });
} }
if (res.locals.isAPI) { res.render('category', data);
res.json(data);
} else {
res.render('category', data);
}
}); });
}; };

@ -85,32 +85,20 @@ Controllers.home = function(req, res, next) {
}; };
Controllers.search = function(req, res, next) { Controllers.search = function(req, res, next) {
var data = { res.render('search', {
show_no_topics: 'hide', show_no_topics: 'hide',
show_no_posts: 'hide', show_no_posts: 'hide',
show_results: 'hide', show_results: 'hide',
search_query: '', search_query: '',
posts: [], posts: [],
topics: [] topics: []
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('search', data);
}
}; };
Controllers.reset = function(req, res, next) { Controllers.reset = function(req, res, next) {
var data = { res.render('reset', {
reset_code: req.params.code ? req.params.code : null reset_code: req.params.code ? req.params.code : null
}; });
if (res.locals.isAPI) {
res.json(data);
} else {
res.render('reset', data);
}
}; };
Controllers.login = function(req, res, next) { Controllers.login = function(req, res, next) {
@ -135,11 +123,7 @@ Controllers.login = function(req, res, next) {
data.token = res.locals.csrf_token; data.token = res.locals.csrf_token;
data.showResetLink = emailersPresent; data.showResetLink = emailersPresent;
if (res.locals.isAPI) { res.render('login', data);
res.json(data);
} else {
res.render('login', data);
}
}; };
Controllers.register = function(req, res, next) { Controllers.register = function(req, res, next) {
@ -167,11 +151,7 @@ Controllers.register = function(req, res, next) {
data.minimumPasswordLength = meta.config.minimumPasswordLength; data.minimumPasswordLength = meta.config.minimumPasswordLength;
data.termsOfUse = meta.config.termsOfUse; data.termsOfUse = meta.config.termsOfUse;
if (res.locals.isAPI) { res.render('register', data);
res.json(data);
} else {
res.render('register', data);
}
}; };
@ -191,11 +171,7 @@ Controllers.confirmEmail = function(req, res, next) {
}; };
} }
if (res.locals.isAPI) { res.render('confirm', data);
res.json(data);
} else {
res.render('confirm', data);
}
}); });
}; };
@ -228,11 +204,7 @@ Controllers.outgoing = function(req, res, next) {
}; };
if (url) { if (url) {
if (res.locals.isAPI) { res.render('outgoing', data);
res.json(data);
} else {
res.render('outgoing', data);
}
} else { } else {
res.status(404); res.status(404);
res.redirect(nconf.get('relative_path') + '/404'); res.redirect(nconf.get('relative_path') + '/404');

@ -3,27 +3,15 @@
var staticController = {}; var staticController = {};
staticController['404'] = function(req, res, next) { staticController['404'] = function(req, res, next) {
if (res.locals.isAPI) { res.render('404', {});
res.json({});
} else {
res.render('404', {});
}
}; };
staticController['403'] = function(req, res, next) { staticController['403'] = function(req, res, next) {
if (res.locals.isAPI) { res.render('403', {});
res.json({});
} else {
res.render('403', {});
}
}; };
staticController['500'] = function(req, res, next) { staticController['500'] = function(req, res, next) {
if (res.locals.isAPI) { res.render('500', {});
res.json({});
} else {
res.render('500', {});
}
}; };
module.exports = staticController; module.exports = staticController;

@ -168,11 +168,7 @@ topicsController.get = function(req, res, next) {
}); });
} }
if (res.locals.isAPI) { res.render('topic', data);
res.json(data);
} else {
res.render('topic', data);
}
}); });
}; };

@ -53,11 +53,7 @@ usersController.getOnlineUsers = function(req, res, next) {
show_anon: anonymousUserCount?'':'hide' show_anon: anonymousUserCount?'':'hide'
}; };
if (res.locals.isAPI) { res.render('users', userData);
res.json(userData);
} else {
res.render('users', userData);
}
}); });
}); });
}); });
@ -72,11 +68,7 @@ usersController.getUsersSortedByPosts = function(req, res, next) {
show_anon: 'hide' show_anon: 'hide'
}; };
if (res.locals.isAPI) { res.render('users', userData);
res.json(userData);
} else {
res.render('users', userData);
}
}); });
}; };
@ -89,11 +81,7 @@ usersController.getUsersSortedByReputation = function(req, res, next) {
show_anon: 'hide' show_anon: 'hide'
}; };
if (res.locals.isAPI) { res.render('users', userData);
res.json(userData);
} else {
res.render('users', userData);
}
}); });
}; };
@ -106,11 +94,7 @@ usersController.getUsersSortedByJoinDate = function(req, res, next) {
show_anon: 'hide' show_anon: 'hide'
}; };
if (res.locals.isAPI) { res.render('users', userData);
res.json(userData);
} else {
res.render('users', userData);
}
}); });
}; };
@ -122,11 +106,7 @@ usersController.getUsersForSearch = function(req, res, next) {
show_anon: 'hide' show_anon: 'hide'
}; };
if (res.locals.isAPI) { res.render('users', data);
res.json(data);
} else {
res.render('users', data);
}
}; };

Loading…
Cancel
Save