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.

654 lines
17 KiB
JavaScript

11 years ago
"use strict";
var accountsController = {};
var fs = require('fs'),
nconf = require('nconf'),
async = require('async'),
10 years ago
validator = require('validator'),
db = require('../database'),
user = require('../user'),
posts = require('../posts'),
topics = require('../topics'),
11 years ago
groups = require('../groups'),
11 years ago
messaging = require('../messaging'),
utils = require('../../public/src/utils'),
meta = require('../meta'),
plugins = require('../plugins'),
languages = require('../languages'),
10 years ago
helpers = require('./helpers');
function getUserDataByUserSlug(userslug, callerUID, callback) {
user.getUidByUserslug(userslug, function(err, uid) {
if (err) {
return callback(err);
}
if (!uid) {
return callback(null, null);
}
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);
11 years ago
},
groups: function(next) {
groups.getUserGroups([uid], next);
}
}, function(err, results) {
if(err || !results.userData) {
return callback(err || new Error('[[error:invalid-uid]]'));
}
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);
10 years ago
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) : '';
if (!(isAdmin || self || (userData.email && userSettings.showemail))) {
11 years ago
userData.email = '';
}
11 years ago
userData.emailClass = (self && !userSettings.showemail) ? '' : 'hide';
if (!self && !userSettings.showfullname) {
userData.fullname = '';
}
if (isAdmin || self) {
userData.ips = results.ips;
}
userData.uid = userData.uid;
userData.yourid = callerUID;
userData.theirid = userData.uid;
10 years ago
userData.isAdmin = isAdmin;
11 years ago
userData.isSelf = self;
10 years ago
userData.showHidden = self || isAdmin;
11 years ago
userData.groups = Array.isArray(results.groups) && results.groups.length ? results.groups[0] : [];
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';
11 years ago
userData.banned = parseInt(userData.banned, 10) === 1;
10 years ago
userData.website = validator.escape(userData.website);
10 years ago
userData.websiteLink = !userData.website.startsWith('http') ? 'http://' + userData.website : userData.website;
10 years ago
userData.websiteName = userData.website.replace(validator.escape('http://'), '').replace(validator.escape('https://'), '');
10 years ago
userData.followingCount = parseInt(userData.followingCount, 10) || 0;
userData.followerCount = parseInt(userData.followerCount, 10) || 0;
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);
userData.aboutme = validator.escape(userData.aboutme || '');
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) {
10 years ago
if (err) {
return next(err);
}
10 years ago
if (!results.userData) {
return helpers.notFound(req, res);
}
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 {
10 years ago
return res.redirect(nconf.get('relative_path') + '/user/' + lowercaseSlug);
}
}
getUserDataByUserSlug(req.params.userslug, req.uid, function (err, userData) {
if (err) {
return next(err);
}
if (!userData) {
10 years ago
return helpers.notFound(req, res);
}
if (req.uid !== parseInt(userData.uid, 10)) {
user.incrementUserFieldBy(userData.uid, 'profileviews', 1);
}
async.parallel({
isFollowing: function(next) {
user.isFollowing(req.uid, userData.theirid, next);
},
posts: function(next) {
posts.getPostSummariesFromSet('uid:' + userData.theirid + ':posts', req.uid, 0, 9, next);
},
signature: function(next) {
posts.parseSignature(userData, req.uid, next);
},
aboutme: function(next) {
if (userData.aboutme) {
plugins.fireHook('filter:parse.raw', userData.aboutme, next);
} else {
next();
}
}
}, function(err, results) {
if(err) {
return next(err);
}
10 years ago
if (parseInt(meta.config['reputation:disabled'], 10) === 1) {
delete userData.reputation;
}
userData.posts = results.posts.posts.filter(function (p) {
return p && parseInt(p.deleted, 10) !== 1;
});
userData.aboutme = results.aboutme;
11 years ago
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) {
if (err) {
return next(err);
}
res.render('account/profile', data.userData);
});
});
});
};
accountsController.getFollowing = function(req, res, next) {
11 years ago
getFollow('account/following', 'following', req, res, next);
};
accountsController.getFollowers = function(req, res, next) {
11 years ago
getFollow('account/followers', 'followers', req, res, next);
};
10 years ago
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) {
10 years ago
return helpers.notFound(req, res);
}
var method = name === 'following' ? 'getFollowing' : 'getFollowers';
10 years ago
user[method](userData.uid, 0, 49, next);
}
], function(err, users) {
if (err) {
return next(err);
}
10 years ago
userData.users = users;
userData.nextStart = 50;
userData.title = '[[pages:' + tpl + ', ' + userData.username + ']]';
10 years ago
res.render(tpl, userData);
});
}
accountsController.getFavourites = function(req, res, next) {
getFromUserSet('account/favourites', 'favourites', posts.getPostSummariesFromSet, 'posts', req, res, next);
};
accountsController.getPosts = function(req, res, next) {
getFromUserSet('account/posts', 'posts', posts.getPostSummariesFromSet, 'posts', req, res, next);
10 years ago
};
11 years ago
10 years ago
accountsController.getWatchedTopics = function(req, res, next) {
getFromUserSet('account/watched', 'followed_tids', topics.getTopicsFromSet, 'topics', req, res, next);
11 years ago
};
accountsController.getTopics = function(req, res, next) {
10 years ago
getFromUserSet('account/topics', 'topics', topics.getTopicsFromSet, 'topics', req, res, next);
};
accountsController.getGroups = function(req, res, next) {
accountsController.getBaseUser(req.params.userslug, req.uid, function(err, userData) {
if (err) {
return next(err);
}
if (!userData) {
return helpers.notFound(req, res);
}
groups.getUserGroups([userData.uid], function(err, groupsData) {
if (err) {
return next(err);
}
userData.groups = groupsData[0];
userData.groups.forEach(groups.escapeGroupData);
userData.title = '[[pages:account/groups, ' + userData.username + ']]';
res.render('account/groups', userData);
});
});
};
10 years ago
function getFromUserSet(tpl, set, method, type, req, res, next) {
10 years ago
async.parallel({
settings: function(next) {
user.getSettings(req.uid, next);
},
userData: function(next) {
accountsController.getBaseUser(req.params.userslug, req.uid, next);
}
}, function(err, results) {
11 years ago
if (err) {
return next(err);
}
10 years ago
var userData = results.userData;
11 years ago
if (!userData) {
10 years ago
return helpers.notFound(req, res);
11 years ago
}
10 years ago
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);
}
10 years ago
},
data: function(next) {
var start = (page - 1) * itemsPerPage;
var stop = start + itemsPerPage - 1;
10 years ago
method(setName, req.uid, start, stop, next);
}
}, function(err, results) {
10 years ago
if (err) {
11 years ago
return next(err);
}
10 years ago
userData[type] = results.data[type];
userData.nextStart = results.data.nextStart;
var pageCount = Math.ceil(results.itemCount / itemsPerPage);
10 years ago
var pagination = require('../pagination');
userData.pagination = pagination.create(page, pageCount);
userData.title = '[[pages:' + tpl + ', ' + userData.username + ']]';
10 years ago
res.render(tpl, userData);
});
});
10 years ago
}
accountsController.getBaseUser = function(userslug, callerUID, callback) {
11 years ago
user.getUidByUserslug(userslug, function (err, uid) {
if (err || !uid) {
return callback(err);
}
async.parallel({
user: function(next) {
user.getUserFields(uid, ['uid', 'username', 'userslug'], next);
},
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);
}
if (!results.user) {
return callback();
}
results.user.yourid = callerUID;
results.user.theirid = uid;
results.user.isSelf = parseInt(callerUID, 10) === parseInt(uid, 10);
10 years ago
results.user.showHidden = results.user.isSelf || results.isAdmin;
results.user.profile_links = results.profile_links;
callback(null, results.user);
});
11 years ago
});
};
11 years ago
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;
userData.title = '[[pages:account/edit, ' + userData.username + ']]';
res.render('account/edit', userData);
});
};
accountsController.accountSettings = function(req, res, next) {
var userData;
async.waterfall([
function(next) {
accountsController.getBaseUser(req.params.userslug, req.uid, next);
},
function(_userData, next) {
userData = _userData;
if (!userData) {
return helpers.notFound(req, res);
}
async.parallel({
settings: function(next) {
user.getSettings(userData.uid, next);
},
userGroups: function(next) {
groups.getUserGroups([userData.uid], next);
},
languages: function(next) {
languages.list(next);
}
}, next);
},
function(results, next) {
10 years ago
userData.settings = results.settings;
userData.languages = results.languages;
userData.userGroups = results.userGroups[0];
10 years ago
plugins.fireHook('filter:user.settings', {settings: results.settings, customSettings: [], uid: req.uid}, next);
},
function(data, next) {
10 years ago
userData.customSettings = data.customSettings;
userData.disableEmailSubscriptions = parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
next();
}
], function(err) {
if (err) {
return next(err);
}
userData.dailyDigestFreqOptions = [
{value: 'off', name: '[[user:digest_off]]', selected: 'off' === userData.settings.dailyDigestFreq},
{value: 'day', name: '[[user:digest_daily]]', selected: 'day' === userData.settings.dailyDigestFreq},
{value: 'week', name: '[[user:digest_weekly]]', selected: 'week' === userData.settings.dailyDigestFreq},
{value: 'month', name: '[[user:digest_monthly]]', selected: 'month' === userData.settings.dailyDigestFreq}
];
10 years ago
userData.bootswatchSkinOptions = [
{
"name": "Default",
"value": "default"
},
{
"name": "Cerulean",
"value": "cerulean"
},
{
"name": "Cosmo",
"value": "cosmo"
},
{
"name": "Cyborg",
"value": "cyborg"
},
{
"name": "Darkly",
"value": "darkly"
},
{
"name": "Flatly",
"value": "flatly"
},
{
"name": "Journal",
"value": "journal"
},
{
"name": "Lumen",
"value": "lumen"
},
{
"name": "Paper",
"value": "paper"
},
{
"name": "Readable",
"value": "readable"
},
{
"name": "Sandstone",
"value": "sandstone"
},
{
"name": "Simplex",
"value": "simplex"
},
{
"name": "Slate",
"value": "slate"
},
{
"name": "Spacelab",
"value": "spacelab"
},
{
"name": "Superhero",
"value": "superhero"
},
{
"name": "United",
"value": "united"
},
{
"name": "Yeti",
"value": "yeti"
}
];
userData.bootswatchSkinOptions.forEach(function(skin) {
skin.selected = skin.value === userData.settings.bootswatchSkin;
});
userData.userGroups.forEach(function(group) {
group.selected = group.name === userData.settings.groupTitle;
});
11 years ago
userData.languages.forEach(function(language) {
10 years ago
language.selected = language.code === userData.settings.userLang;
});
userData.disableCustomUserSkins = parseInt(meta.config.disableCustomUserSkins, 10) === 1;
userData.title = '[[pages:account/settings]]';
res.render('account/settings', userData);
});
};
accountsController.uploadPicture = function (req, res, next) {
var userPhoto = req.files.files[0];
10 years ago
var updateUid = req.uid;
async.waterfall([
function(next) {
user.getUidByUserslug(req.params.userslug, next);
},
function(uid, next) {
10 years ago
if (parseInt(updateUid, 10) === parseInt(uid, 10)) {
return next();
}
user.isAdministrator(req.uid, function(err, isAdmin) {
if (err) {
return next(err);
}
if (!isAdmin) {
return helpers.notAllowed(req, res);
}
updateUid = uid;
next();
});
10 years ago
},
function(next) {
user.uploadPicture(updateUid, userPhoto, next);
}
10 years ago
], function(err, image) {
fs.unlink(userPhoto.path);
11 years ago
if (err) {
10 years ago
return next(err);
}
10 years ago
res.json([{name: userPhoto.name, url: image.url.startsWith('http') ? image.url : nconf.get('relative_path') + image.url}]);
});
};
accountsController.getNotifications = function(req, res, next) {
user.notifications.getAll(req.uid, 40, function(err, notifications) {
if (err) {
return next(err);
}
res.render('notifications', {
notifications: notifications,
title: '[[pages:notifications]]'
});
});
};
11 years ago
accountsController.getChats = function(req, res, next) {
10 years ago
if (parseInt(meta.config.disableChat) === 1) {
10 years ago
return helpers.notFound(req, res);
10 years ago
}
10 years ago
// In case a userNAME is passed in instead of a slug, the route should not 404
var slugified = utils.slugify(req.params.userslug);
if (req.params.userslug && req.params.userslug !== slugified) {
10 years ago
return res.redirect(nconf.get('relative_path') + '/chats/' + slugified);
}
11 years ago
async.parallel({
contacts: async.apply(user.getFollowing, req.user.uid, 0, 199),
11 years ago
recentChats: async.apply(messaging.getRecentChats, req.user.uid, 0, 19)
11 years ago
}, function(err, results) {
11 years ago
if (err) {
return next(err);
}
11 years ago
//Remove entries if they were already present as a followed contact
11 years ago
if (results.contacts && results.contacts.length) {
var contactUids = results.contacts.map(function(contact) {
return parseInt(contact.uid, 10);
});
11 years ago
results.recentChats.users = results.recentChats.users.filter(function(chatObj) {
11 years ago
return contactUids.indexOf(parseInt(chatObj.uid, 10)) === -1;
});
}
11 years ago
if (!req.params.userslug) {
return res.render('chats', {
11 years ago
chats: results.recentChats.users,
nextStart: results.recentChats.nextStart,
contacts: results.contacts,
allowed: true,
title: '[[pages:chats]]'
11 years ago
});
}
async.waterfall([
async.apply(user.getUidByUserslug, req.params.userslug),
function(toUid, next) {
10 years ago
if (!toUid || parseInt(toUid, 10) === parseInt(req.user.uid, 10)) {
10 years ago
return helpers.notFound(req, res);
}
10 years ago
11 years ago
async.parallel({
toUser: async.apply(user.getUserFields, toUid, ['uid', 'username']),
messages: async.apply(messaging.getMessages, {
fromuid: req.user.uid,
touid: toUid,
since: 'recent',
isNew: false
}),
allowed: async.apply(messaging.canMessage, req.user.uid, toUid)
11 years ago
}, next);
}
], function(err, data) {
if (err) {
return next(err);
}
res.render('chats', {
11 years ago
chats: results.recentChats.users,
nextStart: results.recentChats.nextStart,
11 years ago
contacts: results.contacts,
meta: data.toUser,
messages: data.messages,
allowed: data.allowed,
title: '[[pages:chat, ' + data.toUser.username + ']]'
11 years ago
});
11 years ago
});
});
};
module.exports = accountsController;