diff --git a/r2/r2/controllers/api.py b/r2/r2/controllers/api.py index 51c289eb8..03ab5acf8 100644 --- a/r2/r2/controllers/api.py +++ b/r2/r2/controllers/api.py @@ -36,7 +36,8 @@ from r2.lib.utils import get_title, sanitize_url, timeuntil, set_last_modified from r2.lib.utils import query_string, to36, timefromnow, link_from_url from r2.lib.wrapped import Wrapped from r2.lib.pages import FriendList, ContributorList, ModList, \ - BannedList, BoringPage, FormPage, NewLink, CssError, UploadedImage + BannedList, BoringPage, FormPage, NewLink, CssError, UploadedImage, \ + ClickGadget from r2.lib.menus import CommentSortMenu from r2.lib.normalized_hot import expire_hot @@ -1369,19 +1370,10 @@ class ApiController(RedditController): if not links: return - def wrapper(link): - link.embed_voting_style = 'votable' - return Wrapped(link) + content = ClickGadget(links).make_content() - #this will disable the hardcoded widget styles - request.get.style = "off" - - c.render_style = 'htmllite' - builder = IDBuilder([ link._fullname for link in links ], - wrap = wrapper) - listing = LinkListing(builder, nextprev=False, show_nums=False).listing() jquery('.gadget').show().find('.click-gadget').html( - spaceCompress(listing.render())) + spaceCompress(content)) @noresponse() def POST_tb_commentspanel_show(self): diff --git a/r2/r2/controllers/reddit_base.py b/r2/r2/controllers/reddit_base.py index 0a6ab0a1e..fefc24a02 100644 --- a/r2/r2/controllers/reddit_base.py +++ b/r2/r2/controllers/reddit_base.py @@ -141,19 +141,25 @@ def set_user_cookie(name, val): c.cookies[uname + '_' + name] = Cookie(value = val) valid_click_cookie = re.compile(r'(:?t[0-9]+_[a-zA-Z0-9]+)+').match -def read_click_cookie(): - # not used at the moment, if you start using this, you should also - # test it +def set_recent_clicks(): + c.recent_clicks = [] + if not c.user_is_loggedin: + return + click_cookie = read_user_cookie('recentclicks') if click_cookie: if valid_click_cookie(click_cookie): - fullnames = [ x for x in UniqueIterator(click_cookie.split(':')) if x ] + names = [ x for x in UniqueIterator(click_cookie.split(':')) if x ] if len(click_cookie) > 1000: - fullnames = fullnames[:20] - set_user_cookie('recentclicks', ':'.join(fullnames)) - return fullnames + names = names[:20] + set_user_cookie('recentclicks', ':'.join(names)) + #eventually this will look at the user preference + names = names[:5] + c.recent_clicks = Link._by_fullname(names, data = True, + return_dict = False) else: + #if the cookie wasn't valid, clear it set_user_cookie('recentclicks', '') def read_mod_cookie(): @@ -497,6 +503,7 @@ class RedditController(BaseController): set_content_lang() set_colors() set_recent_reddits() + set_recent_clicks() # set some environmental variables in case we hit an abort if not isinstance(c.site, FakeSubreddit): diff --git a/r2/r2/lib/pages/pages.py b/r2/r2/lib/pages/pages.py index c2b4303ab..1e6cdb1dd 100644 --- a/r2/r2/lib/pages/pages.py +++ b/r2/r2/lib/pages/pages.py @@ -308,7 +308,36 @@ class Reddit(Wrapped): """returns a Wrapped (or renderable) item for the main content div.""" return self.content_stack(self.infobar, self.nav_menu, self._content) -class ClickGadget(Wrapped): pass +class ClickGadget(Wrapped): + def __init__(self, links, *a, **kw): + self.links = links + self.content = '' + if c.user_is_loggedin and self.links: + self.content = self.make_content() + Wrapped.__init__(self, *a, **kw) + + def make_content(self): + def wrapper(link): + link.embed_voting_style = 'votable' + return Wrapped(link) + + #temporarily change the render style + orig_render_style = c.render_style + c.render_style = 'htmllite' + + #this will disable the hardcoded widget styles + request.get.style = "off" + + builder = IDBuilder([link._fullname for link in self.links], + wrap = wrapper) + listing = LinkListing(builder, nextprev=False, show_nums=False).listing() + content = listing.render() + + #restore render style + c.render_style = orig_render_style + + return content + class RedditMin(Reddit): """a version of Reddit that has no sidebar, toolbar, footer, diff --git a/r2/r2/public/static/js/reddit.js b/r2/r2/public/static/js/reddit.js index b6fd47177..3fdd9f908 100644 --- a/r2/r2/public/static/js/reddit.js +++ b/r2/r2/public/static/js/reddit.js @@ -790,8 +790,12 @@ $(function() { .filter(".gray").removeClass("gray").attr("value", "") }); /* set cookies to be from this user if there is one */ - if(reddit.logged) + if (reddit.logged) { $.cookie_name_prefix(reddit.logged); + } + else { + populate_click_gadget(); + } /* set up the cookie domain */ $.default_cookie_domain(reddit.cur_domain.split(':')[0]); @@ -802,6 +806,6 @@ $(function() { /* visually mark the last-clicked entry */ last_click(); - populate_click_gadget(); + }); diff --git a/r2/r2/templates/clickgadget.html b/r2/r2/templates/clickgadget.html index 174e1a4ce..6af918b2b 100644 --- a/r2/r2/templates/clickgadget.html +++ b/r2/r2/templates/clickgadget.html @@ -22,11 +22,11 @@ <%namespace file="printable.html" import="simple_button" /> %if c.user.pref_clickgadget: -