refactor: remove async.waterfall

isekai-main
Barış Soner Uşaklı 3 years ago
parent 222dccaf67
commit 58ac55c16a

@ -1,24 +1,14 @@
'use strict'; 'use strict';
const async = require('async');
const db = require('../../database'); const db = require('../../database');
module.exports = { module.exports = {
name: 'Rename maximumImageWidth to resizeImageWidth', name: 'Rename maximumImageWidth to resizeImageWidth',
timestamp: Date.UTC(2018, 9, 24), timestamp: Date.UTC(2018, 9, 24),
method: function (callback) { method: async function () {
const meta = require('../../meta'); const meta = require('../../meta');
async.waterfall([ const value = await meta.configs.get('maximumImageWidth');
function (next) { await meta.configs.set('resizeImageWidth', value);
meta.configs.get('maximumImageWidth', next); await db.deleteObjectField('config', 'maximumImageWidth');
},
function (value, next) {
meta.configs.set('resizeImageWidth', value, next);
},
function (next) {
db.deleteObjectField('config', 'maximumImageWidth', next);
},
], callback);
}, },
}; };

@ -1,47 +1,38 @@
'use strict'; 'use strict';
const async = require('async');
module.exports = { module.exports = {
name: 'Widget visibility groups', name: 'Widget visibility groups',
timestamp: Date.UTC(2018, 10, 10), timestamp: Date.UTC(2018, 10, 10),
method: function (callback) { method: async function () {
const widgetAdmin = require('../../widgets/admin'); const widgetAdmin = require('../../widgets/admin');
const widgets = require('../../widgets'); const widgets = require('../../widgets');
async.waterfall([ const areas = await widgetAdmin.getAreas();
function (next) { for (const area of areas) {
widgetAdmin.getAreas(next); if (area.data.length) {
}, // area.data is actually an array of widgets
function (areas, next) { area.widgets = area.data;
async.eachSeries(areas, (area, next) => { area.widgets.forEach((widget) => {
if (area.data.length) { if (widget && widget.data) {
// area.data is actually an array of widgets const groupsToShow = ['administrators', 'Global Moderators'];
area.widgets = area.data; if (widget.data['hide-guests'] !== 'on') {
area.widgets.forEach((widget) => { groupsToShow.push('guests');
if (widget && widget.data) { }
const groupsToShow = ['administrators', 'Global Moderators']; if (widget.data['hide-registered'] !== 'on') {
if (widget.data['hide-guests'] !== 'on') { groupsToShow.push('registered-users');
groupsToShow.push('guests'); }
}
if (widget.data['hide-registered'] !== 'on') {
groupsToShow.push('registered-users');
}
widget.data.groups = groupsToShow; widget.data.groups = groupsToShow;
// if we are showing to all 4 groups, set to empty array // if we are showing to all 4 groups, set to empty array
// empty groups is shown to everyone // empty groups is shown to everyone
if (groupsToShow.length === 4) { if (groupsToShow.length === 4) {
widget.data.groups.length = 0; widget.data.groups.length = 0;
} }
}
});
widgets.setArea(area, next);
} else {
next();
} }
}, next); });
}, // eslint-disable-next-line no-await-in-loop
], callback); await widgets.setArea(area);
}
}
}, },
}; };

@ -1,6 +1,6 @@
'use strict'; /* eslint-disable no-await-in-loop */
const async = require('async'); 'use strict';
const db = require('../../database'); const db = require('../../database');
const batch = require('../../batch'); const batch = require('../../batch');
@ -9,39 +9,27 @@ const categories = require('../../categories');
module.exports = { module.exports = {
name: 'Update category watch data', name: 'Update category watch data',
timestamp: Date.UTC(2018, 11, 13), timestamp: Date.UTC(2018, 11, 13),
method: function (callback) { method: async function () {
const { progress } = this; const { progress } = this;
let keys;
async.waterfall([
function (next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
},
function (cids, next) {
keys = cids.map(cid => `cid:${cid}:ignorers`);
batch.processSortedSet('users:joindate', (uids, next) => {
progress.incr(uids.length);
async.eachSeries(cids, (cid, next) => { const cids = await db.getSortedSetRange('categories:cid', 0, -1);
db.isSortedSetMembers(`cid:${cid}:ignorers`, uids, (err, isMembers) => { const keys = cids.map(cid => `cid:${cid}:ignorers`);
if (err) {
return next(err); await batch.processSortedSet('users:joindate', async (uids) => {
} progress.incr(uids.length);
uids = uids.filter((uid, index) => isMembers[index]); for (const cid of cids) {
if (!uids.length) { const isMembers = await db.isSortedSetMembers(`cid:${cid}:ignorers`, uids);
return setImmediate(next); uids = uids.filter((uid, index) => isMembers[index]);
} if (uids.length) {
const states = uids.map(() => categories.watchStates.ignoring); const states = uids.map(() => categories.watchStates.ignoring);
db.sortedSetAdd(`cid:${cid}:uid:watch:state`, states, uids, next); await db.sortedSetAdd(`cid:${cid}:uid:watch:state`, states, uids);
}); }
}, next); }
}, { }, {
progress: progress, progress: progress,
batch: 500, batch: 500,
}, next); });
},
function (next) { await db.deleteAll(keys);
db.deleteAll(keys, next);
},
], callback);
}, },
}; };

@ -1,55 +1,35 @@
/* eslint-disable no-await-in-loop */
'use strict'; 'use strict';
const async = require('async');
const db = require('../../database'); const db = require('../../database');
const batch = require('../../batch'); const batch = require('../../batch');
module.exports = { module.exports = {
name: 'Update moderation notes to hashes', name: 'Update moderation notes to hashes',
timestamp: Date.UTC(2019, 3, 5), timestamp: Date.UTC(2019, 3, 5),
method: function (callback) { method: async function () {
const { progress } = this; const { progress } = this;
batch.processSortedSet('users:joindate', (ids, next) => { await batch.processSortedSet('users:joindate', async (uids) => {
async.each(ids, (uid, next) => { await Promise.all(uids.map(async (uid) => {
progress.incr(); progress.incr();
db.getSortedSetRevRange(`uid:${uid}:moderation:notes`, 0, -1, (err, notes) => {
if (err || !notes.length) {
return next(err);
}
async.eachSeries(notes, (note, next) => { const notes = await db.getSortedSetRevRange(`uid:${uid}:moderation:notes`, 0, -1);
let noteData; for (const note of notes) {
async.waterfall([ const noteData = JSON.parse(note);
function (next) { noteData.timestamp = noteData.timestamp || Date.now();
try { await db.sortedSetRemove(`uid:${uid}:moderation:notes`, note);
noteData = JSON.parse(note); await db.setObject(`uid:${uid}:moderation:note:${noteData.timestamp}`, {
noteData.timestamp = noteData.timestamp || Date.now(); uid: noteData.uid,
setImmediate(next); timestamp: noteData.timestamp,
} catch (err) { note: noteData.note,
next(err); });
} await db.sortedSetAdd(`uid:${uid}:moderation:notes`, noteData.timestamp, noteData.timestamp);
}, }
function (next) { }));
db.sortedSetRemove(`uid:${uid}:moderation:notes`, note, next);
},
function (next) {
db.setObject(`uid:${uid}:moderation:note:${noteData.timestamp}`, {
uid: noteData.uid,
timestamp: noteData.timestamp,
note: noteData.note,
}, next);
},
function (next) {
db.sortedSetAdd(`uid:${uid}:moderation:notes`, noteData.timestamp, noteData.timestamp, next);
},
], next);
}, next);
});
}, next);
}, { }, {
progress: this.progress, progress: this.progress,
}, callback); });
}, },
}; };

@ -1,6 +1,7 @@
/* eslint-disable no-await-in-loop */
'use strict'; 'use strict';
const async = require('async');
const db = require('../../database'); const db = require('../../database');
const privileges = require('../../privileges'); const privileges = require('../../privileges');
const groups = require('../../groups'); const groups = require('../../groups');
@ -8,38 +9,19 @@ const groups = require('../../groups');
module.exports = { module.exports = {
name: 'give mod info privilege', name: 'give mod info privilege',
timestamp: Date.UTC(2019, 9, 8), timestamp: Date.UTC(2019, 9, 8),
method: function (callback) { method: async function () {
async.waterfall([ const cids = await db.getSortedSetRevRange('categories:cid', 0, -1);
function (next) { for (const cid of cids) {
db.getSortedSetRevRange('categories:cid', 0, -1, next); await givePrivsToModerators(cid, '');
}, await givePrivsToModerators(cid, 'groups:');
function (cids, next) { }
async.eachSeries(cids, (cid, next) => { await privileges.global.give(['groups:view:users:info'], 'Global Moderators');
async.waterfall([
function (next) { async function givePrivsToModerators(cid, groupPrefix) {
givePrivsToModerators(cid, '', next); const members = await db.getSortedSetRevRange(`group:cid:${cid}:privileges:${groupPrefix}moderate:members`, 0, -1);
}, for (const member of members) {
function (next) { await groups.join(['cid:0:privileges:view:users:info'], member);
givePrivsToModerators(cid, 'groups:', next); }
},
], next);
}, next);
},
function (next) {
privileges.global.give(['groups:view:users:info'], 'Global Moderators', next);
},
], callback);
function givePrivsToModerators(cid, groupPrefix, callback) {
async.waterfall([
function (next) {
db.getSortedSetRevRange(`group:cid:${cid}:privileges:${groupPrefix}moderate:members`, 0, -1, next);
},
function (members, next) {
async.eachSeries(members, (member, next) => {
groups.join(['cid:0:privileges:view:users:info'], member, next);
}, next);
},
], callback);
} }
}, },
}; };

@ -1,6 +1,7 @@
/* eslint-disable no-await-in-loop */
'use strict'; 'use strict';
const async = require('async');
const privileges = require('../../privileges'); const privileges = require('../../privileges');
const groups = require('../../groups'); const groups = require('../../groups');
const db = require('../../database'); const db = require('../../database');
@ -8,7 +9,7 @@ const db = require('../../database');
module.exports = { module.exports = {
name: 'Give mods explicit privileges', name: 'Give mods explicit privileges',
timestamp: Date.UTC(2019, 4, 28), timestamp: Date.UTC(2019, 4, 28),
method: function (callback) { method: async function () {
const defaultPrivileges = [ const defaultPrivileges = [
'find', 'find',
'read', 'read',
@ -43,43 +44,20 @@ module.exports = {
'groups:local:login', 'groups:local:login',
]; ];
async.waterfall([ const cids = await db.getSortedSetRevRange('categories:cid', 0, -1);
function (next) { for (const cid of cids) {
db.getSortedSetRevRange('categories:cid', 0, -1, next); await givePrivsToModerators(cid, '');
}, await givePrivsToModerators(cid, 'groups:');
function (cids, next) { await privileges.categories.give(modPrivileges.map(p => `groups:${p}`), cid, ['Global Moderators']);
async.eachSeries(cids, (cid, next) => { }
async.waterfall([ await privileges.global.give(globalModPrivs, 'Global Moderators');
function (next) {
givePrivsToModerators(cid, '', next);
},
function (next) {
givePrivsToModerators(cid, 'groups:', next);
},
function (next) {
privileges.categories.give(modPrivileges.map(p => `groups:${p}`), cid, ['Global Moderators'], next);
},
], next);
}, next);
},
function (next) {
privileges.global.give(globalModPrivs, 'Global Moderators', next);
},
], callback);
function givePrivsToModerators(cid, groupPrefix, callback) { async function givePrivsToModerators(cid, groupPrefix) {
const privGroups = modPrivileges.map(priv => `cid:${cid}:privileges:${groupPrefix}${priv}`); const privGroups = modPrivileges.map(priv => `cid:${cid}:privileges:${groupPrefix}${priv}`);
const members = await db.getSortedSetRevRange(`group:cid:${cid}:privileges:${groupPrefix}moderate:members`, 0, -1);
async.waterfall([ for (const member of members) {
function (next) { await groups.join(privGroups, member);
db.getSortedSetRevRange(`group:cid:${cid}:privileges:${groupPrefix}moderate:members`, 0, -1, next); }
},
function (members, next) {
async.eachSeries(members, (member, next) => {
groups.join(privGroups, member, next);
}, next);
},
], callback);
} }
}, },
}; };

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const async = require('async');
const db = require('../../database'); const db = require('../../database');
const batch = require('../../batch'); const batch = require('../../batch');
@ -10,36 +10,26 @@ const topics = require('../../topics');
module.exports = { module.exports = {
name: 'Create zsets for user posts per category', name: 'Create zsets for user posts per category',
timestamp: Date.UTC(2019, 5, 23), timestamp: Date.UTC(2019, 5, 23),
method: function (callback) { method: async function () {
const { progress } = this; const { progress } = this;
batch.processSortedSet('posts:pid', (pids, next) => { await batch.processSortedSet('posts:pid', async (pids) => {
progress.incr(pids.length); progress.incr(pids.length);
let postData; const postData = await posts.getPostsFields(pids, ['pid', 'uid', 'tid', 'upvotes', 'downvotes', 'timestamp']);
async.waterfall([ const tids = postData.map(p => p.tid);
function (next) { const topicData = await topics.getTopicsFields(tids, ['cid']);
posts.getPostsFields(pids, ['pid', 'uid', 'tid', 'upvotes', 'downvotes', 'timestamp'], next); const bulk = [];
}, postData.forEach((p, index) => {
function (_postData, next) { if (p && p.uid && p.pid && p.tid && p.timestamp) {
postData = _postData; bulk.push([`cid:${topicData[index].cid}:uid:${p.uid}:pids`, p.timestamp, p.pid]);
const tids = postData.map(p => p.tid); if (p.votes > 0) {
topics.getTopicsFields(tids, ['cid'], next); bulk.push([`cid:${topicData[index].cid}:uid:${p.uid}:pids:votes`, p.votes, p.pid]);
}, }
function (topicData, next) { }
const bulk = []; });
postData.forEach((p, index) => { await db.sortedSetAddBulk(bulk);
if (p && p.uid && p.pid && p.tid && p.timestamp) {
bulk.push([`cid:${topicData[index].cid}:uid:${p.uid}:pids`, p.timestamp, p.pid]);
if (p.votes > 0) {
bulk.push([`cid:${topicData[index].cid}:uid:${p.uid}:pids:votes`, p.votes, p.pid]);
}
}
});
db.sortedSetAddBulk(bulk, next);
},
], next);
}, { }, {
progress: progress, progress: progress,
}, callback); });
}, },
}; };

Loading…
Cancel
Save