forked from amical/discourse-watch-category
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
71 lines (61 loc) · 2.41 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# name: Watch Category
# about: Watches a category for all the users in a particular group
# version: 0.3
# authors: Arpit Jalan
# url: https://github.com/discourse/discourse-watch-category-mcneel
module ::WatchCategory
def self.watch_category!
groups_cats = {
# "group" => ["category", "another-top-level-category", ["parent-category", "sub-category"] ],
"Compagnons" => [ "compagnons" ],
"Farfadets" => [ "farfadets" ],
"Louveteaux" => [ "louveteaux-jeannettes" ],
"Pio-Cara" => [ "pionniers-caravelles" ],
"Scouts-Guides" => [ "scouts-guides" ]
# "everyone" makes every user watch the listed categories
}
WatchCategory.change_notification_pref_for_group(groups_cats, :watching)
groups_cats = {
#"coordinating-cmte" => [ "announcements" ],
#"representatives" => [ "announcements" ]
"everyone" => [ "viegroupe" ]
}
WatchCategory.change_notification_pref_for_group(groups_cats, :watching_first_post)
end
def self.change_notification_pref_for_group(groups_cats, pref)
groups_cats.each do |group_name, cats|
cats.each do |cat_slug|
# If a category is an array, the first value is treated as the top-level category and the second as the sub-category
if cat_slug.respond_to?(:each)
category = Category.find_by_slug(cat_slug[1], cat_slug[0])
else
category = Category.find_by_slug(cat_slug)
end
group = Group.find_by_name(group_name)
unless category.nil? || group.nil?
if group_name == "everyone"
User.all.each do |user|
watched_categories = CategoryUser.lookup(user, pref).pluck(:category_id)
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[pref], category.id) unless watched_categories.include?(category.id)
end
else
group.users.each do |user|
watched_categories = CategoryUser.lookup(user, pref).pluck(:category_id)
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[pref], category.id) unless watched_categories.include?(category.id)
end
end
end
end
end
end
end
after_initialize do
module ::WatchCategory
class WatchCategoryJob < ::Jobs::Scheduled
every 6.hours
def execute(args)
WatchCategory.watch_category!
end
end
end
end