Following the release of Discourse v2.4.0.beta7 (or if you have this commit), admins now have the option to set the category tracking levels of all existing users when setting the default category tracking levels in their site settings:
Add a category to one of the following site settings, then select the green checkbox button.
default categories watching default categories tracking default categories muted default categories watching first post
In site settings.
These setting apply to all new accounts created after the setting is added.
Sometimes, you may decide to make the change historically and apply it to all old accounts. At the moment there is no UI to apply this so you have to crack open a rails console.
If you are a hosted customer contact us and we will sort it out.
If you self-host try the following:
Say you want to set default categories watching first post on all accounts to point to a category.
make sure you have a backup
Run a backup via the UI, if you mess something up you may want to restore to it.
find the category id
Enter the docker container and then launch the rails console.
Say I want to set categories watching first post to on a certain category, I look up per step one and find it is 5.
INSERT INTO category_users(category_id, user_id, notification_level)
SELECT =CATEGORY_ID=, u.id, =NOTIFICATION=
FROM users u
LEFT JOIN category_users cu ON cu.category_id = =CATEGORY_ID= AND cu.user_id = u.id
WHERE cu.user_id IS NULL;
The above query sets it on all users that do not have an explicit default for the category already set, this means you can re-run it if needed.
You can run the SQL by
Enter the docker container and then launch the rails console.
./launcher enter app
rails c
run the query
% User.exec_sql("INSERT INTO category_users(category_id, user_id, notification_level) SELECT =CATEGORY_ID=, u.id, =NOTIFICATION= FROM users u LEFT JOIN category_users cu ON cu.category_id = =CATEGORY_ID= AND cu.user_id = u.id WHERE cu.user_id IS NULL;")
There seems to be an issue with Step 1. (Too many quotes?)
There’s a ; missing from the end of the User.exec_sql("…") command.
Two questions:
Say I’ve previously made everyone “watch” a category with ID 5. How can I remove that setting? (I actually have two categories I need to alter. In one I want to remove the setting altogether and in the other I want to replace it with “watch first”.)
How can I apply this to tags instead of categories?
I don’t think it’s possible, because the category_users table doesn’t have created_at or updated_at columns. If it did, you could delete all the rows which were created/updated at the time you ran the original query.
I’m afraid I’m not communicating accurately: I don’t want to “revert” a change. I want to stop all users from watching the “Announcements” category. I guess an alternative would be to create a new category, move all the topics into the new category, delete the old “Announcements” category and rename the new category “Announcements”. It just seems like it should be less painful than that.
Thanks for bearing with me @Mittineague, but I can’t get this to work. The category I’m interested in (“Announcements”) has an ID of 10.
So I ran this:
User.exec_sql("INSERT INTO category_users(category_id, user_id, notification_level) SELECT 10, u.id, 1 FROM users u LEFT JOIN category_users cu ON cu.category_id = 10 AND cu.user_id = u.id WHERE cu.user_id IS NOT NULL;")
It didn’t remove the “Announcements” category from users’ watched list:
For the record, the console returned this on the first try:
If you promise you have a backup and won’t shoot me no matter what happens, you might want to try this:
User.all.each do |user| CategoryUser.batch_set(user, :regular, [10]) end
This should set the tracking level for all users for category 10 to :regular, adapt as needed. It will overwrite any previous settings users have for that category.
Sorry for taking so long to respond. I had managed to break my local Discourse running some experiments earlier. I set it up again and tried your suggestion, but no joy
I think the difference might be with using “regular”
I have seen “normal” used in code before.
I was thinking that as long as the integer “1” was used, what descriptor was used would make no difference. Not tested, but I have a feeling that what is wanted here is :normal