Save/unsave for comments.

This commit is contained in:
bsimpson63
2012-08-19 03:44:06 -04:00
committed by Max Goodman
parent 029890fdb4
commit 1f72971e5d
8 changed files with 56 additions and 1 deletions

View File

@@ -1731,6 +1731,7 @@ class ApiController(RedditController, OAuth2ResourceController):
@api_doc(api_section.links_and_comments)
def POST_save(self, thing):
if not thing: return
if isinstance(thing, Comment) and not c.user.gold: return
r = thing._save(c.user)
@noresponse(VUser(),

View File

@@ -1163,6 +1163,7 @@ class CommentPane(Templated):
likes = []
dislikes = []
is_friend = set()
saves = set()
for t in self.listing_iter(my_listing):
if not hasattr(t, "likes"):
# this is for MoreComments and MoreRecursion
@@ -1173,9 +1174,12 @@ class CommentPane(Templated):
likes.append(t._fullname)
if t.likes is False:
dislikes.append(t._fullname)
if t.saved:
saves.add(t._fullname)
self.rendered += ThingUpdater(likes = likes,
dislikes = dislikes,
is_friend = is_friend).render()
is_friend = is_friend,
saves = saves).render()
g.log.debug("using comment page cache")
else:
self.rendered = my_listing.render()

View File

@@ -149,6 +149,7 @@ class CommentButtons(PrintableButtons):
is_author = is_author,
profilepage = c.profilepage,
permalink = thing.permalink,
saved = thing.saved,
new_window = c.user.pref_newwindow,
full_comment_path = thing.full_comment_path,
deleted = thing.deleted,

View File

@@ -677,6 +677,12 @@ class Comment(Thing, Printable):
return (c, inbox_rel)
def _save(self, user):
CommentSavesByAccount._save(user, self)
def _unsave(self, user):
CommentSavesByAccount._unsave(user, self)
@property
def subreddit_slow(self):
from subreddit import Subreddit
@@ -758,6 +764,15 @@ class Comment(Thing, Printable):
cname = c.cname
site = c.site
if user_is_loggedin:
try:
saved = CommentSavesByAccount.fast_query(user, wrapped)
except tdb_cassandra.TRANSIENT_EXCEPTIONS as e:
g.log.warning("Cassandra comment save lookup failed: %r", e)
saved = {}
else:
saved = {}
for item in wrapped:
# for caching:
item.profilepage = c.profilepage
@@ -791,6 +806,10 @@ class Comment(Thing, Printable):
if item.link.promoted or age.days < g.REPLY_AGE_LIMIT:
item.can_reply = True
if user_is_loggedin:
item.saved = (user, item) in saved
else:
item.saved = False
# not deleted on profile pages,
# deleted if spam and not author or admin

View File

@@ -6252,3 +6252,8 @@ table.diff {font-size: small;}
.diff_add {background-color:lightgreen}
.diff_chg {background-color:yellow}
.diff_sub {background-color:lightcoral}
.buttons li.comment-save-button { display: none; }
.buttons li.comment-unsave-button { display: inline; }
body.gold .buttons li.comment-save-button { display: inline; }

View File

@@ -1307,6 +1307,17 @@ function show_unfriend(account_fullname) {
});
}
function show_saved(comment_fullname) {
var comment = $('.id-' + comment_fullname),
buttons = comment.find('.buttons').first(),
save = buttons.find('.comment-save-button')
form = '<li class="comment-unsave-button"><form action="/post/unsave" method="post" class="state-button unsave-button">'
form = form + '<input type="hidden" name="executed" value="unsaved"/><span>'
form = form + '<a href="javascript:void(0)" onclick="return change_state(this, \'unsave\', unsave_thing);">unsave</a></span></form></li>'
save.replaceWith(form)
comment.addClass('saved')
}
function search_feedback(elem, approval) {
f = $("form#search");
var q = f.find('input[name="q"]').val();

View File

@@ -251,6 +251,17 @@
<li class="first">
${self.bylink_button(_("permalink"), thing.permalink)}
</li>
%if thing.saved:
<li class="comment-unsave-button">
${self.state_button("unsave", _("unsave"), \
"return change_state(this, 'unsave', unsave_thing);", _("unsaved"))}
</li>
%else:
<li class="comment-save-button">
${self.state_button("save", _("save"), \
"return change_state(this, 'save', save_thing);", _("saved"))}
</li>
%endif
%if c.profilepage:
<li>
${self.bylink_button(_("context"), thing.permalink + "?context=3")}

View File

@@ -35,4 +35,7 @@
});
$.map(friends, show_friend);
var saves = ${unsafe(simplejson.dumps(list(thing.saves)))};
$.map(saves, show_saved);
</script>