mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 14:35:47 +01:00
Update user.js
- Do not use a temp var here, that's just wasting memory since we're only using it once (and it isn't helping readability all that much) - Add notes about console.error use, indicate it's temporary and needs replaced with proper logging - Indicate that resizing code should be split out into another process (perhaps with node's built in cluster module? or child_process.spawn?) - Do not use `res.send` with JSON data; use res.json - Use consistent spacing and whitespace usage
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
var user = require('./../user.js'),
|
var user = require('./../user.js'),
|
||||||
posts = require('./../posts.js'),
|
posts = require('./../posts.js'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
@@ -15,10 +14,13 @@ var user = require('./../user.js'),
|
|||||||
return res.redirect('/404');
|
return res.redirect('/404');
|
||||||
|
|
||||||
user.getUserData(req.params.uid, function(data){
|
user.getUserData(req.params.uid, function(data){
|
||||||
if(data)
|
if(data) {
|
||||||
res.send(data);
|
res.send(data);
|
||||||
else
|
} else {
|
||||||
res.send("User doesn't exist!");
|
res.send("User doesn't exist!");
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -44,7 +46,6 @@ var user = require('./../user.js'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get('/users/:userslug', function(req, res) {
|
app.get('/users/:userslug', function(req, res) {
|
||||||
|
|
||||||
if(!req.params.userslug) {
|
if(!req.params.userslug) {
|
||||||
res.send("User doesn't exist!");
|
res.send("User doesn't exist!");
|
||||||
return;
|
return;
|
||||||
@@ -59,8 +60,7 @@ var user = require('./../user.js'),
|
|||||||
user.getUserData(uid, function(userdata) {
|
user.getUserData(uid, function(userdata) {
|
||||||
if(userdata) {
|
if(userdata) {
|
||||||
res.send(app.build_header(res) + app.create_route('users/'+userdata.userslug, 'account') + templates['footer']);
|
res.send(app.build_header(res) + app.create_route('users/'+userdata.userslug, 'account') + templates['footer']);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
res.redirect('/404');
|
res.redirect('/404');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -68,21 +68,20 @@ var user = require('./../user.js'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get('/users/:userslug/edit', function(req, res){
|
app.get('/users/:userslug/edit', function(req, res){
|
||||||
|
|
||||||
if(!req.user)
|
if(!req.user)
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
|
||||||
user.getUserField(req.user.uid, 'userslug', function(userslug) {
|
user.getUserField(req.user.uid, 'userslug', function(userslug) {
|
||||||
|
|
||||||
if(req.params.userslug && userslug === req.params.userslug)
|
if(req.params.userslug && userslug === req.params.userslug) {
|
||||||
res.send(app.build_header(res) + app.create_route('users/'+req.params.userslug+'/edit','accountedit') + templates['footer']);
|
res.send(app.build_header(res) + app.create_route('users/'+req.params.userslug+'/edit','accountedit') + templates['footer']);
|
||||||
else
|
} else {
|
||||||
return res.redirect('/404');
|
return res.redirect('/404');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/users/doedit', function(req, res){
|
app.post('/users/doedit', function(req, res){
|
||||||
|
|
||||||
if(!req.user)
|
if(!req.user)
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
|
||||||
@@ -96,7 +95,6 @@ var user = require('./../user.js'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.post('/users/uploadpicture', function(req, res) {
|
app.post('/users/uploadpicture', function(req, res) {
|
||||||
|
|
||||||
if(!req.user)
|
if(!req.user)
|
||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
|
||||||
@@ -108,9 +106,8 @@ var user = require('./../user.js'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
||||||
var type = req.files.userPhoto.type;
|
|
||||||
|
|
||||||
if(allowedTypes.indexOf(type) === -1) {
|
if(allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
|
||||||
res.send({
|
res.send({
|
||||||
error: 'Allowed image types are png, jpg and gif!'
|
error: 'Allowed image types are png, jpg and gif!'
|
||||||
});
|
});
|
||||||
@@ -128,7 +125,7 @@ var user = require('./../user.js'),
|
|||||||
|
|
||||||
fs.unlink(absolutePath, function(err) {
|
fs.unlink(absolutePath, function(err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
console.log(err);
|
console.error('[%d] %s', Date.now(), + err);
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
|
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
|
||||||
@@ -138,7 +135,6 @@ var user = require('./../user.js'),
|
|||||||
});
|
});
|
||||||
|
|
||||||
function uploadUserPicture(uid, extension, tempPath, res) {
|
function uploadUserPicture(uid, extension, tempPath, res) {
|
||||||
|
|
||||||
if(!extension) {
|
if(!extension) {
|
||||||
res.send({
|
res.send({
|
||||||
error: 'Error uploading file! Error : Invalid extension!'
|
error: 'Error uploading file! Error : Invalid extension!'
|
||||||
@@ -149,6 +145,7 @@ var user = require('./../user.js'),
|
|||||||
var filename = uid + '-profileimg' + extension;
|
var filename = uid + '-profileimg' + extension;
|
||||||
var uploadPath = path.join(global.configuration['ROOT_DIRECTORY'], config.upload_path, filename);
|
var uploadPath = path.join(global.configuration['ROOT_DIRECTORY'], config.upload_path, filename);
|
||||||
|
|
||||||
|
// @todo move to proper logging code - this should only be temporary
|
||||||
console.log('Info: Attempting upload to: '+ uploadPath);
|
console.log('Info: Attempting upload to: '+ uploadPath);
|
||||||
|
|
||||||
var is = fs.createReadStream(tempPath);
|
var is = fs.createReadStream(tempPath);
|
||||||
@@ -159,9 +156,7 @@ var user = require('./../user.js'),
|
|||||||
|
|
||||||
var imageUrl = config.upload_url + filename;
|
var imageUrl = config.upload_url + filename;
|
||||||
|
|
||||||
res.send({
|
res.json({ path: imageUrl });
|
||||||
path: imageUrl
|
|
||||||
});
|
|
||||||
|
|
||||||
user.setUserField(uid, 'uploadedpicture', imageUrl);
|
user.setUserField(uid, 'uploadedpicture', imageUrl);
|
||||||
user.setUserField(uid, 'picture', imageUrl);
|
user.setUserField(uid, 'picture', imageUrl);
|
||||||
@@ -174,14 +169,18 @@ var user = require('./../user.js'),
|
|||||||
width: 128
|
width: 128
|
||||||
}, function(err, stdout, stderr) {
|
}, function(err, stdout, stderr) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err);
|
// @todo: better logging method; for now, send to stderr.
|
||||||
|
// ideally, this should be happening in another process
|
||||||
|
// to avoid poisoning the main process on error or allowing a significant problem
|
||||||
|
// to crash the main process
|
||||||
|
console.error('[%d] %s', Date.now(), + err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
os.on('error', function(err) {
|
os.on('error', function(err) {
|
||||||
console.log(err);
|
console.error('[%d] %s', Date.now(), + err);
|
||||||
});
|
});
|
||||||
|
|
||||||
is.pipe(os);
|
is.pipe(os);
|
||||||
@@ -217,7 +216,7 @@ var user = require('./../user.js'),
|
|||||||
return res.redirect('/');
|
return res.redirect('/');
|
||||||
|
|
||||||
user.follow(req.user.uid, req.body.uid, function(data) {
|
user.follow(req.user.uid, req.body.uid, function(data) {
|
||||||
res.send({data:data});
|
res.json({ data:data });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -229,7 +228,7 @@ var user = require('./../user.js'),
|
|||||||
return res.redirect('/');
|
return res.redirect('/');
|
||||||
|
|
||||||
user.unfollow(req.user.uid, req.body.uid, function(data) {
|
user.unfollow(req.user.uid, req.body.uid, function(data) {
|
||||||
res.send({data:data});
|
res.json({ data:data });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -251,20 +250,18 @@ var user = require('./../user.js'),
|
|||||||
|
|
||||||
function api_method(req, res) {
|
function api_method(req, res) {
|
||||||
|
|
||||||
var callerUID = req.user?req.user.uid : 0;
|
var callerUID = req.user ? req.user.uid : 0;
|
||||||
|
|
||||||
if (!req.params.section && !req.params.userslug) {
|
if (!req.params.section && !req.params.userslug) {
|
||||||
user.getUserList(function(data) {
|
user.getUserList(function(data) {
|
||||||
data = data.sort(function(a, b) {
|
data = data.sort(function(a, b) {
|
||||||
return b.joindate - a.joindate;
|
return b.joindate - a.joindate;
|
||||||
});
|
});
|
||||||
res.json({search_display: 'none', users:data});
|
res.json({ search_display: 'none', users: data });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if(String(req.params.section).toLowerCase() === 'following') {
|
else if(String(req.params.section).toLowerCase() === 'following') {
|
||||||
|
|
||||||
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) {
|
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) {
|
||||||
|
|
||||||
user.getFollowing(userData.uid, function(followingData){
|
user.getFollowing(userData.uid, function(followingData){
|
||||||
userData.following = followingData;
|
userData.following = followingData;
|
||||||
userData.followingCount = followingData.length;
|
userData.followingCount = followingData.length;
|
||||||
@@ -273,9 +270,7 @@ var user = require('./../user.js'),
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if(String(req.params.section).toLowerCase() === 'followers') {
|
else if(String(req.params.section).toLowerCase() === 'followers') {
|
||||||
|
|
||||||
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) {
|
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) {
|
||||||
|
|
||||||
user.getFollowers(userData.uid, function(followersData){
|
user.getFollowers(userData.uid, function(followersData){
|
||||||
userData.followers = followersData;
|
userData.followers = followersData;
|
||||||
userData.followersCount = followersData.length;
|
userData.followersCount = followersData.length;
|
||||||
@@ -289,22 +284,17 @@ var user = require('./../user.js'),
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) {
|
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) {
|
||||||
|
|
||||||
user.isFollowing(callerUID, userData.theirid, function(isFollowing) {
|
user.isFollowing(callerUID, userData.theirid, function(isFollowing) {
|
||||||
|
|
||||||
posts.getPostsByUid(userData.theirid, 0, 9, function(posts) {
|
posts.getPostsByUid(userData.theirid, 0, 9, function(posts) {
|
||||||
userData.posts = posts;
|
userData.posts = posts;
|
||||||
userData.isFollowing = isFollowing;
|
userData.isFollowing = isFollowing;
|
||||||
|
|
||||||
userData.signature = marked(userData.signature || '');
|
userData.signature = marked(userData.signature || '');
|
||||||
|
|
||||||
res.json(userData);
|
res.json(userData);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get('/api/users/:userslug?/:section?', api_method);
|
app.get('/api/users/:userslug?/:section?', api_method);
|
||||||
@@ -318,7 +308,7 @@ var user = require('./../user.js'),
|
|||||||
data = data.sort(function(a, b) {
|
data = data.sort(function(a, b) {
|
||||||
return b.postcount - a.postcount;
|
return b.postcount - a.postcount;
|
||||||
});
|
});
|
||||||
res.json({search_display: 'none', users:data});
|
res.json({ search_display: 'none', users:data });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,7 +317,7 @@ var user = require('./../user.js'),
|
|||||||
data = data.sort(function(a, b) {
|
data = data.sort(function(a, b) {
|
||||||
return b.reputation - a.reputation;
|
return b.reputation - a.reputation;
|
||||||
});
|
});
|
||||||
res.json({search_display: 'none', users:data});
|
res.json({ search_display: 'none', users:data });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,28 +326,26 @@ var user = require('./../user.js'),
|
|||||||
data = data.sort(function(a, b) {
|
data = data.sort(function(a, b) {
|
||||||
return b.joindate - a.joindate;
|
return b.joindate - a.joindate;
|
||||||
});
|
});
|
||||||
res.json({search_display: 'none', users:data});
|
res.json({ search_display: 'none', users:data });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUsersForSearch(req, res) {
|
function getUsersForSearch(req, res) {
|
||||||
res.json({search_display: 'block', users: []});
|
res.json({ search_display: 'block', users: [] });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function getUserDataByUserSlug(userslug, callerUID, callback) {
|
function getUserDataByUserSlug(userslug, callerUID, callback) {
|
||||||
|
|
||||||
user.get_uid_by_userslug(userslug, function(uid) {
|
user.get_uid_by_userslug(userslug, function(uid) {
|
||||||
|
|
||||||
user.getUserData(uid, function(data) {
|
user.getUserData(uid, function(data) {
|
||||||
if(data) {
|
if(data) {
|
||||||
|
|
||||||
data.joindate = utils.relativeTime(data.joindate);
|
data.joindate = utils.relativeTime(data.joindate);
|
||||||
|
|
||||||
if(!data.birthday)
|
if(!data.birthday) {
|
||||||
data.age = '';
|
data.age = '';
|
||||||
else
|
} else {
|
||||||
data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear();
|
data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear();
|
||||||
|
}
|
||||||
|
|
||||||
data.uid = uid;
|
data.uid = uid;
|
||||||
data.yourid = callerUID;
|
data.yourid = callerUID;
|
||||||
@@ -367,14 +355,13 @@ var user = require('./../user.js'),
|
|||||||
user.getFollowerCount(uid, function(followerCount) {
|
user.getFollowerCount(uid, function(followerCount) {
|
||||||
data.followingCount = followingCount;
|
data.followingCount = followingCount;
|
||||||
data.followerCount = followerCount;
|
data.followerCount = followerCount;
|
||||||
|
|
||||||
callback(data);
|
callback(data);
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
else
|
// @todo why is this an empty object? perhaps the callback should expect NULL or undefined instead.
|
||||||
callback({});
|
callback({});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user