New category automatically as muted or watching for all users

When we add a new category it is automatically as normal for all user can we also set that has a different status like watching or muted?

You can do that via the Rails console, but it’d be a manual command.

1 Like

How would that look like?

I’d assume it’s a loop over all users where for each user, something like

CategoryUser.create!(user_id:, <INSERT HERE>, CategoryUser.notification_levels[:muted])

is run – but my Ruby skills are quite lacking, and I assume this would crash if the user already had an entry here.

CategoryUser.batch_set is also looking promising…


User.all.each do |user| CategoryUser.batch_set(user, :muted, [7]) end

do the trick (for category 7)?

1 Like

After some toying around, it looks like my last command did work :slight_smile:

1 Like

yup. It does, however it removes all other already muted category. Is there a way to append new muted category instead?

1 Like

Not following, can you give me a concrete example of what you are trying to do.

Along similar lines, I’m using groups to allow members to opt in to see special-interest categories.

Ideally, upon opting in to such a category, members would be “watching first post” on that category. I’m not sure if/how I can configure this.

Sorry for late answer.

Here’s my usecase.
I would like every new category to be set to “mute” for every user, so that they need to unmute it if they want to get notifications (specially when using mailinglist mode).
In admin settings under “User preferences” I added all the categories I want to have “muted” by default. Whenever there is newly created category I can add it to that list. This however covers only the new users that just created an account. I’m looking for a way to easily mark each new category for each existing user as “muted” by default.
I used rail console query from above:
User.all.each do |user| CategoryUser.batch_set(user, :muted, [7]) end
but that instead of setting new category to “mute” only, overwrittes all muted categories users setup previously, leaving only this new category as muted.

I’m looking for query that would only add new category to the already setup list by users and not overwrite.

Does this make more sense? Sorry if I make it sound unlcear.

1 Like

Hi @muppeth, apologies for bumping this thread but I’ve been doing some Rails Console Discourse Hacking recently on a similar topic and thought I might be able to offer a solution.

Your tried command User.all.each do |user| CategoryUser.batch_set(user, :muted, [7]) end will overwrite any existing CategoryUsers. This is not what you want. The CategoryUser object creates a link between a User and a Category, setting the relationship between the two. If a user wants to mute (or watch, etc) 2 categories, then there will be 2 CategoryUsers with that user_id. The above command will overwrite every single one of them to mute the same Category, whatever they were set to before.

Here is some Ruby which checks to see if a CategoryUser exists for that user_id and category_id (leaving it alone in that case, allowing the user’s existing preference to be honoured)

In this case I wanted a list of users from a group, but you could do Users.all

# get a list of users
users =  User.joins(:groups).where(groups: {name: "MyGroupName"})

# where category_id is the ID of the category you want to mute
users.each { |u| unless CategoryUser.exists?(user_id:, category_id: 8) then CategoryUser.create(user_id:, category_id: 8, CategoryUser.notification_levels[:muted]) end }

You would need to do this for each new Category you create whch you want to have muted

If you need this to be applied for all new users as they join, then there is a plugin which sets up a scheduled job to do this, which you could adapt to set muting daily for all new users, using similar commands.

1 Like

You can now set category notification defaults in the admin settings for Watching, Watching First Post, Tracking, Normal, and Muted. You can pull them all up by searching for default categories in your admin search box.

There’s also a choice for specific groups too. You can find that in the manage/categories tab on the groups’ page. :+1: