Skip to content
Browse files

Set ratelimits on subreddits

  • Loading branch information...
1 parent 2f56f97 commit a8d55dd77a410251b6306fee8453be1827897e49 @kemitche kemitche committed Apr 27, 2012
Showing with 22 additions and 0 deletions.
  1. +5 −0 r2/example.ini
  2. +11 −0 r2/r2/controllers/api.py
  3. +1 −0 r2/r2/controllers/errors.py
  4. +4 −0 r2/r2/lib/app_globals.py
  5. +1 −0 r2/r2/models/subreddit.py
View
5 r2/example.ini
@@ -441,6 +441,11 @@ comment_visits_period = 600
#user-agents to rate-limit
agents =
+# subreddit ratelimits
+sr_banned_quota = 10000
+sr_moderator_quota = 10000
+sr_contributor_quota = 10000
+sr_quota_time = 7200
# -- email --
# smtp server
View
11 r2/r2/controllers/api.py
@@ -549,6 +549,17 @@ def POST_friend(self, form, jquery, ip, friend,
if (not c.user_is_admin
and (type in sr_types and not container.is_moderator(c.user))):
abort(403,'forbidden')
+
+ if type in sr_types and not c.user_is_admin:
+ quota_key = "sr%squota-%s" % (str(type), container._id36)
+ g.cache.add(quota_key, 0, time=g.sr_quota_time)
+ subreddit_quota = g.cache.incr(quota_key)
+ quota_limit = getattr(g, "sr_%s_quota" % type)
+ if subreddit_quota > quota_limit and container.use_quotas:
+ form.set_html(".status", errors.SUBREDDIT_RATELIMIT)
+ c.errors.add(errors.SUBREDDIT_RATELIMIT)
+ form.set_error(errors.SUBREDDIT_RATELIMIT, None)
+ return
# if we are (strictly) friending, the container
# had better be the current user.
View
1 r2/r2/controllers/errors.py
@@ -62,6 +62,7 @@
('BAD_SR_NAME', _('that name isn\'t going to work')),
('RATELIMIT', _('you are doing that too much. try again in %(time)s.')),
('QUOTA_FILLED', _("You've submitted too many links recently. Please try again in an hour.")),
+ ('SUBREDDIT_RATELIMIT', _("you are doing that too much. try again later.")),
('EXPIRED', _('your session has expired')),
('DRACONIAN', _('you must accept the terms first')),
('BANNED_IP', "IP banned"),
View
4 r2/r2/lib/app_globals.py
@@ -75,6 +75,10 @@ class Globals(object):
'min_membership_create_community',
'bcrypt_work_factor',
'cassandra_pool_size',
+ 'sr_banned_quota',
+ 'sr_moderator_quota',
+ 'sr_contributor_quota',
+ 'sr_quota_time',
],
ConfigValue.float: [
View
1 r2/r2/models/subreddit.py
@@ -79,6 +79,7 @@ class Subreddit(Thing, Printable):
link_flair_position = '', # one of ('', 'left', 'right')
flair_self_assign_enabled = False,
link_flair_self_assign_enabled = False,
+ use_quotas = True,
)
_essentials = ('type', 'name', 'lang')
_data_int_props = Thing._data_int_props + ('mod_actions', 'reported')

0 comments on commit a8d55dd

Please sign in to comment.
Something went wrong with that request. Please try again.