How do I set category tracking level defaults historically

(Allen - Watchman Monitoring) #30

Is there a way to re-apply all default watching/tracking/email preferences to all users?

Not per-category as the commands here describe, but more like “here are the defaults for new users… go ahead & apply them to all users now”?


(Quim Gil) #31

If I can save the time I have spent with this thread to someone in the future…

If you want to reset the notification level of a subcategory for all users from e.g. “muted” to “regular” (being regular a special case), this is what you need to do:

./launcher enter app
rails c

And then paste your version of

level = :muted
category_slug = "cats" # this is the subcategory
parent_category_slug = "mammals" # this is the parent category
CategoryUser.where(category_id: Category.find_by_slug(category_slug, parent_category_slug), notification_level: CategoryUser.notification_levels[level]).delete_all

(Joshua Rosenfeld) #32

Can this be modified to work in group PM inboxes? For example, I was on an extended vacation, and I’m now returning. I updated my notification state for a shared inbox to “tracking” so I didn’t have thousands of green notifications, and now I want to update the state for all messages back to “watching”.

1 Like

(Joshua Rosenfeld) #33

Courtesy of @sam, here’s the code to update the notification level for PMs historically. The following placeholders must be replaced: <user_id>, <group_id_1> ,<group_id_2>.

TopicUser.where('notification_level < 3 and user_id = <user_id> and topic_id in (select topic_id from topic_allowed_groups where group_id in (<group_id_1>,<group_id_2>) )').update_all(notification_level: 3)

(Christoph) #34

Assuming that this still works, I’m wondering whether category permissions will be honoured here.

I have a number of restricted categories and whoever has access to any of those should be watching first post by default. So if I follow the above steps for each of those categories, will only only users who actually hsve access be watching them?


(Joshua Rosenfeld) #35

Category permissions are 100% followed.


User watching categories despite not having access
(Christoph) #36

Okay, just to double check: am I right in assuming that I can use the above procedure to add the category in question to all users but if a user doesn’t have access to that category it will remain ineffective and invisible for that user until the user gains access?

(My reasoning is based on how default categories watching (etc) works)


(Joshua Rosenfeld) #37

That is my understand, yes.

1 Like

(Christoph) #38

How can this be made to work with a two-container setup? If I enter data there is no rails. And if I enter web_only I get NoMethodError: undefined method `id' for nil:NilClass.


(Christoph) #44

Hm, I was searching for “staff”, to be on the safe side:

# rails c
[1] pry(main)> Category.find_by(name: "staff").id
NoMethodError: undefined method `id' for nil:NilClass
from (pry):1:in `__pry__'
[2] pry(main)>

Edit: Okay, got it: it’s case sensitive. This works:

Category.find_by(name: "Staff").id

(James Mc Mahon) #45

Yesterday I discovered where our email volume of 100K+/mo was coming from… All new users were getting set as subscribed to our “General karting” category…

So I need to correct that…

I need some help with the syntax of part 1 in the OP. Bare with me here… I’m not exactly sure what I’m doing wrong…

I tried both " and ’ for quotations, but otherwise, I’m not sure what I need to correct in my syntax

[1] pry(main)> % Category.find_by(name: "General Karting").id
SyntaxError: unexpected ')', expecting end-of-input
...ind_by(name: "General Karting").id

… ^

[1] pry(main)> % Category.find_by(name: 'General Karting').id
SyntaxError: unexpected ')', expecting end-of-input
...ind_by(name: 'General Karting').id
...                              ^

(Joshua Rosenfeld) #46

It looks like you’re typing %? Don’t :wink:. That’s just supposed to be an indication that you’re in the rails console instead of the docker container. I’ll edit the OP to be clearer.

Edit: OP edited.


(James Mc Mahon) #47

Thanks. Rookie error on my part :smiley:

1 Like

(Joshua Rosenfeld) #48

No worries at all. We all start from the beginning at some point.

Is the guide clearer now?

1 Like

(James Mc Mahon) #49

Oh it makes total sense. Thanks


(James Mc Mahon) #50

Actually… I’m stuck on the query. I need to update category ID 1 to notification level of regular (1).
From rails… I’m typing

User.exec_sql("INSERT INTO category_users(category_id, user_id, notification_level) SELECT 1,, 1 FROM users u LEFT JOIN category_users cu ON cu.category_id = 1 AND cu.user_id = WHERE cu.user_id IS NULL;")

I get response

<PG::Result:0x000055fadb534428 status=PGRES_COMMAND_OK ntuples=0 nfields=0 cmd_tuples=0>

Via the GUI/forums, it looks like it hasn’t changed.


Need Help Troubleshooting an "Internal Server Error" message When New Topic is posted
(Joshua Rosenfeld) #51

Going to call in an assist here from @sam, PostgreSQL isn’t my area of expertise.

1 Like

(James Mc Mahon) #52

Circling back to see if anyone has ideas on where I’m going wrong with my sql query?


(David Taylor) #54

The query you tried will only update the tracking level IF the user isn’t already tracking/watching/muting a category. It sounds like you want to remove any users that are watching a category.

If that’s correct, try running this on the rails console. It will find all users that are ‘watching’ the category with id=5, and set them to “regular”:

CategoryUser.where(notification_level: CategoryUser.notification_levels[:watching], category_id: 5).update_all(notification_level: CategoryUser.notification_levels[:regular])

:warning: As always, make sure to take a backup before running anything on the console - the smallest typo can have massive consequences!


(James Mc Mahon) #55

Thanks David. I must have misunderstood the purpose of the query. I thought it was to update (ie lower the subscription level) for all users UNLESS they explicitly set it themselves vs it being assigned via the setting in admin panel.