Skip to content

Commit

Permalink
Anti-Evil: move nofollow logic into a separate classmethod with a hook
Browse files Browse the repository at this point in the history
  • Loading branch information
KeyserSosa committed Feb 26, 2016
1 parent cb72d46 commit 1af69e4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
57 changes: 42 additions & 15 deletions r2/r2/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from printable import Printable
from r2.config import extensions
from r2.lib.memoize import memoize
from r2.lib.wrapped import Wrapped
from r2.lib.filters import _force_utf8, _force_unicode
from r2.lib import hooks, utils
from r2.lib.log import log_text
Expand Down Expand Up @@ -509,6 +510,22 @@ def _should_expunge_selftext(link):
return False
return True

@classmethod
def update_nofollow(cls, user, wrapped):
user_is_loggedin = c.user_is_loggedin
for item in wrapped:
if user_is_loggedin and item.author_id == user._id:
item.nofollow = False
elif item._spam or item.author._spam:
item.nofollow = True
else:
item.nofollow = False

hooks.get_hook('link.update_nofollow').call(
user=user,
wrapped=wrapped,
)

@classmethod
def add_props(cls, user, wrapped):
from r2.lib.pages import make_link_child
Expand Down Expand Up @@ -575,6 +592,9 @@ def add_props(cls, user, wrapped):

sticky_fullnames = site.get_sticky_fullnames()

# set the nofollow state where needed
cls.update_nofollow(user, wrapped)

for item in wrapped:
show_media = False
if not hasattr(item, "score_fmt"):
Expand Down Expand Up @@ -749,13 +769,6 @@ def add_props(cls, user, wrapped):
item.stickied = (not item.different_sr and
item._fullname in sticky_fullnames)

if user_is_loggedin and item.author_id == user._id:
item.nofollow = False
elif item.score <= 1 or item._spam or item.author._spam:
item.nofollow = True
else:
item.nofollow = False

item.subreddit_path = item.subreddit.path
item.domain_path = "/domain/%s/" % item.domain
if item.is_self:
Expand Down Expand Up @@ -1501,6 +1514,22 @@ def _qa(self, children, responder_ids):

return score

@classmethod
def update_nofollow(cls, user, wrapped):
user_is_loggedin = c.user_is_loggedin
for item in wrapped:
if user_is_loggedin and item.author_id == user._id:
item.nofollow = False
elif item._spam or item.link._spam or item.author._spam:
item.nofollow = True
else:
item.nofollow = False

hooks.get_hook("comment.update_nofollow").call(
user=user,
wrapped=wrapped,
)

@classmethod
def add_props(cls, user, wrapped):
from r2.lib.template_helpers import add_submitter_distinguish, get_domain
Expand Down Expand Up @@ -1572,18 +1601,16 @@ def add_props(cls, user, wrapped):
for item in wrapped:
# for caching:
item.profilepage = c.profilepage
item.link = links.get(item.link_id)
item.link = Wrapped(links.get(item.link_id))
item.link.author = authors.get(item.link.author_id)
item.show_admin_context = user_is_admin

if (item.link._score <= 1 or item.score < 3 or
item.link._spam or item._spam or item.author._spam):
item.nofollow = True
else:
item.nofollow = False

if not hasattr(item, 'subreddit'):
item.subreddit = item.subreddit_slow

cls.update_nofollow(user, wrapped)

for item in wrapped:
if item.author_id == item.link.author_id and not item.link._deleted:
add_submitter_distinguish(item.attribs, item.link, item.subreddit)

Expand Down Expand Up @@ -1671,7 +1698,7 @@ def add_props(cls, user, wrapped):
if profilepage:
item.nsfw = user.pref_label_nsfw and (item.link.is_nsfw or item.subreddit.over_18)

link_author = authors[item.link.author_id]
link_author = item.link.author
if ((item.link._deleted or link_author._deleted) and
not user_is_admin):
link_author = DeletedUser()
Expand Down
4 changes: 4 additions & 0 deletions r2/r2/models/printable.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class Printable(object):
'keep_item',
])

@classmethod
def update_nofollow(cls, user, wrapped):
pass

@classmethod
def add_props(cls, user, wrapped):
from r2.lib.wrapped import CachedVariable
Expand Down

0 comments on commit 1af69e4

Please sign in to comment.