forked from tyrbo/discourse-flexible-rate-limits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
executable file
·76 lines (60 loc) · 2.57 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
# 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, frl.cooldown.minutes.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, frl.cooldown.minutes.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
}
}