Skip to content

Node.js based forum software built for the modern web

refactor: remove async.waterfall

Barış Soner Uşaklıf35a0f430a00
diff --git a/src/upgrades/1.0.0/global_moderators.js b/src/upgrades/1.0.0/global_moderators.js
index 2c7895996a..2152bdcfc7 100644
--- a/src/upgrades/1.0.0/global_moderators.js
+++ b/src/upgrades/1.0.0/global_moderators.js
@@ -1,32 +1,22 @@
'use strict';
-const async = require('async');
-
module.exports = {
name: 'Creating Global moderators group',
timestamp: Date.UTC(2016, 0, 23),
- method: function (callback) {
+ method: async function () {
const groups = require('../../groups');
- async.waterfall([
- function (next) {
- groups.exists('Global Moderators', next);
- },
- function (exists, next) {
- if (exists) {
- return next(null, null);
- }
- groups.create({
- name: 'Global Moderators',
- userTitle: 'Global Moderator',
- description: 'Forum wide moderators',
- hidden: 0,
- private: 1,
- disableJoinRequests: 1,
- }, next);
- },
- function (groupData, next) {
- groups.show('Global Moderators', next);
- },
- ], callback);
+ const exists = await groups.exists('Global Moderators');
+ if (exists) {
+ return;
+ }
+ await groups.create({
+ name: 'Global Moderators',
+ userTitle: 'Global Moderator',
+ description: 'Forum wide moderators',
+ hidden: 0,
+ private: 1,
+ disableJoinRequests: 1,
+ });
+ await groups.show('Global Moderators');
},
};
diff --git a/src/upgrades/1.0.0/theme_to_active_plugins.js b/src/upgrades/1.0.0/theme_to_active_plugins.js
index a541fde331..35f5b5865c 100644
--- a/src/upgrades/1.0.0/theme_to_active_plugins.js
+++ b/src/upgrades/1.0.0/theme_to_active_plugins.js
@@ -1,16 +1,13 @@
'use strict';
-const async = require('async');
const db = require('../../database');
module.exports = {
name: 'Adding theme to active plugins sorted set',
timestamp: Date.UTC(2015, 11, 23),
- method: function (callback) {
- async.waterfall([
- async.apply(db.getObjectField, 'config', 'theme:id'),
- async.apply(db.sortedSetAdd, 'plugins:active', 0),
- ], callback);
+ method: async function () {
+ const themeId = await db.getObjectField('config', 'theme:id');
+ await db.sortedSetAdd('plugins:active', 0, themeId);
},
};
diff --git a/src/upgrades/1.1.0/assign_topic_read_privilege.js b/src/upgrades/1.1.0/assign_topic_read_privilege.js
index 2b11904773..94c405cc83 100644
--- a/src/upgrades/1.1.0/assign_topic_read_privilege.js
+++ b/src/upgrades/1.1.0/assign_topic_read_privilege.js
@@ -1,71 +1,35 @@
-'use strict';
+/* eslint-disable no-await-in-loop */
+'use strict';
-const async = require('async');
const winston = require('winston');
const db = require('../../database');
module.exports = {
- name: 'Giving topics:read privs to any group that was previously allowed to Find & Access Category',
+ name: 'Giving topics:read privs to any group/user that was previously allowed to Find & Access Category',
timestamp: Date.UTC(2016, 4, 28),
- method: function (callback) {
+ method: async function () {
const groupsAPI = require('../../groups');
const privilegesAPI = require('../../privileges');
- db.getSortedSetRange('categories:cid', 0, -1, (err, cids) => {
- if (err) {
- return callback(err);
- }
-
- async.eachSeries(cids, (cid, next) => {
- privilegesAPI.categories.list(cid, (err, data) => {
- if (err) {
- return next(err);
- }
-
- const { groups } = data;
- const { users } = data;
-
- async.waterfall([
- function (next) {
- async.eachSeries(groups, (group, next) => {
- if (group.privileges['groups:read']) {
- return groupsAPI.join(`cid:${cid}:privileges:groups:topics:read`, group.name, (err) => {
- if (!err) {
- winston.verbose(`cid:${cid}:privileges:groups:topics:read granted to gid: ${group.name}`);
- }
+ const cids = await db.getSortedSetRange('categories:cid', 0, -1);
+ for (const cid of cids) {
+ const { groups, users } = await privilegesAPI.categories.list(cid);
- return next(err);
- });
- }
-
- next(null);
- }, next);
- },
- function (next) {
- async.eachSeries(users, (user, next) => {
- if (user.privileges.read) {
- return groupsAPI.join(`cid:${cid}:privileges:topics:read`, user.uid, (err) => {
- if (!err) {
- winston.verbose(`cid:${cid}:privileges:topics:read granted to uid: ${user.uid}`);
- }
-
- return next(err);
- });
- }
-
- next(null);
- }, next);
- },
- ], (err) => {
- if (!err) {
- winston.verbose(`-- cid ${cid} upgraded`);
- }
+ for (const group of groups) {
+ if (group.privileges['groups:read']) {
+ await groupsAPI.join(`cid:${cid}:privileges:groups:topics:read`, group.name);
+ winston.verbose(`cid:${cid}:privileges:groups:topics:read granted to gid: ${group.name}`);
+ }
+ }
- next(err);
- });
- });
- }, callback);
- });
+ for (const user of users) {
+ if (user.privileges.read) {
+ await groupsAPI.join(`cid:${cid}:privileges:topics:read`, user.uid);
+ winston.verbose(`cid:${cid}:privileges:topics:read granted to uid: ${user.uid}`);
+ }
+ }
+ winston.verbose(`-- cid ${cid} upgraded`);
+ }
},
};
diff --git a/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js b/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js
index b8f0325e31..0fa43b9090 100644
--- a/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js
+++ b/src/upgrades/1.1.0/dismiss_flags_from_deleted_topics.js
@@ -1,101 +1,56 @@
'use strict';
-const async = require('async');
const winston = require('winston');
const db = require('../../database');
module.exports = {
name: 'Dismiss flags from deleted topics',
timestamp: Date.UTC(2016, 3, 29),
- method: function (callback) {
+ method: async function () {
const posts = require('../../posts');
const topics = require('../../topics');
- let pids;
- let tids;
+ const pids = await db.getSortedSetRange('posts:flagged', 0, -1);
+ const postData = await posts.getPostsFields(pids, ['tid']);
+ const tids = postData.map(t => t.tid);
+ const topicData = await topics.getTopicsFields(tids, ['deleted']);
+ const toDismiss = topicData.map((t, idx) => (parseInt(t.deleted, 10) === 1 ? pids[idx] : null)).filter(Boolean);
- async.waterfall([
- async.apply(db.getSortedSetRange, 'posts:flagged', 0, -1),
- function (_pids, next) {
- pids = _pids;
- posts.getPostsFields(pids, ['tid'], next);
- },
- function (_tids, next) {
- tids = _tids.map(a => a.tid);
-
- topics.getTopicsFields(tids, ['deleted'], next);
- },
- function (state, next) {
- const toDismiss = state.map((a, idx) => (parseInt(a.deleted, 10) === 1 ? pids[idx] : null)).filter(Boolean);
-
- winston.verbose(`[2016/04/29] ${toDismiss.length} dismissable flags found`);
- async.each(toDismiss, dismissFlag, next);
- },
- ], callback);
+ winston.verbose(`[2016/04/29] ${toDismiss.length} dismissable flags found`);
+ await Promise.all(toDismiss.map(dismissFlag));
},
};
// copied from core since this function was removed
// https://github.com/NodeBB/NodeBB/blob/v1.x.x/src/posts/flags.js
-function dismissFlag(pid, callback) {
- async.waterfall([
- function (next) {
- db.getObjectFields(`post:${pid}`, ['pid', 'uid', 'flags'], next);
- },
- function (postData, next) {
- if (!postData.pid) {
- return callback();
- }
- async.parallel([
- function (next) {
- if (parseInt(postData.uid, 10)) {
- if (parseInt(postData.flags, 10) > 0) {
- async.parallel([
- async.apply(db.sortedSetIncrBy, 'users:flags', -postData.flags, postData.uid),
- async.apply(db.incrObjectFieldBy, `user:${postData.uid}`, 'flags', -postData.flags),
- ], next);
- } else {
- next();
- }
- } else {
- next();
- }
- },
- function (next) {
- db.sortedSetsRemove([
- 'posts:flagged',
- 'posts:flags:count',
- `uid:${postData.uid}:flag:pids`,
- ], pid, next);
- },
- function (next) {
- async.series([
- function (next) {
- db.getSortedSetRange(`pid:${pid}:flag:uids`, 0, -1, (err, uids) => {
- if (err) {
- return next(err);
- }
-
- async.each(uids, (uid, next) => {
- const nid = `post_flag:${pid}:uid:${uid}`;
- async.parallel([
- async.apply(db.delete, `notifications:${nid}`),
- async.apply(db.sortedSetRemove, 'notifications', `post_flag:${pid}:uid:${uid}`),
- ], next);
- }, next);
- });
- },
- async.apply(db.delete, `pid:${pid}:flag:uids`),
- ], next);
- },
- async.apply(db.deleteObjectField, `post:${pid}`, 'flags'),
- async.apply(db.delete, `pid:${pid}:flag:uid:reason`),
- async.apply(db.deleteObjectFields, `post:${pid}`, ['flag:state', 'flag:assignee', 'flag:notes', 'flag:history']),
- ], next);
- },
- function (results, next) {
- db.sortedSetsRemoveRangeByScore(['users:flags'], '-inf', 0, next);
- },
- ], callback);
+async function dismissFlag(pid) {
+ const postData = await db.getObjectFields(`post:${pid}`, ['pid', 'uid', 'flags']);
+ if (!postData.pid) {
+ return;
+ }
+ if (parseInt(postData.uid, 10) && parseInt(postData.flags, 10) > 0) {
+ await Promise.all([
+ db.sortedSetIncrBy('users:flags', -postData.flags, postData.uid),
+ db.incrObjectFieldBy(`user:${postData.uid}`, 'flags', -postData.flags),
+ ]);
+ }
+ const uids = await db.getSortedSetRange(`pid:${pid}:flag:uids`, 0, -1);
+ const nids = uids.map(uid => `post_flag:${pid}:uid:${uid}`);
+
+ await Promise.all([
+ db.deleteAll(nids.map(nid => `notifications:${nid}`)),
+ db.sortedSetRemove('notifications', nids),
+ db.delete(`pid:${pid}:flag:uids`),
+ db.sortedSetsRemove([
+ 'posts:flagged',
+ 'posts:flags:count',
+ `uid:${postData.uid}:flag:pids`,
+ ], pid),
+ db.deleteObjectField(`post:${pid}`, 'flags'),
+ db.delete(`pid:${pid}:flag:uid:reason`),
+ db.deleteObjectFields(`post:${pid}`, ['flag:state', 'flag:assignee', 'flag:notes', 'flag:history']),
+ ]);
+
+ await db.sortedSetsRemoveRangeByScore(['users:flags'], '-inf', 0);
}
diff --git a/src/upgrades/1.10.0/search_privileges.js b/src/upgrades/1.10.0/search_privileges.js
index 0d53aa07b6..f5576e818b 100644
--- a/src/upgrades/1.10.0/search_privileges.js
+++ b/src/upgrades/1.10.0/search_privileges.js
@@ -1,30 +1,23 @@
'use strict';
-const async = require('async');
-
module.exports = {
name: 'Give global search privileges',
timestamp: Date.UTC(2018, 4, 28),
- method: function (callback) {
+ method: async function () {
const meta = require('../../meta');
const privileges = require('../../privileges');
const allowGuestSearching = parseInt(meta.config.allowGuestSearching, 10) === 1;
const allowGuestUserSearching = parseInt(meta.config.allowGuestUserSearching, 10) === 1;
- async.waterfall([
- function (next) {
- privileges.global.give(['groups:search:content', 'groups:search:users', 'groups:search:tags'], 'registered-users', next);
- },
- function (next) {
- const guestPrivs = [];
- if (allowGuestSearching) {
- guestPrivs.push('groups:search:content');
- }
- if (allowGuestUserSearching) {
- guestPrivs.push('groups:search:users');
- }
- guestPrivs.push('groups:search:tags');
- privileges.global.give(guestPrivs, 'guests', next);
- },
- ], callback);
+
+ await privileges.global.give(['groups:search:content', 'groups:search:users', 'groups:search:tags'], 'registered-users');
+ const guestPrivs = [];
+ if (allowGuestSearching) {
+ guestPrivs.push('groups:search:content');
+ }
+ if (allowGuestUserSearching) {
+ guestPrivs.push('groups:search:users');
+ }
+ guestPrivs.push('groups:search:tags');
+ await privileges.global.give(guestPrivs, 'guests');
},
};