I found this plugin
# frozen_string_literal: true
# name: Watch Category
# about: Watches a category for all the users in a particular group
# version: 0.2
# authors: Arpit Jalan
# url: https://github.com/discourse/discourse-watch-category-mcneel
module ::WatchCategory
def self.watch_category!
mcneel_private_category = Category.find_by_slug("mcneel-private")
mcneel_group = Group.find_by_name("mcneel")
unless mcneel_private_category.nil? || mcneel_group.nil?
mcneel_group.users.each do |user|
watched_categories = CategoryUser.lookup(user, :watching).pluck(:category_id)
if watched_categories.exclude?(mcneel_private_category.id)
CategoryUser.set_notification_level_for_category(
user,
CategoryUser.notification_levels[:watching],
mcneel_private_category.id,
This file has been truncated. show original
Is this only hardcoded for one category and one group? I’d like to try and set this up on a larger scale.
@tobiaseigen
1 Like
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.
# name: Watch Category
# about: Watches a category for all the users in a particular group
# version: 0.2
# authors: Arpit Jalan
# url: https://github.com/discourse/discourse-watch-category-mcneel
module ::WatchCategory
def self.watch_category!
announcements_category = Category.find_by_slug("announcements")
namati_staff_group = Group.find_by_name("namati_staff")
return if announcements_category.nil? || namati_staff_group.nil?
namati_staff_group.users.each do |user|
watched_categories = CategoryUser.lookup(user, :watching).pluck(:category_id)
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[:watching], announcements_category.id) unless watched_categories.include?(announcements_category.id)
end
leadership_category = Category.find_by_slug("leadership")
namati_leadership_group = Group.find_by_name("namati_leadership")
return if leadership_category.nil? || namati_leadership_group.nil?
This file has been truncated. show original
2 Likes
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…
# name: Watch Category
# about: Watches a category for all the users in a particular group
# version: 1.0
# authors: Jared Needell
module ::WatchCategory
def self.watch_by_group(category_slug, group_name)
category = Category.find_by(slug: category_slug)
group = Group.find_by_name(group_name)
return if category.nil? || group.nil?
group.users.each do |user|
watched_categories = CategoryUser.lookup(user, :watching).pluck(:category_id)
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[:watching], category.id) unless watched_categories.include?(category.id) || user.staged
end
end
def self.watch_all(category_slug)
category = Category.find_by(slug: category_slug)
User.all.each do |user|
This file has been truncated. show original
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.
# name: Watch Category
# about: Watches a category for all the users in a particular group
# version: 1.0
# authors: Jared Needell
module ::WatchCategory
def self.watch_by_group(category_slug, group_name)
category = Category.find_by(slug: category_slug)
group = Group.find_by_name(group_name)
return if category.nil? || group.nil?
group.users.each do |user|
watched_categories = CategoryUser.lookup(user, :watching).pluck(:category_id)
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[:watching], category.id) unless watched_categories.include?(category.id) || user.staged
end
end
def self.watch_all(category_slug)
category = Category.find_by(slug: category_slug)
User.all.each do |user|
This file has been truncated. show original
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
Good points @Mittineague but where I need this is in cases where:
there is a new feature in discourse and I need to set its value for everyone retroactively
if I add a new Category and want it to be in everyone’s Watch list by default (they can unfollow if they want)
if we are generally changing how the forum works for whatever internal politicking reason.
In all of these cases, the need here is to be able to Fix Our Forum for our users. If I expected to have to use this feature regularly on…
and even more relevant:
We have a private category, limited to a particular opt-in group of Discourse members for our organization. Say it’s “staff” – it’s not actually, but it’s along those lines.
I want to make sure that all staff receive prompt notifications of discussions in that private category. We encourage them to set their category settings to “watching” but I don’t think many of them have.
Under the category’s security settings, when I say “staff can create/reply/see” I’d love it if there were a way to sa…
1 Like
Agreed, there’s a lot of features that would make me even happier
Luckily for a first experience debugging a rails plugin it wasn’t too complicated.
downey
(Michael Downey)
March 25, 2016, 2:03am
14
Any chance you can submit a pull request back with your fixes?
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
downey
(Michael Downey)
March 25, 2016, 4:16am
16
That’s awesome. Keep up the great work, this could make a big impact!
techAPJ
(Arpit Jalan)
April 11, 2016, 5:44pm
17
Jared_Needell:
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.
This is because I added a find_by_slug method in Category model:
def self.find_by_slug(category_slug, parent_category_slug=nil)
if parent_category_slug
parent_category_id = self.where(slug: parent_category_slug, parent_category_id: nil).pluck(:id).first
self.where(slug: category_slug, parent_category_id: parent_category_id).first
else
self.where(slug: category_slug, parent_category_id: nil).first
end
end
end
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.
cpradio
(cpradio)
April 11, 2016, 6:01pm
19
That likely handles top-level categories only. If you want a sub-category, it seems the parent category is required.
3 Likes
techAPJ
(Arpit Jalan)
April 11, 2016, 6:13pm
20
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.