Finding out how many users are watching or tracking?

AI-generated summary

In a discussion about tracking user activity on Discourse, Alavi1412 asks how to find the number of users watching or tracking a topic or category. Mittineague explains that this data is stored in tables like category_users, topic_users, group_users, and tag_users, where the notification_level field indicates preferences (0: Muted, 1: Normal, 2: Tracked, 3: Watched). They provide a SQL query example to count watchers per category. Alavi1412 then inquires about creating a plugin function in plugin.rb to expose this data to JavaScript, specifically wanting to call trackingNumbers(categoryID) within a widget. After Alavi1412 shares their GitHub repository for the nova-widget plugin, Mittineague suggests that others might offer better advice via a pull request. They propose that the data could be accessed via XHR to existing JSON endpoints or by creating a custom controller to return JSON, noting that while custom queries with JOINs are possible, they have not personally experimented with that approach.

Where Can we find how many users are watching or tracking a topic or a category? Does it save any where in discourse?

There are “_user” tables.
category_users, topic_users, group_users and tag_users
Each has a notification_level field where the integer indicates a members preference - 0 Muted, 1 Normal, 2 Tracked, 3 Watched

So a query something like this should work

SELECT COUNT(id) 
FROM category_users 
WHERE notification_level = 3 
GROUP BY category_id 

to give you how many are Watching each category.

How can I define a function in plugin.rb for using this code?
I want to use a function in my JavaScript code to get the tracking but I don’t know how to configure the plugin.rb file to create a function like that?
Can any body tell me what should I add to plugin.rb to access a function like trakingNumbers(categoryID) in my widget’s javascript code?

If you post a link to your GitHub repo others could look and offer suggestions / PRs

Here is my repo on github.
It is a widget for @angus sidebar widget plugin. but I have stock in finding tracking number. I want to use this number in This file.

I think others will know better than I as to what the best approach might be. My take is that certain values are already available by making an XHR to get the JSON and when they aren’t a controller can be put together to return JSON eg.

I have seen plugins that use custom queries involving JOINs etc. but I have not experimented doing similar yet.