-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
157 lines (117 loc) · 4.4 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# coding: utf-8
# name: thrive-discourse
# about: discourse customizations for thrive community
# version: 1.0
# authors: Henri Hyyryläinen
# url: https://github.com/Revolutionary-Games/thrive-discourse
enabled_site_setting :thrive_groups_enabled
module ThriveAssignGroupPlugin
# Updates a single user's group. Called once per day on each user (that has more than 0 posts)
# or whenever an event is triggered that warrants re-checking (like posting)
# user needs to be an instance of User class
def self.verifyUserGroup(user)
# Skip system users
if user.id < 1
return
end
Rails.logger.debug "Verifying user group for #{user.username} (#{user.id})"
# There doesn't seem to be a better place to do this than here
groups = SiteSetting.thrive_groups_post_groups.split '|'
required_posts = SiteSetting.thrive_groups_required_post_counts.split('|').map(&:to_i)
required_times = SiteSetting.thrive_groups_required_read_time.split('|').map(&:to_i)
# Throw if invalid settings
if groups.length != required_posts.length || groups.length != required_times.length
Rails.logger.error "Invalid thrive_groups settings! All the lists should be " +
"the same length"
return
end
# Skip if user not part of any of the current groups (if they are in
# a group to skip overwriting custom ones)
if user.primary_group != nil
begin
primary_group = Group.find(user.primary_group_id)
rescue ActiveRecord::RecordNotFound
primary_group = nil
end
else
primary_group = nil
end
if primary_group
if !groups.include?(primary_group.name)
Rails.logger.debug "Skipping user that has some weird primary group"
return
end
end
stats = user.user_stat
# Perhaps newly registered?
if stats
users_posts = stats.post_count + stats.topic_count
users_time_read = stats.time_read / 60
else
users_posts = 0
users_time_read = 0
end
Rails.logger.debug "With #{users_posts} posts " +
"and #{users_time_read} read time"
# Check which group they should be in
should_be_group = nil
for i in 0..groups.length - 1
if users_posts < required_posts[i] || users_time_read < required_times[i]
break
end
# Should be at least in this group
should_be_group = groups[i]
end
if !should_be_group
Rails.logger.warn "Didn't find a group for user"
return
end
Rails.logger.debug "Making sure #{user.username}'s primary group " +
"is '#{should_be_group}'"
if !primary_group || primary_group.name != should_be_group
new_group = Group.find_by name: should_be_group
if !new_group
Rails.logger.error "Invalid thrive_groups settings! Group with name " +
"'#{should_be_group}' doesn't exist!"
return
end
if primary_group
# Remove from old group
primary_group.remove(user)
Rails.logger.debug "Removed user from previous group"
end
begin
new_group.add(user)
rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
# we don't care about this
end
Rails.logger.debug "Updated user's group"
end
end
end
after_initialize do
return unless SiteSetting.thrive_groups_enabled
# Register events
DiscourseEvent.on(:post_created){|post, opts, user|
ThriveAssignGroupPlugin.verifyUserGroup user
}
DiscourseEvent.on(:user_first_logged_in){|user|
ThriveAssignGroupPlugin.verifyUserGroup user
}
# This approach is partly copied from trust-level-groups plugin
class ::Jobs::PostAmountGroupsMembership < Jobs::Scheduled
every SiteSetting.thrive_groups_post_amount_full_verify_minutes.minute
#every 20
Rails.logger.info "PostAmountGroupsMembership full verification running every " +
SiteSetting.thrive_groups_post_amount_full_verify_minutes.to_s +
" minutes"
def execute(args)
Rails.logger.debug "Running PostAmountGroupsMembership"
# Only process people who have posted at some point
User.where("id > 0 AND last_posted_at IS NOT NULL").find_each do |user|
Rails.logger.debug "Processing user '#{user.username}'"
ThriveAssignGroupPlugin.verifyUserGroup user
end
end
end
end