From 0c89540ab78115a90018e5d9fd85e2f2080aadbc Mon Sep 17 00:00:00 2001 From: Max Goodman Date: Fri, 21 Mar 2014 16:48:23 -0400 Subject: [PATCH] Display trending subreddits on the front page. --- r2/example.ini | 2 + r2/r2/controllers/listingcontroller.py | 39 +++++++++++++++ r2/r2/lib/pages/pages.py | 18 ++++++- r2/r2/public/static/css/reddit.less | 54 +++++++++++++++++++++ r2/r2/public/static/js/analytics.js | 2 +- r2/r2/public/static/trending.png | Bin 0 -> 497 bytes r2/r2/templates/linklisting.html | 3 ++ r2/r2/templates/trendingsubredditsbar.html | 31 ++++++++++++ 8 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 r2/r2/public/static/trending.png create mode 100644 r2/r2/templates/trendingsubredditsbar.html diff --git a/r2/example.ini b/r2/example.ini index 86246f7c6..330168bf1 100644 --- a/r2/example.ini +++ b/r2/example.ini @@ -61,6 +61,8 @@ default_sr = reddit.com admin_message_acct = reddit # subreddit used for DMCA takedowns takedown_sr = _takedowns +# subreddit used for trending subreddits postings +trending_sr = trendingsubreddits # list of subreddits to auto-subscribe users to automatic_reddits = # special subreddit that only reddit gold subscribers can use diff --git a/r2/r2/controllers/listingcontroller.py b/r2/r2/controllers/listingcontroller.py index 370bc34a0..d0da3fce7 100755 --- a/r2/r2/controllers/listingcontroller.py +++ b/r2/r2/controllers/listingcontroller.py @@ -20,6 +20,7 @@ # Inc. All Rights Reserved. ############################################################################### +import re import urllib from oauth2 import require_oauth2_scope @@ -43,6 +44,7 @@ import r2.lib.search as search from r2.lib.template_helpers import add_sr from r2.lib.utils import iters, check_cheating, timeago from r2.lib import sup +from r2.lib.memoize import memoize from r2.lib.validator import * from r2.lib.butler import extract_user_mentions import socket @@ -302,6 +304,8 @@ class ListingWithPromos(ListingController): elif show_sponsors: spotlight = self.make_single_ad() + self.spotlight = spotlight + if spotlight: return PaneStack([spotlight, self.listing_obj], css_class='spacer') @@ -341,6 +345,41 @@ class HotController(ListingWithPromos): # no sticky or sticky hidden return c.site.get_links('hot', 'all') + subreddit_re = re.compile(r'/r/(\w+)') + + @classmethod + @memoize('trending_subreddits', time=5*60) + def trending_subreddits(cls): + subreddit = Subreddit._by_name(g.config['trending_sr']) + q = subreddit.get_links('new', 'all') + ids = list(q) + builder = IDBuilder(ids, skip=True, num=1) + posts = builder.get_items()[0] + if not posts: + return None + post = posts[0] + subreddits = cls.subreddit_re.findall(post.title) + + return { + 'subreddits': subreddits, + 'comment_url': post.permalink, + 'comment_count': post.num_comments, + } + + def content(self): + content = super(HotController, self).content() + if (c.render_style == "html" and isinstance(c.site, DefaultSR) and + not self.listing_obj.prev): + trending_info = self.trending_subreddits() + if trending_info: + return PaneStack(filter(None, [ + self.spotlight, + TrendingSubredditsBar(**trending_info), + self.listing_obj, + ]), css_class='spacer') + return content + + def title(self): return c.site.title diff --git a/r2/r2/lib/pages/pages.py b/r2/r2/lib/pages/pages.py index e8bc69c85..8690a804e 100644 --- a/r2/r2/lib/pages/pages.py +++ b/r2/r2/lib/pages/pages.py @@ -79,7 +79,13 @@ from r2.lib.strings import plurals, rand_strings, strings, Score from r2.lib.utils import title_to_url, query_string, UrlParser, vote_hash from r2.lib.utils import url_links_builder, make_offset_date, median, to36 from r2.lib.utils import trunc_time, timesince, timeuntil, weighted_lottery -from r2.lib.template_helpers import add_sr, get_domain, format_number, media_https_if_secure +from r2.lib.template_helpers import ( + add_sr, + get_domain, + format_number, + media_https_if_secure, + comment_label, +) from r2.lib.subreddit_search import popular_searches from r2.lib.log import log_text from r2.lib.memoize import memoize @@ -4371,3 +4377,13 @@ class ExploreItemListing(Templated): rec.sr = wrapped_srs[rec.sr._id] self.things.append(rec) Templated.__init__(self) + + +class TrendingSubredditsBar(Templated): + def __init__(self, subreddits, comment_url, comment_count): + Templated.__init__(self) + self.subreddits = subreddits + self.comment_url = comment_url + self.comment_count = comment_count + self.comment_label, self.comment_label_cls = \ + comment_label(comment_count) diff --git a/r2/r2/public/static/css/reddit.less b/r2/r2/public/static/css/reddit.less index 3aaa037e4..57f5c2d45 100755 --- a/r2/r2/public/static/css/reddit.less +++ b/r2/r2/public/static/css/reddit.less @@ -8324,3 +8324,57 @@ body.with-listing-chooser { font-style: italic; } } + +.trending-subreddits { + // required so that ex units are in sync with .midcol + font-size: small; + margin-top: -4px; + line-height: 1.75em; + + // the combined padding/margins of .link and .midcol + margin-left: 17px; + body.compressed-display & { + margin-left: 15px; + padding-left: 15px; + } + + strong, ul, a { + font-size: x-small; + } + + strong { + color: #29541c; + + &:before { + height: 14px; + width: 14px; + display: inline-block; + content: " "; + margin-right: 5px; + background-image: url(../trending.png); /* SPRITE */ + vertical-align: middle; + } + } + + ul { + display: inline; + } + + li { + display: inline-block; + margin-left: 0.5em; + } + + li:first-child { + margin-left: 0; + } + + ul, .comments { + margin-left: 1em; + } + + .comments { + color: #888; + font-weight: bold; + } +} diff --git a/r2/r2/public/static/js/analytics.js b/r2/r2/public/static/js/analytics.js index 9fc7f257b..814fd6620 100644 --- a/r2/r2/public/static/js/analytics.js +++ b/r2/r2/public/static/js/analytics.js @@ -170,7 +170,7 @@ r.analytics = { } r.analytics.breadcrumbs = { - selector: '.thing, .side, .sr-list, .srdrop, .tagline, .md, .organic-listing, .gadget, .sr-interest-bar, a, button, input', + selector: '.thing, .side, .sr-list, .srdrop, .tagline, .md, .organic-listing, .gadget, .sr-interest-bar, .trending-subreddits, a, button, input', maxLength: 3, sendLength: 2, diff --git a/r2/r2/public/static/trending.png b/r2/r2/public/static/trending.png new file mode 100644 index 0000000000000000000000000000000000000000..1661af703c586b1952eebdbd835708c432cce480 GIT binary patch literal 497 zcmV#fmCE60^M^P;ED`x7O@7IC8HQQl(A3yKcqxY+i zm|FAoN}5;%yc9OFDm1@TBm(eCnpl(yMNL*GgU%5oApv+x3qghw1Jm7eSd2D~?`2Zy zB+~y-RhfxV_XKs-4*hMQbr_4$#?8wGsdN$`7mjsIGUW0x+ZVuAVaI5wWXRM!y#ScYt<3bybCHTuVW=XWJmUN@mLHCK#t{g1ucEZs-PD^L)SDb=XhdS6 ztY&)E_8JfF+D<-|4YE~00000NkvXXu0mjfZGP3X literal 0 HcmV?d00001 diff --git a/r2/r2/templates/linklisting.html b/r2/r2/templates/linklisting.html index 955ee7f2f..f327ff245 100644 --- a/r2/r2/templates/linklisting.html +++ b/r2/r2/templates/linklisting.html @@ -29,5 +29,8 @@ body > .content .link .midcol { width: ${thing.midcol_width}ex } + .content .trending-subreddits { + padding-left: ${thing.midcol_width}ex + } <%include file="listing.html"/> diff --git a/r2/r2/templates/trendingsubredditsbar.html b/r2/r2/templates/trendingsubredditsbar.html new file mode 100644 index 000000000..0c86bfa40 --- /dev/null +++ b/r2/r2/templates/trendingsubredditsbar.html @@ -0,0 +1,31 @@ +## The contents of this file are subject to the Common Public Attribution +## License Version 1.0. (the "License"); you may not use this file except in +## compliance with the License. You may obtain a copy of the License at +## http://code.reddit.com/LICENSE. The License is based on the Mozilla Public +## License Version 1.1, but Sections 14 and 15 have been added to cover use of +## software over a computer network and provide for limited attribution for the +## Original Developer. In addition, Exhibit A has been modified to be +## consistent with Exhibit B. +## +## Software distributed under the License is distributed on an "AS IS" basis, +## WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for +## the specific language governing rights and limitations under the License. +## +## The Original Code is reddit. +## +## The Original Developer is the Initial Developer. The Initial Developer of +## the Original Code is reddit Inc. +## +## All portions of the code written by reddit are Copyright (c) 2006-2013 +## reddit Inc. All Rights Reserved. +############################################################################### + +