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

91 lines
3.0 KiB
JavaScript

'use strict';
var meta = require('./../meta'),
11 years ago
db = require('./../database'),
plugins = require('./../plugins');
module.exports = function(User) {
User.getSettings = function(uid, callback) {
db.getObject('user:' + uid + ':settings', function(err, settings) {
if(err) {
return callback(err);
}
if(!settings) {
settings = {};
}
11 years ago
plugins.fireHook('filter:user.getSettings', {uid: uid, settings: settings}, function(err, data) {
if(err) {
return callback(err);
}
11 years ago
settings = data.settings;
settings.showemail = settings.showemail ? parseInt(settings.showemail, 10) !== 0 : false;
11 years ago
settings.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab ? parseInt(settings.openOutgoingLinksInNewTab, 10) !== 0 : false;
settings.dailyDigestFreq = settings.dailyDigestFreq || 'off';
11 years ago
settings.usePagination = settings.usePagination ? parseInt(settings.usePagination, 10) === 1 : parseInt(meta.config.usePagination, 10) === 1;
settings.topicsPerPage = settings.topicsPerPage ? parseInt(settings.topicsPerPage, 10) : parseInt(meta.config.topicsPerPage, 10) || 20;
settings.postsPerPage = settings.postsPerPage ? parseInt(settings.postsPerPage, 10) : parseInt(meta.config.postsPerPage, 10) || 10;
settings.notificationSounds = settings.notificationSounds ? parseInt(settings.notificationSounds, 10) === 1 : true;
settings.language = settings.language || meta.config.defaultLang || 'en_GB';
settings.topicPostSort = settings.topicPostSort || meta.config.topicPostSort || 'oldest_to_newest';
11 years ago
callback(null, settings);
});
});
};
11 years ago
User.getMultipleUserSettings = function(uids, callback) {
if (!Array.isArray(uids) || !uids.length) {
return callback(null, []);
}
var keys = uids.map(function(uid) {
return 'user:' + uid + ':settings';
});
db.getObjects(keys, function(err, settings) {
if (err) {
return callback(err);
}
// Associate uid
settings = settings.map(function(setting, idx) {
setting = setting || {};
setting.uid = uids[idx];
return setting;
});
callback(null, settings);
});
};
User.saveSettings = function(uid, data, callback) {
if(!data.topicsPerPage || !data.postsPerPage || parseInt(data.topicsPerPage, 10) <= 0 || parseInt(data.postsPerPage, 10) <= 0) {
return callback(new Error('[[error:invalid-pagination-value]]'));
}
data.language = data.language || meta.config.defaultLang;
11 years ago
plugins.fireHook('action:user.saveSettings', {uid: uid, settings: data});
db.setObject('user:' + uid + ':settings', {
showemail: data.showemail,
11 years ago
openOutgoingLinksInNewTab: data.openOutgoingLinksInNewTab,
dailyDigestFreq: data.dailyDigestFreq || 'off',
usePagination: data.usePagination,
topicsPerPage: data.topicsPerPage,
postsPerPage: data.postsPerPage,
notificationSounds: data.notificationSounds,
language: data.language || meta.config.defaultLang
}, callback);
};
User.setSetting = function(uid, key, value, callback) {
db.setObjectField('user:' + uid + ':settings', key, value, callback);
};
};