Files
NodeBB/src/topics/teaser.js

120 lines
3.0 KiB
JavaScript
Raw Normal View History

2014-11-11 19:47:56 -05:00
'use strict';
var async = require('async'),
S = require('string'),
2014-11-11 19:47:56 -05:00
2015-07-10 16:43:25 -04:00
meta = require('../meta'),
2014-11-11 19:47:56 -05:00
db = require('../database'),
user = require('../user'),
posts = require('../posts'),
2015-03-20 12:21:27 -04:00
plugins = require('../plugins'),
2014-11-11 19:47:56 -05:00
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));
2015-07-10 16:43:25 -04:00
if (topic) {
if (meta.config.teaserPost === 'first') {
teaserPids.push(topic.mainPid);
} else {
teaserPids.push(topic.teaserPid);
}
2014-12-19 23:56:51 -05:00
}
});
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 = {};
async.each(postData, function(post, next) {
2014-12-19 23:56:51 -05:00
post.user = users[post.uid];
post.timestamp = utils.toISOString(post.timestamp);
tidToPost[post.tid] = post;
posts.parsePost(post, next);
}, function(err) {
if (err) {
return callback(err);
2014-11-11 19:47:56 -05:00
}
var teasers = topics.map(function(topic, index) {
2015-05-29 00:02:18 -04:00
if (!topic) {
return null;
}
if (tidToPost[topic.tid]) {
2015-07-10 16:43:25 -04:00
tidToPost[topic.tid].index = meta.config.teaserPost === 'first' ? 1 : counts[index];
if (tidToPost[topic.tid].content) {
var s = S(tidToPost[topic.tid].content);
tidToPost[topic.tid].content = s.stripTags.apply(s, utils.stripTags).s;
}
}
return tidToPost[topic.tid];
});
plugins.fireHook('filter:teasers.get', {teasers: teasers}, function(err, data) {
callback(err, data ? data.teasers : null);
});
2015-03-20 12:21:27 -04:00
});
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);
};
Topics.getTeaser = function(tid, callback) {
2014-12-19 23:56:51 -05:00
Topics.getTeasersByTids([tid], function(err, teasers) {
callback(err, Array.isArray(teasers) && teasers.length ? teasers[0] : null);
});
};
Topics.updateTeaser = function(tid, callback) {
Topics.getLatestUndeletedReply(tid, function(err, pid) {
if (err) {
2014-11-11 19:47:56 -05:00
return callback(err);
}
pid = pid || null;
Topics.setTopicField(tid, 'teaserPid', pid, callback);
2014-11-11 19:47:56 -05:00
});
};
};