You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

571 lines
14 KiB
JavaScript

var user = require('./../user.js'),
posts = require('./../posts.js'),
postTools = require('../postTools'),
fs = require('fs'),
utils = require('./../../public/src/utils.js'),
path = require('path'),
winston = require('winston'),
12 years ago
nconf = require('nconf'),
meta = require('./../meta'),
async= require('async'),
RDB = require('./../redis'),
websockets = require('./../websockets.js');
12 years ago
(function (User) {
11 years ago
User.createRoutes = function (app) {
12 years ago
app.namespace('/users', function () {
app.get('', function (req, res) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route("users", "users") + templates['footer']);
});
});
12 years ago
app.get('/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']);
});
});
12 years ago
app.get('/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']);
});
});
12 years ago
app.get('/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']);
});
12 years ago
});
app.get('/online', function (req, res) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route("users/online", "users") + templates['footer']);
});
});
app.get('/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']);
});
12 years ago
});
12 years ago
});
12 years ago
app.namespace('/user', function () {
app.get('/:userslug', function (req, res, next) {
if (!req.params.userslug) {
next();
return;
}
user.getUidByUserslug(req.params.userslug, function (err, uid) {
if (!uid) {
return next();
}
12 years ago
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug, 'account') + templates['footer']);
});
});
12 years ago
});
app.get('/:userslug/edit', function (req, res) {
12 years ago
if (!req.user)
return res.redirect('/403');
12 years ago
user.getUserField(req.user.uid, 'userslug', function (err, 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('user/' + req.params.userslug + '/edit', 'accountedit') + templates['footer']);
});
} else {
return res.redirect('/404');
}
});
12 years ago
});
12 years ago
app.get('/:userslug/settings', function (req, res) {
12 years ago
if (!req.user)
return res.redirect('/403');
12 years ago
user.getUserField(req.user.uid, 'userslug', function (err, 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('user/' + req.params.userslug + '/settings', 'accountsettings') + templates['footer']);
})
} else {
return res.redirect('/404');
}
});
});
12 years ago
app.post('/uploadpicture', function (req, res) {
if (!req.user)
return res.redirect('/403');
12 years ago
var uploadSize = meta.config.maximumProfileImageSize || 256;
if (req.files.userPhoto.size > uploadSize * 1024) {
res.send({
error: 'Images must be smaller than ' + uploadSize + ' kb!'
});
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;
}
user.getUserField(req.user.uid, 'uploadedpicture', function (err, oldpicture) {
if (!oldpicture) {
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
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);
}
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
});
});
});
});
12 years ago
function uploadUserPicture(uid, extension, tempPath, res) {
if (!extension) {
res.send({
error: 'Error uploading file! Error : Invalid extension!'
});
return;
}
var filename = uid + '-profileimg' + extension;
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
12 years ago
winston.info('Attempting upload to: ' + uploadPath);
12 years ago
var is = fs.createReadStream(tempPath);
var os = fs.createWriteStream(uploadPath);
is.on('end', function () {
fs.unlinkSync(tempPath);
12 years ago
require('node-imagemagick').crop({
srcPath: uploadPath,
dstPath: uploadPath,
12 years ago
width: 128,
height: 128
}, function (err, stdout, stderr) {
if (err) {
winston.err(err);
11 years ago
res.send({
error: 'Invalid image file!'
});
return;
}
11 years ago
var imageUrl = nconf.get('upload_url') + filename;
user.setUserField(uid, 'uploadedpicture', imageUrl);
user.setUserField(uid, 'picture', imageUrl);
res.json({
path: imageUrl
});
});
});
os.on('error', function (err) {
fs.unlinkSync(tempPath);
winston.err(err);
});
is.pipe(os);
}
app.get('/user/:userslug/following', function (req, res) {
12 years ago
if (!req.user)
return res.redirect('/403');
12 years ago
user.getUidByUserslug(req.params.userslug, function (err, uid) {
if (!uid) {
res.redirect('/404');
return;
}
12 years ago
app.build_header({
req: req,
res: res
}, function (err, header) {
12 years ago
res.send(header + app.create_route('user/' + req.params.userslug + '/following', 'following') + templates['footer']);
});
});
});
12 years ago
app.get('/user/:userslug/followers', function (req, res) {
if (!req.user)
return res.redirect('/403');
12 years ago
user.getUidByUserslug(req.params.userslug, function (err, uid) {
if (!uid) {
res.redirect('/404');
return;
}
app.build_header({
req: req,
res: res
}, function (err, header) {
12 years ago
res.send(header + app.create_route('user/' + req.params.userslug + '/followers', 'followers') + templates['footer']);
});
});
});
app.get('/user/:userslug/favourites', function (req, res) {
12 years ago
if (!req.user)
12 years ago
return res.redirect('/403');
user.getUidByUserslug(req.params.userslug, function (err, uid) {
if (!uid) {
12 years ago
res.redirect('/404');
return;
}
app.build_header({
req: req,
res: res
}, function (err, header) {
12 years ago
res.send(header + app.create_route('user/' + req.params.userslug + '/favourites', 'favourites') + templates['footer']);
12 years ago
});
});
});
app.get('/api/user/:userslug/following', function (req, res) {
12 years ago
var callerUID = req.user ? req.user.uid : '0';
12 years ago
getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) {
if (userData) {
user.getFollowing(userData.uid, function (followingData) {
userData.following = followingData;
userData.followingCount = followingData.length;
res.json(userData);
});
12 years ago
} else {
res.json(404, {
error: 'User not found!'
});
}
});
});
app.get('/api/user/:userslug/followers', function (req, res) {
12 years ago
var callerUID = req.user ? req.user.uid : '0';
12 years ago
getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) {
if (userData) {
user.getFollowers(userData.uid, function (followersData) {
userData.followers = followersData;
userData.followersCount = followersData.length;
res.json(userData);
});
} else {
res.json(404, {
error: 'User not found!'
});
12 years ago
}
});
});
app.get('/api/user/:userslug/edit', function (req, res) {
12 years ago
var callerUID = req.user ? req.user.uid : '0';
getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) {
res.json(userData);
});
});
app.get('/api/user/:userslug/settings', function (req, res, next) {
12 years ago
var callerUID = req.user ? req.user.uid : '0';
user.getUidByUserslug(req.params.userslug, function (err, uid) {
if (!uid) {
res.json(404, {
error: 'User not found!'
});
12 years ago
return;
}
12 years ago
12 years ago
if (uid != callerUID || callerUID == '0') {
res.json(403, {
error: 'Not allowed!'
});
12 years ago
return;
}
12 years ago
12 years ago
user.getUserFields(uid, ['username', 'userslug', 'showemail'], function (err, userData) {
if (err)
return next(err);
if (userData) {
if (userData.showemail && userData.showemail === "1")
12 years ago
userData.showemail = "checked";
else
userData.showemail = "";
res.json(userData);
} else {
res.json(404, {
error: 'User not found!'
});
12 years ago
}
});
});
});
app.get('/api/user/:userslug/favourites', function (req, res, next) {
12 years ago
var callerUID = req.user ? req.user.uid : '0';
12 years ago
user.getUidByUserslug(req.params.userslug, function (err, uid) {
if (!uid) {
res.json(404, {
error: 'User not found!'
});
12 years ago
return;
}
12 years ago
12 years ago
if (uid != callerUID || callerUID == '0') {
res.json(403, {
error: 'Not allowed!'
});
12 years ago
return;
}
user.getUserFields(uid, ['username', 'userslug'], function (err, userData) {
if (err)
return next(err);
if (userData) {
posts.getFavourites(uid, function (err, posts) {
if (err)
12 years ago
return next(err);
userData.posts = posts;
userData.show_nofavourites = posts.length ? 'hide' : 'show';
12 years ago
res.json(userData);
});
} else {
res.json(404, {
error: 'User not found!'
});
12 years ago
}
12 years ago
});
12 years ago
});
});
app.get('/api/user/:userslug', function (req, res) {
12 years ago
var callerUID = req.user ? req.user.uid : '0';
getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) {
if (userData) {
user.isFollowing(callerUID, userData.theirid, function (isFollowing) {
posts.getPostsByUid(userData.theirid, 0, 9, function (posts) {
userData.posts = posts.filter(function (p) {
return p && p.deleted !== "1";
});
userData.isFollowing = isFollowing;
if (!userData.profileviews)
userData.profileviews = 1;
if (callerUID !== userData.uid)
user.incrementUserFieldBy(userData.uid, 'profileviews', 1);
postTools.parse(userData.signature, function (err, signature) {
userData.signature = signature;
res.json(userData);
});
});
});
} else {
res.json(404, {
error: 'User not found!'
});
}
12 years ago
});
});
app.get('/api/users', getUsersSortedByJoinDate);
12 years ago
app.get('/api/users/sort-posts', getUsersSortedByPosts);
app.get('/api/users/sort-reputation', getUsersSortedByReputation);
app.get('/api/users/latest', getUsersSortedByJoinDate);
12 years ago
app.get('/api/users/online', getOnlineUsers);
12 years ago
app.get('/api/users/search', getUsersForSearch);
12 years ago
function getUsersSortedByJoinDate(req, res) {
user.getUsers('users:joindate', 0, 49, function (err, data) {
res.json({
search_display: 'none',
loadmore_display: 'block',
users: data,
show_anon: 'hide'
});
12 years ago
});
}
12 years ago
function getUsersSortedByPosts(req, res) {
user.getUsers('users:postcount', 0, 49, function (err, data) {
res.json({
search_display: 'none',
loadmore_display: 'block',
users: data,
show_anon: 'hide'
});
12 years ago
});
}
function getUsersSortedByReputation(req, res) {
user.getUsers('users:reputation', 0, 49, function (err, data) {
res.json({
search_display: 'none',
loadmore_display: 'block',
users: data,
show_anon: 'hide'
});
12 years ago
});
}
12 years ago
12 years ago
function getOnlineUsers(req, res) {
user.getUsers('users:online', 0, 49, function (err, data) {
var onlineUsers = [];
function iterator(user, callback) {
if(websockets.isUserOnline(user.uid)) {
onlineUsers.push(user);
} else {
RDB.zrem('users:online', user.uid);
}
callback(null);
}
var anonymousUserCount = websockets.getOnlineAnonCount();
async.each(data, iterator, function(err) {
res.json({
search_display: 'none',
loadmore_display: 'block',
users: onlineUsers,
anonymousUserCount: anonymousUserCount,
show_anon: anonymousUserCount?'':'hide'
});
12 years ago
});
});
}
12 years ago
function getUsersForSearch(req, res) {
res.json({
search_display: 'block',
loadmore_display: 'none',
users: [],
show_anon: 'hide'
});
12 years ago
}
function getUserDataByUserSlug(userslug, callerUID, callback) {
user.getUidByUserslug(userslug, function (err, uid) {
12 years ago
if (uid === null) {
12 years ago
callback(null);
return;
}
12 years ago
user.getUserData(uid, function (err, data) {
if (data) {
12 years ago
data.joindate = new Date(parseInt(data.joindate, 10)).toISOString();
if (!data.birthday) {
data.age = '';
} else {
data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear();
}
12 years ago
12 years ago
function canSeeEmail() {
12 years ago
return callerUID == uid || (data.email && (data.showemail && data.showemail === "1"));
12 years ago
}
if (!canSeeEmail())
12 years ago
data.email = "";
12 years ago
if (callerUID == uid && (!data.showemail || data.showemail === "0"))
12 years ago
data.emailClass = "";
12 years ago
else
12 years ago
data.emailClass = "hide";
12 years ago
11 years ago
data.websiteName = data.website.replace('http://', '').replace('https://', '');
data.show_banned = data.banned === '1' ? '' : 'hide';
12 years ago
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 {
callback(null);
}
});
12 years ago
});
}
12 years ago
};
}(exports));