refactor: remove async.waterfall

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

@ -1,7 +1,6 @@
'use strict'; /* eslint-disable no-await-in-loop */
const async = require('async'); 'use strict';
const groups = require('../../groups'); const groups = require('../../groups');
const db = require('../../database'); const db = require('../../database');
@ -9,19 +8,15 @@ const db = require('../../database');
module.exports = { module.exports = {
name: 'Give deleted post viewing privilege to moderators on all categories', name: 'Give deleted post viewing privilege to moderators on all categories',
timestamp: Date.UTC(2018, 5, 8), timestamp: Date.UTC(2018, 5, 8),
method: function (callback) { method: async function () {
db.getSortedSetRange('categories:cid', 0, -1, (err, cids) => { const { progress } = this;
if (err) { const cids = await db.getSortedSetRange('categories:cid', 0, -1);
return callback(err); for (const cid of cids) {
const uids = await db.getSortedSetRange(`group:cid:${cid}:privileges:moderate:members`, 0, -1);
for (const uid of uids) {
await groups.join(`cid:${cid}:privileges:posts:view_deleted`, uid);
} }
async.eachSeries(cids, (cid, next) => { progress.incr();
async.waterfall([ }
async.apply(db.getSortedSetRange.bind(db), `group:cid:${cid}:privileges:moderate:members`, 0, -1),
function (uids, next) {
async.eachSeries(uids, (uid, next) => groups.join(`cid:${cid}:privileges:posts:view_deleted`, uid, next), next);
},
], next);
}, 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 batch = require('../../batch'); const batch = require('../../batch');
@ -8,39 +9,29 @@ const batch = require('../../batch');
module.exports = { module.exports = {
name: 'add filters to events', name: 'add filters to events',
timestamp: Date.UTC(2018, 9, 4), timestamp: Date.UTC(2018, 9, 4),
method: function (callback) { method: async function () {
const { progress } = this; const { progress } = this;
batch.processSortedSet('events:time', (eids, next) => { await batch.processSortedSet('events:time', async (eids) => {
async.eachSeries(eids, (eid, next) => { for (const eid of eids) {
progress.incr(); progress.incr();
db.getObject(`event:${eid}`, (err, eventData) => { const eventData = await db.getObject(`event:${eid}`);
if (err) { if (!eventData) {
return next(err); await db.sortedSetRemove('events:time', eid);
} return;
if (!eventData) { }
return db.sortedSetRemove('events:time', eid, next); // privilege events we're missing type field
} if (!eventData.type && eventData.privilege) {
// privilege events we're missing type field eventData.type = 'privilege-change';
if (!eventData.type && eventData.privilege) { await db.setObjectField(`event:${eid}`, 'type', 'privilege-change');
eventData.type = 'privilege-change'; await db.sortedSetAdd(`events:time:${eventData.type}`, eventData.timestamp, eid);
async.waterfall([ return;
function (next) { }
db.setObjectField(`event:${eid}`, 'type', 'privilege-change', next); await db.sortedSetAdd(`events:time:${eventData.type || ''}`, eventData.timestamp, eid);
}, }
function (next) {
db.sortedSetAdd(`events:time:${eventData.type}`, eventData.timestamp, eid, next);
},
], next);
return;
}
db.sortedSetAdd(`events:time:${eventData.type || ''}`, eventData.timestamp, eid, 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 batch = require('../../batch'); const batch = require('../../batch');
@ -8,32 +9,22 @@ const batch = require('../../batch');
module.exports = { module.exports = {
name: 'Fix category topic zsets', name: 'Fix category topic zsets',
timestamp: Date.UTC(2018, 9, 11), timestamp: Date.UTC(2018, 9, 11),
method: function (callback) { method: async function () {
const { progress } = this; const { progress } = this;
const topics = require('../../topics'); const topics = require('../../topics');
batch.processSortedSet('topics:tid', (tids, next) => { await batch.processSortedSet('topics:tid', async (tids) => {
async.eachSeries(tids, (tid, next) => { for (const tid of tids) {
progress.incr(); progress.incr();
const topicData = await db.getObjectFields(`topic:${tid}`, ['cid', 'pinned', 'postcount']);
async.waterfall([ if (parseInt(topicData.pinned, 10) !== 1) {
function (next) { topicData.postcount = parseInt(topicData.postcount, 10) || 0;
db.getObjectFields(`topic:${tid}`, ['cid', 'pinned', 'postcount'], next); await db.sortedSetAdd(`cid:${topicData.cid}:tids:posts`, topicData.postcount, tid);
}, }
function (topicData, next) { await topics.updateLastPostTimeFromLastPid(tid);
if (parseInt(topicData.pinned, 10) === 1) { }
return setImmediate(next);
}
topicData.postcount = parseInt(topicData.postcount, 10) || 0;
db.sortedSetAdd(`cid:${topicData.cid}:tids:posts`, topicData.postcount, tid, next);
},
function (next) {
topics.updateLastPostTimeFromLastPid(tid, next);
},
], next);
}, next);
}, { }, {
progress: progress, progress: progress,
}, callback); });
}, },
}; };

@ -1,69 +1,39 @@
/* 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');
// var user = require('../../user');
module.exports = { module.exports = {
name: 'Upgrade bans to hashes', name: 'Upgrade bans to hashes',
timestamp: Date.UTC(2018, 8, 24), timestamp: Date.UTC(2018, 8, 24),
method: function (callback) { method: async function () {
const { progress } = this; const { progress } = this;
batch.processSortedSet('users:joindate', (uids, next) => { await batch.processSortedSet('users:joindate', async (uids) => {
async.eachSeries(uids, (uid, next) => { for (const uid of uids) {
progress.incr(); progress.incr();
const [bans, reasons, userData] = await Promise.all([
db.getSortedSetRevRangeWithScores(`uid:${uid}:bans`, 0, -1),
db.getSortedSetRevRangeWithScores(`banned:${uid}:reasons`, 0, -1),
db.getObjectFields(`user:${uid}`, ['banned', 'banned:expire', 'joindate', 'lastposttime', 'lastonline']),
]);
async.parallel({ // has no history, but is banned, create plain object with just uid and timestmap
bans: function (next) { if (!bans.length && parseInt(userData.banned, 10)) {
db.getSortedSetRevRangeWithScores(`uid:${uid}:bans`, 0, -1, next); const banTimestamp = (
}, userData.lastonline ||
reasons: function (next) { userData.lastposttime ||
db.getSortedSetRevRangeWithScores(`banned:${uid}:reasons`, 0, -1, next); userData.joindate ||
}, Date.now()
userData: function (next) { );
db.getObjectFields(`user:${uid}`, ['banned', 'banned:expire', 'joindate', 'lastposttime', 'lastonline'], next); const banKey = `uid:${uid}:ban:${banTimestamp}`;
}, await addBan(uid, banKey, { uid: uid, timestamp: banTimestamp });
}, (err, results) => { } else if (bans.length) {
function addBan(key, data, callback) {
async.waterfall([
function (next) {
db.setObject(key, data, next);
},
function (next) {
db.sortedSetAdd(`uid:${uid}:bans:timestamp`, data.timestamp, key, next);
},
], callback);
}
if (err) {
return next(err);
}
// has no ban history and isn't banned, skip
if (!results.bans.length && !parseInt(results.userData.banned, 10)) {
return next();
}
// has no history, but is banned, create plain object with just uid and timestmap
if (!results.bans.length && parseInt(results.userData.banned, 10)) {
const banTimestamp = (
results.userData.lastonline ||
results.userData.lastposttime ||
results.userData.joindate ||
Date.now()
);
const banKey = `uid:${uid}:ban:${banTimestamp}`;
addBan(banKey, { uid: uid, timestamp: banTimestamp }, next);
return;
}
// process ban history // process ban history
async.eachSeries(results.bans, (ban, next) => { for (const ban of bans) {
function findReason(score) { const reasonData = reasons.find(reasonData => reasonData.score === ban.score);
return results.reasons.find(reasonData => reasonData.score === score);
}
const reasonData = findReason(ban.score);
const banKey = `uid:${uid}:ban:${ban.score}`; const banKey = `uid:${uid}:ban:${ban.score}`;
const data = { const data = {
uid: uid, uid: uid,
@ -73,14 +43,17 @@ module.exports = {
if (reasonData) { if (reasonData) {
data.reason = reasonData.value; data.reason = reasonData.value;
} }
addBan(banKey, data, next); await addBan(uid, banKey, data);
}, (err) => { }
next(err); }
}); }
});
}, next);
}, { }, {
progress: this.progress, progress: this.progress,
}, callback); });
}, },
}; };
async function addBan(uid, key, data) {
await db.setObject(key, data);
await db.sortedSetAdd(`uid:${uid}:bans:timestamp`, data.timestamp, key);
}

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const async = require('async');
const db = require('../../database'); const db = require('../../database');
const batch = require('../../batch'); const batch = require('../../batch');
@ -9,61 +8,30 @@ const user = require('../../user');
module.exports = { module.exports = {
name: 'Record first entry in username/email history', name: 'Record first entry in username/email history',
timestamp: Date.UTC(2018, 7, 28), timestamp: Date.UTC(2018, 7, 28),
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) => { async function updateHistory(uid, set, fieldName) {
async.parallel([ const count = await db.sortedSetCard(set);
function (next) { if (count <= 0) {
// Username // User has not changed their username/email before, record original username
async.waterfall([ const userData = await user.getUserFields(uid, [fieldName, 'joindate']);
async.apply(db.sortedSetCard, `user:${uid}:usernames`), if (userData && userData.joindate && userData[fieldName]) {
(count, next) => { await db.sortedSetAdd(set, userData.joindate, [userData[fieldName], userData.joindate].join(':'));
if (count > 0) { }
// User has changed their username before, no record of original username, skip. }
return setImmediate(next, null, null); }
}
await Promise.all(uids.map(async (uid) => {
user.getUserFields(uid, ['username', 'joindate'], next); await Promise.all([
}, updateHistory(uid, `user:${uid}:usernames`, 'username'),
(userdata, next) => { updateHistory(uid, `user:${uid}:emails`, 'email'),
if (!userdata || !userdata.joindate) { ]);
return setImmediate(next); progress.incr();
} }));
db.sortedSetAdd(`user:${uid}:usernames`, userdata.joindate, [userdata.username, userdata.joindate].join(':'), next);
},
], next);
},
function (next) {
// Email
async.waterfall([
async.apply(db.sortedSetCard, `user:${uid}:emails`),
(count, next) => {
if (count > 0) {
// User has changed their email before, no record of original email, skip.
return setImmediate(next, null, null);
}
user.getUserFields(uid, ['email', 'joindate'], next);
},
(userdata, next) => {
if (!userdata || !userdata.joindate) {
return setImmediate(next);
}
db.sortedSetAdd(`user:${uid}:emails`, userdata.joindate, [userdata.email, userdata.joindate].join(':'), next);
},
], next);
},
], (err) => {
progress.incr();
setImmediate(next, err);
});
}, next);
}, { }, {
progress: this.progress, progress: this.progress,
}, callback); });
}, },
}; };

@ -1,36 +1,28 @@
'use strict'; 'use strict';
const async = require('async');
module.exports = { module.exports = {
name: 'Navigation item visibility groups', name: 'Navigation item visibility groups',
timestamp: Date.UTC(2018, 10, 10), timestamp: Date.UTC(2018, 10, 10),
method: function (callback) { method: async function () {
const navigationAdmin = require('../../navigation/admin'); const navigationAdmin = require('../../navigation/admin');
async.waterfall([ const data = await navigationAdmin.get();
function (next) { data.forEach((navItem) => {
navigationAdmin.get(next); if (navItem && navItem.properties) {
}, navItem.groups = [];
function (data, next) { if (navItem.properties.adminOnly) {
data.forEach((navItem) => { navItem.groups.push('administrators');
if (navItem && navItem.properties) { } else if (navItem.properties.globalMod) {
navItem.groups = []; navItem.groups.push('Global Moderators');
if (navItem.properties.adminOnly) { }
navItem.groups.push('administrators');
} else if (navItem.properties.globalMod) {
navItem.groups.push('Global Moderators');
}
if (navItem.properties.loggedIn) { if (navItem.properties.loggedIn) {
navItem.groups.push('registered-users'); navItem.groups.push('registered-users');
} else if (navItem.properties.guestOnly) { } else if (navItem.properties.guestOnly) {
navItem.groups.push('guests'); navItem.groups.push('guests');
} }
} }
}); });
navigationAdmin.save(data, next); await navigationAdmin.save(data);
},
], callback);
}, },
}; };

Loading…
Cancel
Save