From fa103b2d6ea3f94b254dd8bd13d3246672f15468 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 28 Feb 2014 15:33:25 -0500 Subject: [PATCH] finally done with routes/user.js; accounts.getUserByUID; bug fixing and cleanup --- src/controllers/accounts.js | 24 ++- src/controllers/users.js | 5 +- src/routes/user.js | 292 ------------------------------------ src/webserver.js | 2 + 4 files changed, 26 insertions(+), 297 deletions(-) delete mode 100644 src/routes/user.js diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js index 229de0b7c4..725ef706c2 100644 --- a/src/controllers/accounts.js +++ b/src/controllers/accounts.js @@ -1,9 +1,19 @@ -var accountsController = {}, +var accountsController = {}; + +var fs = require('fs'), + path = require('path'), + winston = require('winston'), + nconf = require('nconf'), + async= require('async'), + user = require('./../user'), posts = require('./../posts'), + postTools = require('../postTools'), + utils = require('./../../public/src/utils'), + meta = require('./../meta'), plugins = require('./../plugins'), - meta = require('./../meta'); - + image = require('./../image'), + file = require('./../file'); function userNotFound(res) { if (res.locals.isAPI) { @@ -108,6 +118,14 @@ function getUserDataByUserSlug(userslug, callerUID, callback) { }); } +accountsController.getUserByUID = function(req, res, next) { + var uid = req.params.uid ? req.params.uid : 0; + + user.getUserData(uid, function(err, userData) { + res.json(userData); + }); +}; + accountsController.getAccount = function(req, res, next) { var callerUID = req.user ? parseInt(req.user.uid, 10) : 0; diff --git a/src/controllers/users.js b/src/controllers/users.js index 34fdbf334e..977576fc10 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -1,7 +1,8 @@ var usersController = {}, - user = require('./../user'), - posts = require('./../posts'); +var async = require('async'), + user = require('./../user'), + db = require('./../database'); usersController.getOnlineUsers = function(req, res, next) { var websockets = require('../socket.io'); diff --git a/src/routes/user.js b/src/routes/user.js deleted file mode 100644 index 1c9c0e2894..0000000000 --- a/src/routes/user.js +++ /dev/null @@ -1,292 +0,0 @@ -var fs = require('fs'), - path = require('path'), - winston = require('winston'), - nconf = require('nconf'), - async= require('async'), - imagemagick = require('node-imagemagick'), - - user = require('./../user'), - posts = require('./../posts'), - postTools = require('../postTools'), - utils = require('./../../public/src/utils'), - templates = require('./../../public/src/templates'), - meta = require('./../meta'), - plugins = require('./../plugins'), - image = require('./../image'), - file = require('./../file'), - db = require('./../database'); - -(function (User) { - User.createRoutes = function (app) { - - app.namespace('/users', function () { - var routes = ['', '/latest', '/sort-posts', '/sort-reputation', '/online', '/search']; - - function createRoute(routeName) { - app.get(routeName, function (req, res) { - - if(!req.user && !!parseInt(meta.config.privateUserInfo, 10)) { - return res.redirect('/403'); - } - - app.build_header({ - req: req, - res: res - }, function (err, header) { - res.send(header + app.create_route("users" + routeName, "users") + templates['footer']); - }); - }); - } - - for (var i=0; i uploadSize * 1024) { - return res.send({ - error: 'Images must be smaller than ' + uploadSize + ' kb!' - }); - } - - var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']; - if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) { - return res.send({ - error: 'Allowed image types are png, jpg and gif!' - }); - } - - var extension = path.extname(req.files.userPhoto.name); - if (!extension) { - return res.send({ - error: 'Error uploading file! Error : Invalid extension!' - }); - } - - var updateUid = req.user.uid; - - async.waterfall([ - function(next) { - image.resizeImage(req.files.userPhoto.path, extension, 128, 128, next); - }, - function(next) { - image.convertImageToPng(req.files.userPhoto.path, extension, next); - }, - function(next) { - try { - var params = JSON.parse(req.body.params); - if(parseInt(updateUid, 10) === parseInt(params.uid, 10)) { - return next(); - } - - user.isAdministrator(req.user.uid, function(err, isAdmin) { - if(err) { - return next(err); - } - - if(!isAdmin) { - return res.json(403, { - error: 'Not allowed!' - }); - } - updateUid = params.uid; - next(); - }); - } catch(err) { - next(err); - } - } - ], function(err, result) { - - function done(err, image) { - fs.unlink(req.files.userPhoto.path); - if(err) { - return res.send({error: err.message}); - } - - user.setUserField(updateUid, 'uploadedpicture', image.url); - user.setUserField(updateUid, 'picture', image.url); - res.json({ - path: image.url - }); - } - - if(err) { - return res.send({error:err.message}); - } - - if(plugins.hasListeners('filter:uploadImage')) { - return plugins.fireHook('filter:uploadImage', req.files.userPhoto, done); - } - - var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10); - var filename = updateUid + '-profileimg' + (convertToPNG ? '.png' : extension); - - user.getUserField(updateUid, 'uploadedpicture', function (err, oldpicture) { - if (!oldpicture) { - file.saveFileToLocal(filename, req.files.userPhoto.path, done); - return; - } - - var absolutePath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), path.basename(oldpicture)); - - fs.unlink(absolutePath, function (err) { - if (err) { - winston.err(err); - } - - file.saveFileToLocal(filename, req.files.userPhoto.path, done); - }); - }); - }); - }); - }); - - function isAllowed(req, res, next) { - - } - - //app.get('/api/user/:userslug/following', isAllowed, getUserFollowing); - //app.get('/api/user/:userslug/followers', isAllowed, getUserFollowers); - //app.get('/api/user/:userslug/edit', isAllowed, getUserEdit); - //app.get('/api/user/:userslug/settings', isAllowed, getUserSettings); - //app.get('/api/user/:userslug/favourites', isAllowed, getUserFavourites); - //app.get('/api/user/:userslug/posts', isAllowed, getUserPosts); - app.get('/api/user/uid/:uid', isAllowed, getUserData); - //app.get('/api/user/:userslug', isAllowed, getUserProfile); - - app.get('/api/users', isAllowed, getOnlineUsers); - app.get('/api/users/sort-posts', isAllowed, getUsersSortedByPosts); - app.get('/api/users/sort-reputation', isAllowed, getUsersSortedByReputation); - app.get('/api/users/latest', isAllowed, getUsersSortedByJoinDate); - app.get('/api/users/online', isAllowed, getOnlineUsers); - app.get('/api/users/search', isAllowed, getUsersForSearch); - - - function getUserProfile(req, res, next) { - - } - - function getUserData(req, res, next) { - var uid = req.params.uid ? req.params.uid : 0; - - user.getUserData(uid, function(err, userData) { - res.json(userData); - }); - } - - function getUserPosts(req, res, next) { - - } - - function getUserFavourites(req, res, next) { - - } - - function getUserSettings(req, res, next) { - - } - - //function getUserEdit(req, res, next) { - // - } - - function getUserFollowers(req, res, next) { - - } - - function getUserFollowing(req, res, next) { - - } - - function getUsersSortedByJoinDate(req, res) { - - } - - function getUsersSortedByPosts(req, res) { - - } - - function getUsersSortedByReputation(req, res) { - - } - - function getOnlineUsers(req, res, next) { - - } - - function getUsersForSearch(req, res) { - - } - - - }; - -}(exports)); diff --git a/src/webserver.js b/src/webserver.js index 14840844fd..150a052987 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -727,6 +727,8 @@ process.on('uncaughtException', function(err) { app.get'/user/:userslug/settings', app.buildHeader, app.checkGlobalPrivacySettings, app.checkAccountPermissions, controllers.accounts.accountSettings); app.get'/api/user/:userslug/settings', app.prepareAPI, app.checkGlobalPrivacySettings, app.checkAccountPermissions, controllers.accounts.accountSettings); + app.get('/api/user/uid/:uid', app.checkGlobalPrivacySettings, controllers.accounts.getUserByUID); + // this should have been in the API namespace app.post'/user/uploadpicture', app.prepareAPI, app.checkGlobalPrivacySettings, app.checkAccountPermissions, controllers.accounts.uploadPicture);