• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

How to add main post content of topic to topic lists?

Scheduled Pinned Locked Moved Solved Developer FAQ
3 Posts 2 Posters 516 Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nullpointerN Offline
    nullpointerN Offline
    nullpointer
    wrote on last edited by baris
    #1

    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.

    1 Reply Last reply
    0
  • nullpointerN nullpointer marked this topic as a question on
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    wrote on last edited by baris
    #2

    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 of mainPid 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.

    nullpointerN 1 Reply Last reply
    3
  • barisB baris moved this topic from NodeBB Development on
  • nullpointerN Offline
    nullpointerN Offline
    nullpointer
    replied to <baris> on last edited by
    #3

    @baris thank you! I'll try your solution and update here

    1 Reply Last reply
    1
  • nullpointerN nullpointer has marked this topic as solved on
  • barisB baris referenced this topic on

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development