@ -9,12 +9,12 @@ var user = require('./../user.js'),
( function ( User ) {
User . create _routes = function ( app ) {
app . get ( '/uid/:uid' , function ( req , res ) {
if ( ! req . params . uid )
return res . redirect ( '/404' ) ;
user . getUserData ( req . params . uid , function ( data ) {
if ( data ) {
res . send ( data ) ;
@ -22,7 +22,7 @@ var user = require('./../user.js'),
res . json ( 404 , { error : "User doesn't exist!" } ) ;
}
} ) ;
} ) ;
app . get ( '/users' , function ( req , res ) {
@ -30,25 +30,25 @@ var user = require('./../user.js'),
res . send ( header + app . create _route ( "users" , "users" ) + templates [ 'footer' ] ) ;
} ) ;
} ) ;
app . get ( '/users-latest' , function ( req , res ) {
app . build _header ( { req : req , res : res } , function ( err , header ) {
res . send ( header + app . create _route ( "users-latest" , "users" ) + templates [ 'footer' ] ) ;
} ) ;
} ) ;
app . get ( '/users-sort-posts' , function ( req , res ) {
app . build _header ( { req : req , res : res } , function ( err , header ) {
res . send ( header + app . create _route ( "users-sort-posts" , "users" ) + templates [ 'footer' ] ) ;
} ) ;
} ) ;
app . get ( '/users-sort-reputation' , function ( req , res ) {
app . build _header ( { req : req , res : res } , function ( err , header ) {
res . send ( header + app . create _route ( "users-sort-reputation" , "users" ) + templates [ 'footer' ] ) ;
} ) ;
} ) ;
app . get ( '/users-search' , function ( req , res ) {
app . build _header ( { req : req , res : res } , function ( err , header ) {
res . send ( header + app . create _route ( "users-search" , "users" ) + templates [ 'footer' ] ) ;
@ -71,16 +71,16 @@ var user = require('./../user.js'),
res . send ( header + app . create _route ( 'users/' + req . params . userslug , 'account' ) + templates [ 'footer' ] ) ;
} ) ;
} ) ;
} ) ;
} ) ;
app . get ( '/users/:userslug/edit' , function ( req , res ) {
if ( ! req . user )
return res . redirect ( '/403' ) ;
user . getUserField ( req . user . uid , 'userslug' , function ( userslug ) {
if ( req . params . userslug && userslug === req . params . userslug ) {
app . build _header ( { req : req , res : res } , function ( err , header ) {
res . send ( header + app . create _route ( 'users/' + req . params . userslug + '/edit' , 'accountedit' ) + templates [ 'footer' ] ) ;
@ -88,43 +88,43 @@ var user = require('./../user.js'),
} else {
return res . redirect ( '/404' ) ;
}
} ) ;
} ) ;
} ) ;
app . get ( '/users/:userslug/settings' , function ( req , res ) {
if ( ! req . user )
return res . redirect ( '/403' ) ;
user . getUserField ( req . user . uid , 'userslug' , function ( userslug ) {
if ( req . params . userslug && userslug === req . params . userslug ) {
app . build _header ( { req : req , res : res } , function ( err , header ) {
res . send ( header + app . create _route ( 'users/' + req . params . userslug + '/settings' , 'accountsettings' ) + templates [ 'footer' ] ) ;
} )
} )
} else {
return res . redirect ( '/404' ) ;
}
} ) ;
} ) ;
} ) ;
app . post ( '/users/uploadpicture' , function ( req , res ) {
if ( ! req . user )
return res . redirect ( '/403' ) ;
if ( req . files . userPhoto . size > 262144 ) {
res . send ( {
error : 'Images must be smaller than 256kb!'
} ) ;
return ;
}
var allowedTypes = [ 'image/png' , 'image/jpeg' , 'image/jpg' , 'image/gif' ] ;
if ( allowedTypes . indexOf ( req . files . userPhoto . type ) === - 1 ) {
res . send ( {
error : 'Allowed image types are png, jpg and gif!'
} ) ;
return ;
return ;
}
user . getUserField ( req . user . uid , 'uploadedpicture' , function ( oldpicture ) {
@ -136,7 +136,7 @@ var user = require('./../user.js'),
var absolutePath = path . join ( global . configuration [ 'ROOT_DIRECTORY' ] , global . nconf . get ( 'upload_path' ) , path . basename ( oldpicture ) ) ;
fs . unlink ( absolutePath , function ( err ) {
if ( err ) {
if ( err ) {
winston . error ( '[%d] %s' , Date . now ( ) , + err ) ;
}
@ -144,7 +144,7 @@ var user = require('./../user.js'),
} ) ;
} ) ;
} ) ;
function uploadUserPicture ( uid , extension , tempPath , res ) {
if ( ! extension ) {
res . send ( {
@ -155,9 +155,9 @@ var user = require('./../user.js'),
var filename = uid + '-profileimg' + extension ;
var uploadPath = path . join ( global . configuration [ 'ROOT_DIRECTORY' ] , global . nconf . get ( 'upload_path' ) , filename ) ;
winston . info ( 'Attempting upload to: ' + uploadPath ) ;
var is = fs . createReadStream ( tempPath ) ;
var os = fs . createWriteStream ( uploadPath ) ;
@ -195,24 +195,24 @@ var user = require('./../user.js'),
if ( ! req . user )
return res . redirect ( '/403' ) ;
user . get _uid _by _userslug ( req . params . userslug , function ( uid ) {
if ( ! uid ) {
res . redirect ( '/404' ) ;
return ;
}
app . build _header ( { req : req , res : res } , function ( err , header ) {
res . send ( header + app . create _route ( 'users/' + req . params . userslug + '/following' , 'following' ) + templates [ 'footer' ] ) ;
} ) ;
} ) ;
} ) ;
app . get ( '/users/:userslug/followers' , function ( req , res ) {
if ( ! req . user )
return res . redirect ( '/403' ) ;
user . get _uid _by _userslug ( req . params . userslug , function ( uid ) {
if ( ! uid ) {
res . redirect ( '/404' ) ;
@ -224,6 +224,22 @@ var user = require('./../user.js'),
} ) ;
} ) ;
app . get ( '/users/:userslug/favourites' , function ( req , res ) {
if ( ! req . user )
return res . redirect ( '/403' ) ;
user . get _uid _by _userslug ( req . params . userslug , function ( uid ) {
if ( ! uid ) {
res . redirect ( '/404' ) ;
return ;
}
app . build _header ( { req : req , res : res } , function ( err , header ) {
res . send ( header + app . create _route ( 'users/' + req . params . userslug + '/favourites' , 'favourites' ) + templates [ 'footer' ] ) ;
} ) ;
} ) ;
} ) ;
app . get ( '/api/users/:userslug/following' , function ( req , res ) {
var callerUID = req . user ? req . user . uid : 0 ;
@ -234,7 +250,7 @@ var user = require('./../user.js'),
userData . followingCount = followingData . length ;
res . json ( userData ) ;
} ) ;
} else {
res . json ( 404 , { error : 'User not found!' } ) ;
}
@ -243,7 +259,7 @@ var user = require('./../user.js'),
app . get ( '/api/users/:userslug/followers' , function ( req , res ) {
var callerUID = req . user ? req . user . uid : 0 ;
getUserDataByUserSlug ( req . params . userslug , callerUID , function ( userData ) {
if ( userData ) {
user . getFollowers ( userData . uid , function ( followersData ) {
@ -253,7 +269,7 @@ var user = require('./../user.js'),
} ) ;
} else {
res . json ( 404 , { error : 'User not found!' } ) ;
}
}
} ) ;
} ) ;
@ -273,7 +289,7 @@ var user = require('./../user.js'),
res . json ( 404 , { error : 'User not found!' } ) ;
return ;
}
if ( uid !== callerUID || callerUID === "0" ) {
res . json ( 403 , { error : 'Not allowed!' } ) ;
return ;
@ -287,9 +303,39 @@ var user = require('./../user.js'),
res . json ( userData ) ;
} else {
res . json ( 404 , { error : 'User not found!' } ) ;
}
}
} ) ;
} ) ;
} ) ;
app . get ( '/api/users/:userslug/favourites' , function ( req , res , next ) {
var callerUID = req . user ? req . user . uid : 0 ;
user . get _uid _by _userslug ( req . params . userslug , function ( uid ) {
if ( ! uid ) {
res . json ( 404 , { error : 'User not found!' } ) ;
return ;
}
if ( uid !== callerUID || callerUID === "0" ) {
res . json ( 403 , { error : 'Not allowed!' } ) ;
return ;
}
user . getUserFields ( uid , [ 'username' , 'userslug' ] , function ( userData ) {
if ( userData ) {
posts . getFavourites ( uid , function ( err , posts ) {
if ( err )
return next ( err ) ;
userData . posts = posts ;
userData . show _nofavourites = posts . length ? 'hide' : 'show' ;
res . json ( userData ) ;
} ) ;
} else {
res . json ( 404 , { error : 'User not found!' } ) ;
}
} ) ;
} ) ;
} ) ;
} ) ;
app . get ( '/api/users/:userslug' , function ( req , res ) {
@ -313,7 +359,7 @@ var user = require('./../user.js'),
} else {
res . json ( 404 , { error : 'User not found!' } ) ;
}
} ) ;
} ) ;
} ) ;
app . get ( '/api/users' , getUsersSortedByJoinDate ) ;
@ -321,14 +367,14 @@ var user = require('./../user.js'),
app . get ( '/api/users-sort-reputation' , getUsersSortedByReputation ) ;
app . get ( '/api/users-latest' , getUsersSortedByJoinDate ) ;
app . get ( '/api/users-search' , getUsersForSearch ) ;
function getUsersSortedByJoinDate ( req , res ) {
user . getUsers ( 'users:joindate' , 0 , 49 , function ( err , data ) {
res . json ( { search _display : 'none' , loadmore _display : 'block' , users : data } ) ;
} ) ;
}
function getUsersSortedByPosts ( req , res ) {
user . getUsers ( 'users:postcount' , 0 , 49 , function ( err , data ) {
res . json ( { search _display : 'none' , loadmore _display : 'block' , users : data } ) ;
@ -340,19 +386,19 @@ var user = require('./../user.js'),
res . json ( { search _display : 'none' , loadmore _display : 'block' , users : data } ) ;
} ) ;
}
function getUsersForSearch ( req , res ) {
function getUsersForSearch ( req , res ) {
res . json ( { search _display : 'block' , loadmore _display : 'none' , users : [ ] } ) ;
}
function getUserDataByUserSlug ( userslug , callerUID , callback ) {
user . get _uid _by _userslug ( userslug , function ( uid ) {
if ( uid === null ) {
callback ( null ) ;
return ;
}
user . getUserData ( uid , function ( data ) {
if ( data ) {
data . joindate = utils . relativeTime ( data . joindate ) ;
@ -362,21 +408,21 @@ var user = require('./../user.js'),
} else {
data . age = new Date ( ) . getFullYear ( ) - new Date ( data . birthday ) . getFullYear ( ) ;
}
function canSeeEmail ( ) {
return callerUID === uid || ( data . email && ( data . showemail && data . showemail === "1" ) ) ;
}
if ( ! canSeeEmail ( ) )
if ( ! canSeeEmail ( ) )
data . email = "" ;
if ( callerUID === uid && ( ! data . showemail || data . showemail === "0" ) )
data . emailClass = "" ;
else
else
data . emailClass = "hide" ;
data . show _banned = data . banned === '1' ? '' : 'hide' ;
data . uid = uid ;
data . yourid = callerUID ;
data . theirid = uid ;
@ -392,7 +438,7 @@ var user = require('./../user.js'),
callback ( null ) ;
}
} ) ;
} ) ;
}