Issues with creating a post and locking the topic via the API

I’m locking a topic via the API (gem) and am able to lock it and unlock if that is the only command in the chain, but when I create a post in the topic beforehand, and the next command is to lock it - it makes the post, then locks it, but then immediately unlocks it!

This is really odd. If I just make the post, or just lock the topic, it works fine. But if I try to do both it ends up unlocking the topic at the end of its own accord!

For now I’ve decided to just not lock the topic - but thought I’d post here in case it’s something worth investigating.

3 Likes

Are you locking it immediately after creating a post in the topic? If so, try waiting a minute and then try locking it and see if it stays locked. Just a thought.

1 Like

Good find, I’ll investigate this when I get a chance. Combining these two api calls into a single request doesn’t strictly follow how it is done in the UI (you can’t create a new topic that is locked), so that API endpoint is likely not looking for the locked flag or it is defaulting to setting it to unlocked on topic create.

3 Likes

I looked into this for a bit today just to see if it would be a quick change, but many of the actions that you can apply to topics are best performed after the topic exists. One reason is that we really should be using the TopicStatusUpdater to close the topic rather than updating it in the db directly, but when we update the status during topic create it appears to close the topic before the first post is created resulting in this weird behavior:

For now you will need to make two separate API requests. A POST request to /posts.json and then if that succeeds a PUT request to /t/{id}/status.json.

2 Likes

Hi Blake, the topic I was trying it for already exists :slight_smile:

This was my process:

  bot.create_post(topic_id, post_content)
  bot.switch_to_master_bot!("system")
  bot.lock_topic("", topic_id)

  def create_post(topic_id, post_content)
    @client.create_post(
      topic_id: "#{topic_id}",
      raw: "#{post_content}",
    )
  end

  def lock_topic(slug, topic_id)
    params = { status: 'closed', enabled: true }
    @client.change_topic_status(slug, topic_id, params)
  end

I’m using the Discourse API Gem.

Are you saying the above should work (or is working for you)?

1 Like

Oh I see, I did misunderstand exactly what you were doing. I thought you were trying to create a topic and close it at the same time. However my previous reply still stands when creating a post in an existing topic and then closing it at the same time. You should just use the two separate endpoints.

3 Likes

Does the API Gem use the correct/seperate endpoints Blake? (I thought they did?)

The client.create_post and client.change_topic_status methods are part of the API Gem :slight_smile:

2 Likes

Yep those methods use the correct endpoints :slightly_smiling_face:

3 Likes

It should work then? (But it doesn’t :sob:)

1 Like

I just tested your code locally and it is working fine for me. What isn’t working? I’m assuming the post is being created, but the topic just isn’t being closed? If so could you use puts and output the result of @client.change_topic_status(slug, topic_id, params)?

As a side note looking at the code in the api gem we aren’t using the slug variable at all, so I’ll make an update to remove it.

2 Likes