Async refactor in place (#7736)

* feat: allow both callback&and await

* feat: ignore async key

* feat: callbackify and promisify in same file

* Revert "feat: callbackify and promisify in same file"

This reverts commit cea206a9b8.

* feat: no need to store .callbackify

* feat: change getTopics to async

* feat: remove .async

* fix: byScore

* feat: rewrite topics/index and social with async/await

* fix: rewrite topics/data.js

fix issue with async.waterfall, only pass result if its not undefined

* feat: add callbackify to redis/psql

* feat: psql use await

* fix: redis 🌋

* feat: less returns

* feat: more await rewrite

* fix: redis tests

* feat: convert sortedSetAdd

rewrite psql transaction to async/await

* feat: 🐶

* feat: test

* feat: log client and query

* feat: log bind

* feat: more logs

* feat: more logs

* feat: check perform

* feat: dont callbackify transaction

* feat: remove logs

* fix: main functions

* feat: more logs

* fix: increment

* fix: rename

* feat: remove cls

* fix: remove console.log

* feat: add deprecation message to .async usage

* feat: update more dbal methods

* fix: redis :voodoo:

* feat:  fix redis zrem, convert setObject

* feat: upgrade getObject methods

* fix: psql getObjectField

* fix: redis tests

* feat: getObjectKeys

* feat: getObjectValues

* feat: isObjectField

* fix: add missing return

* feat: delObjectField

* feat: incrObjectField

* fix: add missing await

* feat: remove exposed helpers

* feat: list methods

* feat: flush/empty

* feat: delete

* fix: redis delete all

* feat: get/set

* feat: incr/rename

* feat: type

* feat: expire

* feat: setAdd

* feat: setRemove

* feat: isSetMember

* feat: getSetMembers

* feat: setCount, setRemoveRandom

* feat: zcard,zcount

* feat: sortedSetRank

* feat: isSortedSetMember

* feat: zincrby

* feat: sortedSetLex

* feat: processSortedSet

* fix: add mising await

* feat: debug psql

* fix: psql test

* fix: test

* fix: another test

* fix: test fix

* fix: psql tests

* feat: remove logs

* feat: user arrow func

use builtin async promises

* feat: topic bookmarks

* feat: topic.delete

* feat: topic.restore

* feat: topics.purge

* feat: merge

* feat: suggested

* feat: topics/user.js

* feat: topics modules

* feat: topics/follow

* fix: deprecation msg

* feat: fork

* feat: topics/posts

* feat: sorted/recent

* feat: topic/teaser

* feat: topics/tools

* feat: topics/unread

* feat: add back node versions

disable deprecation notice
wrap async controllers in try/catch

* feat: use db directly

* feat: promisify in place

* fix: redis/psql

* feat: deprecation message

logs for psql

* feat: more logs

* feat: more logs

* feat: logs again

* feat: more logs

* fix: call release

* feat: restore travis, remove logs

* fix: loops

* feat: remove .async. usage
This commit is contained in:
Barış Soner Uşaklı
2019-07-09 12:46:49 -04:00
committed by GitHub
parent 43ce5f8af3
commit 805dcd7ca2
73 changed files with 4030 additions and 6110 deletions

View File

@@ -1,7 +1,6 @@
'use strict';
const async = require('async');
const _ = require('lodash');
const db = require('../database');
@@ -12,7 +11,7 @@ const meta = require('../meta');
const plugins = require('../plugins');
module.exports = function (Topics) {
Topics.getSortedTopics = function (params, callback) {
Topics.getSortedTopics = async function (params) {
var data = {
nextStart: 0,
topicCount: 0,
@@ -25,51 +24,31 @@ module.exports = function (Topics) {
if (params.hasOwnProperty('cids') && params.cids && !Array.isArray(params.cids)) {
params.cids = [params.cids];
}
async.waterfall([
function (next) {
getTids(params, next);
},
function (tids, next) {
data.topicCount = tids.length;
data.tids = tids;
getTopics(tids, params, next);
},
function (topicData, next) {
data.topics = topicData;
data.nextStart = params.stop + 1;
next(null, data);
},
], callback);
data.tids = await getTids(params);
data.topicCount = data.tids.length;
data.topics = await getTopics(data.tids, params);
data.nextStart = params.stop + 1;
return data;
};
function getTids(params, callback) {
async.waterfall([
function (next) {
if (params.term === 'alltime') {
if (params.cids) {
getCidTids(params.cids, params.sort, next);
} else {
db.getSortedSetRevRange('topics:' + params.sort, 0, 199, next);
}
} else {
Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term, next);
}
},
function (tids, next) {
if (params.term !== 'alltime' || (params.cids && params.sort !== 'recent')) {
sortTids(tids, params, next);
} else {
next(null, tids);
}
},
function (tids, next) {
filterTids(tids, params, next);
},
], callback);
async function getTids(params) {
let tids = [];
if (params.term === 'alltime') {
if (params.cids) {
tids = await getCidTids(params.cids, params.sort);
} else {
tids = await db.getSortedSetRevRange('topics:' + params.sort, 0, 199);
}
} else {
tids = await Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term);
}
if (params.term !== 'alltime' || (params.cids && params.sort !== 'recent')) {
tids = await sortTids(tids, params);
}
return await filterTids(tids, params);
}
function getCidTids(cids, sort, callback) {
async function getCidTids(cids, sort) {
const sets = [];
const pinnedSets = [];
cids.forEach(function (cid) {
@@ -80,35 +59,23 @@ module.exports = function (Topics) {
sets.push('cid:' + cid + ':tids' + (sort ? ':' + sort : ''));
pinnedSets.push('cid:' + cid + ':tids:pinned');
});
async.waterfall([
function (next) {
async.parallel({
tids: async.apply(db.getSortedSetRevRange, sets, 0, 199),
pinnedTids: async.apply(db.getSortedSetRevRange, pinnedSets, 0, -1),
}, next);
},
function (results, next) {
next(null, results.pinnedTids.concat(results.tids));
},
], callback);
const [tids, pinnedTids] = await Promise.all([
db.getSortedSetRevRange(sets, 0, 199),
db.getSortedSetRevRange(pinnedSets, 0, -1),
]);
return pinnedTids.concat(tids);
}
function sortTids(tids, params, callback) {
async.waterfall([
function (next) {
Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount'], next);
},
function (topicData, next) {
var sortFn = sortRecent;
if (params.sort === 'posts') {
sortFn = sortPopular;
} else if (params.sort === 'votes') {
sortFn = sortVotes;
}
tids = topicData.sort(sortFn).map(topic => topic && topic.tid);
next(null, tids);
},
], callback);
async function sortTids(tids, params) {
const topicData = await Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount']);
var sortFn = sortRecent;
if (params.sort === 'posts') {
sortFn = sortPopular;
} else if (params.sort === 'votes') {
sortFn = sortVotes;
}
tids = topicData.sort(sortFn).map(topic => topic && topic.tid);
return tids;
}
function sortRecent(a, b) {
@@ -129,71 +96,52 @@ module.exports = function (Topics) {
return b.viewcount - a.viewcount;
}
function filterTids(tids, params, callback) {
async function filterTids(tids, params) {
const filter = params.filter;
const uid = params.uid;
let topicData;
let topicCids;
async.waterfall([
function (next) {
if (filter === 'watched') {
Topics.filterWatchedTids(tids, uid, next);
} else if (filter === 'new') {
Topics.filterNewTids(tids, uid, next);
} else if (filter === 'unreplied') {
Topics.filterUnrepliedTids(tids, next);
} else {
Topics.filterNotIgnoredTids(tids, uid, next);
}
},
function (tids, next) {
privileges.topics.filterTids('topics:read', tids, uid, next);
},
function (tids, next) {
Topics.getTopicsFields(tids, ['uid', 'tid', 'cid'], next);
},
function (_topicData, next) {
topicData = _topicData;
topicCids = _.uniq(topicData.map(topic => topic.cid)).filter(Boolean);
if (filter === 'watched') {
tids = await Topics.filterWatchedTids(tids, uid);
} else if (filter === 'new') {
tids = await Topics.filterNewTids(tids, uid);
} else if (filter === 'unreplied') {
tids = await Topics.filterUnrepliedTids(tids);
} else {
tids = await Topics.filterNotIgnoredTids(tids, uid);
}
async.parallel({
ignoredCids: function (next) {
if (filter === 'watched' || meta.config.disableRecentCategoryFilter) {
return next(null, []);
}
categories.isIgnored(topicCids, uid, next);
},
filtered: async.apply(user.blocks.filter, uid, topicData),
}, next);
},
function (results, next) {
const isCidIgnored = _.zipObject(topicCids, results.ignoredCids);
topicData = results.filtered;
tids = await privileges.topics.filterTids('topics:read', tids, uid);
let topicData = await Topics.getTopicsFields(tids, ['uid', 'tid', 'cid']);
const topicCids = _.uniq(topicData.map(topic => topic.cid)).filter(Boolean);
const cids = params.cids && params.cids.map(String);
tids = topicData.filter(function (topic) {
return topic && topic.cid && !isCidIgnored[topic.cid] && (!cids || (cids.length && cids.includes(topic.cid.toString())));
}).map(topic => topic.tid);
plugins.fireHook('filter:topics.filterSortedTids', { tids: tids, params: params }, next);
},
function (data, next) {
next(null, data && data.tids);
},
], callback);
async function getIgnoredCids() {
if (filter === 'watched' || meta.config.disableRecentCategoryFilter) {
return [];
}
return await categories.isIgnored(topicCids, uid);
}
const [ignoredCids, filtered] = await Promise.all([
getIgnoredCids(),
user.blocks.filter(uid, topicData),
]);
const isCidIgnored = _.zipObject(topicCids, ignoredCids);
topicData = filtered;
const cids = params.cids && params.cids.map(String);
tids = topicData.filter(function (topic) {
return topic && topic.cid && !isCidIgnored[topic.cid] && (!cids || (cids.length && cids.includes(topic.cid.toString())));
}).map(topic => topic.tid);
const result = await plugins.fireHook('filter:topics.filterSortedTids', { tids: tids, params: params });
return result.tids;
}
function getTopics(tids, params, callback) {
async.waterfall([
function (next) {
tids = tids.slice(params.start, params.stop !== -1 ? params.stop + 1 : undefined);
Topics.getTopicsByTids(tids, params, next);
},
function (topicData, next) {
Topics.calculateTopicIndices(topicData, params.start);
next(null, topicData);
},
], callback);
async function getTopics(tids, params) {
tids = tids.slice(params.start, params.stop !== -1 ? params.stop + 1 : undefined);
const topicData = await Topics.getTopicsByTids(tids, params);
Topics.calculateTopicIndices(topicData, params.start);
return topicData;
}
Topics.calculateTopicIndices = function (topicData, start) {