Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sound settings update script to reduce nested functions (src/upgrades/1.4.4/sound_settings.js) #90

Open
wants to merge 5 commits into
base: f24
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 61 additions & 49 deletions src/upgrades/1.4.4/sound_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};