Files
NodeBB/src/posts/topics.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-03-09 13:16:14 -04:00
'use strict';
var async = require('async');
var topics = require('../topics');
var utils = require('../../public/src/utils');
2015-03-09 13:16:14 -04:00
module.exports = function (Posts) {
2015-03-09 13:16:14 -04:00
Posts.getPostsFromSet = function (set, start, stop, uid, reverse, callback) {
2015-05-11 15:38:14 -04:00
async.waterfall([
function (next) {
2015-05-11 15:38:14 -04:00
Posts.getPidsFromSet(set, start, stop, reverse, next);
},
function (pids, next) {
2015-05-11 15:38:14 -04:00
Posts.getPostsByPids(pids, uid, next);
2015-03-09 13:16:14 -04:00
}
2015-05-11 15:38:14 -04:00
], callback);
2015-03-09 13:16:14 -04:00
};
Posts.isMain = function (pid, callback) {
2015-05-11 15:38:14 -04:00
async.waterfall([
function (next) {
2015-05-11 15:38:14 -04:00
Posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
2015-05-11 15:38:14 -04:00
topics.getTopicField(tid, 'mainPid', next);
},
function (mainPid, next) {
2015-05-11 15:38:14 -04:00
next(null, parseInt(pid, 10) === parseInt(mainPid, 10));
2015-03-09 13:16:14 -04:00
}
2015-05-11 15:38:14 -04:00
], callback);
2015-03-09 13:16:14 -04:00
};
Posts.getTopicFields = function (pid, fields, callback) {
2015-05-11 15:38:14 -04:00
async.waterfall([
function (next) {
2015-05-11 15:38:14 -04:00
Posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
2015-05-11 15:38:14 -04:00
topics.getTopicFields(tid, fields, next);
2015-03-09 13:16:14 -04:00
}
2015-05-11 15:38:14 -04:00
], callback);
2015-03-09 13:16:14 -04:00
};
Posts.generatePostPath = function (pid, uid, callback) {
Posts.generatePostPaths([pid], uid, function (err, paths) {
callback(err, Array.isArray(paths) && paths.length ? paths[0] : 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) {
var tids = postData.map(function (post) {
return post ? post.tid : null;
});
2016-05-09 22:37:03 +03:00
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);
};
2015-03-09 13:16:14 -04:00
};