From 7c873c4314fc9f383a5e11cd3f3252b31e376080 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 28 Feb 2014 14:08:06 -0500 Subject: [PATCH] decided to functionally separate accounts from users --- src/controllers/accounts.js | 277 ++++++++++++++++++++++++++++++++++++ src/controllers/index.js | 4 + src/controllers/users.js | 270 ----------------------------------- src/webserver.js | 32 ++--- 4 files changed, 297 insertions(+), 286 deletions(-) create mode 100644 src/controllers/accounts.js diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js new file mode 100644 index 0000000000..5a7679d93d --- /dev/null +++ b/src/controllers/accounts.js @@ -0,0 +1,277 @@ +var accountsController = {}, + user = require('./../user'), + posts = require('./../posts'); + + +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.' + }); + } +} + +accountsController.getAccount = function(req, res, next) { + var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; + + getUserDataByUserSlug(req.params.userslug, callerUID, function (err, userData) { + if(err) { + return next(err); + } + + if(!userData) { + return res.json(404, { + error: 'User not found!' + }); + } + + user.isFollowing(callerUID, userData.theirid, function (isFollowing) { + posts.getPostsByUid(callerUID, userData.theirid, 0, 9, function (err, userPosts) { + if(err) { + return next(err); + } + + userData.posts = userPosts.posts.filter(function (p) { + return p && parseInt(p.deleted, 10) !== 1; + }); + + userData.isFollowing = isFollowing; + + if (!userData.profileviews) { + userData.profileviews = 1; + } + + if (callerUID !== parseInt(userData.uid, 10) && callerUID) { + user.incrementUserFieldBy(userData.uid, 'profileviews', 1); + } + + postTools.parse(userData.signature, function (err, signature) { + userData.signature = signature; + + if (res.locals.isAPI) { + res.json({}); + } else { + res.render('account', {}); + }; + }); + }); + }); + }); +}; + +accountsController.getFollowing = function(req, res, next) { + var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; + + getUserDataByUserSlug(req.params.userslug, callerUID, function (err, userData) { + if(err) { + return next(err); + } + + if (userData) { + user.getFollowing(userData.uid, function (err, followingData) { + if(err) { + return next(err); + } + userData.following = followingData; + userData.followingCount = followingData.length; + + if (res.locals.isAPI) { + res.json(userData); + } else { + res.render('following', userData); + } + }); + + } else { + return userNotFound(); + } + }); +}; + +accountsController.getFollowers = function(req, res, next) { + var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; + + getUserDataByUserSlug(req.params.userslug, callerUID, function (err, userData) { + if(err) { + return next(err); + } + + if (userData) { + user.getFollowers(userData.uid, function (err, followersData) { + if(err) { + return next(err); + } + userData.followers = followersData; + userData.followersCount = followersData.length; + + if (res.locals.isAPI) { + res.json(userData); + } else { + res.render('followers', userData); + } + }); + } else { + return userNotFound(); + } + }); +}; + +accountsController.getFavourites = function(req, res, next) { + var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; + + user.getUidByUserslug(req.params.userslug, function (err, uid) { + if (!uid) { + return userNotFound(); + } + + if (parseInt(uid, 10) !== callerUID) { + return userNotAllowed(); + } + + user.getUserFields(uid, ['username', 'userslug'], function (err, userData) { + if (err) { + return next(err); + } + + if (!userData) { + return userNotFound(); + } + + posts.getFavourites(uid, 0, 9, function (err, favourites) { + if (err) { + return next(err); + } + + userData.theirid = uid; + userData.yourid = callerUID; + userData.posts = favourites.posts; + userData.nextStart = favourites.nextStart; + + if (res.locals.isAPI) { + res.json(userData); + } else { + res.render('favourites', userData); + } + }); + }); + }); +}; + +accountsController.getPosts = function(req, res, next) { + var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; + + user.getUidByUserslug(req.params.userslug, function (err, uid) { + if (!uid) { + return userNotFound(); + } + + user.getUserFields(uid, ['username', 'userslug'], function (err, userData) { + if (err) { + return next(err); + } + + if (!userData) { + return userNotFound(); + } + + posts.getPostsByUid(callerUID, uid, 0, 19, function (err, userPosts) { + if (err) { + return next(err); + } + userData.uid = uid; + userData.theirid = uid; + userData.yourid = callerUID; + userData.posts = userPosts.posts; + userData.nextStart = userPosts.nextStart; + + if (res.locals.isAPI) { + res.json(userData); + } else { + res.render('accountposts', userData); + } + }); + }); + }); +}; + +accountsController.accountEdit = function(req, res, next) { + var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; + + getUserDataByUserSlug(req.params.userslug, callerUID, function (err, userData) { + if(err) { + return next(err); + } + + if (res.locals.isAPI) { + res.json(userData); + } else { + res.render('accountedit', userData); + } + }); +}; + +accountsController.accountSettings = function(req, res, next) { + var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; + + user.getUidByUserslug(req.params.userslug, function(err, uid) { + if (err) { + return next(err); + } + + if (!uid) { + return userNotFound(); + } + + if (parseInt(uid, 10) !== callerUID) { + return userNotAllowed(); + } + + plugins.fireHook('filter:user.settings', [], function(err, settings) { + if (err) { + return next(err); + } + + user.getUserFields(uid, ['username', 'userslug'], function(err, userData) { + if (err) { + return next(err); + } + + if(!userData) { + return userNotFound(); + } + userData.yourid = req.user.uid; + userData.theirid = uid; + userData.settings = settings; + + if (res.locals.isAPI) { + res.json(userData); + } else { + res.render('accountsettings', userData); + } + }); + }); + + }); + + +}; + + + +module.exports = accountsController; \ No newline at end of file diff --git a/src/controllers/index.js b/src/controllers/index.js index 257d77a061..8bc57bd8dc 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -1,5 +1,7 @@ var topicsController = require('./topics'), categoriesController = require('./categories'), + usersController = require('./users'), + accountsController = require('./accounts'), staticController = require('./static'), async = require('async'), nconf = require('nconf'), @@ -14,6 +16,8 @@ var topicsController = require('./topics'), Controllers = { topics: topicsController, categories: categoriesController, + users: usersController, + accounts: accountsController, static: staticController }; diff --git a/src/controllers/users.js b/src/controllers/users.js index 3f905cba74..5187cf7e20 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -3,275 +3,5 @@ var usersController = {}, posts = require('./../posts'); -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.' - }); - } -} - -usersController.getAccount = function(req, res, next) { - var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; - - getUserDataByUserSlug(req.params.userslug, callerUID, function (err, userData) { - if(err) { - return next(err); - } - - if(!userData) { - return res.json(404, { - error: 'User not found!' - }); - } - - user.isFollowing(callerUID, userData.theirid, function (isFollowing) { - posts.getPostsByUid(callerUID, userData.theirid, 0, 9, function (err, userPosts) { - if(err) { - return next(err); - } - - userData.posts = userPosts.posts.filter(function (p) { - return p && parseInt(p.deleted, 10) !== 1; - }); - - userData.isFollowing = isFollowing; - - if (!userData.profileviews) { - userData.profileviews = 1; - } - - if (callerUID !== parseInt(userData.uid, 10) && callerUID) { - user.incrementUserFieldBy(userData.uid, 'profileviews', 1); - } - - postTools.parse(userData.signature, function (err, signature) { - userData.signature = signature; - - if (res.locals.isAPI) { - res.json({}); - } else { - res.render('account', {}); - }; - }); - }); - }); - }); -}; - -usersController.getFollowing = function(req, res, next) { - var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; - - getUserDataByUserSlug(req.params.userslug, callerUID, function (err, userData) { - if(err) { - return next(err); - } - - if (userData) { - user.getFollowing(userData.uid, function (err, followingData) { - if(err) { - return next(err); - } - userData.following = followingData; - userData.followingCount = followingData.length; - - if (res.locals.isAPI) { - res.json(userData); - } else { - res.render('following', userData); - } - }); - - } else { - return userNotFound(); - } - }); -}; - -usersController.getFollowers = function(req, res, next) { - var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; - - getUserDataByUserSlug(req.params.userslug, callerUID, function (err, userData) { - if(err) { - return next(err); - } - - if (userData) { - user.getFollowers(userData.uid, function (err, followersData) { - if(err) { - return next(err); - } - userData.followers = followersData; - userData.followersCount = followersData.length; - - if (res.locals.isAPI) { - res.json(userData); - } else { - res.render('followers', userData); - } - }); - } else { - return userNotFound(); - } - }); -}; - -usersController.getFavourites = function(req, res, next) { - var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; - - user.getUidByUserslug(req.params.userslug, function (err, uid) { - if (!uid) { - return userNotFound(); - } - - if (parseInt(uid, 10) !== callerUID) { - return userNotAllowed(); - } - - user.getUserFields(uid, ['username', 'userslug'], function (err, userData) { - if (err) { - return next(err); - } - - if (!userData) { - return userNotFound(); - } - - posts.getFavourites(uid, 0, 9, function (err, favourites) { - if (err) { - return next(err); - } - - userData.theirid = uid; - userData.yourid = callerUID; - userData.posts = favourites.posts; - userData.nextStart = favourites.nextStart; - - if (res.locals.isAPI) { - res.json(userData); - } else { - res.render('favourites', userData); - } - }); - }); - }); -}; - -usersController.getPosts = function(req, res, next) { - var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; - - user.getUidByUserslug(req.params.userslug, function (err, uid) { - if (!uid) { - return userNotFound(); - } - - user.getUserFields(uid, ['username', 'userslug'], function (err, userData) { - if (err) { - return next(err); - } - - if (!userData) { - return userNotFound(); - } - - posts.getPostsByUid(callerUID, uid, 0, 19, function (err, userPosts) { - if (err) { - return next(err); - } - userData.uid = uid; - userData.theirid = uid; - userData.yourid = callerUID; - userData.posts = userPosts.posts; - userData.nextStart = userPosts.nextStart; - - if (res.locals.isAPI) { - res.json(userData); - } else { - res.render('accountposts', userData); - } - }); - }); - }); -}; - -usersController.accountEdit = function(req, res, next) { - var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; - - getUserDataByUserSlug(req.params.userslug, callerUID, function (err, userData) { - if(err) { - return next(err); - } - - if (res.locals.isAPI) { - res.json(userData); - } else { - res.render('accountedit', userData); - } - }); -}; - -usersController.accountSettings = function(req, res, next) { - var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; - - user.getUidByUserslug(req.params.userslug, function(err, uid) { - if (err) { - return next(err); - } - - if (!uid) { - return userNotFound(); - } - - if (parseInt(uid, 10) !== callerUID) { - return userNotAllowed(); - } - - plugins.fireHook('filter:user.settings', [], function(err, settings) { - if (err) { - return next(err); - } - - user.getUserFields(uid, ['username', 'userslug'], function(err, userData) { - if (err) { - return next(err); - } - - if(!userData) { - return userNotFound(); - } - userData.yourid = req.user.uid; - userData.theirid = uid; - userData.settings = settings; - - if (res.locals.isAPI) { - res.json(userData); - } else { - res.render('accountsettings', userData); - } - }); - }); - - }); - - -}; - - module.exports = usersController; \ No newline at end of file diff --git a/src/webserver.js b/src/webserver.js index 68725e1240..ee6b039339 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -647,6 +647,7 @@ process.on('uncaughtException', function(err) { app.get('/outgoing', app.buildHeader, controllers.outgoing); app.get('/api/outgoing', app.prepareAPI, controllers.outgoing); + /* Static Pages */ app.get('/404', app.buildHeader, controllers.static['404']); app.get('/api/404', app.prepareAPI, controllers.static['404']); @@ -656,7 +657,6 @@ process.on('uncaughtException', function(err) { app.get('/500', app.buildHeader, controllers.static['500']); app.get('/api/500', app.prepareAPI, controllers.static['500']); - /* Topics */ app.get('/topic/:topic_id/:slug?', app.buildHeader, controllers.topics.get); app.get('/api/topic/:topic_id/:slug?', app.prepareAPI, controllers.topics.get); @@ -677,27 +677,27 @@ process.on('uncaughtException', function(err) { app.get('/category/:category_id/:slug?', app.buildHeader, controllers.categories.get); app.get('/api/category/:category_id/:slug?', app.prepareAPI, controllers.categories.get); - /* Users */ - app.get'/user/:userslug', app.buildHeader, app.checkGlobalPrivacySettings, controllers.users.getAccount); - app.get'/api/user/:userslug', app.prepareAPI, app.checkGlobalPrivacySettings controllers.users.getAccount); + /* Accounts */ + app.get'/user/:userslug', app.buildHeader, app.checkGlobalPrivacySettings, controllers.accounts.getAccount); + app.get'/api/user/:userslug', app.prepareAPI, app.checkGlobalPrivacySettings controllers.accounts.getAccount); - app.get'/user/:userslug/following', app.buildHeader, app.checkGlobalPrivacySettings, controllers.users.getFollowing); - app.get'/api/user/:userslug/following', app.prepareAPI, app.checkGlobalPrivacySettings controllers.users.getFollowing); + app.get'/user/:userslug/following', app.buildHeader, app.checkGlobalPrivacySettings, controllers.accounts.getFollowing); + app.get'/api/user/:userslug/following', app.prepareAPI, app.checkGlobalPrivacySettings controllers.accounts.getFollowing); - app.get'/user/:userslug/followers', app.buildHeader, app.checkGlobalPrivacySettings, controllers.users.getFollowers); - app.get'/api/user/:userslug/followers', app.prepareAPI, app.checkGlobalPrivacySettings controllers.users.getFollowers); + app.get'/user/:userslug/followers', app.buildHeader, app.checkGlobalPrivacySettings, controllers.accounts.getFollowers); + app.get'/api/user/:userslug/followers', app.prepareAPI, app.checkGlobalPrivacySettings controllers.accounts.getFollowers); - app.get'/user/:userslug/favourites', app.buildHeader, app.checkGlobalPrivacySettings, controllers.users.getFavourites); - app.get'/api/user/:userslug/favourites', app.prepareAPI, app.checkGlobalPrivacySettings controllers.users.getFavourites); + app.get'/user/:userslug/favourites', app.buildHeader, app.checkGlobalPrivacySettings, controllers.accounts.getFavourites); + app.get'/api/user/:userslug/favourites', app.prepareAPI, app.checkGlobalPrivacySettings controllers.accounts.getFavourites); - app.get'/user/:userslug/posts', app.buildHeader, app.checkGlobalPrivacySettings, controllers.users.getPosts); - app.get'/api/user/:userslug/posts', app.prepareAPI, app.checkGlobalPrivacySettings controllers.users.getPosts); + app.get'/user/:userslug/posts', app.buildHeader, app.checkGlobalPrivacySettings, controllers.accounts.getPosts); + app.get'/api/user/:userslug/posts', app.prepareAPI, app.checkGlobalPrivacySettings controllers.accounts.getPosts); - app.get'/user/:userslug/edit', app.buildHeader, app.checkGlobalPrivacySettings, controllers.users.accountEdit); - app.get'/api/user/:userslug/edit', app.prepareAPI, app.checkGlobalPrivacySettings controllers.users.accountEdit); + app.get'/user/:userslug/edit', app.buildHeader, app.checkGlobalPrivacySettings, controllers.accounts.accountEdit); + app.get'/api/user/:userslug/edit', app.prepareAPI, app.checkGlobalPrivacySettings controllers.accounts.accountEdit); - app.get'/user/:userslug/settings', app.buildHeader, app.checkGlobalPrivacySettings, controllers.users.accountSettings); - app.get'/api/user/:userslug/settings', app.prepareAPI, app.checkGlobalPrivacySettings controllers.users.accountSettings); + app.get'/user/:userslug/settings', app.buildHeader, app.checkGlobalPrivacySettings, controllers.accounts.accountSettings); + app.get'/api/user/:userslug/settings', app.prepareAPI, app.checkGlobalPrivacySettings controllers.accounts.accountSettings);