Can't undelete category description posts created by deleted user

At some point after some categories were created, the user that created them was deleted. Looking at the log, I can see that Discourse deleted those posts automatically at the same time the user was deleted.

We failed to notice that this caused any problems until some time later, when it became clear that while admins can still see (and edit) the affected category description posts, when regular users try to view those posts, they get this instead: “Error; while trying to load; Something went wrong.”

These category description posts appear with red backgrounds. I can see that they were deleted, and when. I can see the ‘undelete’ button, but clicking it does nothing at all.

If I go to the settings for one of the affected categories (by clicking its Edit button), and click the Edit Description button, I’m taken to the corresponding deleted post. I can make changes and save them, and after I’ve refreshed the page, the new description appears. But non-admin users still can’t see the contents of the post (just that error).

I was able to change the ownership for one of the posts to my own admin user, but the undelete button still doesn’t do anything.

I’ve found similar reports on meta Discourse, but so far none of the advice helped.

I found one reference to a person who was able to undelete a similarly-affected post using the Discourse API, and I started looking into that, but I was unable to find anything about undeleting posts in the API documentation. See Can't recover a topic by a deleted user

I hope there’s a solution that doesn’t involve recreating the affected categories from scratch.

Update: when I click the undelete button for one of these posts, it generates an entry in the error log:

Url: mysite/assets/ember_jquery-189e46ebcb33594b835e782fd1ce916ec750bc0cf980ebc4fb7796649161a18d.js
Line: 1
Column: 266460
Window Location: mysite/t/about-the-showcases-and-use-cases-category/28```
3 Likes

So someone issued a destroy command on the user who created the categories? Is there any particular reason this user was destroyed instead of anonymized?

It is true that the category description topic will be owned by the user who creates the category, so destroying that user (and all their posts) could potentially cause this. @tshenry can we test this scenario next week?

3 Likes

I was able to run through it quickly just now and repro the same results.

  1. Create a category with an admin user
  2. Revoke their admin status and delete the user
  3. The “About” topic for the category is soft-deleted when the user is deleted
  4. Attempting to undelete/restore the topic doesn’t work
  5. Changing ownership does technically work, but the topic remains in a strange deleted state no matter what you do from the UI.

From the console it looks like the posts record that is associated with the topic is deleted when the user is deleted. The topic record has a lot of 0’s when looking at various post related attributes.

It might be worth having an engineer see if we can be a bit more graceful here, but I suspect it’s easier said than done.


I was able to come up with a workaround to get back into a good place, but I had to use the command line. @jrivettcsa I would recommend taking a backup before you try this:

Values needed

Be sure to copy any of the content of the About topic you want to use later into a notepad or similar, then proceed with the following:

Enter the rails console

cd /var/discourse
./launcher enter app
rails c

Replace <ABOUT_TOPIC_ID> in the command below with the Category’s “About” topic ID, then run to hard-delete the broken topic.

Topic.where(id: <ABOUT_TOPIC_ID>).destroy_all

Press the q key to get back to the console if needed

The category is still associated with the bad topic, so we need to clear that out. Replace <CATEGORY_ID> in the command below with the Category’s ID, then run.

Category.where(id: <CATEGORY_ID>).update_all(topic_id: nil)

Press the q key to get back to the console if needed

Exit the rails console and run following rake task to create a new About topic for the category:

exit
rake categories:create_definition

Now that category should be fixed! You can rinse and repeat as needed.

6 Likes

Yes, some unfortunate changes were made by someone who should have known better. I cannot say more. Luckily these things are logged by Discourse.

1 Like

Thanks for confirming the problem and providing a solution. I will attempt it, and post the results here.

1 Like

This solution worked perfectly. Thank you!

I fixed each one individually, but I think I could have done the destroy_all and update_all stuff in one rails console session, then issued the rake categories:create_definition command once at the end. What do you think? It might save somone a few minutes at some point.

4 Likes

It’s definitely a bug @jrivettcsa – when deleting a user can lead to a broken site, that’s bad! We’ll get it fixed.

4 Likes

Thank you for lodging that issue. I made a fix in this PR

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

When a user is deleted post describing category is not deleted anymore. Instead, the author of the post is changed from the original user to the system user.

4 Likes