From 596374c30e079db82a14bcbadea08d196838c586 Mon Sep 17 00:00:00 2001 From: Seckhen Date: Wed, 4 Sep 2024 23:15:12 +0300 Subject: [PATCH 1/5] added-functions-to-reduce-code-depth --- src/upgrades/1.4.4/sound_settings.js | 106 ++++++++++++++------------- 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/src/upgrades/1.4.4/sound_settings.js b/src/upgrades/1.4.4/sound_settings.js index ae0a6d8fa3..cf3b46514a 100644 --- a/src/upgrades/1.4.4/sound_settings.js +++ b/src/upgrades/1.4.4/sound_settings.js @@ -2,64 +2,72 @@ 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) { + 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); + }); +} - db.getObject('settings:sounds', (err, settings) => { - if (err || !settings) { - return cb(err); - } +function updateUserSoundSettings(callback) { + const keys = ['notificationSound', 'incomingChatSound', 'outgoingChatSound']; + + batch.processSortedSet('users:joindate', processUserBatch, callback); - keys.forEach((key) => { - if (settings[key] && !settings[key].includes(' | ')) { - settings[key] = map[settings[key]] || ''; - } - }); + function processUserBatch(ids, next) { + async.each(ids, updateUserSettings, next); + } - meta.configs.setMultiple(settings, cb); - }); - }, - function (cb) { - const keys = ['notificationSound', 'incomingChatSound', 'outgoingChatSound']; + function updateUserSettings(uid, next) { + db.getObject(`user:${uid}:settings`, (err, settings) => { + if (err || !settings) { + return next(err); + } - 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]] || ''; - } - }); + 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); - } - }); - }, next); - }, cb); - }, + 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); }, }; From fd3c2a890e5b6739dbee2820340837154cc0cbd7 Mon Sep 17 00:00:00 2001 From: Seckhen Date: Wed, 4 Sep 2024 23:17:09 +0300 Subject: [PATCH 2/5] fixed-trailing-spaces-errors --- src/upgrades/1.4.4/sound_settings.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/upgrades/1.4.4/sound_settings.js b/src/upgrades/1.4.4/sound_settings.js index cf3b46514a..8a3e1aa5d5 100644 --- a/src/upgrades/1.4.4/sound_settings.js +++ b/src/upgrades/1.4.4/sound_settings.js @@ -13,7 +13,6 @@ const soundMap = { function updateGlobalSoundSettings(callback) { const keys = ['chat-incoming', 'chat-outgoing', 'notification']; - db.getObject('settings:sounds', (err, settings) => { if (err || !settings) { return callback(err); @@ -34,7 +33,7 @@ function updateUserSoundSettings(callback) { const keys = ['notificationSound', 'incomingChatSound', 'outgoingChatSound']; batch.processSortedSet('users:joindate', processUserBatch, callback); - + function processUserBatch(ids, next) { async.each(ids, updateUserSettings, next); } From d9e12400944682e193697b701a7300bc54d5031d Mon Sep 17 00:00:00 2001 From: Seckhen Date: Wed, 4 Sep 2024 23:19:47 +0300 Subject: [PATCH 3/5] fixed-more-trailing-spaces-errors --- src/upgrades/1.4.4/sound_settings.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/upgrades/1.4.4/sound_settings.js b/src/upgrades/1.4.4/sound_settings.js index 8a3e1aa5d5..c070ab0c19 100644 --- a/src/upgrades/1.4.4/sound_settings.js +++ b/src/upgrades/1.4.4/sound_settings.js @@ -31,9 +31,9 @@ function updateGlobalSoundSettings(callback) { function updateUserSoundSettings(callback) { const keys = ['notificationSound', 'incomingChatSound', 'outgoingChatSound']; - + batch.processSortedSet('users:joindate', processUserBatch, callback); - + function processUserBatch(ids, next) { async.each(ids, updateUserSettings, next); } From f853b134015ac471c66e58f69c310a881304682f Mon Sep 17 00:00:00 2001 From: Seckhen Date: Thu, 5 Sep 2024 17:34:29 +0300 Subject: [PATCH 4/5] added-console-messages --- src/upgrades/1.4.4/sound_settings.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/upgrades/1.4.4/sound_settings.js b/src/upgrades/1.4.4/sound_settings.js index c070ab0c19..6aada2fcd2 100644 --- a/src/upgrades/1.4.4/sound_settings.js +++ b/src/upgrades/1.4.4/sound_settings.js @@ -12,6 +12,7 @@ const soundMap = { }; function updateGlobalSoundSettings(callback) { + console.log("updateGlobalSoundSettings"); const keys = ['chat-incoming', 'chat-outgoing', 'notification']; db.getObject('settings:sounds', (err, settings) => { if (err || !settings) { @@ -30,15 +31,19 @@ function updateGlobalSoundSettings(callback) { } function updateUserSoundSettings(callback) { + console.log("updateUserSoundSettings"); + const keys = ['notificationSound', 'incomingChatSound', 'outgoingChatSound']; batch.processSortedSet('users:joindate', processUserBatch, callback); function processUserBatch(ids, next) { + console.log("processUserBatch"); async.each(ids, updateUserSettings, next); } function updateUserSettings(uid, next) { + console.log("updateUserSettings"); db.getObject(`user:${uid}:settings`, (err, settings) => { if (err || !settings) { return next(err); From c9073b7628aa1ce041e9a140b04691cb171b9775 Mon Sep 17 00:00:00 2001 From: Seckhen Date: Thu, 5 Sep 2024 19:55:37 +0300 Subject: [PATCH 5/5] fixed-lint-errors-and-corrected-console-logs --- src/upgrades/1.4.4/sound_settings.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/upgrades/1.4.4/sound_settings.js b/src/upgrades/1.4.4/sound_settings.js index 6aada2fcd2..dcd857d246 100644 --- a/src/upgrades/1.4.4/sound_settings.js +++ b/src/upgrades/1.4.4/sound_settings.js @@ -12,7 +12,7 @@ const soundMap = { }; function updateGlobalSoundSettings(callback) { - console.log("updateGlobalSoundSettings"); + console.log('Seckhen Ariel Andrade Cuellar'); const keys = ['chat-incoming', 'chat-outgoing', 'notification']; db.getObject('settings:sounds', (err, settings) => { if (err || !settings) { @@ -31,19 +31,19 @@ function updateGlobalSoundSettings(callback) { } function updateUserSoundSettings(callback) { - console.log("updateUserSoundSettings"); + console.log('Seckhen Ariel Andrade Cuellar'); const keys = ['notificationSound', 'incomingChatSound', 'outgoingChatSound']; batch.processSortedSet('users:joindate', processUserBatch, callback); function processUserBatch(ids, next) { - console.log("processUserBatch"); + console.log('Seckhen Ariel Andrade Cuellar'); async.each(ids, updateUserSettings, next); } function updateUserSettings(uid, next) { - console.log("updateUserSettings"); + console.log('Seckhen Ariel Andrade Cuellar'); db.getObject(`user:${uid}:settings`, (err, settings) => { if (err || !settings) { return next(err);