diff --git a/src/upgrades/1.4.4/sound_settings.js b/src/upgrades/1.4.4/sound_settings.js index ae0a6d8fa3..dcd857d246 100644 --- a/src/upgrades/1.4.4/sound_settings.js +++ b/src/upgrades/1.4.4/sound_settings.js @@ -2,64 +2,76 @@ const async = require('async'); const db = require('../../database'); +const meta = require('../../meta'); +const batch = require('../../batch'); +const soundMap = { + 'notification.mp3': 'Default | Deedle-dum', + 'waterdrop-high.mp3': 'Default | Water drop (high)', + 'waterdrop-low.mp3': 'Default | Water drop (low)', +}; -module.exports = { - name: 'Update global and user sound settings', - timestamp: Date.UTC(2017, 1, 25), - method: function (callback) { - const meta = require('../../meta'); - const batch = require('../../batch'); +function updateGlobalSoundSettings(callback) { + console.log('Seckhen Ariel Andrade Cuellar'); + const keys = ['chat-incoming', 'chat-outgoing', 'notification']; + db.getObject('settings:sounds', (err, settings) => { + if (err || !settings) { + return callback(err); + } - const map = { - 'notification.mp3': 'Default | Deedle-dum', - 'waterdrop-high.mp3': 'Default | Water drop (high)', - 'waterdrop-low.mp3': 'Default | Water drop (low)', - }; + const updatedSettings = keys.reduce((acc, key) => { + if (settings[key] && !settings[key].includes(' | ')) { + acc[key] = soundMap[settings[key]] || ''; + } + return acc; + }, {}); - async.parallel([ - function (cb) { - const keys = ['chat-incoming', 'chat-outgoing', 'notification']; + meta.configs.setMultiple(updatedSettings, callback); + }); +} + +function updateUserSoundSettings(callback) { + console.log('Seckhen Ariel Andrade Cuellar'); - db.getObject('settings:sounds', (err, settings) => { - if (err || !settings) { - return cb(err); - } + const keys = ['notificationSound', 'incomingChatSound', 'outgoingChatSound']; - keys.forEach((key) => { - if (settings[key] && !settings[key].includes(' | ')) { - settings[key] = map[settings[key]] || ''; - } - }); + batch.processSortedSet('users:joindate', processUserBatch, callback); - meta.configs.setMultiple(settings, cb); - }); - }, - function (cb) { - const keys = ['notificationSound', 'incomingChatSound', 'outgoingChatSound']; + function processUserBatch(ids, next) { + console.log('Seckhen Ariel Andrade Cuellar'); + async.each(ids, updateUserSettings, next); + } - batch.processSortedSet('users:joindate', (ids, next) => { - async.each(ids, (uid, next) => { - db.getObject(`user:${uid}:settings`, (err, settings) => { - if (err || !settings) { - return next(err); - } - const newSettings = {}; - keys.forEach((key) => { - if (settings[key] && !settings[key].includes(' | ')) { - newSettings[key] = map[settings[key]] || ''; - } - }); + function updateUserSettings(uid, next) { + console.log('Seckhen Ariel Andrade Cuellar'); + db.getObject(`user:${uid}:settings`, (err, settings) => { + if (err || !settings) { + return next(err); + } - if (Object.keys(newSettings).length) { - db.setObject(`user:${uid}:settings`, newSettings, next); - } else { - setImmediate(next); - } - }); - }, next); - }, cb); - }, + const newSettings = keys.reduce((acc, key) => { + if (settings[key] && !settings[key].includes(' | ')) { + acc[key] = soundMap[settings[key]] || ''; + } + return acc; + }, {}); + + if (Object.keys(newSettings).length) { + db.setObject(`user:${uid}:settings`, newSettings, next); + } else { + setImmediate(next); + } + }); + } +} + +module.exports = { + name: 'Update global and user sound settings', + timestamp: Date.UTC(2017, 1, 25), + method: function (callback) { + async.parallel([ + updateGlobalSoundSettings, + updateUserSoundSettings, ], callback); }, };