Files
NodeBB/src/routes/user.js

350 lines
9.5 KiB
JavaScript
Raw Normal View History

2013-05-14 14:04:19 -04:00
var user = require('./../user.js'),
2013-06-26 23:04:49 -04:00
posts = require('./../posts.js'),
postTools = require('../postTools'),
2013-05-14 14:04:19 -04:00
fs = require('fs'),
utils = require('./../../public/src/utils.js'),
path = require('path'),
marked = require('marked');
2013-05-14 13:04:12 -04:00
(function(User) {
User.create_routes = function(app) {
app.get('/uid/:uid', function(req, res) {
if(!req.params.uid)
2013-06-20 14:45:38 -04:00
return res.redirect('/404');
2013-05-14 13:04:12 -04:00
2013-07-05 16:31:55 -04:00
user.getUserData(req.params.uid, function(data) {
if(data) {
2013-05-14 13:04:12 -04:00
res.send(data);
} else {
res.json(404, {error:"User doesn't exist!"});
}
2013-05-14 13:04:12 -04:00
});
});
app.get('/users', function(req, res) {
2013-06-29 17:17:09 -04:00
res.send(app.build_header(res) + app.create_route("users", "users") + templates['footer']);
});
app.get('/users-latest', function(req, res) {
res.send(app.build_header(res) + app.create_route("users-latest", "users") + templates['footer']);
});
app.get('/users-sort-posts', function(req, res) {
res.send(app.build_header(res) + app.create_route("users-sort-posts", "users") + templates['footer']);
});
app.get('/users-sort-reputation', function(req, res) {
res.send(app.build_header(res) + app.create_route("users-sort-reputation", "users") + templates['footer']);
});
app.get('/users-search', function(req, res) {
res.send(app.build_header(res) + app.create_route("users-search", "users") + templates['footer']);
2013-05-14 13:04:12 -04:00
});
2013-07-08 16:02:54 -04:00
app.get('/users/:userslug', function(req, res, next) {
2013-06-24 14:33:53 -04:00
if(!req.params.userslug) {
2013-07-08 16:02:54 -04:00
next();
2013-05-14 13:04:12 -04:00
return;
}
2013-06-24 14:33:53 -04:00
user.get_uid_by_userslug(req.params.userslug, function(uid) {
2013-05-14 13:04:12 -04:00
if(!uid) {
2013-07-08 16:02:54 -04:00
next();
2013-05-14 13:04:12 -04:00
return;
}
2013-07-05 16:31:55 -04:00
res.send(app.build_header(res) + app.create_route('users/'+req.params.userslug, 'account') + templates['footer']);
2013-05-14 13:04:12 -04:00
});
});
2013-07-05 16:31:55 -04:00
app.get('/users/:userslug/edit', function(req, res) {
2013-07-15 14:35:33 -04:00
2013-05-14 14:04:19 -04:00
if(!req.user)
return res.redirect('/403');
2013-06-24 14:33:53 -04:00
user.getUserField(req.user.uid, 'userslug', function(userslug) {
2013-05-14 14:04:19 -04:00
if(req.params.userslug && userslug === req.params.userslug) {
2013-06-24 14:33:53 -04:00
res.send(app.build_header(res) + app.create_route('users/'+req.params.userslug+'/edit','accountedit') + templates['footer']);
} else {
2013-06-20 14:45:38 -04:00
return res.redirect('/404');
}
2013-05-14 14:04:19 -04:00
});
});
app.post('/users/uploadpicture', function(req, res) {
if(!req.user)
return res.redirect('/403');
if(req.files.userPhoto.size > 262144) {
2013-05-14 14:04:19 -04:00
res.send({
error: 'Images must be smaller than 256kb!'
2013-05-14 14:04:19 -04:00
});
return;
}
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
2013-05-14 14:04:19 -04:00
if(allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
res.send({
error: 'Allowed image types are png, jpg and gif!'
});
return;
}
2013-05-14 14:04:19 -04:00
user.getUserField(req.user.uid, 'uploadedpicture', function(oldpicture) {
if(!oldpicture) {
uploadUserPicture(req.user.uid, req.files.userPhoto.name, req.files.userPhoto.path, res);
return;
}
2013-07-18 14:47:41 -04:00
var absolutePath = path.join(global.configuration['ROOT_DIRECTORY'], global.nconf.get('upload_path'), path.basename(oldpicture));
2013-06-19 10:32:48 -04:00
2013-05-14 14:04:19 -04:00
fs.unlink(absolutePath, function(err) {
if(err) {
console.error('[%d] %s', Date.now(), + err);
2013-05-14 13:04:12 -04:00
}
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
2013-05-14 14:04:19 -04:00
});
});
});
function uploadUserPicture(uid, extension, tempPath, res) {
if(!extension) {
2013-05-14 14:04:19 -04:00
res.send({
error: 'Error uploading file! Error : Invalid extension!'
2013-05-14 14:04:19 -04:00
});
2013-06-19 10:32:48 -04:00
return;
2013-05-14 14:04:19 -04:00
}
var filename = uid + '-profileimg' + extension;
2013-07-18 14:47:41 -04:00
var uploadPath = path.join(global.configuration['ROOT_DIRECTORY'], global.nconf.get('upload_path'), filename);
2013-05-14 14:04:19 -04:00
// @todo move to proper logging code - this should only be temporary
console.log('Info: Attempting upload to: '+ uploadPath);
2013-05-14 14:04:19 -04:00
2013-05-15 11:46:41 -04:00
var is = fs.createReadStream(tempPath);
var os = fs.createWriteStream(uploadPath);
2013-05-15 11:46:41 -04:00
is.on('end', function() {
2013-06-19 10:32:48 -04:00
fs.unlinkSync(tempPath);
2013-05-15 11:46:41 -04:00
2013-07-18 14:47:41 -04:00
var imageUrl = global.nconf.get('upload_url') + filename;
2013-06-19 10:32:48 -04:00
res.json({ path: imageUrl });
2013-06-19 10:32:48 -04:00
user.setUserField(uid, 'uploadedpicture', imageUrl);
user.setUserField(uid, 'picture', imageUrl);
2013-05-15 11:46:41 -04:00
2013-06-19 10:32:48 -04:00
var im = require('node-imagemagick');
2013-05-15 15:01:05 -04:00
2013-06-19 10:32:48 -04:00
im.resize({
srcPath: uploadPath,
dstPath: uploadPath,
2013-06-19 10:32:48 -04:00
width: 128
}, function(err, stdout, stderr) {
2013-06-19 10:32:48 -04:00
if (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);
2013-06-19 10:32:48 -04:00
}
2013-05-15 15:01:05 -04:00
});
2013-05-15 11:46:41 -04:00
});
os.on('error', function(err) {
console.error('[%d] %s', Date.now(), + err);
2013-05-15 11:46:41 -04:00
});
2013-05-15 11:48:40 -04:00
is.pipe(os);
2013-05-14 14:04:19 -04:00
}
2013-06-24 14:33:53 -04:00
app.get('/users/:userslug/following', function(req, res) {
2013-05-15 13:57:13 -04:00
if(!req.user)
return res.redirect('/403');
2013-05-14 14:04:19 -04:00
user.get_uid_by_userslug(req.params.userslug, function(uid) {
if(!uid) {
res.redirect('/404');
return;
}
res.send(app.build_header(res) + app.create_route('users/'+req.params.userslug+'/following','following') + templates['footer']);
});
});
2013-06-24 14:33:53 -04:00
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');
return;
}
res.send(app.build_header(res) + app.create_route('users/'+req.params.userslug+'/followers','followers') + templates['footer']);
});
});
function api_method(req, res) {
var callerUID = req.user ? req.user.uid : 0;
2013-07-05 16:31:55 -04:00
var userslug = req.params.userslug;
var section = req.params.section ? String(req.params.section).toLowerCase() : null;
if (!section && !userslug) {
2013-06-24 14:33:53 -04:00
user.getUserList(function(data) {
2013-07-03 14:33:26 -04:00
data = data.sort(function(a, b) {
return b.joindate - a.joindate;
});
res.json({ search_display: 'none', users: data });
2013-06-27 14:38:45 -04:00
});
}
2013-07-05 16:31:55 -04:00
else if(section === 'following') {
2013-07-05 16:16:03 -04:00
getFollowing(req, res, callerUID);
}
2013-07-05 16:31:55 -04:00
else if(section === 'followers') {
2013-07-05 16:16:03 -04:00
getFollowers(req, res, callerUID);
2013-05-14 14:04:19 -04:00
}
2013-07-05 16:31:55 -04:00
else if (section === 'edit') {
getUserDataByUserSlug(userslug, callerUID, function(userData) {
res.json(userData);
2013-05-14 14:04:19 -04:00
});
} else {
2013-07-05 16:31:55 -04:00
getUserDataByUserSlug(userslug, callerUID, function(userData) {
if(userData) {
user.isFollowing(callerUID, userData.theirid, function(isFollowing) {
posts.getPostsByUid(userData.theirid, 0, 9, function(posts) {
2013-07-18 15:44:42 -04:00
2013-07-19 10:37:18 -04:00
userData.posts = posts.filter(function(p) {return p.deleted !== "1";});
2013-07-05 16:31:55 -04:00
userData.isFollowing = isFollowing;
userData.signature = postTools.markdownToHTML(userData.signature, true);
2013-07-05 16:31:55 -04:00
res.json(userData);
});
2013-06-26 23:04:49 -04:00
});
2013-07-05 16:31:55 -04:00
} else {
res.json(404, { error: 'User not found!' }) ;
2013-07-05 16:31:55 -04:00
}
2013-05-14 14:04:19 -04:00
});
}
2013-05-14 13:04:12 -04:00
}
2013-07-05 16:16:03 -04:00
function getFollowing(req, res, callerUid) {
2013-07-05 16:31:55 -04:00
getUserDataByUserSlug(req.params.userslug, callerUid, function(userData) {
2013-07-05 16:16:03 -04:00
if(userData) {
2013-07-05 16:31:55 -04:00
user.getFollowing(userData.uid, function(followingData) {
2013-07-05 16:16:03 -04:00
userData.following = followingData;
userData.followingCount = followingData.length;
res.json(userData);
});
} else {
res.json(404, { error: 'User not found!' }) ;
2013-07-05 16:16:03 -04:00
}
});
}
function getFollowers(req, res, callerUid) {
getUserDataByUserSlug(req.params.userslug, callerUid, function(userData) {
if(userData) {
2013-07-05 16:31:55 -04:00
user.getFollowers(userData.uid, function(followersData) {
2013-07-05 16:16:03 -04:00
userData.followers = followersData;
userData.followersCount = followersData.length;
res.json(userData);
});
} else {
res.json(404, { error: 'User not found!' }) ;
2013-07-05 16:16:03 -04:00
}
});
}
2013-06-24 14:33:53 -04:00
app.get('/api/users/:userslug?/:section?', api_method);
2013-06-29 17:17:09 -04:00
app.get('/api/users-sort-posts', getUsersSortedByPosts);
app.get('/api/users-sort-reputation', getUsersSortedByReputation);
app.get('/api/users-latest', getUsersSortedByJoinDate);
app.get('/api/users-search', getUsersForSearch);
function getUsersSortedByPosts(req, res) {
user.getUserList(function(data) {
data = data.sort(function(a, b) {
return b.postcount - a.postcount;
});
res.json({ search_display: 'none', users:data });
2013-06-29 17:17:09 -04:00
});
}
2013-06-29 17:17:09 -04:00
function getUsersSortedByReputation(req, res) {
user.getUserList(function(data) {
data = data.sort(function(a, b) {
return b.reputation - a.reputation;
});
res.json({ search_display: 'none', users:data });
2013-06-29 17:17:09 -04:00
});
}
function getUsersSortedByJoinDate(req, res) {
user.getUserList(function(data) {
data = data.sort(function(a, b) {
return b.joindate - a.joindate;
});
res.json({ search_display: 'none', users:data });
2013-06-29 17:17:09 -04:00
});
}
function getUsersForSearch(req, res) {
res.json({ search_display: 'block', users: [] });
2013-06-29 17:17:09 -04:00
}
2013-06-24 14:33:53 -04:00
function getUserDataByUserSlug(userslug, callerUID, callback) {
user.get_uid_by_userslug(userslug, function(uid) {
2013-07-05 16:16:03 -04:00
if(uid === null) {
callback(null);
return;
}
2013-05-14 14:04:19 -04:00
user.getUserData(uid, function(data) {
if(data) {
data.joindate = utils.relativeTime(data.joindate);
2013-05-16 11:53:27 -04:00
if(!data.birthday) {
2013-05-14 14:04:19 -04:00
data.age = '';
} else {
2013-05-14 14:04:19 -04:00
data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear();
}
2013-05-14 14:04:19 -04:00
data.uid = uid;
data.yourid = callerUID;
data.theirid = uid;
user.getFollowingCount(uid, function(followingCount) {
user.getFollowerCount(uid, function(followerCount) {
data.followingCount = followingCount;
data.followerCount = followerCount;
callback(data);
});
});
} else {
2013-07-05 16:16:03 -04:00
callback(null);
}
2013-05-14 14:04:19 -04:00
});
});
}
2013-05-14 13:04:12 -04:00
};
}(exports));