Subreddit.random_reddits(): fix logic error

The logic of this code contained a couple subtle errors that could cause
strange behavior. In reddit's current state of having two "automatic
subreddits" (which are always included in the front page set, and not
counted towards the limit), the fact that the automatic_ids list could
have an item removed while being iterated over meant that unsubscribing
from the first automatic subreddit (/r/blog) made it so that it was
effectively impossible to unsubscribe from the second one
(/r/announcements). If you unsubscribed, it would still be present in
your front page regardless, and if you stayed subscribed it would
actually be present twice.
This commit is contained in:
Chad Birch
2015-01-12 13:21:05 -07:00
parent 5c4f80d6ee
commit 6b51528648
2 changed files with 8 additions and 10 deletions

View File

@@ -86,7 +86,7 @@ admin_message_acct = reddit
takedown_sr = _takedowns
# subreddit used for trending subreddits postings. Ignored if blank.
trending_sr =
# list of subreddits to auto-subscribe users to
# list of subreddits to always include in a user's front page (unless they unsubscribe)
automatic_reddits =
# special subreddit that only reddit gold subscribers can use
lounge_reddit =

View File

@@ -791,23 +791,21 @@ class Subreddit(Thing, Printable, BaseSite):
"""Select a random subset from sr_ids.
Used for limiting the number of subscribed subreddits shown on a user's
front page. Subreddits that are automatically subscribed aren't counted
against the limit. Selection is cached for a while so the front page
doesn't jump around.
front page. Selection is cached for a while so the front page doesn't
jump around.
"""
if not limit:
return sr_ids
if g.automatic_reddits and len(sr_ids) > limit:
# if the user is subscribed to them, the automatic subreddits should
# always be in the front page set and not count towards the limit
if g.automatic_reddits:
automatics = Subreddit._by_name(g.automatic_reddits).values()
automatic_ids = [sr._id for sr in automatics]
automatic_ids = [sr._id for sr in automatics if sr._id in sr_ids]
for sr_id in automatic_ids:
try:
sr_ids.remove(sr_id)
except ValueError:
automatic_ids.remove(sr_id)
sr_ids.remove(sr_id)
else:
automatic_ids = []