Watching Categories based on on group in discourse

AI-generated summary

The discussion focuses on adapting the discourse-watch-category-mcneel plugin to automatically subscribe specific user groups to designated categories. Jared_Needell initially inquired about scaling the plugin beyond hardcoded single-category setups. tobiaseigen advised forking the repository to define multiple group-category mappings, providing a working example. Jared_Needell confirmed understanding and began testing with an EmployeesOnly group and a CONFIDENTIALEmployees Only category. However, the plugin failed to trigger subscriptions even after manual Sidekiq job execution. tobiaseigen suspected the plugin was outdated due to changes in Discourse’s user settings architecture, suggesting an update. Jared_Needell successfully debugged the issue by identifying that the plugin’s use of Category.find_by_slug was incompatible with sub-categories or required specific parameters. By switching to the standard ActiveRecord Category.find_by(slug: ...) method, the plugin began functioning correctly. techAPJ and cpradio clarified that the original find_by_slug method requires a parent_category_slug for sub-categories, whereas find_by queries the table directly without such constraints. armando reported similar installation issues, to which Jared_Needell responded that the background job runs daily and can be manually triggered via Sidekiq. The thread concludes with downey encouraging a pull request, which Jared_Needell accepted, noting further UI improvements are planned.

I found this plugin

Is this only hardcoded for one category and one group? I’d like to try and set this up on a larger scale.

@tobiaseigen

What you want to do is fork the plugin on github, update your forked version according to your needs, and then include your forked version in app.yml. I’ve got it working now for I think 5 categories.

My version - to give you an example.

Ok sweet

So for your code just make sure I’m reading this right

Namati_Staff gets subscribed to Annoucements
Namati_Leadership get subscribed to Leadership
Network_NGC gets subscribed to NGC
Namati_Culture get subscribed to Culture
namati_assessingopp gets subscribed to assessingopp
namati_scale gets subbed to scale

If that’s right I’m gonna start testing this out, it would be cool to build out a UI for this.

yup - you’ve got it right.

So testing this out

I have a EmployeesOnly group and a category called : “CONFIDENTIALEmployees Only”

So I want everyone in that group subscribed to this category,

I forked the code and tweaked it to this…

Added the plugin to app.yml , rebuilt

I can see this plugin in admin panel

And finally I went to sidekiq to manually trigger the job,

My account has permission to see this category and is a member of of the EmployeesOnly group. Even with triggering the job, my account isn’t being set to watch “CONFIDENTIALEmployees Only”

Any way to see an error log of some type for this?

Tried with the everyone group as well and doesn’t work.

I just tested as well and can confirm that this plugin no longer works.

@techAPJ is it possible this plugin of yours is outdated, now that user settings have moved to the user options table? This is not a priority for me right now - perhaps you or @Jared_Needell could update the plugin.

https://meta.discourse.org/t/edit-a-user-setting-for-all-discourse-users/25162/7?u=tobiaseigen

@codinghorror might be able to provide insight on this too.

@techapj worked on it – he can elaborate.

So teaching myself rails tonight!

Breaking down the plugin I did some testing.

Category.find_by_slug(“site-feedback”) doesn’t seem to work, I get a nil return

but if I do

Category.find_by(slug: “site-feedback”) appears to return data.

I made these changes on the plugin and appears to working..

Here’s my code, I’m going to be updating / tweaking it through the night to fit my site needs.

Plugin also supports looping through all users just not groups. Helpful when it comes to enforcing notifications

One day this functionality will be in Discourse core, and I will be even happier with the product.

See also

and even more relevant:

Agreed, there’s a lot of features that would make me even happier :slight_smile:

Luckily for a first experience debugging a rails plugin it wasn’t too complicated.

Any chance you can submit a pull request back with your fixes? :slight_smile:

If you notice, I changed it quite a bit utilizing methods to reduce repetitive code. I might take advantage of that and see if there is some way I can incorporate a UI to this.

I can do the pull request but I still plan on making more changes.

That’s awesome. Keep up the great work, this could make a big impact! :slight_smile:

This is because I added a find_by_slug method in Category model:

As you can see in above snippet, this method requires both category_slug and parent_category_slug, so you will have to pass parent_category_slug as well if applicable.

Doesn’t look like it requires the parent, the method you linked has an if statement to proceed without the parent category if it isn’t provided.

That likely handles top-level categories only. If you want a sub-category, it seems the parent category is required.

In case the parent_category_slug is not passed the method will look for top level categories only, as per this query:

self.where(slug: category_slug, parent_category_id: nil).first

In your case I can see from console screenshot that parent_category_id is 5, so the above condition will not be satisified, hence the method returns nil.