Skip to content

Instantly share code, notes, and snippets.

@psychobunny
Last active April 24, 2016 10:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psychobunny/572d867935469774c6e8 to your computer and use it in GitHub Desktop.
Save psychobunny/572d867935469774c6e8 to your computer and use it in GitHub Desktop.
NodeBB plugin - adding main post data to category view
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;
@psychobunny
Copy link
Author

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