all files / src/posts/ category.js

33.33% Statements 8/24
0% Branches 0/8
36.36% Functions 4/11
33.33% Lines 8/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                       
 
'use strict';
 
var async = require('async');
var topics = require('../topics');
 
module.exports = function(Posts) {
 
	Posts.getCidByPid = function(pid, callback) {
		async.waterfall([
			function(next) {
				Posts.getPostField(pid, 'tid', next);
			},
			function(tid, next) {
				topics.getTopicField(tid, 'cid', next);
			}
		], callback);
	};
 
	Posts.getCidsByPids = function(pids, callback) {
		Posts.getPostsFields(pids, ['tid'], function(err, posts) {
			if (err) {
				return callback(err);
			}
 
			var tids = posts.map(function(post) {
				return post.tid;
			}).filter(function(tid, index, array) {
				return tid && array.indexOf(tid) === index;
			});
 
			topics.getTopicsFields(tids, ['cid'], function(err, topics) {
				if (err) {
					return callback(err);
				}
 
				var map = {};
				topics.forEach(function(topic, index) {
					if (topic) {
						map[tids[index]] = topic.cid;
					}
				});
 
				var cids = posts.map(function(post) {
					return map[post.tid];
				});
 
				callback(null, cids);
			});
		});
	};
};