Add hooks into comment/vote endpoints.

This stuff is needed for the server naming subreddit overrides.

Would love to see it replaced with custom Thing subclasses in the
future.
This commit is contained in:
Neil Williams
2013-11-04 11:30:41 -08:00
parent 633ed54a2d
commit 817f8d66c2
3 changed files with 25 additions and 0 deletions

View File

@@ -1355,6 +1355,9 @@ class ApiController(RedditController, OAuth2ResourceController):
if not link.promoted and parent_age.days > g.REPLY_AGE_LIMIT:
c.errors.add(errors.TOO_OLD, field = "parent")
hooks.get_hook("comment.validate").call(sr=sr, link=link,
parent_comment=parent_comment)
#remove the ratelimit error if the user's karma is high
if not should_ratelimit:
c.errors.remove((errors.RATELIMIT, 'ratelimit'))
@@ -1550,6 +1553,8 @@ class ApiController(RedditController, OAuth2ResourceController):
if not thing or thing._deleted:
return
hooks.get_hook("vote.validate").call(thing=thing)
if vote_info == 'rejected':
reject_vote(thing)
store = False

View File

@@ -43,6 +43,19 @@ class Hook(object):
"""
return [handler(**kwargs) for handler in self.handlers]
def call_until_return(self, **kwargs):
"""Call handlers until one returns a non-None value.
As with call, handlers are called in the same order they are
registered. Only the return value of the first non-None handler is
returned.
"""
for handler in self.handlers:
ret = handler(**kwargs)
if ret is not None:
return ret
def get_hook(name):
"""Return the named hook `name` creating it if necessary."""

View File

@@ -50,6 +50,7 @@ from r2.models.wiki import WikiPage
from r2.lib.merge import ConflictException
from r2.lib.cache import CL_ONE
from r2.lib.contrib.rcssmin import cssmin
from r2.lib import hooks
from r2.models.query_cache import MergedCachedQuery
import pycassa
@@ -423,6 +424,12 @@ class Subreddit(Thing, Printable, BaseSite):
def can_comment(self, user):
if c.user_is_admin:
return True
override = hooks.get_hook("subreddit.can_comment").call_until_return(
sr=self, user=user)
if override is not None:
return override
elif self.is_banned(user):
return False
elif self.type == 'gold_restricted' and user.gold: