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.
nodebb/src/user/settings.js

179 lines
6.7 KiB
JavaScript

'use strict';
9 years ago
var async = require('async');
8 years ago
var meta = require('../meta');
var db = require('../database');
var plugins = require('../plugins');
module.exports = function (User) {
User.getSettings = function (uid, callback) {
if (!parseInt(uid, 10)) {
10 years ago
return onSettingsLoaded(0, {}, callback);
}
8 years ago
async.waterfall([
function (next) {
db.getObject('user:' + uid + ':settings', next);
},
function (settings, next) {
settings = settings || {};
settings.uid = uid;
onSettingsLoaded(uid, settings, next);
8 years ago
},
], callback);
};
User.getMultipleUserSettings = function (uids, callback) {
11 years ago
if (!Array.isArray(uids) || !uids.length) {
return callback(null, []);
}
var keys = uids.map(function (uid) {
8 years ago
return 'user:' + uid + ':settings';
11 years ago
});
8 years ago
async.waterfall([
function (next) {
db.getObjects(keys, next);
},
function (settings, next) {
settings = settings.map(function (userSettings, index) {
8 years ago
userSettings = userSettings || {};
userSettings.uid = uids[index];
return userSettings;
8 years ago
});
async.map(settings, function (userSettings, next) {
onSettingsLoaded(userSettings.uid, userSettings, next);
}, next);
8 years ago
},
], callback);
11 years ago
};
10 years ago
function onSettingsLoaded(uid, settings, callback) {
8 years ago
async.waterfall([
function (next) {
plugins.fireHook('filter:user.getSettings', { uid: uid, settings: settings }, next);
},
function (data, next) {
settings = data.settings;
var defaultTopicsPerPage = parseInt(meta.config.topicsPerPage, 10) || 20;
var defaultPostsPerPage = parseInt(meta.config.postsPerPage, 10) || 20;
settings.showemail = parseInt(getSetting(settings, 'showemail', 0), 10) === 1;
settings.showfullname = parseInt(getSetting(settings, 'showfullname', 0), 10) === 1;
settings.openOutgoingLinksInNewTab = parseInt(getSetting(settings, 'openOutgoingLinksInNewTab', 0), 10) === 1;
settings.dailyDigestFreq = getSetting(settings, 'dailyDigestFreq', 'off');
settings.usePagination = parseInt(getSetting(settings, 'usePagination', 0), 10) === 1;
settings.topicsPerPage = Math.min(settings.topicsPerPage ? parseInt(settings.topicsPerPage, 10) : defaultTopicsPerPage, defaultTopicsPerPage);
settings.postsPerPage = Math.min(settings.postsPerPage ? parseInt(settings.postsPerPage, 10) : defaultPostsPerPage, defaultPostsPerPage);
settings.userLang = settings.userLang || meta.config.defaultLang || 'en-GB';
settings.topicPostSort = getSetting(settings, 'topicPostSort', 'oldest_to_newest');
settings.categoryTopicSort = getSetting(settings, 'categoryTopicSort', 'newest_to_oldest');
settings.followTopicsOnCreate = parseInt(getSetting(settings, 'followTopicsOnCreate', 1), 10) === 1;
settings.followTopicsOnReply = parseInt(getSetting(settings, 'followTopicsOnReply', 0), 10) === 1;
settings.sendChatNotifications = parseInt(getSetting(settings, 'sendChatNotifications', 0), 10) === 1;
settings.sendPostNotifications = parseInt(getSetting(settings, 'sendPostNotifications', 0), 10) === 1;
settings.restrictChat = parseInt(getSetting(settings, 'restrictChat', 0), 10) === 1;
settings.topicSearchEnabled = parseInt(getSetting(settings, 'topicSearchEnabled', 0), 10) === 1;
settings.delayImageLoading = parseInt(getSetting(settings, 'delayImageLoading', 1), 10) === 1;
settings.bootswatchSkin = settings.bootswatchSkin || meta.config.bootswatchSkin || 'default';
settings.scrollToMyPost = parseInt(getSetting(settings, 'scrollToMyPost', 1), 10) === 1;
next(null, settings);
},
], callback);
10 years ago
}
function getSetting(settings, key, defaultValue) {
10 years ago
if (settings[key] || settings[key] === 0) {
return settings[key];
} else if (meta.config[key] || meta.config[key] === 0) {
return meta.config[key];
}
return defaultValue;
10 years ago
}
User.saveSettings = function (uid, data, callback) {
7 years ago
var maxPostsPerPage = meta.config.maxPostsPerPage || 20;
if (!data.postsPerPage || parseInt(data.postsPerPage, 10) <= 1 || parseInt(data.postsPerPage, 10) > maxPostsPerPage) {
return callback(new Error('[[error:invalid-pagination-value, 2, ' + maxPostsPerPage + ']]'));
9 years ago
}
7 years ago
var maxTopicsPerPage = meta.config.maxTopicsPerPage || 20;
if (!data.topicsPerPage || parseInt(data.topicsPerPage, 10) <= 1 || parseInt(data.topicsPerPage, 10) > maxTopicsPerPage) {
return callback(new Error('[[error:invalid-pagination-value, 2, ' + maxTopicsPerPage + ']]'));
}
data.userLang = data.userLang || meta.config.defaultLang;
11 years ago
plugins.fireHook('action:user.saveSettings', { uid: uid, settings: data });
10 years ago
var settings = {
showemail: data.showemail,
showfullname: data.showfullname,
openOutgoingLinksInNewTab: data.openOutgoingLinksInNewTab,
dailyDigestFreq: data.dailyDigestFreq || 'off',
usePagination: data.usePagination,
7 years ago
topicsPerPage: Math.min(data.topicsPerPage, parseInt(maxTopicsPerPage, 10) || 20),
postsPerPage: Math.min(data.postsPerPage, parseInt(maxPostsPerPage, 10) || 20),
10 years ago
userLang: data.userLang || meta.config.defaultLang,
followTopicsOnCreate: data.followTopicsOnCreate,
followTopicsOnReply: data.followTopicsOnReply,
sendChatNotifications: data.sendChatNotifications,
sendPostNotifications: data.sendPostNotifications,
restrictChat: data.restrictChat,
topicSearchEnabled: data.topicSearchEnabled,
delayImageLoading: data.delayImageLoading,
homePageRoute: ((data.homePageRoute === 'custom' ? data.homePageCustom : data.homePageRoute) || '').replace(/^\//, ''),
9 years ago
scrollToMyPost: data.scrollToMyPost,
notificationSound: data.notificationSound,
incomingChatSound: data.incomingChatSound,
outgoingChatSound: data.outgoingChatSound,
10 years ago
};
if (data.bootswatchSkin) {
settings.bootswatchSkin = data.bootswatchSkin;
}
async.waterfall([
function (next) {
plugins.fireHook('filter:user.saveSettings', { settings: settings, data: data }, next);
},
function (result, next) {
db.setObject('user:' + uid + ':settings', result.settings, next);
},
function (next) {
9 years ago
User.updateDigestSetting(uid, data.dailyDigestFreq, next);
10 years ago
},
function (next) {
User.getSettings(uid, next);
},
], callback);
};
User.updateDigestSetting = function (uid, dailyDigestFreq, callback) {
10 years ago
async.waterfall([
function (next) {
10 years ago
db.sortedSetsRemove(['digest:day:uids', 'digest:week:uids', 'digest:month:uids'], uid, next);
},
function (next) {
10 years ago
if (['day', 'week', 'month'].indexOf(dailyDigestFreq) !== -1) {
db.sortedSetAdd('digest:' + dailyDigestFreq + ':uids', Date.now(), uid, next);
} else {
next();
}
},
10 years ago
], callback);
9 years ago
};
10 years ago
User.setSetting = function (uid, key, value, callback) {
8 years ago
if (!parseInt(uid, 10)) {
8 years ago
return setImmediate(callback);
8 years ago
}
db.setObjectField('user:' + uid + ':settings', key, value, callback);
};
};