forked from namati/discourse-watch-category-mcneel
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.rb
52 lines (44 loc) · 1.89 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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|
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_category!
WatchCategory.watch_by_group("confidential-employees-only", "EmployeesOnly")
WatchCategory.watch_by_group("facilities-us","Morrisville")
WatchCategory.watch_by_group("facilities-us","Seattle")
WatchCategory.watch_by_group("facilities-us","RemoteUS")
WatchCategory.watch_by_group("facilities-uk","London")
WatchCategory.watch_by_group("dev-notifications","Engineering")
WatchCategory.watch_by_group("ProdNotifications","Engineering")
WatchCategory.watch_all("company-announcements")
WatchCategory.watch_all("Corporate-Scheduled-Maintenance")
WatchCategory.watch_all("Corporate-System-Status")
end
end
after_initialize do
module ::WatchCategory
class WatchCategoryJob < ::Jobs::Scheduled
every 1.day
def execute(args)
WatchCategory.watch_category!
end
end
end
end