Files
NodeBB/src/controllers/accounts.js

514 lines
14 KiB
JavaScript
Raw Normal View History

2014-03-02 23:07:16 -05:00
"use strict";
var accountsController = {};
var fs = require('fs'),
nconf = require('nconf'),
2014-11-15 23:44:34 -05:00
async = require('async'),
2014-12-26 15:44:00 -05:00
validator = require('validator'),
db = require('../database'),
user = require('../user'),
posts = require('../posts'),
topics = require('../topics'),
2014-10-06 16:16:31 -04:00
groups = require('../groups'),
2014-05-12 18:42:13 -04:00
messaging = require('../messaging'),
utils = require('../../public/src/utils'),
meta = require('../meta'),
plugins = require('../plugins'),
languages = require('../languages'),
2015-04-13 15:01:38 -04:00
helpers = require('./helpers');
2014-02-28 14:22:49 -05:00
function getUserDataByUserSlug(userslug, callerUID, callback) {
user.getUidByUserslug(userslug, function(err, uid) {
if (err) {
return callback(err);
}
if (!uid) {
return callback(null, null);
2014-02-28 14:22:49 -05:00
}
async.parallel({
userData : function(next) {
user.getUserData(uid, next);
},
userSettings : function(next) {
user.getSettings(uid, next);
},
isAdmin : function(next) {
user.isAdministrator(callerUID, next);
},
ips: function(next) {
user.getIPs(uid, 4, next);
},
profile_links: function(next) {
plugins.fireHook('filter:user.profileLinks', [], next);
2014-10-06 16:16:31 -04:00
},
groups: function(next) {
groups.getUserGroups([uid], next);
2014-02-28 14:22:49 -05:00
}
}, function(err, results) {
if(err || !results.userData) {
2014-04-09 21:56:30 -04:00
return callback(err || new Error('[[error:invalid-uid]]'));
2014-02-28 14:22:49 -05:00
}
var userData = results.userData;
var userSettings = results.userSettings;
var isAdmin = results.isAdmin;
var self = parseInt(callerUID, 10) === parseInt(userData.uid, 10);
userData.joindate = utils.toISOString(userData.joindate);
2014-11-15 23:54:34 -05:00
userData.lastonline = userData.lastonline ? utils.toISOString(userData.lastonline) : userData.joindate;
userData.age = userData.birthday ? Math.floor((new Date().getTime() - new Date(userData.birthday).getTime()) / 31536000000) : '';
2014-02-28 14:22:49 -05:00
if (!(isAdmin || self || (userData.email && userSettings.showemail))) {
2014-10-06 16:03:59 -04:00
userData.email = '';
2014-02-28 14:22:49 -05:00
}
2014-10-06 16:03:59 -04:00
userData.emailClass = (self && !userSettings.showemail) ? '' : 'hide';
if (!self && !userSettings.showfullname) {
userData.fullname = '';
2014-02-28 14:22:49 -05:00
}
if (isAdmin || self) {
userData.ips = results.ips;
}
userData.uid = userData.uid;
userData.yourid = callerUID;
userData.theirid = userData.uid;
2014-10-06 16:03:59 -04:00
userData.isSelf = self;
2015-01-13 14:54:13 -05:00
userData.showHidden = self || isAdmin;
2014-10-06 16:16:31 -04:00
userData.groups = Array.isArray(results.groups) && results.groups.length ? results.groups[0] : [];
2014-02-28 14:22:49 -05:00
userData.disableSignatures = meta.config.disableSignatures !== undefined && parseInt(meta.config.disableSignatures, 10) === 1;
userData['email:confirmed'] = !!parseInt(userData['email:confirmed'], 10);
userData.profile_links = results.profile_links;
userData.status = require('../socket.io').isUserOnline(userData.uid) ? (userData.status || 'online') : 'offline';
2014-10-06 16:16:31 -04:00
userData.banned = parseInt(userData.banned, 10) === 1;
userData.websiteName = userData.website.replace('http://', '').replace('https://', '');
2015-01-13 17:15:10 -05:00
userData.followingCount = parseInt(userData.followingCount, 10) || 0;
userData.followerCount = parseInt(userData.followerCount, 10) || 0;
2014-02-28 14:22:49 -05:00
userData.username = validator.escape(userData.username);
userData.email = validator.escape(userData.email);
userData.fullname = validator.escape(userData.fullname);
userData.location = validator.escape(userData.location);
userData.signature = validator.escape(userData.signature);
2014-02-28 14:22:49 -05:00
callback(null, userData);
});
});
}
accountsController.getUserByUID = function(req, res, next) {
var uid = req.params.uid ? req.params.uid : 0;
async.parallel({
settings: async.apply(user.getSettings, uid),
userData: async.apply(user.getUserData, uid)
}, function(err, results) {
2014-10-27 01:49:57 -04:00
if (err) {
return next(err);
}
results.userData.email = results.settings.showemail ? results.userData.email : undefined;
results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined;
res.json(results.userData);
});
};
accountsController.getAccount = function(req, res, next) {
var lowercaseSlug = req.params.userslug.toLowerCase();
if (req.params.userslug !== lowercaseSlug) {
if (res.locals.isAPI) {
req.params.userslug = lowercaseSlug;
} else {
2014-11-18 23:27:40 -05:00
return res.redirect(nconf.get('relative_path') + '/user/' + lowercaseSlug);
}
}
getUserDataByUserSlug(req.params.userslug, req.uid, function (err, userData) {
2014-11-15 23:44:34 -05:00
if (err) {
return next(err);
}
2014-11-15 23:44:34 -05:00
if (!userData) {
2014-12-01 20:28:36 -05:00
return helpers.notFound(req, res);
}
if (req.uid !== parseInt(userData.uid, 10)) {
2015-01-10 18:59:24 -05:00
user.incrementUserFieldBy(userData.uid, 'profileviews', 1);
}
async.parallel({
isFollowing: function(next) {
user.isFollowing(req.uid, userData.theirid, next);
},
posts: function(next) {
posts.getPostsFromSet('uid:' + userData.theirid + ':posts', req.uid, 0, 9, next);
},
signature: function(next) {
posts.parseSignature(userData, req.uid, next);
}
}, function(err, results) {
if(err) {
return next(err);
}
userData.posts = results.posts.posts.filter(function (p) {
return p && parseInt(p.deleted, 10) !== 1;
});
2014-09-15 14:34:01 -04:00
userData.nextStart = results.posts.nextStart;
userData.isFollowing = results.isFollowing;
if (!userData.profileviews) {
userData.profileviews = 1;
}
plugins.fireHook('filter:user.account', {userData: userData, uid: req.uid}, function(err, data) {
2014-11-06 00:29:06 -05:00
if (err) {
return next(err);
}
res.render('account/profile', data.userData);
});
});
});
};
accountsController.getFollowing = function(req, res, next) {
2014-05-06 16:28:31 -04:00
getFollow('account/following', 'following', req, res, next);
};
accountsController.getFollowers = function(req, res, next) {
2014-05-06 16:28:31 -04:00
getFollow('account/followers', 'followers', req, res, next);
};
2015-01-13 14:54:13 -05:00
function getFollow(tpl, name, req, res, next) {
var userData;
async.waterfall([
function(next) {
getUserDataByUserSlug(req.params.userslug, req.uid, next);
},
function(data, next) {
userData = data;
if (!userData) {
2014-12-01 20:28:36 -05:00
return helpers.notFound(req, res);
}
var method = name === 'following' ? 'getFollowing' : 'getFollowers';
2015-01-13 17:15:10 -05:00
user[method](userData.uid, 0, 49, next);
}
], function(err, users) {
if (err) {
return next(err);
}
2015-01-13 17:15:10 -05:00
userData.users = users;
userData.nextStart = 50;
2015-01-13 14:54:13 -05:00
res.render(tpl, userData);
});
}
accountsController.getFavourites = function(req, res, next) {
2015-01-13 14:54:13 -05:00
getFromUserSet('account/favourites', 'favourites', posts.getPostsFromSet, 'posts', req, res, next);
};
accountsController.getPosts = function(req, res, next) {
2015-01-13 14:54:13 -05:00
getFromUserSet('account/posts', 'posts', posts.getPostsFromSet, 'posts', req, res, next);
};
2014-04-03 12:42:40 -04:00
2015-01-13 14:54:13 -05:00
accountsController.getWatchedTopics = function(req, res, next) {
getFromUserSet('account/watched', 'followed_tids', topics.getTopicsFromSet, 'topics', req, res, next);
2014-04-03 12:42:40 -04:00
};
accountsController.getTopics = function(req, res, next) {
2015-01-13 14:54:13 -05:00
getFromUserSet('account/topics', 'topics', topics.getTopicsFromSet, 'topics', req, res, next);
};
2015-01-30 15:48:17 -05:00
accountsController.getGroups = function(req, res, next) {
accountsController.getBaseUser(req.params.userslug, req.uid, function(err, userData) {
2015-01-30 15:48:17 -05:00
if (err) {
return next(err);
}
if (!userData) {
return helpers.notFound(req, res);
}
groups.getUserGroups([userData.uid], function(err, groups) {
if (err) {
return next(err);
}
userData.groups = groups[0];
res.render('account/groups', userData);
});
});
};
2015-01-13 14:54:13 -05:00
function getFromUserSet(tpl, set, method, type, req, res, next) {
2015-04-22 13:47:41 -04:00
async.parallel({
settings: function(next) {
user.getSettings(req.uid, next);
},
userData: function(next) {
accountsController.getBaseUser(req.params.userslug, req.uid, next);
}
}, function(err, results) {
2014-04-03 12:42:40 -04:00
if (err) {
return next(err);
}
2015-04-22 13:47:41 -04:00
var userData = results.userData;
2014-04-03 12:42:40 -04:00
if (!userData) {
2014-12-01 20:28:36 -05:00
return helpers.notFound(req, res);
2014-04-03 12:42:40 -04:00
}
2015-04-22 13:47:41 -04:00
var setName = 'uid:' + userData.uid + ':' + set;
var page = Math.max(1, parseInt(req.query.page, 10) || 1);
var itemsPerPage = (tpl === 'account/topics' || tpl === 'account/watched') ? results.settings.topicsPerPage : results.settings.postsPerPage;
async.parallel({
itemCount: function(next) {
if (results.settings.usePagination) {
db.sortedSetCard(setName, next);
} else {
next(null, 0);
}
2015-04-22 13:47:41 -04:00
},
data: function(next) {
var start = (page - 1) * itemsPerPage;
var stop = start + itemsPerPage;
method(setName, req.uid, start, stop, next);
}
}, function(err, results) {
2015-01-13 14:54:13 -05:00
if (err) {
2014-04-03 12:42:40 -04:00
return next(err);
}
2015-04-22 13:47:41 -04:00
userData[type] = results.data[type];
userData.nextStart = results.data.nextStart;
var pageCount = Math.ceil(results.itemCount / itemsPerPage);
2015-04-22 13:47:41 -04:00
var pagination = require('../pagination');
userData.pagination = pagination.create(page, pageCount);
2015-01-13 14:54:13 -05:00
res.render(tpl, userData);
});
});
2015-01-13 14:54:13 -05:00
}
2015-03-02 14:12:45 -05:00
accountsController.getBaseUser = function(userslug, callerUID, callback) {
2014-04-03 12:42:40 -04:00
user.getUidByUserslug(userslug, function (err, uid) {
if (err || !uid) {
return callback(err);
}
async.parallel({
user: function(next) {
user.getUserFields(uid, ['uid', 'username', 'userslug'], next);
},
2014-07-31 20:15:11 -04:00
isAdmin: function(next) {
user.isAdministrator(callerUID, next);
},
profile_links: function(next) {
plugins.fireHook('filter:user.profileLinks', [], next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
2014-07-31 20:15:11 -04:00
if (!results.user) {
return callback();
}
2014-07-31 20:15:11 -04:00
results.user.yourid = callerUID;
results.user.theirid = uid;
results.user.isSelf = parseInt(callerUID, 10) === parseInt(uid, 10);
2015-01-13 14:54:13 -05:00
results.user.showHidden = results.user.isSelf || results.isAdmin;
results.user.profile_links = results.profile_links;
callback(null, results.user);
});
2014-04-03 12:42:40 -04:00
});
2015-03-02 14:12:45 -05:00
};
2014-04-03 12:42:40 -04:00
accountsController.accountEdit = function(req, res, next) {
var userData;
async.waterfall([
function(next) {
getUserDataByUserSlug(req.params.userslug, req.uid, next);
},
function(data, next) {
userData = data;
db.getObjectField('user:' + userData.uid, 'password', next);
}
], function(err, password) {
if (err) {
return next(err);
}
userData.hasPassword = !!password;
res.render('account/edit', userData);
});
};
accountsController.accountSettings = function(req, res, next) {
accountsController.getBaseUser(req.params.userslug, req.uid, function(err, userData) {
if (err) {
return next(err);
}
if (!userData) {
2014-12-01 20:28:36 -05:00
return helpers.notFound(req, res);
}
async.parallel({
settings: function(next) {
plugins.fireHook('filter:user.settings', [], next);
},
2015-02-27 12:53:00 -05:00
userGroups: function(next) {
groups.getUserGroups([userData.uid], next);
},
languages: function(next) {
languages.list(next);
}
}, function(err, results) {
if (err) {
return next(err);
}
userData.settings = results.settings;
userData.languages = results.languages;
2015-02-27 12:53:00 -05:00
userData.userGroups = results.userGroups[0];
2014-11-13 15:47:25 -05:00
userData.disableEmailSubscriptions = parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
2014-08-05 15:12:17 -04:00
res.render('account/settings', userData);
});
});
};
2014-02-28 15:26:39 -05:00
accountsController.uploadPicture = function (req, res, next) {
2015-01-10 18:59:24 -05:00
var userPhoto = req.files.files[0];
2015-04-13 15:01:38 -04:00
var updateUid = req.uid;
2014-02-28 15:26:39 -05:00
async.waterfall([
function(next) {
2014-06-30 16:33:10 -04:00
user.getUidByUserslug(req.params.userslug, next);
},
function(uid, next) {
2015-02-18 21:09:33 -05:00
if (parseInt(updateUid, 10) === parseInt(uid, 10)) {
2014-06-30 16:33:10 -04:00
return next();
}
user.isAdministrator(req.uid, function(err, isAdmin) {
2014-06-30 16:33:10 -04:00
if (err) {
return next(err);
2014-02-28 15:26:39 -05:00
}
2014-06-30 16:33:10 -04:00
if (!isAdmin) {
2014-11-15 23:44:34 -05:00
return helpers.notAllowed(req, res);
2014-06-30 16:33:10 -04:00
}
updateUid = uid;
next();
});
2015-04-13 15:01:38 -04:00
},
function(next) {
user.uploadPicture(updateUid, userPhoto, next);
2014-02-28 15:26:39 -05:00
}
2015-04-13 15:01:38 -04:00
], function(err, image) {
fs.unlink(userPhoto.path);
2014-05-12 18:42:13 -04:00
if (err) {
2015-01-12 17:33:11 -05:00
return next(err);
2014-02-28 15:26:39 -05:00
}
2015-04-13 15:01:38 -04:00
res.json([{name: userPhoto.name, url: image.url.startsWith('http') ? image.url : nconf.get('relative_path') + image.url}]);
2014-02-28 15:26:39 -05:00
});
};
accountsController.getNotifications = function(req, res, next) {
user.notifications.getAll(req.uid, 40, function(err, notifications) {
2014-09-08 23:03:37 -04:00
if (err) {
return next(err);
}
res.render('notifications', {
notifications: notifications
});
});
};
2014-05-12 18:42:13 -04:00
accountsController.getChats = function(req, res, next) {
2014-11-13 15:47:25 -05:00
if (parseInt(meta.config.disableChat) === 1) {
2014-12-01 20:28:36 -05:00
return helpers.notFound(req, res);
2014-11-13 15:47:25 -05:00
}
2014-07-21 15:03:01 -04:00
async.parallel({
2015-01-15 21:05:04 -05:00
contacts: async.apply(user.getFollowing, req.user.uid, 0, 19),
2014-09-06 02:59:40 -04:00
recentChats: async.apply(messaging.getRecentChats, req.user.uid, 0, 19)
2014-07-21 15:03:01 -04:00
}, function(err, results) {
2014-05-12 18:42:13 -04:00
if (err) {
return next(err);
}
2014-09-15 16:26:25 -04:00
//Remove entries if they were already present as a followed contact
2014-07-21 15:03:01 -04:00
if (results.contacts && results.contacts.length) {
var contactUids = results.contacts.map(function(contact) {
return parseInt(contact.uid, 10);
});
2014-09-15 16:26:25 -04:00
results.recentChats.users = results.recentChats.users.filter(function(chatObj) {
2014-07-21 15:03:01 -04:00
return contactUids.indexOf(parseInt(chatObj.uid, 10)) === -1;
});
}
2014-07-21 15:03:01 -04:00
if (!req.params.userslug) {
return res.render('chats', {
2014-09-15 16:26:25 -04:00
chats: results.recentChats.users,
nextStart: results.recentChats.nextStart,
2014-10-31 01:50:20 -04:00
contacts: results.contacts,
allowed: true
2014-07-21 15:03:01 -04:00
});
}
async.waterfall([
async.apply(user.getUidByUserslug, req.params.userslug),
function(toUid, next) {
2015-02-11 16:16:32 -05:00
if (!toUid || parseInt(toUid, 10) === parseInt(req.user.uid, 10)) {
2014-12-01 20:28:36 -05:00
return helpers.notFound(req, res);
2014-11-15 23:22:57 -05:00
}
2015-02-18 21:09:33 -05:00
2014-07-21 15:03:01 -04:00
async.parallel({
toUser: async.apply(user.getUserFields, toUid, ['uid', 'username']),
messages: async.apply(messaging.getMessages, req.user.uid, toUid, 'recent', false),
allowed: async.apply(messaging.canMessage, req.user.uid, toUid)
2014-07-21 15:03:01 -04:00
}, next);
}
], function(err, data) {
if (err) {
return next(err);
}
res.render('chats', {
2014-09-15 16:26:25 -04:00
chats: results.recentChats.users,
nextStart: results.recentChats.nextStart,
2014-07-21 15:03:01 -04:00
contacts: results.contacts,
meta: data.toUser,
messages: data.messages,
allowed: data.allowed
2014-07-21 15:03:01 -04:00
});
2014-05-12 18:42:13 -04:00
});
});
};
2014-04-10 20:31:57 +01:00
module.exports = accountsController;