How do I set tag tracking level defaults historically

Discourse allows a user to specify the notification level for tags in their user settings.

You may decide as an admin that you want to set the default notification level of a specific tag for your current users. To do this we must use the rails console.

If you are a hosted customer contact us and we will sort it out for you.

If you are self-hosted follow the steps below:

  1. Make sure you have a backup :warning:

    Create a backup via the UI before going any further. If you mess something up, you will want to make sure you have a way to restore your site.

  2. Determine the notification_level you wish to use:

    muted: 0 
    regular: 1
    tracking: 2
    watching: 3
    watching_first_post: 4
    
  3. Run a query to backfill the changes

    • Enter the docker container and launch the rails console:

      ./launcher enter app 
      rails c
      
    • Run the query

      • Change the tag name below from EXAMPLE to the name of the tag you wish to use

      • At the the very end of the DB.exec() command, you will see level: 3. This will set the notification level to “watching”. Be sure to determine the correct number for your needs according to step 1.

      • Once you have made the above changes, running the following will add the tag to the specified notification level setting for any user that hasn’t already added the tag to one of the settings.

         tag = Tag.find_by_name("EXAMPLE")
         DB.exec("INSERT INTO tag_users(tag_id, user_id, notification_level, created_at, updated_at) SELECT :id, u.id, :level, NOW(), NOW() FROM users u LEFT JOIN tag_users tu ON tu.tag_id = :id AND tu.user_id = u.id WHERE tu.user_id IS NULL AND u.id > 0;", id: tag.id, level: 3)
        

Note:

We are looking to add Default tags watching/tracking/muted to the site settings in Discourse Version 2.4. Once this feature is added, these instructions will only be necessary if you need to change the user setting retroactively. The default site settings will only apply to users created after the setting is enabled.

Related:

How do I set category tracking level defaults historically

6 Likes