2014-11-11 19:47:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async'),
|
|
|
|
|
|
|
|
|
|
db = require('../database'),
|
|
|
|
|
user = require('../user'),
|
|
|
|
|
posts = require('../posts'),
|
|
|
|
|
utils = require('../../public/src/utils');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function(Topics) {
|
|
|
|
|
|
2014-12-19 23:56:51 -05:00
|
|
|
Topics.getTeasers = function(topics, callback) {
|
|
|
|
|
if (!Array.isArray(topics) || !topics.length) {
|
2014-11-11 19:47:56 -05:00
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 23:56:51 -05:00
|
|
|
var counts = [];
|
|
|
|
|
var teaserPids = [];
|
|
|
|
|
|
|
|
|
|
topics.forEach(function(topic) {
|
|
|
|
|
counts.push(topic && (parseInt(topic.postcount, 10) || 0));
|
|
|
|
|
if (topic && topic.teaserPid) {
|
|
|
|
|
teaserPids.push(topic.teaserPid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-19 15:50:21 -04:00
|
|
|
posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content'], function(err, postData) {
|
2014-11-11 19:47:56 -05:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 23:56:51 -05:00
|
|
|
var uids = postData.map(function(post) {
|
|
|
|
|
return post.uid;
|
|
|
|
|
}).filter(function(uid, index, array) {
|
|
|
|
|
return array.indexOf(uid) === index;
|
2014-11-18 22:19:17 -05:00
|
|
|
});
|
|
|
|
|
|
2014-12-19 23:56:51 -05:00
|
|
|
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, usersData) {
|
2014-11-11 19:47:56 -05:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-19 23:56:51 -05:00
|
|
|
var users = {};
|
|
|
|
|
usersData.forEach(function(user) {
|
|
|
|
|
users[user.uid] = user;
|
|
|
|
|
});
|
|
|
|
|
var tidToPost = {};
|
|
|
|
|
postData.forEach(function(post) {
|
|
|
|
|
post.user = users[post.uid];
|
|
|
|
|
post.timestamp = utils.toISOString(post.timestamp);
|
|
|
|
|
tidToPost[post.tid] = post;
|
2014-11-11 19:47:56 -05:00
|
|
|
});
|
|
|
|
|
|
2014-12-19 23:56:51 -05:00
|
|
|
var teasers = topics.map(function(topic, index) {
|
|
|
|
|
if (tidToPost[topic.tid]) {
|
|
|
|
|
tidToPost[topic.tid].index = counts[index];
|
2014-11-11 19:47:56 -05:00
|
|
|
}
|
2014-12-19 23:56:51 -05:00
|
|
|
return tidToPost[topic.tid];
|
2014-11-11 19:47:56 -05:00
|
|
|
});
|
2014-12-19 23:56:51 -05:00
|
|
|
|
|
|
|
|
callback(null, teasers);
|
2014-11-11 19:47:56 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-19 23:56:51 -05:00
|
|
|
Topics.getTeasersByTids = function(tids, callback) {
|
|
|
|
|
if (!Array.isArray(tids) || !tids.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
|
|
|
|
Topics.getTopicsFields(tids, ['tid', 'postcount', 'teaserPid'], next);
|
|
|
|
|
},
|
|
|
|
|
function(topics, next) {
|
|
|
|
|
Topics.getTeasers(topics, next);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-16 16:15:49 -05:00
|
|
|
Topics.getTeaser = function(tid, callback) {
|
2014-12-19 23:56:51 -05:00
|
|
|
Topics.getTeasersByTids([tid], function(err, teasers) {
|
2014-12-12 18:52:37 -05:00
|
|
|
callback(err, Array.isArray(teasers) && teasers.length ? teasers[0] : null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Topics.updateTeaser = function(tid, callback) {
|
|
|
|
|
db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
|
|
|
|
|
if (err) {
|
2014-11-11 19:47:56 -05:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-12-12 18:52:37 -05:00
|
|
|
var pid = Array.isArray(pids) && pids.length ? pids[0] : null;
|
|
|
|
|
Topics.setTopicField(tid, 'teaserPid', pid, callback);
|
2014-11-11 19:47:56 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|