How to call hook inside a custom hook?

Hello,

I have created a custom route inside a plugin that gets the post request from the frontend. Inside the custom route I have called an ‹ action:api.video-channel.created › hook which returns channel id after a video channel is created and inside that I have added some query to save certain information to the database utilizing the channel id and req.body from the post request.

In the frontend, I have added an input field to get the new data and to send the data to backend I have called the custom API by adding eventListener in the create button of the create video channel page.

Problem: The problem I am facing is that when the plugin is installed for the first time it works as it should but after creating one channel when I tried to create next channel, the channel id changes but req.body remains the same from the one from the first channel created. From debugging I found that the ‹ action:api.video-channel.created › is being called twice in the next request once with the previous channels data with new channel id and once with the new data with new channel id. Here the channel id does not changes but only the data changes in the next channel creatio and only the first data gets saved.

// post request to save the custom data of a video channel while creating 
    router.post('/channel-data',  (req, res) => {
      try {
        let {data}  = req.body;
        registerHook({
            target: 'action:api.video-channel.created',
            handler: ({videoChannel}) => {
              peertubeHelpers.database.query(insertInDb, {type: 'INSERT', bind: {videoChannelId: videoChannel.dataValues.id, data: data}})
              }
          })
           res.send(`added data`)
        
      } catch (error) {
        console.log(error)
      }
        })

You missunderstood how the hook mechanism works.

When you call registerHook, you tell Peertube that you want to listen the hook 'action:api.video-channel.created'. You call it once, and then everytime a channel is created, your callback function will be called.

Here, you are registering a callback for that hook every time someone make a POST request to /channel-data. This is not good. Your callback will be called 1 time, then 2, then 3, then 4, …

You must call registerHook outside any route, directly in the plugin register method.

In your case, this will not work either, because if you move the registerHook, you will not have the data variable. I don’t know what you try to achieve, so I can’t tell you how to make it work.

Problem statement: We are trying to POST data with videoChannelId (only once) in our custom table after the channel is created, using channel creation hook. Is there a mechanism to access videoChannelID of recently added channel because we need it to be saved in our custom table with data from POST request? This is our issue @JohnLivingston.

What kind of data? Have you added fields in the front-end form? If so, the trigger callback receives the req object, from where you can probably get the values.

If so, I don’t know how you added these fields. But please avoid dirty workarounds if Peertube does not provide functionnality you need. For example, if you want custom fields in the channel form, you should make a «feature request» on github, to have something like this for channels: add custom fields to video form

Thank you @JohnLivingston for helping me out.

In the frontend I added the field accessing the DOM element. Is there a way to add the custom fields to the form other than add custom fields to video form for now so that I can receive the req object from the trigger callback?

I don’t know, i’ll let someone else respond.