Not getting custom fields on :approved_post event

AI-generated summary

The discussion revolves around an issue where custom fields are not being preserved in the :approved_post event for reviewable posts. fzngagan reports that custom fields are available in the NewPostManager handler but not in the :approved_post callback, citing the polls plugin as an example where this should work. zogstrip requests code to reproduce the issue. eviltrout identifies a potential bug, noting that NewPostManager arguments aren’t automatically stored in the reviewable’s payload. Roman confirms the bug and provides a fix in a PR, adding a new interface to extend allowed attributes. fzngagan thanks the team, and merefield praises the effort. fzngagan asks about backporting, but merefield notes the fix is already on tests-passed.

I am trying to save custom fields for a post that needs review. When I approve the post, the custom fields don’t get saved.

I checked the :approved_post event and the custom fields aren’t getting set in the payload.

How to preserve custom fields for reviewable posts.

Also, I used NewPostManager, and there in the handler, the custom fields are available, but ofcourse it gets triggered before saving the data to reviewables.

Can you show us some code? Ideally the minimal amount we can use to reproduce this issue.

I’m fixing an issue with events plugin where the events data in the post_custom_field doesn’t get saved if the category is set to require approval for topics.
I reffered to the polls plugin and there it seems that the custom fields are being set on
:appoved_post.
this is the snippet from polls.

NewPostManager.add_handler(1) do |manager|
    post = Post.new(raw: manager.args[:raw])

    if !DiscoursePoll::PollsValidator.new(post).validate_polls
      result = NewPostResult.new(:poll, false)

      post.errors.full_messages.each do |message|
        result.errors[:base] << message
      end

      result
    else
      manager.args["is_poll"] = true
      nil
    end
  end
on(:approved_post) do |queued_post, created_post|
    if queued_post.payload["is_poll"]
      created_post.validate_polls(true)
    end
  end

In my attempt, the custom fields data is available at, NewPostManager, but the ReviewableQueuedPost instance in the callback of :approved_post (which ideally should have the custom field in its payload) doesn’t have it.

I tried


NewPostManager.add_handler do |manager|
 if manager.args['event'] && NewPostManager.post_needs_approval?(manager) # this condition stands true 
      manager.args['is_event'] = true
end

then

on(:approved_post) do |reviewable, post|
   p reviewable.payload['is_event'] #data isn't available here
 end

This is interesting because I would not expect the polls to work here either. The args passed to manager are not automatically put into the reviewable’s payload.

I suspect there’s a bug here with polls and queued posts. I suspect validate_polls is never called after approval. @Roman can you look into it?

I confirm this is a bug. is_poll is not being stored inside the payload, making the callback useless.

Here’s a PR to fix it:

https://github.com/discourse/discourse/pull/8009

I also added a new interface so we can extend the list of allowed attributes from a plugin.

Thanks a lot for this @Roman, @eviltrout. I spent 2 complete days figuring this out.

Well done @fzngagan!

@Roman, Do you think this feature needs to be backported?
I think it should be so that people on tests-passed and release branches can also benifit. I think some if not most plugins that use `post_custom_fields’ need to handle this edge case.

@fzngagan looks like this is already on tests-passed, see Commits · discourse/discourse · GitHub

@merefield. Ohh, I didn’t notice.