From 0ec2cb39d703e8252421e000a30d87157181a8da Mon Sep 17 00:00:00 2001 From: spez Date: Fri, 15 May 2009 16:36:55 -0700 Subject: [PATCH] update the choosing of the reddits to fix the empty-next-page bug --- r2/r2/controllers/listingcontroller.py | 3 +-- r2/r2/models/subreddit.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/r2/r2/controllers/listingcontroller.py b/r2/r2/controllers/listingcontroller.py index a198667c9..ef67d1c55 100644 --- a/r2/r2/controllers/listingcontroller.py +++ b/r2/r2/controllers/listingcontroller.py @@ -224,8 +224,7 @@ class HotController(FixListing, ListingController): self.fix_listing = False if c.site == Default: - sr_ids = Subreddit.user_subreddits(c.user, - limit = g.num_default_reddits) + sr_ids = Subreddit.user_subreddits(c.user) return normalized_hot(sr_ids) #if not using the query_cache we still want cached front pages elif (not g.use_query_cache diff --git a/r2/r2/models/subreddit.py b/r2/r2/models/subreddit.py index 634de83f2..af230aa55 100644 --- a/r2/r2/models/subreddit.py +++ b/r2/r2/models/subreddit.py @@ -323,6 +323,14 @@ class Subreddit(Thing, Printable): srs = cls.top_lang_srs(c.content_langs, limit) return [s._id for s in srs] if ids else srs + @classmethod + @memoize('random_reddits', time = 1800) + def random_reddits(cls, user_name, sr_ids, limit): + """This gets called when a user is subscribed to more than 50 + reddits. Randomly choose 50 of those reddits and cache it for + a while so their front page doesn't jump around.""" + return random.sample(sr_ids, limit) + @classmethod def user_subreddits(cls, user, ids = True, limit = sr_limit): """ @@ -336,7 +344,8 @@ class Subreddit(Thing, Printable): if user and user.has_subscribed: sr_ids = Subreddit.reverse_subscriber_ids(user) if limit and len(sr_ids) > limit: - sr_ids = random.sample(sr_ids, limit) + sr_ids.sort() + sr_ids = cls.random_reddits(user.name, sr_ids, limit) return sr_ids if ids else Subreddit._byID(sr_ids, True, False) else: # if there is a limit, we want *at most* limit subreddits.