Skip to content

Node.js based forum software built for the modern web

refactor: remove async.waterfall

Barış Soner Uşaklı58ac55c16a53
diff --git a/src/upgrades/1.11.0/resize_image_width.js b/src/upgrades/1.11.0/resize_image_width.js
index faab4bb843..a29a45f4f3 100644
--- a/src/upgrades/1.11.0/resize_image_width.js
+++ b/src/upgrades/1.11.0/resize_image_width.js
@@ -1,24 +1,14 @@
'use strict';
-const async = require('async');
const db = require('../../database');
-
module.exports = {
name: 'Rename maximumImageWidth to resizeImageWidth',
timestamp: Date.UTC(2018, 9, 24),
- method: function (callback) {
+ method: async function () {
const meta = require('../../meta');
- async.waterfall([
- function (next) {
- meta.configs.get('maximumImageWidth', next);
- },
- function (value, next) {
- meta.configs.set('resizeImageWidth', value, next);
- },
- function (next) {
- db.deleteObjectField('config', 'maximumImageWidth', next);
- },
- ], callback);
+ const value = await meta.configs.get('maximumImageWidth');
+ await meta.configs.set('resizeImageWidth', value);
+ await db.deleteObjectField('config', 'maximumImageWidth');
},
};
diff --git a/src/upgrades/1.11.0/widget_visibility_groups.js b/src/upgrades/1.11.0/widget_visibility_groups.js
index 41db65a086..66ceac8795 100644
--- a/src/upgrades/1.11.0/widget_visibility_groups.js
+++ b/src/upgrades/1.11.0/widget_visibility_groups.js
@@ -1,47 +1,38 @@
'use strict';
-const async = require('async');
-
module.exports = {
name: 'Widget visibility groups',
timestamp: Date.UTC(2018, 10, 10),
- method: function (callback) {
+ method: async function () {
const widgetAdmin = require('../../widgets/admin');
const widgets = require('../../widgets');
- async.waterfall([
- function (next) {
- widgetAdmin.getAreas(next);
- },
- function (areas, next) {
- async.eachSeries(areas, (area, next) => {
- if (area.data.length) {
- // area.data is actually an array of widgets
- area.widgets = area.data;
- area.widgets.forEach((widget) => {
- if (widget && widget.data) {
- const groupsToShow = ['administrators', 'Global Moderators'];
- if (widget.data['hide-guests'] !== 'on') {
- groupsToShow.push('guests');
- }
- if (widget.data['hide-registered'] !== 'on') {
- groupsToShow.push('registered-users');
- }
+ const areas = await widgetAdmin.getAreas();
+ for (const area of areas) {
+ if (area.data.length) {
+ // area.data is actually an array of widgets
+ area.widgets = area.data;
+ area.widgets.forEach((widget) => {
+ if (widget && widget.data) {
+ const groupsToShow = ['administrators', 'Global Moderators'];
+ if (widget.data['hide-guests'] !== 'on') {
+ 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
- // empty groups is shown to everyone
- if (groupsToShow.length === 4) {
- widget.data.groups.length = 0;
- }
- }
- });
- widgets.setArea(area, next);
- } else {
- next();
+ // if we are showing to all 4 groups, set to empty array
+ // empty groups is shown to everyone
+ if (groupsToShow.length === 4) {
+ widget.data.groups.length = 0;
+ }
}
- }, next);
- },
- ], callback);
+ });
+ // eslint-disable-next-line no-await-in-loop
+ await widgets.setArea(area);
+ }
+ }
},
};
diff --git a/src/upgrades/1.12.0/category_watch_state.js b/src/upgrades/1.12.0/category_watch_state.js
index 71e430bedb..1363d50bc2 100644
--- a/src/upgrades/1.12.0/category_watch_state.js
+++ b/src/upgrades/1.12.0/category_watch_state.js
@@ -1,6 +1,6 @@
-'use strict';
+/* eslint-disable no-await-in-loop */
-const async = require('async');
+'use strict';
const db = require('../../database');
const batch = require('../../batch');
@@ -9,39 +9,27 @@ const categories = require('../../categories');
module.exports = {
name: 'Update category watch data',
timestamp: Date.UTC(2018, 11, 13),
- method: function (callback) {
+ method: async function () {
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) => {
- db.isSortedSetMembers(`cid:${cid}:ignorers`, uids, (err, isMembers) => {
- if (err) {
- return next(err);
- }
- uids = uids.filter((uid, index) => isMembers[index]);
- if (!uids.length) {
- return setImmediate(next);
- }
- const states = uids.map(() => categories.watchStates.ignoring);
- db.sortedSetAdd(`cid:${cid}:uid:watch:state`, states, uids, next);
- });
- }, next);
- }, {
- progress: progress,
- batch: 500,
- }, next);
- },
- function (next) {
- db.deleteAll(keys, next);
- },
- ], callback);
+ const cids = await db.getSortedSetRange('categories:cid', 0, -1);
+ const keys = cids.map(cid => `cid:${cid}:ignorers`);
+
+ await batch.processSortedSet('users:joindate', async (uids) => {
+ progress.incr(uids.length);
+ for (const cid of cids) {
+ const isMembers = await db.isSortedSetMembers(`cid:${cid}:ignorers`, uids);
+ uids = uids.filter((uid, index) => isMembers[index]);
+ if (uids.length) {
+ const states = uids.map(() => categories.watchStates.ignoring);
+ await db.sortedSetAdd(`cid:${cid}:uid:watch:state`, states, uids);
+ }
+ }
+ }, {
+ progress: progress,
+ batch: 500,
+ });
+
+ await db.deleteAll(keys);
},
};
diff --git a/src/upgrades/1.12.1/moderation_notes_refactor.js b/src/upgrades/1.12.1/moderation_notes_refactor.js
index 59ea6e6b38..390273d74a 100644
--- a/src/upgrades/1.12.1/moderation_notes_refactor.js
+++ b/src/upgrades/1.12.1/moderation_notes_refactor.js
@@ -1,55 +1,35 @@
+/* eslint-disable no-await-in-loop */
+
'use strict';
-const async = require('async');
const db = require('../../database');
const batch = require('../../batch');
-
module.exports = {
name: 'Update moderation notes to hashes',
timestamp: Date.UTC(2019, 3, 5),
- method: function (callback) {
+ method: async function () {
const { progress } = this;
- batch.processSortedSet('users:joindate', (ids, next) => {
- async.each(ids, (uid, next) => {
+ await batch.processSortedSet('users:joindate', async (uids) => {
+ await Promise.all(uids.map(async (uid) => {
progress.incr();
- db.getSortedSetRevRange(`uid:${uid}:moderation:notes`, 0, -1, (err, notes) => {
- if (err || !notes.length) {
- return next(err);
- }
- async.eachSeries(notes, (note, next) => {
- let noteData;
- async.waterfall([
- function (next) {
- try {
- noteData = JSON.parse(note);
- noteData.timestamp = noteData.timestamp || Date.now();
- setImmediate(next);
- } catch (err) {
- next(err);
- }
- },
- 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);
+ const notes = await db.getSortedSetRevRange(`uid:${uid}:moderation:notes`, 0, -1);
+ for (const note of notes) {
+ const noteData = JSON.parse(note);
+ noteData.timestamp = noteData.timestamp || Date.now();
+ await db.sortedSetRemove(`uid:${uid}:moderation:notes`, note);
+ await db.setObject(`uid:${uid}:moderation:note:${noteData.timestamp}`, {
+ uid: noteData.uid,
+ timestamp: noteData.timestamp,
+ note: noteData.note,
+ });
+ await db.sortedSetAdd(`uid:${uid}:moderation:notes`, noteData.timestamp, noteData.timestamp);
+ }
+ }));
}, {
progress: this.progress,
- }, callback);
+ });
},
};
diff --git a/src/upgrades/1.12.3/give_mod_info_privilege.js b/src/upgrades/1.12.3/give_mod_info_privilege.js
index c613bf6205..e8c51978bb 100644
--- a/src/upgrades/1.12.3/give_mod_info_privilege.js
+++ b/src/upgrades/1.12.3/give_mod_info_privilege.js
@@ -1,6 +1,7 @@
+/* eslint-disable no-await-in-loop */
+
'use strict';
-const async = require('async');
const db = require('../../database');
const privileges = require('../../privileges');
const groups = require('../../groups');
@@ -8,38 +9,19 @@ const groups = require('../../groups');
module.exports = {
name: 'give mod info privilege',
timestamp: Date.UTC(2019, 9, 8),
- method: function (callback) {
- async.waterfall([
- function (next) {
- db.getSortedSetRevRange('categories:cid', 0, -1, next);
- },
- function (cids, next) {
- async.eachSeries(cids, (cid, next) => {
- async.waterfall([
- function (next) {
- givePrivsToModerators(cid, '', next);
- },
- function (next) {
- 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);
+ method: async function () {
+ const cids = await db.getSortedSetRevRange('categories:cid', 0, -1);
+ for (const cid of cids) {
+ await givePrivsToModerators(cid, '');
+ await givePrivsToModerators(cid, 'groups:');
+ }
+ await privileges.global.give(['groups:view:users:info'], 'Global Moderators');
+
+ async function givePrivsToModerators(cid, groupPrefix) {
+ const members = await db.getSortedSetRevRange(`group:cid:${cid}:privileges:${groupPrefix}moderate:members`, 0, -1);
+ for (const member of members) {
+ await groups.join(['cid:0:privileges:view:users:info'], member);
+ }
}
},
};
diff --git a/src/upgrades/1.12.3/give_mod_privileges.js b/src/upgrades/1.12.3/give_mod_privileges.js
index 03c6c6ff67..cf7439a16b 100644
--- a/src/upgrades/1.12.3/give_mod_privileges.js
+++ b/src/upgrades/1.12.3/give_mod_privileges.js
@@ -1,6 +1,7 @@
+/* eslint-disable no-await-in-loop */
+
'use strict';
-const async = require('async');
const privileges = require('../../privileges');
const groups = require('../../groups');
const db = require('../../database');
@@ -8,7 +9,7 @@ const db = require('../../database');
module.exports = {
name: 'Give mods explicit privileges',
timestamp: Date.UTC(2019, 4, 28),
- method: function (callback) {
+ method: async function () {
const defaultPrivileges = [
'find',
'read',
@@ -43,43 +44,20 @@ module.exports = {
'groups:local:login',
];
- async.waterfall([
- function (next) {
- db.getSortedSetRevRange('categories:cid', 0, -1, next);
- },
- function (cids, next) {
- async.eachSeries(cids, (cid, next) => {
- async.waterfall([
- 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);
+ const cids = await db.getSortedSetRevRange('categories:cid', 0, -1);
+ for (const cid of cids) {
+ await givePrivsToModerators(cid, '');
+ await givePrivsToModerators(cid, 'groups:');
+ await privileges.categories.give(modPrivileges.map(p => `groups:${p}`), cid, ['Global Moderators']);
+ }
+ await privileges.global.give(globalModPrivs, 'Global Moderators');
- function givePrivsToModerators(cid, groupPrefix, callback) {
+ async function givePrivsToModerators(cid, groupPrefix) {
const privGroups = modPrivileges.map(priv => `cid:${cid}:privileges:${groupPrefix}${priv}`);
-
- 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(privGroups, member, next);
- }, next);
- },
- ], callback);
+ const members = await db.getSortedSetRevRange(`group:cid:${cid}:privileges:${groupPrefix}moderate:members`, 0, -1);
+ for (const member of members) {
+ await groups.join(privGroups, member);
+ }
}
},
};
diff --git a/src/upgrades/1.12.3/user_pid_sets.js b/src/upgrades/1.12.3/user_pid_sets.js
index d98b4f9aab..543059705e 100644
--- a/src/upgrades/1.12.3/user_pid_sets.js
+++ b/src/upgrades/1.12.3/user_pid_sets.js
@@ -1,6 +1,6 @@
+
'use strict';
-const async = require('async');
const db = require('../../database');
const batch = require('../../batch');
@@ -10,36 +10,26 @@ const topics = require('../../topics');
module.exports = {
name: 'Create zsets for user posts per category',
timestamp: Date.UTC(2019, 5, 23),
- method: function (callback) {
+ method: async function () {
const { progress } = this;
- batch.processSortedSet('posts:pid', (pids, next) => {
+ await batch.processSortedSet('posts:pid', async (pids) => {
progress.incr(pids.length);
- let postData;
- async.waterfall([
- function (next) {
- posts.getPostsFields(pids, ['pid', 'uid', 'tid', 'upvotes', 'downvotes', 'timestamp'], next);
- },
- function (_postData, next) {
- postData = _postData;
- const tids = postData.map(p => p.tid);
- topics.getTopicsFields(tids, ['cid'], next);
- },
- function (topicData, next) {
- const bulk = [];
- postData.forEach((p, index) => {
- 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);
+ const postData = await posts.getPostsFields(pids, ['pid', 'uid', 'tid', 'upvotes', 'downvotes', 'timestamp']);
+ const tids = postData.map(p => p.tid);
+ const topicData = await topics.getTopicsFields(tids, ['cid']);
+ const bulk = [];
+ postData.forEach((p, index) => {
+ 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]);
+ }
+ }
+ });
+ await db.sortedSetAddBulk(bulk);
}, {
progress: progress,
- }, callback);
+ });
},
};