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.

449 lines
13 KiB
JavaScript

11 years ago
"use strict";
var fs = require('fs'),
nconf = require('nconf'),
async = require('async'),
10 years ago
validator = require('validator'),
10 years ago
winston = require('winston'),
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');
var accountsController = {
profile: require('./accounts/profile'),
edit: require('./accounts/edit')
};
accountsController.getUserByUID = function(req, res, next) {
var uid = req.params.uid ? req.params.uid : 0;
async.parallel({
10 years ago
userData: async.apply(user.getUserData, uid),
settings: async.apply(user.getSettings, uid)
}, function(err, results) {
10 years ago
if (err || !results.userData) {
10 years ago
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.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, callback) {
var userData;
async.waterfall([
function(next) {
accountsController.getBaseUser(req.params.userslug, req.uid, next);
},
function(data, next) {
userData = data;
if (!userData) {
10 years ago
return callback();
}
var method = name === 'following' ? 'getFollowing' : 'getFollowers';
10 years ago
user[method](userData.uid, 0, 49, next);
}
], function(err, users) {
if (err) {
10 years ago
return callback(err);
}
10 years ago
userData.users = users;
userData.nextStart = 50;
userData.title = '[[pages:' + tpl + ', ' + userData.username + ']]';
10 years ago
userData.breadcrumbs = helpers.buildBreadcrumbs([{text: userData.username, url: '/user/' + userData.userslug}, {text: '[[user:' + name + ']]'}]);
10 years ago
res.render(tpl, userData);
});
}
accountsController.getFavourites = function(req, res, next) {
10 years ago
getFromUserSet('account/favourites', 'favourites', '[[user:favourites]]', posts.getPostSummariesFromSet, 'posts', req, res, next);
};
accountsController.getPosts = function(req, res, next) {
10 years ago
getFromUserSet('account/posts', 'posts', '[[global:posts]]', posts.getPostSummariesFromSet, 'posts', req, res, next);
10 years ago
};
11 years ago
10 years ago
accountsController.getWatchedTopics = function(req, res, next) {
10 years ago
getFromUserSet('account/watched', 'followed_tids', '[[user:watched]]',topics.getTopicsFromSet, 'topics', req, res, next);
11 years ago
};
accountsController.getTopics = function(req, res, next) {
10 years ago
getFromUserSet('account/topics', 'topics', '[[global:topics]]', topics.getTopicsFromSet, 'topics', req, res, next);
10 years ago
};
accountsController.getGroups = function(req, res, next) {
var userData;
var groupsData;
async.waterfall([
function (next) {
accountsController.getBaseUser(req.params.userslug, req.uid, next);
},
function (_userData, next) {
userData = _userData;
groups.getUserGroups([userData.uid], next);
},
function (_groupsData, next) {
groupsData = _groupsData[0];
var groupNames = groupsData.map(function(group) {
return group.name;
});
groups.getMemberUsers(groupNames, 0, 3, next);
},
function (members, next) {
groupsData.forEach(function(group, index) {
group.members = members[index];
});
next();
}
], function(err) {
if (err) {
return next(err);
}
userData.groups = groupsData;
userData.title = '[[pages:account/groups, ' + userData.username + ']]';
userData.breadcrumbs = helpers.buildBreadcrumbs([{text: userData.username, url: '/user/' + userData.userslug}, {text: '[[global:header.groups]]'}]);
res.render('account/groups', userData);
});
};
10 years ago
10 years ago
function getFromUserSet(tpl, set, crumb, 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) {
10 years ago
if (err || !results.userData) {
11 years ago
return next(err);
}
10 years ago
10 years ago
var userData = results.userData;
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
userData.breadcrumbs = helpers.buildBreadcrumbs([{text: userData.username, url: '/user/' + userData.userslug}, {text: crumb}]);
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
10 years ago
accountsController.accountSettings = function(req, res, callback) {
var userData;
async.waterfall([
function(next) {
accountsController.getBaseUser(req.params.userslug, req.uid, next);
},
function(_userData, next) {
userData = _userData;
if (!userData) {
10 years ago
return callback();
}
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.customSettings', {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) {
10 years ago
return callback(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 = [
10 years ago
{ "name": "Default", "value": "default" },
{ "name": "Cerulean", "value": "cerulean" },
{ "name": "Cosmo", "value": "cosmo" },
{ "name": "Cyborg", "value": "cyborg" },
10 years ago
{ "name": "Darkly", "value": "darkly" },
{ "name": "Flatly", "value": "flatly" },
10 years ago
{ "name": "Journal", "value": "journal" },
{ "name": "Lumen", "value": "lumen" },
10 years ago
{ "name": "Paper", "value": "paper" },
10 years ago
{ "name": "Readable", "value": "readable" },
10 years ago
{ "name": "Sandstone", "value": "sandstone" },
10 years ago
{ "name": "Simplex", "value": "simplex" },
{ "name": "Slate", "value": "slate" },
{ "name": "Spacelab", "value": "spacelab" },
10 years ago
{ "name": "Superhero", "value": "superhero" },
10 years ago
{ "name": "United", "value": "united" },
{ "name": "Yeti", "value": "yeti" }
10 years ago
];
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]]';
10 years ago
userData.breadcrumbs = helpers.buildBreadcrumbs([{text: userData.username, url: '/user/' + userData.userslug}, {text: '[[user: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) {
10 years ago
fs.unlink(userPhoto.path, function(err) {
10 years ago
winston.error('unable to delete picture ' + userPhoto.path, err);
10 years ago
});
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,
10 years ago
title: '[[pages:notifications]]',
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:notifications]]'}])
});
});
};
10 years ago
accountsController.getChats = function(req, res, callback) {
if (parseInt(meta.config.disableChat, 10) === 1) {
return callback();
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) {
10 years ago
return callback(err);
11 years ago
}
if (results.recentChats.users && results.recentChats.users.length) {
var contactUids = results.recentChats.users.map(function(chatObj) {
return parseInt(chatObj.uid, 10);
});
results.contacts = results.contacts.filter(function(contact) {
return contactUids.indexOf(parseInt(contact.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,
10 years ago
title: '[[pages:chats]]',
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[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 callback();
}
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) {
10 years ago
return callback(err);
11 years ago
}
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,
10 years ago
title: '[[pages:chat, ' + data.toUser.username + ']]',
10 years ago
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]', url: '/chats'}, {text: data.toUser.username}])
11 years ago
});
11 years ago
});
});
};
module.exports = accountsController;