Watching Categories based on on group in discourse

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…
https://github.com/jaredneedell/discourse-watch-category-mcneel/blob/master/plugin.rb

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

1 Like

@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.

https://github.com/jaredneedell/discourse-watch-category-mcneel/blob/master/plugin.rb

1 Like

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:

1 Like

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:

1 Like

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.

2 Likes

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:

https://github.com/discourse/discourse/blob/be3a5a56ccc284b352aa65080fc6b955f73cc72a/app/models/category.rb#L454-L462

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.

2 Likes

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.

3 Likes

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.

Odd that it’s working without having to do that.

Because you switched to a different method

Category.find_by(slug: "site-feedback")

Does not equal

Category.find_by_slug("site-feedback")

Note how in the file there is no method defined find_by, there is find_by_slug and find_by_email.

That is because find_by is built into ActiveRecord, and you are querying the category table for any record containing slug = 'whatever value you asked for', which is inherently different than using the method that exists in the category.rb file.

4 Likes

I have installed your plugin, but it doesn’t seem to work… :sob:
I’ve followed instruction here Install Plugins in Discourse

After installation Discourse works fine, but it seems nothing changed: if I look at users’ profiles of the group I wanted to force to watch a category, nothing changed, the category doesn’t appear among the watched ones.

This is my fork, maybe I did some mistakes…?
https://github.com/don-durito/discourse-watch-category-mcneel/blob/master/plugin.rb

1 Like

The job runs once a day, if you look in sidekiq (http://discoursever/sidekiq) under scheduler, you can manually trigger a task. If you want the script to run more frequently, just update the frequency in the script.

2 Likes