Can reveal identity or write a message in gildings

This allows the option for non-anonymous gildings or to write a message.
Whether the user purchased anonymously or non-anonymously for gildings will
be remembered and applied to future gildings (initializes to False so it's
still anonymous until the user decides to change it).
This commit is contained in:
MelissaCole
2014-12-15 12:07:14 -08:00
parent e51d9f2f0f
commit 0055f9a6cd
7 changed files with 132 additions and 22 deletions

View File

@@ -97,7 +97,7 @@ from r2.lib.search import SearchQuery
from r2.controllers.oauth2 import require_oauth2_scope, allow_oauth2_access
from r2.lib.template_helpers import add_sr, get_domain, make_url_protocol_relative
from r2.lib.system_messages import notify_user_added
from r2.controllers.ipn import generate_blob
from r2.controllers.ipn import generate_blob, update_blob
from r2.lib.lock import TimeoutExpired
from r2.lib.csrf import csrf_exempt
@@ -4129,10 +4129,13 @@ class ApiController(RedditController):
jquery('#developed-app-%s .edit-app-icon-button'
% client._id).toggleClass('collapsed')
@json_validate(VUser(),
VModhash(),
thing=VByName("thing"))
def POST_generate_payment_blob(self, responder, thing):
@json_validate(
VUser(),
VModhash(),
thing=VByName("thing"),
signed=VBoolean("signed")
)
def POST_generate_payment_blob(self, responder, thing, signed):
if not thing:
abort(400, "Bad Request")
@@ -4157,12 +4160,31 @@ class ApiController(RedditController):
account_id=c.user._id,
account_name=c.user.name,
status="initialized",
signed=False,
signed=signed,
recipient=recipient.name,
giftmessage=None,
thing=thing._fullname,
))
@json_validate(
VUser(),
VModhash(),
code=nop("code"),
signed=VBoolean("signed", default=False),
message=nop("message")
)
def POST_modify_payment_blob(self, responder, code, signed, message):
if c.user.gild_reveal_username != signed:
c.user.gild_reveal_username = signed
c.user._commit()
updates = {}
updates["signed"] = signed
if message and message.strip() != "":
updates["giftmessage"] = message
update_blob(str(code), updates)
@csrf_exempt
@validate(srnames=VPrintable("srnames", max_length=2100))
def POST_request_promo(self, srnames):

View File

@@ -77,13 +77,15 @@ from r2.models import (
generate_token,
)
BLOB_TTL = 86400 * 30
stripe.api_key = g.secrets['stripe_secret_key']
def generate_blob(data):
passthrough = generate_token(15)
g.hardcache.set("payment_blob-" + passthrough,
data, 86400 * 30)
data, BLOB_TTL)
g.log.info("just set payment_blob-%s", passthrough)
return passthrough
@@ -98,9 +100,23 @@ def get_blob(code):
raise ValueError("payment_blob %s has status = %s" %
(code, blob.get('status', None)))
blob['status'] = "locked"
g.hardcache.set(key, blob, 86400 * 30)
g.hardcache.set(key, blob, BLOB_TTL)
return key, blob
def update_blob(code, updates=None):
blob = g.hardcache.get("payment_blob-%s" % code)
if not blob:
raise NotFound("No payment_blob-" + code)
if blob.get('account_id', None) != c.user._id:
raise ValueError("%s doesn't have access to payment_blob %s" %
(c.user._id, code))
for item, value in updates.iteritems():
blob[item] = value
g.hardcache.set("payment_blob-%s" % code, blob, BLOB_TTL)
def has_blob(custom):
if not custom:
return False
@@ -271,17 +287,20 @@ def send_gift(buyer, recipient, months, days, signed, giftmessage,
if not thing:
subject = _('Let there be gold! %s just sent you reddit gold!') % sender
message = strings.youve_got_gold % dict(sender=md_sender, amount=amount)
if giftmessage and giftmessage.strip():
message += "\n\n" + strings.giftgold_note + giftmessage + '\n\n----'
else:
url = thing.make_permalink_slow()
if isinstance(thing, Comment):
subject = _('Your comment has been gilded!')
message = strings.youve_been_gilded_comment % {'url': url}
message = strings.youve_been_gilded_comment
message %= {'sender': md_sender, 'url': url}
else:
subject = _('Your submission has been gilded!')
message = strings.youve_been_gilded_link % {'url': url}
message = strings.youve_been_gilded_link
message %= {'sender': md_sender, 'url': url}
if giftmessage and giftmessage.strip():
message += ("\n\n" + strings.giftgold_note +
_force_unicode(giftmessage) + '\n\n----')
message += '\n\n' + strings.gold_benefits_msg
if g.lounge_reddit:
@@ -424,7 +443,7 @@ class IpnController(RedditController):
form.find("button").hide()
payment_blob["status"] = "processed"
g.hardcache.set(blob_key, payment_blob, 86400 * 30)
g.hardcache.set(blob_key, payment_blob, BLOB_TTL)
if thing:
gilding_message = make_gold_message(thing, user_gilded=True)
@@ -626,7 +645,7 @@ class IpnController(RedditController):
g.log.error('finish: could not send system message')
payment_blob["status"] = "processed"
g.hardcache.set(blob_key, payment_blob, 86400 * 30)
g.hardcache.set(blob_key, payment_blob, BLOB_TTL)
class Webhook(object):

View File

@@ -133,8 +133,8 @@ string_dict = dict(
over_comment_limit_gold = _("Sorry, the maximum number of comments is %d."),
youve_got_gold = _("%(sender)s just gifted you %(amount)s of reddit gold!"),
giftgold_note = _("Here's a note that was included:\n\n----\n\n"),
youve_been_gilded_comment = _("Another user liked [your comment](%(url)s) so much that they gilded it, giving you reddit gold.\n\n"),
youve_been_gilded_link = _("Another user liked [your submission](%(url)s) so much that they gilded it, giving you reddit gold.\n\n"),
youve_been_gilded_comment = _("%(sender)s liked [your comment](%(url)s) so much that they gilded it, giving you reddit gold.\n\n"),
youve_been_gilded_link = _("%(sender)s liked [your submission](%(url)s) so much that they gilded it, giving you reddit gold.\n\n"),
respond_to_anonymous_gilder = _("Want to say thanks to your mysterious benefactor? Reply to this message. You will find out their username if they choose to reply back."),
unsupported_respond_to_gilder = _("Sorry, replying directly to your mysterious benefactor is not yet supported for this gilding."),
anonymous_gilder_warning = _("***WARNING: Responding to this message will reveal your username to the gildee.***\n\n"),

View File

@@ -132,6 +132,10 @@ class Account(Thing):
pref_hide_locationbar=False,
pref_creddit_autorenew=False,
update_sent_messages=True,
num_payment_methods=0,
num_failed_payments=0,
pref_show_snoovatar=False,
gild_reveal_username=False,
)
_preference_attrs = tuple(k for k in _defaults.keys()
if k.startswith("pref_"))

View File

@@ -2,11 +2,26 @@ r.gold = {
_googleCheckoutAnalyticsLoaded: false,
init: function () {
$('div.content').on(
'click',
'[name="message"]',
this._toggleGiftMessage.bind(this)
);
$('div.content').on(
'click',
'a.give-gold, .gold-payment .close-button',
$.proxy(this, '_toggleThingGoldForm')
)
this._toggleThingGoldForm.bind(this)
);
// this fires when any of the checkout buttons are clicked
// updates the signed and giftmessage properties in the payment_blob
// failures should be rare and it's probably safe for any updates to be lost
$('div.content').on(
'click',
'.gold-button',
this._setGildingProperties.bind(this)
);
$('.stripe-gold').click(function(){
$("#stripe-payment").slideToggle()
@@ -47,6 +62,15 @@ r.gold = {
})
},
_toggleGiftMessage: function(e){
var messageCheckbox = e.target;
var includeMsg = messageCheckbox.checked;
var giftmessage_id = $(e.target).parents('.gold-form').find('[name="giftmessage"]').attr('id');
var $form = $('#' + giftmessage_id);
$form.toggleClass('hidden', !includeMsg);
},
_toggleThingGoldForm: function (e) {
var $link = $(e.target)
var $thing = $link.thing()
@@ -85,6 +109,7 @@ r.gold = {
var authorName = $link.thing().find('.entry .author:first').text()
var passthroughs = form.find('.passthrough')
var cbBaseUrl = form.find('[name="cbbaseurl"]').val()
var signed = !(form.find('[name="signed"]')).is(':checked');
goldwrap
.removeClass(cloneClass)
@@ -103,7 +128,7 @@ r.gold = {
form.find('button').addClass('disabled')
}, 200)
$.request('generate_payment_blob.json', {thing: thingFullname}, function (token) {
$.request('generate_payment_blob.json', {thing: thingFullname, signed: signed}, function (token) {
clearTimeout(workingTimer)
form.removeClass('working')
passthroughs.val(token)
@@ -115,6 +140,30 @@ r.gold = {
return false
},
_setGildingProperties: function (e) {
var $button = $(e.target);
var thingFullname = $button.thing_id();
// If /gold, then don't set signed and message properties
if (!thingFullname) {
return;
}
var wrapId = 'gold_wrap_' + thingFullname;
var $goldwrap = $('#' + wrapId);
var passthroughs = $goldwrap.find('.passthrough');
var code = passthroughs.val();
var signed = !$goldwrap.find('[name="signed"]').is(':checked');
var includeMsg = $goldwrap.find('[name="message"]').is(':checked');
var giftmessage = "";
if (includeMsg) {
giftmessage = ($goldwrap.find('[name="giftmessage"]')).val();
}
$.request('modify_payment_blob.json', {code: code, signed: signed, message: giftmessage})
},
// When spending creddits, update the templates we use to generate the gilding form to display the
// new total creddits remaining, or hide it if we have less than the current cost of gilding (1 creddit).
_expendCreddits: function() {

View File

@@ -184,7 +184,7 @@
</label>
</li>
<li>
<textarea rows="5" cols="30" name="giftmessage" id="giftmessage" placeholder="${_('enter your message')}" class="giftmessage">${thing.giftmessage}</textarea>
<textarea rows="5" cols="30" name="giftmessage" id="giftmessage" placeholder="${_('enter your message')}" class="giftmessage" maxlength="500">${thing.giftmessage}</textarea>
</li>
</ul>
</div>

View File

@@ -63,7 +63,21 @@
%endif
</div>
</div>
<div class="buttons">
%if thing.clone_template:
<ul class="indent">
<li>
<input type="checkbox" id="signed-false" name="signed" ${'checked' if not c.user.gild_reveal_username else ''}>${_('make my gift anonymous')}
</li>
<li>
<input type="checkbox" id="message" name="message">${_('include a message')}
</li>
<li>
<textarea rows="3" cols="50" id="giftmessage" name="giftmessage" placeholder="${_('enter your message')}" class="hidden giftmessage" maxlength="500">${thing.giftmessage}</textarea>
</li>
</ul>
%endif
<div class="buttons">
<p>${_("Please select a payment method.")}</p>
%if thing.can_use_creddits:
@@ -133,6 +147,7 @@
<%def name="stripe_button()">
<span class="gold-checkout">
<button class="btn stripe-gold gold-button">${_('Credit Card')}</button>
<input type="hidden" name="custom" value="${thing.passthrough}" class="passthrough">
</span>
</%def>
@@ -232,6 +247,7 @@
%endif
>${_('Bitcoin')}</button>
<input type="hidden" name="cbbaseurl" value="https://coinbase.com/checkouts/${thing.coinbase_button_id}">
<input type="hidden" name="custom" value="${thing.passthrough}" class="passthrough">
</%def>
% if not thing.clone_template: