From d75bc9578b69e15ee8e550db2de549d5c7d41cf1 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Sun, 2 Mar 2014 23:49:15 -0500 Subject: [PATCH] deprecating middleware.prepareAPI in favour of using app.all('*') in /api namespace --- src/middleware/middleware.js | 5 ---- src/routes/api.js | 3 +- src/routes/index.js | 56 ++++++++++++++++++------------------ 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index b872466641..12d23aab59 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -15,11 +15,6 @@ var app, translator = require('./../../public/src/translator'), user = require('./../user'); -middleware.prepareAPI = function(req, res, next) { - res.locals.isAPI = true; - next(); -}; - middleware.authenticate = function(req, res, next) { if(!req.user) { if (res.locals.isAPI) { diff --git a/src/routes/api.js b/src/routes/api.js index 3d61acedf3..7e48f5bac9 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -23,14 +23,13 @@ var path = require('path'), Api.createRoutes = function (app) { app.namespace('/api', function () { - app.all('*', function(req, res, next) { - if(req.user) { user.updateLastOnlineTime(req.user.uid); } db.sortedSetAdd('ip:recent', Date.now(), req.ip || 'Unknown'); + res.locals.isAPI = true; next(); }); diff --git a/src/routes/index.js b/src/routes/index.js index d61aaec5fd..f44135c06d 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -44,101 +44,101 @@ module.exports = function(app, middleware) { /* Main */ app.get('/', middleware.buildHeader, controllers.home); - app.get('/api/home', middleware.prepareAPI, controllers.home); + app.get('/api/home', controllers.home); app.get('/login', middleware.buildHeader, controllers.login); - app.get('/api/login', middleware.prepareAPI, controllers.login); + app.get('/api/login', controllers.login); app.get('/register', middleware.buildHeader, controllers.register); - app.get('/api/register', middleware.prepareAPI, controllers.register); + app.get('/api/register', controllers.register); app.get('/confirm/:code', middleware.buildHeader, controllers.confirmEmail); - app.get('/api/confirm/:code', middleware.prepareAPI, controllers.confirmEmail); + app.get('/api/confirm/:code', controllers.confirmEmail); app.get('/sitemap.xml', controllers.sitemap); app.get('/robots.txt', controllers.robots); app.get('/outgoing', middleware.buildHeader, controllers.outgoing); - app.get('/api/outgoing', middleware.prepareAPI, controllers.outgoing); + app.get('/api/outgoing', controllers.outgoing); /* Static Pages */ app.get('/404', middleware.buildHeader, controllers.static['404']); - app.get('/api/404', middleware.prepareAPI, controllers.static['404']); + app.get('/api/404', controllers.static['404']); app.get('/403', middleware.buildHeader, controllers.static['403']); - app.get('/api/403', middleware.prepareAPI, controllers.static['403']); + app.get('/api/403', controllers.static['403']); app.get('/500', middleware.buildHeader, controllers.static['500']); - app.get('/api/500', middleware.prepareAPI, controllers.static['500']); + app.get('/api/500', controllers.static['500']); /* Topics */ app.get('/topic/:topic_id/:slug?', middleware.buildHeader, controllers.topics.get); - app.get('/api/topic/:topic_id/:slug?', middleware.prepareAPI, controllers.topics.get); + app.get('/api/topic/:topic_id/:slug?', controllers.topics.get); /* Categories */ app.get('/popular/:set?', middleware.buildHeader, controllers.categories.popular); - app.get('/api/popular/:set?', middleware.prepareAPI, controllers.categories.popular); + app.get('/api/popular/:set?', controllers.categories.popular); app.get('/recent/:term?', middleware.buildHeader, controllers.categories.recent); - app.get('/api/recent/:term?', middleware.prepareAPI, controllers.categories.recent); + app.get('/api/recent/:term?', controllers.categories.recent); app.get('/unread/', middleware.buildHeader, middleware.authenticate, controllers.categories.unread); - app.get('/api/unread/', middleware.prepareAPI, middleware.authenticate, controllers.categories.unread); + app.get('/api/unread/', middleware.authenticate, controllers.categories.unread); app.get('/unread/total', middleware.buildHeader, middleware.authenticate, controllers.categories.unreadTotal); - app.get('/api/unread/total', middleware.prepareAPI, middleware.authenticate, controllers.categories.unreadTotal); + app.get('/api/unread/total', middleware.authenticate, controllers.categories.unreadTotal); app.get('/category/:category_id/:slug?', middleware.buildHeader, controllers.categories.get); - app.get('/api/category/:category_id/:slug?', middleware.prepareAPI, controllers.categories.get); + app.get('/api/category/:category_id/:slug?', controllers.categories.get); /* Accounts */ app.get('/user/:userslug', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.accounts.getAccount); - app.get('/api/user/:userslug', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.accounts.getAccount); + app.get('/api/user/:userslug', middleware.checkGlobalPrivacySettings, controllers.accounts.getAccount); app.get('/user/:userslug/following', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.accounts.getFollowing); - app.get('/api/user/:userslug/following', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.accounts.getFollowing); + app.get('/api/user/:userslug/following', middleware.checkGlobalPrivacySettings, controllers.accounts.getFollowing); app.get('/user/:userslug/followers', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.accounts.getFollowers); - app.get('/api/user/:userslug/followers', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.accounts.getFollowers); + app.get('/api/user/:userslug/followers', middleware.checkGlobalPrivacySettings, controllers.accounts.getFollowers); app.get('/user/:userslug/favourites', middleware.buildHeader, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.getFavourites); - app.get('/api/user/:userslug/favourites', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.getFavourites); + app.get('/api/user/:userslug/favourites', middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.getFavourites); app.get('/user/:userslug/posts', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.accounts.getPosts); - app.get('/api/user/:userslug/posts', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.accounts.getPosts); + app.get('/api/user/:userslug/posts', middleware.checkGlobalPrivacySettings, controllers.accounts.getPosts); app.get('/user/:userslug/edit', middleware.buildHeader, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.accountEdit); - app.get('/api/user/:userslug/edit', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.accountEdit); + app.get('/api/user/:userslug/edit', middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.accountEdit); // todo: admin recently gained access to this page, pls check if it actually works app.get('/user/:userslug/settings', middleware.buildHeader, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.accountSettings); - app.get('/api/user/:userslug/settings', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.accountSettings); + app.get('/api/user/:userslug/settings', middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.accountSettings); app.get('/api/user/uid/:uid', middleware.checkGlobalPrivacySettings, controllers.accounts.getUserByUID); // this should have been in the API namespace // also, perhaps pass in :userslug so we can use checkAccountPermissions middleware, in future will allow admins to upload a picture for a user - app.post('/user/uploadpicture', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, /*middleware.checkAccountPermissions,*/ controllers.accounts.uploadPicture); + app.post('/user/uploadpicture', middleware.checkGlobalPrivacySettings, /*middleware.checkAccountPermissions,*/ controllers.accounts.uploadPicture); /* Users */ app.get('/users', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); - app.get('/api/users', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); + app.get('/api/users', middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); // was this duped by accident or purpose? app.get('/users/online', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); - app.get('/api/users/online', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); + app.get('/api/users/online', middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); app.get('/users/sort-posts', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByPosts); - app.get('/api/users/sort-posts', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByPosts); + app.get('/api/users/sort-posts', middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByPosts); app.get('/users/sort-reputation', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByReputation); - app.get('/api/users/sort-reputation', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByReputation); + app.get('/api/users/sort-reputation', middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByReputation); app.get('/users/latest', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByJoinDate); - app.get('/api/users/latest', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByJoinDate); + app.get('/api/users/latest', middleware.checkGlobalPrivacySettings, controllers.users.getUsersSortedByJoinDate); app.get('/users/search', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getUsersForSearch); - app.get('/api/users/search', middleware.prepareAPI, middleware.checkGlobalPrivacySettings, controllers.users.getUsersForSearch); + app.get('/api/users/search', middleware.checkGlobalPrivacySettings, controllers.users.getUsersForSearch);