forked from amical/discourse-watch-category
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
86 lines (76 loc) · 3.61 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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"] ],
"coordinating-cmte" => [ ["private", "coordinating-committee"] ],
"digcol-cmte" => [ ["private", "digital-collections-committee"] ],
"digped-cmte" => [ ["private", "digital-pedagogy-committee"] ],
"digschol-cmte" => [ ["private", "digital-scholarship-committee"] ],
"eresources-cmte" => [ ["private", "e-resources-committee"], ["libraries", "buyers-group"] ],
"infolit-cmte" => [ ["private", "information-literacy-committee"] ],
"inst-research-cmte" => [ ["private", "institutional-research-assessment-committee"] ],
"oclc-cmte" => [ ["private", "oclc-programs-committee"] ],
"profdev-cmte" => [ ["private", "professional-development-committee"] ],
"grant-review-cmte" => [ ["private", "grant-review-cmte"] ],
"amical-2018-joint-org" => [ ["private", "amical-2018-joint-org"] ],
"amical-2018-program-cmte" => [ ["private", "amical-2018-program-cmte"] ],
"dhsi-2018-cohort" => [ ["private", "dhsi-2018-cohort"] ],
"chairs" => [ ["private", "chairs"] ],
"facdevcenters" => [ ["private", "facdevcenters"] ]
# "everyone" makes every user watch the listed categories
# "everyone" => [ "announcements" ]
}
WatchCategory.change_notification_pref_for_group(groups_cats, :watching)
groups_cats = {
"infolit" => [ ["interest-groups", "information-literacy"] ],
"pedagogy" => [ ["interest-groups", "pedagogy"] ],
"open-source-library-systems" => [ ["interest-groups", "open-source-library-systems"] ],
"digital-literacy" => [ ["interest-groups", "digital-literacy"] ],
"coordinating-cmte" => [ "announcements" ],
"representatives" => [ "announcements" ],
"lib-buyers" => [ ["private", "buyers-group"] ]
}
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 1.hours
def execute(args)
WatchCategory.watch_category!
end
end
end
end