mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
got rid of all the res.locals.isAPI checks in all controller methods. now 1000% cleaner :shipit:
This commit is contained in:
@@ -18,28 +18,16 @@ var fs = require('fs'),
|
||||
file = require('./../file');
|
||||
|
||||
function userNotFound(res) {
|
||||
if (res.locals.isAPI) {
|
||||
return res.json(404, {
|
||||
error: 'User not found!'
|
||||
});
|
||||
} else {
|
||||
return res.render('404', {
|
||||
error: 'User not found!'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function userNotAllowed(res) {
|
||||
if (res.locals.isAPI) {
|
||||
return res.json(403, {
|
||||
error: 'Not allowed.'
|
||||
});
|
||||
} else {
|
||||
return res.render('403', {
|
||||
error: 'Not allowed.'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getUserDataByUserSlug(userslug, callerUID, callback) {
|
||||
user.getUidByUserslug(userslug, function(err, uid) {
|
||||
@@ -165,11 +153,7 @@ accountsController.getAccount = function(req, res, next) {
|
||||
postTools.parse(userData.signature, function (err, signature) {
|
||||
userData.signature = signature;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('account', userData);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -192,11 +176,7 @@ accountsController.getFollowing = function(req, res, next) {
|
||||
userData.following = followingData;
|
||||
userData.followingCount = followingData.length;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('following', userData);
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
@@ -221,11 +201,7 @@ accountsController.getFollowers = function(req, res, next) {
|
||||
userData.followers = followersData;
|
||||
userData.followersCount = followersData.length;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('followers', userData);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return userNotFound();
|
||||
@@ -264,11 +240,7 @@ accountsController.getFavourites = function(req, res, next) {
|
||||
userData.posts = favourites.posts;
|
||||
userData.nextStart = favourites.nextStart;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('favourites', userData);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -301,11 +273,7 @@ accountsController.getPosts = function(req, res, next) {
|
||||
userData.posts = userPosts.posts;
|
||||
userData.nextStart = userPosts.nextStart;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('accountposts', userData);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -319,11 +287,7 @@ accountsController.accountEdit = function(req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('accountedit', userData);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -360,11 +324,7 @@ accountsController.accountSettings = function(req, res, next) {
|
||||
userData.theirid = uid;
|
||||
userData.settings = settings;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('accountsettings', userData);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -475,15 +435,9 @@ accountsController.uploadPicture = function (req, res, next) {
|
||||
|
||||
accountsController.getNotifications = function(req, res, next) {
|
||||
user.notifications.getAll(req.user.uid, null, null, function(err, notifications) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json({
|
||||
notifications: notifications
|
||||
});
|
||||
} else {
|
||||
res.render('notifications', {
|
||||
notifications: notifications
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -28,97 +28,61 @@ var adminController = {
|
||||
};
|
||||
|
||||
adminController.home = function(req, res, next) {
|
||||
var data = {
|
||||
res.render('admin/index', {
|
||||
version: pkg.version,
|
||||
emailerInstalled: plugins.hasListeners('action:email.send'),
|
||||
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) {
|
||||
var data = {
|
||||
res.render('admin/users', {
|
||||
search_display: 'block',
|
||||
loadmore_display: 'none',
|
||||
users: []
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('admin/users', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
adminController.users.latest = function(req, res, next) {
|
||||
user.getUsers('users:joindate', 0, 49, function(err, users) {
|
||||
var data = {
|
||||
res.render('admin/users', {
|
||||
search_display: 'none',
|
||||
loadmore_display: 'block',
|
||||
users: users,
|
||||
yourid: req.user.uid
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('admin/users', data);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
adminController.users.sortByPosts = function(req, res, next) {
|
||||
user.getUsers('users:postcount', 0, 49, function(err, users) {
|
||||
var data = {
|
||||
res.render('admin/users', {
|
||||
search_display: 'none',
|
||||
loadmore_display: 'block',
|
||||
users: users,
|
||||
yourid: req.user.uid
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('admin/users', data);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
adminController.users.sortByReputation = function(req, res, next) {
|
||||
user.getUsers('users:reputation', 0, 49, function(err, users) {
|
||||
var data = {
|
||||
res.render('admin/users', {
|
||||
search_display: 'none',
|
||||
loadmore_display: 'block',
|
||||
users: users,
|
||||
yourid: req.user.uid
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('admin/users', data);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
adminController.users.sortByJoinDate = function(req, res, next) {
|
||||
user.getUsers('users:joindate', 0, 49, function(err, users) {
|
||||
var data = {
|
||||
res.render('admin/users', {
|
||||
search_display: 'none',
|
||||
users: users,
|
||||
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) {
|
||||
return !category.disabled;
|
||||
});
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
|
||||
res.render('admin/categories', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -141,37 +102,23 @@ adminController.categories.disabled = function(req, res, next) {
|
||||
return category.disabled;
|
||||
});
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('admin/categories', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
adminController.database.get = function(req, res, next) {
|
||||
db.info(function (err, data) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('admin/database', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// todo: deprecate this seemingly useless view
|
||||
adminController.topics.get = function(req, res, next) {
|
||||
topics.getAllTopics(0, 19, function (err, topics) {
|
||||
var data = {
|
||||
res.render('admin/topics', {
|
||||
topics: topics,
|
||||
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');
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json({eventdata: data});
|
||||
} else {
|
||||
res.render('admin/events', {eventdata: data});
|
||||
}
|
||||
res.render('admin/events', {
|
||||
eventdata: data
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -197,38 +141,26 @@ adminController.plugins.get = function(req, res, next) {
|
||||
plugins = [];
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json({plugins: plugins});
|
||||
} else {
|
||||
res.render('admin/plugins', {plugins: plugins});
|
||||
}
|
||||
res.render('admin/plugins', {
|
||||
plugins: plugins
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
adminController.languages.get = function(req, res, next) {
|
||||
languages.list(function(err, languages) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json({languages: languages});
|
||||
} else {
|
||||
res.render('admin/languages', {languages: languages});
|
||||
}
|
||||
res.render('admin/languages', {
|
||||
languages: languages
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
adminController.settings.get = function(req, res, next) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json({});
|
||||
} else {
|
||||
res.render('admin/settings', {});
|
||||
}
|
||||
};
|
||||
|
||||
adminController.logger.get = function(req, res, next) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json({});
|
||||
} else {
|
||||
res.render('admin/logger', {});
|
||||
}
|
||||
};
|
||||
|
||||
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,
|
||||
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);
|
||||
}
|
||||
], function(err, groupData) {
|
||||
var groups = groupData[0].concat(groupData[1]),
|
||||
data = {
|
||||
var groups = groupData[0].concat(groupData[1]);
|
||||
|
||||
res.render('admin/groups', {
|
||||
groups: groups,
|
||||
yourid: req.user.uid
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('admin/groups', data);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -16,11 +16,7 @@ categoriesController.recent = function(req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('recent', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -36,11 +32,7 @@ categoriesController.popular = function(req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('popular', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -52,11 +44,7 @@ categoriesController.unread = function(req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('unread', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -68,11 +56,7 @@ categoriesController.unreadTotal = function(req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('unread', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -182,11 +166,7 @@ categoriesController.get = function(req, res, next) {
|
||||
});
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('category', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -85,32 +85,20 @@ Controllers.home = function(req, res, next) {
|
||||
};
|
||||
|
||||
Controllers.search = function(req, res, next) {
|
||||
var data = {
|
||||
res.render('search', {
|
||||
show_no_topics: 'hide',
|
||||
show_no_posts: 'hide',
|
||||
show_results: 'hide',
|
||||
search_query: '',
|
||||
posts: [],
|
||||
topics: []
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('search', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Controllers.reset = function(req, res, next) {
|
||||
var data = {
|
||||
res.render('reset', {
|
||||
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) {
|
||||
@@ -135,11 +123,7 @@ Controllers.login = function(req, res, next) {
|
||||
data.token = res.locals.csrf_token;
|
||||
data.showResetLink = emailersPresent;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('login', data);
|
||||
}
|
||||
};
|
||||
|
||||
Controllers.register = function(req, res, next) {
|
||||
@@ -167,11 +151,7 @@ Controllers.register = function(req, res, next) {
|
||||
data.minimumPasswordLength = meta.config.minimumPasswordLength;
|
||||
data.termsOfUse = meta.config.termsOfUse;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('register', data);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -191,11 +171,7 @@ Controllers.confirmEmail = function(req, res, next) {
|
||||
};
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('confirm', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -228,11 +204,7 @@ Controllers.outgoing = function(req, res, next) {
|
||||
};
|
||||
|
||||
if (url) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('outgoing', data);
|
||||
}
|
||||
} else {
|
||||
res.status(404);
|
||||
res.redirect(nconf.get('relative_path') + '/404');
|
||||
|
||||
@@ -3,27 +3,15 @@
|
||||
var staticController = {};
|
||||
|
||||
staticController['404'] = function(req, res, next) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json({});
|
||||
} else {
|
||||
res.render('404', {});
|
||||
}
|
||||
};
|
||||
|
||||
staticController['403'] = function(req, res, next) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json({});
|
||||
} else {
|
||||
res.render('403', {});
|
||||
}
|
||||
};
|
||||
|
||||
staticController['500'] = function(req, res, next) {
|
||||
if (res.locals.isAPI) {
|
||||
res.json({});
|
||||
} else {
|
||||
res.render('500', {});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = staticController;
|
||||
@@ -168,11 +168,7 @@ topicsController.get = function(req, res, next) {
|
||||
});
|
||||
}
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('topic', data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -53,11 +53,7 @@ usersController.getOnlineUsers = function(req, res, next) {
|
||||
show_anon: anonymousUserCount?'':'hide'
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('users', userData);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -72,11 +68,7 @@ usersController.getUsersSortedByPosts = function(req, res, next) {
|
||||
show_anon: 'hide'
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('users', userData);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -89,11 +81,7 @@ usersController.getUsersSortedByReputation = function(req, res, next) {
|
||||
show_anon: 'hide'
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('users', userData);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -106,11 +94,7 @@ usersController.getUsersSortedByJoinDate = function(req, res, next) {
|
||||
show_anon: 'hide'
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(userData);
|
||||
} else {
|
||||
res.render('users', userData);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -122,11 +106,7 @@ usersController.getUsersForSearch = function(req, res, next) {
|
||||
show_anon: 'hide'
|
||||
};
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.render('users', data);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user