Files
NodeBB/src/controllers/accounts/settings.js

233 lines
6.3 KiB
JavaScript
Raw Normal View History

'use strict';
2016-02-24 09:58:14 +02:00
var async = require('async');
2016-02-24 09:58:14 +02:00
var user = require('../../user');
var languages = require('../../languages');
var meta = require('../../meta');
var plugins = require('../../plugins');
var privileges = require('../../privileges');
var categories = require('../../categories');
var db = require('../../database');
var helpers = require('../helpers');
var accountHelpers = require('./helpers');
var settingsController = {};
settingsController.get = function (req, res, callback) {
var userData;
async.waterfall([
function (next) {
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
},
function (_userData, next) {
userData = _userData;
if (!userData) {
return callback();
}
async.parallel({
settings: function (next) {
user.getSettings(userData.uid, next);
},
languages: function (next) {
languages.list(next);
2015-11-04 05:55:02 -05:00
},
soundsMapping: function (next) {
meta.sounds.getUserSoundMap(userData.uid, next);
2017-02-17 19:31:21 -07:00
},
}, next);
},
function (results, next) {
userData.settings = results.settings;
2016-02-24 09:58:14 +02:00
userData.languages = results.languages;
2016-08-29 12:43:32 +03:00
var types = [
'notification',
'chat-incoming',
'chat-outgoing',
];
var aliases = {
notification: 'notificationSound',
'chat-incoming': 'incomingChatSound',
'chat-outgoing': 'outgoingChatSound',
};
types.forEach(function (type) {
var soundpacks = plugins.soundpacks.map(function (pack) {
var sounds = Object.keys(pack.sounds).map(function (soundName) {
var value = pack.name + ' | ' + soundName;
return {
name: soundName,
value: value,
selected: value === results.soundsMapping[type],
};
});
return {
name: pack.name,
sounds: sounds,
};
2016-08-29 12:43:32 +03:00
});
2017-02-17 11:19:30 -07:00
userData[type + '-sound'] = soundpacks;
// fallback
userData[aliases[type]] = soundpacks.concat.apply([], soundpacks.map(function (pack) {
return pack.sounds.map(function (sound) {
return {
name: sound.value,
selected: sound.selected,
};
});
}));
2016-08-29 12:43:32 +03:00
});
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:user.customSettings', { settings: results.settings, customSettings: [], uid: req.uid }, next);
},
function (data, next) {
getHomePageRoutes(userData, function (err, routes) {
userData.homePageRoutes = routes;
next(err, data);
});
},
function (data, next) {
userData.customSettings = data.customSettings;
userData.disableEmailSubscriptions = parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
next();
2017-02-17 19:31:21 -07:00
},
], function (err) {
if (err) {
return callback(err);
}
userData.dailyDigestFreqOptions = [
2017-02-18 14:00:29 -07:00
{ value: 'off', name: '[[user:digest_off]]', selected: userData.settings.dailyDigestFreq === 'off' },
{ value: 'day', name: '[[user:digest_daily]]', selected: userData.settings.dailyDigestFreq === 'day' },
{ value: 'week', name: '[[user:digest_weekly]]', selected: userData.settings.dailyDigestFreq === 'week' },
{ value: 'month', name: '[[user:digest_monthly]]', selected: userData.settings.dailyDigestFreq === 'month' },
];
userData.bootswatchSkinOptions = [
2017-03-07 13:09:14 +03:00
{ name: 'No skin', value: 'noskin' },
2017-02-18 01:56:23 -07:00
{ 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.languages.forEach(function (language) {
language.selected = language.code === userData.settings.userLang;
});
userData.disableCustomUserSkins = parseInt(meta.config.disableCustomUserSkins, 10) === 1;
2015-11-04 05:55:02 -05:00
userData.allowUserHomePage = parseInt(meta.config.allowUserHomePage, 10) === 1;
2016-05-17 15:45:42 +03:00
2017-07-26 14:32:53 -04:00
userData.hideFullname = parseInt(meta.config.hideFullname, 10) === 1;
userData.hideEmail = parseInt(meta.config.hideEmail, 10) === 1;
userData.inTopicSearchAvailable = plugins.hasListeners('filter:topic.search');
2015-11-04 05:55:02 -05:00
userData.title = '[[pages:account/settings]]';
2017-02-18 12:30:49 -07:00
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:settings]]' }]);
res.render('account/settings', userData);
});
};
function getHomePageRoutes(userData, callback) {
2015-12-29 10:46:57 +02:00
async.waterfall([
function (next) {
db.getSortedSetRange('cid:0:children', 0, -1, next);
},
function (cids, next) {
privileges.categories.filterCids('find', cids, 0, next);
},
function (cids, next) {
categories.getCategoriesFields(cids, ['name', 'slug'], next);
},
function (categoryData, next) {
categoryData = categoryData.map(function (category) {
2015-12-29 10:46:57 +02:00
return {
route: 'category/' + category.slug,
2017-02-17 19:31:21 -07:00
name: 'Category: ' + category.name,
2015-12-29 10:46:57 +02:00
};
});
2016-04-22 19:48:41 +03:00
categoryData = categoryData || [];
2015-12-29 10:46:57 +02:00
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:homepage.get', { routes: [
2016-04-22 19:48:41 +03:00
{
route: 'categories',
2017-02-17 19:31:21 -07:00
name: 'Categories',
2016-04-22 19:48:41 +03:00
},
2016-07-15 12:39:54 +03:00
{
route: 'unread',
2017-02-17 19:31:21 -07:00
name: 'Unread',
2016-07-15 12:39:54 +03:00
},
2016-04-22 19:48:41 +03:00
{
route: 'recent',
2017-02-17 19:31:21 -07:00
name: 'Recent',
2016-04-22 19:48:41 +03:00
},
{
route: 'popular',
2017-02-17 19:31:21 -07:00
name: 'Popular',
},
].concat(categoryData, [
{
route: 'custom',
name: 'Custom',
},
]) }, next);
2016-04-22 19:48:41 +03:00
},
function (data, next) {
// Set selected for each route
var customIdx;
var hasSelected = false;
data.routes = data.routes.map(function (route, idx) {
if (route.route === userData.settings.homePageRoute) {
route.selected = true;
hasSelected = true;
} else {
route.selected = false;
}
if (route.route === 'custom') {
customIdx = idx;
}
return route;
});
if (!hasSelected && customIdx && userData.settings.homePageRoute !== 'none') {
data.routes[customIdx].selected = true;
}
2016-04-22 19:48:41 +03:00
next(null, data.routes);
2017-02-17 19:31:21 -07:00
},
2016-04-22 19:48:41 +03:00
], callback);
2015-12-29 10:46:57 +02:00
}
2017-02-18 02:30:48 -07:00
module.exports = settingsController;