Last active
April 24, 2016 10:30
-
-
Save psychobunny/572d867935469774c6e8 to your computer and use it in GitHub Desktop.
NodeBB plugin - adding main post data to category view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var topics = module.parent.require('./topics'), | |
async = module.parent.require('async'); | |
var plugin = {}; | |
plugin.addPostData = function(data, uid, callback) { | |
async.map(data.topics, function(topic, next) { | |
topics.getMainPost(topic.tid, uid, function(err, mainPost) { | |
topic.mainPost = mainPost; | |
next(err, topic); | |
}); | |
}, function(err, topics) { | |
data.topics = topics; | |
callback(null, data); | |
}); | |
}; | |
module.exports = plugin; |
And finally, here's a variation that either picks the latest post (teaser) or mainPost if no replies have been made:
"use strict";
var topics = module.parent.require('./topics'),
posts = module.parent.require('./posts');
var theme = {};
theme.addPostData = function(params, callback) {
var pids = params.topics.map(function(topic) {
return topic.teaser ? topic.teaser.pid : topic.mainPid;
});
posts.getPostsByPids(pids, params.uid, function(err, postData) {
if (err) {
return callback(err);
}
topics.addPostData(postData, params.uid, function(err, posts) {
posts.forEach(function(post, index) {
params.topics[index].mainPost = post;
});
callback(null, params);
});
});
};
module.exports = theme;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here it is re-written to use the recommended
filter:category.topics.get
instead (this way infinite loaded posts will still work):