Repository navigation
Add newsletter subscription to Communications settings - #2901
Conversation
Add a Communications settings tab where users can subscribe one of their verified email addresses to the newsletter. The subscription is stored on AssociatedEmail rather than Profile, so it is clear which address receives the newsletter and that it is verified. newsletter_subscribed_at records when consent was given and is kept after unsubscribing. Each address has a signed, non-expiring unsubscribe link that works without logging in. A GET shows a confirmation button, so mail scanners following the link do not unsubscribe anyone, and a POST without a CSRF token is accepted to support one-click unsubscribe (RFC 8058). The token carries a hash of the address rather than the address itself. Subscribers can be exported through the console users CSV and filtered in the AssociatedEmail admin. Also place the KHDP settings tab relative to ORCID rather than at a fixed index, which put it before ORCID when the Password tab was shown.
Chrystinne
left a comment
There was a problem hiding this comment.
@tompollard, LGTM. I checked the unsubscribe token (own salt, digest instead of address, constant-time compare, invalid once the address changes), that csrf_exempt only lets a valid token unsubscribe its own address, that the form only offers the user's own verified addresses, and that 0080 follows 0079 on dev with a deploy-safe db_default.
Suggestion before merge: a partial unique constraint on (user) where newsletter_subscribed=True would enforce the one-address rule in the database and make the CSV subquery deterministic.
Optional: mask the address on the public unsubscribe page. Approving
…e page Add a partial unique constraint on AssociatedEmail.user where newsletter_subscribed is true, so the database enforces what the form already does. The constraint goes in 0080, which has not been released. The unsubscribe page is public, so show a masked address (e.g. r***@example.com) rather than the full one.
|
Thanks @Chrystinne, I made both changes in 4602a9a:
Both have tests, and the full suite passes locally. |
Chrystinne
left a comment
There was a problem hiding this comment.
Thanks @tompollard, both changes look good. The partial unique constraint matches between the model and 0080, the form unsubscribes before subscribing inside one transaction so switching addresses doesn't trip it, and the CSV subqueries now return at most one row per user.
Non-blocking (UX): user/templates/user/edit_communications.html: while testing locally after verifying my email, it wasn't clear whether I was already subscribed. The intro ("You can unsubscribe at any time…") reads as if I already am, and the only indicator is the checkbox, whose label stays "Send me the newsletter" either way.
Could we add a status line above the form, e.g. "You are subscribed at x@example.com since 18 Nov 2026." / "You are not subscribed to the newsletter."? Rewording the intro to something like "If you subscribe, you can unsubscribe at any time…" would also help.
Add a status line above the form that shows the subscribed address and the date of subscription, or that the user is not subscribed. Reword the intro so it no longer reads as if the user is already subscribed.
|
Thanks @Chrystinne, good catch. Done in 77e3fba:
Tests cover both states. |
Thanks, @tompollard. Looks great now. I'll approve it once the tests have completed |
Adds a Communications tab to the user settings where people can opt in to the newsletter. Closes #2338.
This replaces the newsletter half of #2723, following @lukepayyapilli's review there. The reviewer-volunteer half is tracked separately in #2722.
Changes
AssociatedEmail(newsletter_subscribed,newsletter_subscribed_at), not onProfile, so it is clear which address gets the newsletter. Only verified addresses can be chosen, and a partial unique constraint ensures a user has at most one subscribed address.newsletter_subscribed_atrecords when the user opted in. Saving the settings again does not change it, and it is kept after unsubscribing as a record of the last consent./newsletter/unsubscribe/<token>/takes a signed token that does not expire. Opening the link shows a confirmation button instead of unsubscribing straight away, because mail scanners follow links automatically. A POST without a CSRF token is accepted so that one-click unsubscribe in mail clients (RFC 8058) works. The token carries a hash of the address rather than the address itself, and stops working if the address changes. Because the page is public, it shows a masked address (e.g.r***@example.com).newsletter_emailandnewsletter_subscribed_atcolumns, and theAssociatedEmailadmin can filter by subscription.0080) usesdb_default, so code that has not been updated yet can still insert email rows while it deploys. A separate null-then-backfill step is not needed.Sending the newsletter is out of scope. Whatever sends it should include each address's link from
get_newsletter_unsubscribe_token().Testing
user/test_views.py(TestNewsletter) andconsole/test_views.py. Theuserandconsoletest suites pass.@tompollard 🟪 purple 🦒 giraffe · ledger