Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause donations option for Goal #1999

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions emails/donations_paused.spt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ _("Liberapay donation paused") }}

[---] text/html
<p>{{ _("{0} has temporarily paused your donations to them.", recipient) }}</p>
4 changes: 4 additions & 0 deletions emails/donations_resumed.spt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ _("Liberapay donation resumed") }}

[---] text/html
<p>{{ _("{0} has resumed your donations to them.", recipient) }}</p>
15 changes: 14 additions & 1 deletion liberapay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2309,16 +2309,19 @@ def update_avatar(self, src=None, cursor=None, avatar_email=None, check=True):
def update_goal(self, goal, cursor=None):
with self.db.get_cursor(cursor) as c:
json = None if goal is None else str(goal)
donations_paused = goal == -2 and self.goal != -1
donations_resumed = goal != -1 and self.goal == -2
self.add_event(c, 'set_goal', json)
c.run("UPDATE participants SET goal=%s WHERE id=%s", (goal, self.id))
self.set_attributes(goal=goal)
if not self.accepts_tips:
if not self.accepts_tips or donations_resumed:
tippers = c.all("""
SELECT p
FROM current_tips t
JOIN participants p ON p.id = t.tipper
WHERE t.tippee = %s
""", (self.id,))
if not self.accepts_tips:
for tipper in tippers:
tipper.update_giving(cursor=c)
r = c.one("""
Expand All @@ -2329,6 +2332,16 @@ def update_goal(self, goal, cursor=None):
RETURNING receiving, npatrons
""", (self.id,))
self.set_attributes(**r._asdict())
if donations_paused or donations_resumed:
for tipper in tippers:
event = 'donations_paused' if donations_paused else 'donations_resumed'
tipper.send_email(
event,
tipper.get_email(tipper.get_email_address()),
recipient=self.username,
donations_paused=donations_paused,
donations_url=tipper.url('giving/')
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't send a potentially large number of emails while processing a user request. Firstly because the request will time out, secondly because it can allow an attacker to flood users with duplicate emails, and thirdly because the user should be able to reverse their decision before the emails are sent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be accomplished by using Participant's notify() method? Also when I switched to using notify(), the request seemed to get stuck. Do you know why this might be happening?

also for the last point

thirdly because the user should be able to reverse their decision before the emails are sent

If a decision is reversed, what should occur? Do the emails that were queued to send get dequeued?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The notify method should be used, but in a separate function which would be either a payday method or a cron job, like send_donation_reminder_notifications or send_account_disabled_notifications.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also when I switched to using notify(), the request seemed to get stuck. Do you know why this might be happening?

update_goal runs in a database transaction (which is confusingly called a "cursor" for legacy reasons), but notify doesn't, so if you tried to call notify from inside the with block you could have created a "deadlock" (two database workers trying to modify the same row, each one waiting for the other to complete its transaction before continuing).

If a decision is reversed, what should occur? Do the emails that were queued to send get dequeued?

No. Once the email is queued for sending we don't cancel it. What we do instead is delay the creation of the notification which results in the email being queued for sending. Donors don't need to be notified immediately that donations have been paused, this kind of information can be delayed for an hour or even a week.


def update_status(self, status, cursor=None):
with self.db.get_cursor(cursor) as c:
Expand Down
34 changes: 33 additions & 1 deletion tests/py/test_goal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from liberapay.testing import EUR, Harness

from liberapay.testing.emails import EmailHarness

class Tests(Harness):

Expand Down Expand Up @@ -62,3 +62,35 @@ def test_team_member_can_change_team_goal(self):
)
assert r.code == 302
assert team.refetch().goal == EUR('99.99')

class TestPauseDonations(EmailHarness):
def setUp(self):
EmailHarness.setUp(self)
self.alice = self.make_participant('alice', email='[email protected]')

def change_goal(self, goal, goal_custom="", auth_as="alice", expect_success=False):
r = self.client.PxST(
"/alice/edit/goal",
{'goal': goal, 'goal_custom': goal_custom},
auth_as=self.alice if auth_as == 'alice' else auth_as
)
if expect_success and r.code >= 400:
raise r
return r

def test_pause(self):
bob = self.make_participant('bob', email='[email protected]')
bob.set_tip_to(self.alice, EUR('0.99'), renewal_mode=2)
self.change_goal("-2", expect_success=True)

assert self.mailer.call_count == 1
last_email = self.get_last_email()
assert last_email['to'][0] == 'bob <[email protected]>'
assert "paused" in last_email['text']

self.change_goal("null", "", expect_success=True)

assert self.mailer.call_count == 2
last_email = self.get_last_email()
assert last_email['to'][0] == 'bob <[email protected]>'
assert "resume" in last_email['text']
19 changes: 14 additions & 5 deletions www/%username/edit/goal.spt
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ if request.method == "POST":
elif goal == "custom":
goal = request.body["goal_custom"]
goal = locale.parse_money_amount(goal, participant.main_currency)
if not (goal > 0 or participant.is_person and goal.amount in (0, -1)):
if not (goal > 0 or participant.is_person and goal.amount in (0, -1, -2)):
raise response.invalid_input(goal, 'goal_custom', 'body')
else:
goal = Money(request.body.get_int("goal"), participant.main_currency).round_down()
if not (goal > 0 or participant.is_person and goal.amount in (0, -1)):
if not (goal > 0 or participant.is_person and goal.amount in (0, -1, -2)):
raise response.invalid_input(goal, 'goal', 'body')
participant.update_goal(goal)
if goal != participant.goal:
participant.update_goal(goal)
form_post_success(state)

if participant.kind == 'individual':
GRATEFUL = _("I'm grateful for gifts, but don't have a specific funding goal.")
GRATEFUL_PAUSED_GIFTS = _("I'm grateful for gifts, but they are currently paused.")
PATRON = _("I'm here as a patron.")
PATRON_NO_GIFTS = _("I'm here as a patron, and politely decline to receive gifts.")
GOAL_RAW = _("My goal is to receive {0}")
else:
GRATEFUL = _("We're grateful for gifts, but don't have a specific funding goal.")
GRATEFUL_PAUSED_GIFTS = _("We're grateful for gifts, but they are currently paused.")
PATRON = _("We're here as a patron.")
PATRON_NO_GIFTS = _("We're here as a patron, and politely decline to receive gifts.")
GOAL_RAW = _("Our goal is to receive {0}")
Expand Down Expand Up @@ -67,15 +70,21 @@ subhead = _("Goal")
</label><br>

% if participant.kind != 'group'
<label>
<input type="radio" name="goal" id="goal--2" value="-2"
{{ 'checked' if participant.goal == -2 else '' }} />
{{ GRATEFUL_PAUSED_GIFTS }}
</label><br>

<label>
<input type="radio" name="goal" id="goal-0" value="0"
{{ 'checked' if participant.goal == 0 else '' }} />
{{ PATRON }}
</label><br>

<label>
<input type="radio" name="goal" id="goal-negative" value="-1"
{{ 'checked' if participant.goal and participant.goal < 0 else '' }} />
<input type="radio" name="goal" id="goal--1" value="-1"
{{ 'checked' if participant.goal == -1 else '' }} />
{{ PATRON_NO_GIFTS }}
</label>
% endif
Expand Down
4 changes: 4 additions & 0 deletions www/%username/giving/index.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ next_payday = compute_next_payday_date()
<p class="text-warning">{{ glyphicon('warning-sign') }} {{ _(
"Inactive because the account of the recipient is closed."
) }}</p>
% elif tippee.goal == -2
<p class="text-info">{{ glyphicon('pause') }} {{ _(
"This donation has been paused by the recipient."
) }}</p>
Changaco marked this conversation as resolved.
Show resolved Hide resolved
% elif not tippee.accepts_tips
<p class="text-warning">{{ glyphicon('warning-sign') }} {{ _(
"Inactive because the recipient no longer accepts donations."
Expand Down