Not getting custom fields on :approved_post event

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.

2 Likes

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

2 Likes

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_Rizzi can you look into it?

4 Likes

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:

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

3 Likes

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

4 Likes

Well done @fzngagan!

3 Likes

@Roman_Rizzi, 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

1 Like

@merefield. Ohh, I didn’t notice.

1 Like