|
|
|
@ -1,96 +1,73 @@
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
var plugins = require('../plugins');
|
|
|
|
|
var notifications = require('../notifications');
|
|
|
|
|
const meta = require('../meta');
|
|
|
|
|
const db = require('../database');
|
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
const notifications = require('../notifications');
|
|
|
|
|
|
|
|
|
|
module.exports = function (User) {
|
|
|
|
|
User.getSettings = function (uid, callback) {
|
|
|
|
|
User.getSettings = async function (uid) {
|
|
|
|
|
if (parseInt(uid, 10) <= 0) {
|
|
|
|
|
return onSettingsLoaded(0, {}, callback);
|
|
|
|
|
return await onSettingsLoaded(0, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getObject('user:' + uid + ':settings', next);
|
|
|
|
|
},
|
|
|
|
|
function (settings, next) {
|
|
|
|
|
settings = settings || {};
|
|
|
|
|
settings.uid = uid;
|
|
|
|
|
onSettingsLoaded(uid, settings, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
let settings = await db.getObject('user:' + uid + ':settings');
|
|
|
|
|
settings = settings || {};
|
|
|
|
|
settings.uid = uid;
|
|
|
|
|
return await onSettingsLoaded(uid, settings);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.getMultipleUserSettings = function (uids, callback) {
|
|
|
|
|
User.getMultipleUserSettings = async function (uids) {
|
|
|
|
|
if (!Array.isArray(uids) || !uids.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var keys = uids.map(uid => 'user:' + uid + ':settings');
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getObjects(keys, next);
|
|
|
|
|
},
|
|
|
|
|
function (settings, next) {
|
|
|
|
|
settings = settings.map(function (userSettings, index) {
|
|
|
|
|
userSettings = userSettings || {};
|
|
|
|
|
userSettings.uid = uids[index];
|
|
|
|
|
return userSettings;
|
|
|
|
|
});
|
|
|
|
|
async.map(settings, function (userSettings, next) {
|
|
|
|
|
onSettingsLoaded(userSettings.uid, userSettings, next);
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
const keys = uids.map(uid => 'user:' + uid + ':settings');
|
|
|
|
|
let settings = await db.getObjects(keys);
|
|
|
|
|
settings = settings.map(function (userSettings, index) {
|
|
|
|
|
userSettings = userSettings || {};
|
|
|
|
|
userSettings.uid = uids[index];
|
|
|
|
|
return userSettings;
|
|
|
|
|
});
|
|
|
|
|
return await Promise.all(settings.map(s => onSettingsLoaded(s.uid, s)));
|
|
|
|
|
// async.map(settings, function (userSettings, next) {
|
|
|
|
|
// onSettingsLoaded(userSettings.uid, userSettings, next);
|
|
|
|
|
// }, next);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function onSettingsLoaded(uid, settings, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
plugins.fireHook('filter:user.getSettings', { uid: uid, settings: settings }, next);
|
|
|
|
|
},
|
|
|
|
|
function (data, next) {
|
|
|
|
|
settings = data.settings;
|
|
|
|
|
|
|
|
|
|
var defaultTopicsPerPage = meta.config.topicsPerPage;
|
|
|
|
|
var defaultPostsPerPage = meta.config.postsPerPage;
|
|
|
|
|
|
|
|
|
|
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.acpLang = settings.acpLang || settings.userLang;
|
|
|
|
|
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.upvoteNotifFreq = getSetting(settings, 'upvoteNotifFreq', 'all');
|
|
|
|
|
settings.restrictChat = parseInt(getSetting(settings, 'restrictChat', 0), 10) === 1;
|
|
|
|
|
settings.topicSearchEnabled = parseInt(getSetting(settings, 'topicSearchEnabled', 0), 10) === 1;
|
|
|
|
|
settings.bootswatchSkin = settings.bootswatchSkin || '';
|
|
|
|
|
settings.scrollToMyPost = parseInt(getSetting(settings, 'scrollToMyPost', 1), 10) === 1;
|
|
|
|
|
settings.categoryWatchState = getSetting(settings, 'categoryWatchState', 'notwatching');
|
|
|
|
|
|
|
|
|
|
notifications.getAllNotificationTypes(next);
|
|
|
|
|
},
|
|
|
|
|
function (notificationTypes, next) {
|
|
|
|
|
notificationTypes.forEach(function (notificationType) {
|
|
|
|
|
settings[notificationType] = getSetting(settings, notificationType, 'notification');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
next(null, settings);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
async function onSettingsLoaded(uid, settings) {
|
|
|
|
|
const data = await plugins.fireHook('filter:user.getSettings', { uid: uid, settings: settings });
|
|
|
|
|
settings = data.settings;
|
|
|
|
|
|
|
|
|
|
const defaultTopicsPerPage = meta.config.topicsPerPage;
|
|
|
|
|
const defaultPostsPerPage = meta.config.postsPerPage;
|
|
|
|
|
|
|
|
|
|
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.acpLang = settings.acpLang || settings.userLang;
|
|
|
|
|
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.upvoteNotifFreq = getSetting(settings, 'upvoteNotifFreq', 'all');
|
|
|
|
|
settings.restrictChat = parseInt(getSetting(settings, 'restrictChat', 0), 10) === 1;
|
|
|
|
|
settings.topicSearchEnabled = parseInt(getSetting(settings, 'topicSearchEnabled', 0), 10) === 1;
|
|
|
|
|
settings.bootswatchSkin = settings.bootswatchSkin || '';
|
|
|
|
|
settings.scrollToMyPost = parseInt(getSetting(settings, 'scrollToMyPost', 1), 10) === 1;
|
|
|
|
|
settings.categoryWatchState = getSetting(settings, 'categoryWatchState', 'notwatching');
|
|
|
|
|
|
|
|
|
|
const notificationTypes = await notifications.getAllNotificationTypes();
|
|
|
|
|
notificationTypes.forEach(function (notificationType) {
|
|
|
|
|
settings[notificationType] = getSetting(settings, notificationType, 'notification');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSetting(settings, key, defaultValue) {
|
|
|
|
@ -102,22 +79,22 @@ module.exports = function (User) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
User.saveSettings = function (uid, data, callback) {
|
|
|
|
|
User.saveSettings = async function (uid, data) {
|
|
|
|
|
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 + ']]'));
|
|
|
|
|
throw new Error('[[error:invalid-pagination-value, 2, ' + maxPostsPerPage + ']]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var maxTopicsPerPage = meta.config.maxTopicsPerPage || 20;
|
|
|
|
|
const 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 + ']]'));
|
|
|
|
|
throw new Error('[[error:invalid-pagination-value, 2, ' + maxTopicsPerPage + ']]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.userLang = data.userLang || meta.config.defaultLang;
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('action:user.saveSettings', { uid: uid, settings: data });
|
|
|
|
|
|
|
|
|
|
var settings = {
|
|
|
|
|
const settings = {
|
|
|
|
|
showemail: data.showemail,
|
|
|
|
|
showfullname: data.showfullname,
|
|
|
|
|
openOutgoingLinksInNewTab: data.openOutgoingLinksInNewTab,
|
|
|
|
@ -140,51 +117,30 @@ module.exports = function (User) {
|
|
|
|
|
bootswatchSkin: data.bootswatchSkin,
|
|
|
|
|
categoryWatchState: data.categoryWatchState,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
notifications.getAllNotificationTypes(next);
|
|
|
|
|
},
|
|
|
|
|
function (notificationTypes, next) {
|
|
|
|
|
notificationTypes.forEach(function (notificationType) {
|
|
|
|
|
if (data[notificationType]) {
|
|
|
|
|
settings[notificationType] = data[notificationType];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
plugins.fireHook('filter:user.saveSettings', { settings: settings, data: data }, next);
|
|
|
|
|
},
|
|
|
|
|
function (result, next) {
|
|
|
|
|
db.setObject('user:' + uid + ':settings', result.settings, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
User.updateDigestSetting(uid, data.dailyDigestFreq, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
User.getSettings(uid, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
const notificationTypes = await notifications.getAllNotificationTypes();
|
|
|
|
|
notificationTypes.forEach(function (notificationType) {
|
|
|
|
|
if (data[notificationType]) {
|
|
|
|
|
settings[notificationType] = data[notificationType];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const result = await plugins.fireHook('filter:user.saveSettings', { settings: settings, data: data });
|
|
|
|
|
await db.setObject('user:' + uid + ':settings', result.settings);
|
|
|
|
|
await User.updateDigestSetting(uid, data.dailyDigestFreq);
|
|
|
|
|
return await User.getSettings(uid);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.updateDigestSetting = function (uid, dailyDigestFreq, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.sortedSetsRemove(['digest:day:uids', 'digest:week:uids', 'digest:month:uids'], uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
if (['day', 'week', 'month'].includes(dailyDigestFreq)) {
|
|
|
|
|
db.sortedSetAdd('digest:' + dailyDigestFreq + ':uids', Date.now(), uid, next);
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
User.updateDigestSetting = async function (uid, dailyDigestFreq) {
|
|
|
|
|
await db.sortedSetsRemove(['digest:day:uids', 'digest:week:uids', 'digest:month:uids'], uid);
|
|
|
|
|
if (['day', 'week', 'month'].includes(dailyDigestFreq)) {
|
|
|
|
|
await db.sortedSetAdd('digest:' + dailyDigestFreq + ':uids', Date.now(), uid);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.setSetting = function (uid, key, value, callback) {
|
|
|
|
|
User.setSetting = async function (uid, key, value) {
|
|
|
|
|
if (parseInt(uid, 10) <= 0) {
|
|
|
|
|
return setImmediate(callback);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.setObjectField('user:' + uid + ':settings', key, value, callback);
|
|
|
|
|
await db.setObjectField('user:' + uid + ':settings', key, value);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|