2015-03-09 13:16:14 -04:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-05-05 20:24:03 +03:00
|
|
|
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-05-11 15:31:31 -04:00
|
|
|
Posts.getPostsFromSet = function(set, start, stop, uid, reverse, callback) {
|
2015-05-11 15:38:14 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
|
|
|
|
Posts.getPidsFromSet(set, start, stop, reverse, next);
|
|
|
|
|
},
|
|
|
|
|
function(pids, next) {
|
|
|
|
|
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) {
|
|
|
|
|
Posts.getPostField(pid, 'tid', next);
|
|
|
|
|
},
|
|
|
|
|
function(tid, next) {
|
|
|
|
|
topics.getTopicField(tid, 'mainPid', next);
|
|
|
|
|
},
|
|
|
|
|
function(mainPid, next) {
|
|
|
|
|
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) {
|
|
|
|
|
Posts.getPostField(pid, 'tid', next);
|
|
|
|
|
},
|
|
|
|
|
function(tid, next) {
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2016-05-05 20:24:03 +03: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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
topics.getTopicsFields(tids, ['slug', 'deleted'], next);
|
|
|
|
|
}
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (results, next) {
|
|
|
|
|
var paths = pids.map(function(pid, index) {
|
|
|
|
|
if (parseInt(results.topics[index].deleted, 10) === 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|