-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin.rb
executable file
·100 lines (79 loc) · 3.16 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
# name: flexible-rate-limits
# version: 0.2.1
# author: Muhlis Budi Cahyono ([email protected])
# url: https://github.com/ryanerwin/discourse-flexible-rate-limits
enabled_site_setting :flexible_rate_limits_enabled
%i(common desktop mobile).each do |layout|
register_asset "stylesheets/flexible-rate-limits/#{layout}.scss", layout
end
add_admin_route "flexible_rate_limits.admin.nav_label", "flexible-rate-limits"
require_relative "lib/flexible_rate_limits"
after_initialize {
load File.expand_path("../lib/controllers/flexible_rate_limits_controller.rb", __FILE__)
load File.expand_path("../lib/controllers/admin/flexible_rate_limits_controller.rb", __FILE__)
Discourse::Application.routes.append {
scope("flexible_rate_limits") {
post "/:category_id/:limits_type" => "flexible_rate_limits#index"
}
scope "/admin/plugins/flexible-rate-limits", constraints: AdminConstraint.new do
get "" => "admin/flexible_rate_limits#index"
post "save" => "admin/flexible_rate_limits#save"
end
}
require_dependency "topic"
Topic.class_eval {
alias_method :orig_limit_topics_per_day, :limit_topics_per_day
def limit_topics_per_day
if user && user.new_user_posting_on_first_day?
limit_first_day_topics_per_day
else
if SiteSetting.flexible_rate_limits_enabled
frl = FlexibleRateLimits.new(self.user, self.category_id)
if frl.category_group_name
RateLimiter.new(self.user, "cg-#{frl.category_group_name}-topic", frl.topic_limit, 1.day.to_i)
else
apply_per_day_rate_limit_for("topics", :max_topics_per_day)
end
else
apply_per_day_rate_limit_for("topics", :max_topics_per_day)
end
end
end
}
require_dependency "post"
Post.class_eval {
alias_method :orig_limit_posts_per_day, :limit_posts_per_day
def limit_posts_per_day
return if !user
return if (!post_number || post_number <= 1)
return orig_limit_posts_per_day if !SiteSetting.flexible_rate_limits_enabled
return default_limit if user.new_user_posting_on_first_day?
frl = FlexibleRateLimits.new(self.user, self.topic&.category_id) # int or nil
if frl.category_group_name
RateLimiter.new(self.user, "cg-#{frl.category_group_name}-post", frl.post_limit, 1.day.to_i)
end
end
def default_limit
RateLimiter.new(user, "first-day-replies-per-day", SiteSetting.max_replies_in_first_day, 1.day.to_i)
end
}
require_dependency "rate_limiter"
RateLimiter.class_eval {
alias_method :orig_remaining, :remaining
def remaining
return orig_remaining if !SiteSetting.flexible_rate_limits_enabled
arr = redis.lrange(prefixed_key, 0, @max) || []
t0 = Time.now.to_i
arr.reject! { |a| (t0 - a.to_i) > @secs }
@max - arr.size
end
alias_method :orig_rate_unlimited?, :rate_unlimited?
def rate_unlimited?
return orig_rate_unlimited? if !SiteSetting.flexible_rate_limits_enabled
!!RateLimiter.disabled?
end
def wait_seconds
seconds_to_wait
end
}
}