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