This commit is contained in:
Baris Usakli
2019-07-10 14:29:11 -04:00
parent 0ac49d63d9
commit e1ecc36d04
3 changed files with 125 additions and 218 deletions

View File

@@ -1,83 +1,52 @@
'use strict';
var async = require('async');
var topics = require('../topics');
var utils = require('../utils');
module.exports = function (Posts) {
Posts.getPostsFromSet = function (set, start, stop, uid, reverse, callback) {
async.waterfall([
function (next) {
Posts.getPidsFromSet(set, start, stop, reverse, next);
},
function (pids, next) {
Posts.getPostsByPids(pids, uid, next);
},
], callback);
Posts.getPostsFromSet = async function (set, start, stop, uid, reverse) {
const pids = await Posts.getPidsFromSet(set, start, stop, reverse);
return await Posts.getPostsByPids(pids, uid);
};
Posts.isMain = function (pid, callback) {
async.waterfall([
function (next) {
Posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
topics.getTopicField(tid, 'mainPid', next);
},
function (mainPid, next) {
next(null, parseInt(pid, 10) === parseInt(mainPid, 10));
},
], callback);
Posts.isMain = async function (pids) {
const isArray = Array.isArray(pids);
pids = isArray ? pids : [pids];
const postData = await Posts.getPostsFields(pids, ['tid']);
const topicData = await topics.getTopicsFields(postData.map(t => t.tid), ['mainPid']);
const result = pids.map((pid, i) => parseInt(pid, 10) === parseInt(topicData[i].mainPid, 10));
return isArray ? result : result[0];
};
Posts.getTopicFields = function (pid, fields, callback) {
async.waterfall([
function (next) {
Posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
topics.getTopicFields(tid, fields, next);
},
], callback);
Posts.getTopicFields = async function (pid, fields) {
const tid = await Posts.getPostField(pid, 'tid');
return await topics.getTopicFields(tid, fields);
};
Posts.generatePostPath = function (pid, uid, callback) {
Posts.generatePostPaths([pid], uid, function (err, paths) {
callback(err, Array.isArray(paths) && paths.length ? paths[0] : null);
Posts.generatePostPath = async function (pid, uid) {
const paths = await Posts.generatePostPaths([pid], uid);
return Array.isArray(paths) && paths.length ? paths[0] : null;
};
Posts.generatePostPaths = async function (pids, uid) {
const postData = await Posts.getPostsFields(pids, ['pid', 'tid']);
const tids = postData.map(post => post && post.tid);
const [indices, topicData] = await Promise.all([
Posts.getPostIndices(postData, uid),
topics.getTopicsFields(tids, ['slug']),
]);
const paths = pids.map(function (pid, index) {
var slug = topicData[index] ? topicData[index].slug : null;
var postIndex = utils.isNumber(indices[index]) ? parseInt(indices[index], 10) + 1 : null;
if (slug && postIndex) {
return '/topic/' + slug + '/' + postIndex;
}
return null;
});
};
Posts.generatePostPaths = function (pids, uid, callback) {
async.waterfall([
function (next) {
Posts.getPostsFields(pids, ['pid', 'tid'], next);
},
function (postData, next) {
async.parallel({
indices: function (next) {
Posts.getPostIndices(postData, uid, next);
},
topics: function (next) {
const tids = postData.map(post => post && post.tid);
topics.getTopicsFields(tids, ['slug'], next);
},
}, next);
},
function (results, next) {
var paths = pids.map(function (pid, index) {
var slug = results.topics[index] ? results.topics[index].slug : null;
var postIndex = utils.isNumber(results.indices[index]) ? parseInt(results.indices[index], 10) + 1 : null;
if (slug && postIndex) {
return '/topic/' + slug + '/' + postIndex;
}
return null;
});
next(null, paths);
},
], callback);
return paths;
};
};