How to add main post content of topic to topic lists?
-
Hello, I am creating a custom frontend using nodebb API. I want to get all the topics inside a category, along with the main post of that topic. Currently I can get all the topics inside a category using this endpoint:
http://localhost:4567/api/category/1
And I got this json response:
... ... "topics": [ { "cid": 1, "lastposttime": 1670832226781, "mainPid": 7, "postcount": 4, "slug": "5/bbca-mencapai-ath-di-harga-9000", "tid": 5, "timestamp": 1670829972612, "title": "$BBCA mencapai ATH di harga 9000", "uid": 3, "viewcount": 11, "postercount": 3, "teaserPid": 11, ... ...
This response only give me the mainPid that forces me to make another API call to get the post detail content. How can I modify this response, so I can get the whole content of the main post (not only the mainPid)? or at least the complete post content?
Please give me some advice. Thank you.
-
-
This is fairly easy to do in a plugin by using the hook
filter:topics.get
. This hook gets triggered whenever a list of topics is loaded. So you will have to add the parsed post content ofmainPid
to each topic object. Here is some sample code that does that.const posts = require.main.require('./src/posts'); myPlugin.filterTopicsGet = async function (hookData) { // get all mainPids const mainPids = hookData.topics.map(t => t.mainPid); // load all mainPosts const mainPosts = await posts.getPostsByPids(mainPids, hookData.uid); // set a mainPost object on all topics returned hookData.topics.forEach((topic, index) => { topic.mainPost = mainPosts[index]; }); return hookData; };
Once you have a plugin with that hook, you should see a
mainPost
object inside each topic object on places like /recent and /category/1. -
-
-