Hook "plugins.fireHook('action:post.purge', pid)" should be synchronous.

NodeBB Development
  • Hi,
    I want to make some operation on the post which is going to PURGED.
    I am using following hook to do this.
    plugins.fireHook('action:post.purge', pid);

    Problem :

    1. This HOOK call is asynchronous meaning , hook gets called but the next function gets executed without
      waiting for response from HOOK.

    I think this HOOK call should be synchronous , next function should wait for response from HOOK.

    How can we do that?
    Please let me know.


  • If you add another parameter of function-type it gets called when all plugins are done.

    plugins.fireHook('action:post.purge', pid, function () {
      // all plugins are done

    NodeBB is mostly written with callbacks, so you can experience such code-style quite often.

    PS: please enclose your code either in ` for inline-codes (``` for code-blocks) or indent them by 4 spaces (with leading and trailing blank lines)

  • Actually I did the same thing , but it shows me "Undefined is not a function " into my plugin.
    It works if we make it 'filter' instead of 'action'.
    Have try this in you plugin?
    What is the difference between 'filter' and 'action' implementation wise?
    I think we can not pass callbacks to 'action' , what do you think?

  • @akumbhare said:

    What is the difference between 'filter' and 'action' implementation wise?


    NodeBB speaks to your plugin

    filter : "I wanna perform an action with something, do you want to participate?" and gives you a data and will wait until you'll return the data (maybe modified by you, maybe no, it doesn't really matter for NodeBB). So, in your plugin method you'll have to call callback(err, data) function when you've done. In general filter mean you can modify something

    action : "Hey, I just made some actions with something" that's it. NodeBB just gives you some data about the action that was performed.

  • ah, sorry. it seems I didn't use any actions anywhere yet so I didn't thought much about the difference...

    so as of @Mega your original request is completely valid, but it should be one of the following:

    I need a new hook action:post.purged

    I think if you explain the use-case the devs will gladly add this additional line into core...

    I think the hook action:post.purge should become a filter-hook

    Since there is no reason that I can think of (since the only reason would be the plugin-capability of changing the pid to purge) for NodeBB to wait for plugin-response before purging a post, you'd have to give a bit more information on this.

    I need to get the post-content (or other additional data of the post) within the hook action:post.purge

    Again I think the devs would gladly help if you specify what you're intending to do...
    Maybe a filter:post.purge (co-existing to the action, called before the action) would be the best solution here since you can get any information you need by the pid before it gets purged and call the next callback before actually processing.

    Am I right? Which one is it?

  • Hey, get ready to be confused @frissdiegurke -- there's a third type of hook, rounding out the behaviours 😆

    1. action hooks are, as @Mega describes, fired when something happens, and NodeBB doesn't care about the response.
    2. filters are fired when plugins might change things. The majority of hooks in NodeBB are filters.
    3. static hooks are fired when you need NodeBB to wait until your plugin is complete in order to do something. There is only one static hook available right now: static:app.load, which is fired on startup.

    In this case, since the post is already slated to be deleted, but you want to do something before it is deleted... we may want to consider adding a new hook called static:post.purge.

Suggested Topics