Skip to content

Add newsletter subscription to Communications settings - #2901

Merged
tompollard merged 7 commits into
devfrom
tp/newsletter-subscription
Nov 19, 2026
Merged

tompollard merged 7 commits into
devfrom
tp/newsletter-subscription

Conversation

@tompollard

@tompollard tompollard commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

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

  • Subscription is stored per email address. It is held on AssociatedEmail (newsletter_subscribed, newsletter_subscribed_at), not on Profile, 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.
  • Consent date. newsletter_subscribed_at records 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.
  • Unsubscribe without logging in. /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).
  • Getting the list out. The console users CSV has new newsletter_email and newsletter_subscribed_at columns, and the AssociatedEmail admin can filter by subscription.
  • Migration. A single migration (0080) uses db_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.
  • KHDP tab. The KHDP settings tab is now placed after ORCID by name rather than at a fixed index. The fixed index put it before ORCID whenever the Password tab was shown.

Sending the newsletter is out of scope. Whatever sends it should include each address's link from get_newsletter_unsubscribe_token().

Testing

  • New tests in user/test_views.py (TestNewsletter) and console/test_views.py. The user and console test suites pass.
  • Checked by hand on a local dev server: subscribing, the settings page reloading with the saved choice, unsubscribing from the link, the page for an address that is already unsubscribed, and a tampered link.

@tompollard 🟪 purple 🦒 giraffe · ledger

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
Chrystinne self-requested a review November 18, 2026 21:35

@Chrystinne Chrystinne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@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.
@tompollard

tompollard commented Nov 18, 2026 •

Copy link
Copy Markdown
Member Author

Thanks @Chrystinne, I made both changes in 4602a9a:

  • Partial unique constraint. Added unique_newsletter_email_per_user on AssociatedEmail(user) where newsletter_subscribed=True. The form already unsubscribes the old address before subscribing the new one, all in one transaction, so switching addresses doesn't trip the constraint. The CSV subquery now has at most one row per user.
  • Masked address. The unsubscribe page now shows the first character, a fixed *** and the domain, e.g. r***@example.com, so the address length isn't revealed either.

Both have tests, and the full suite passes locally.

@Chrystinne Chrystinne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

image

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.
@tompollard

Copy link
Copy Markdown
Member Author

Thanks @Chrystinne, good catch. Done in 77e3fba:

  • Status line. Above the form, the page now shows either "You are subscribed at x@example.com since 18 Nov 2026." or "You are not subscribed to the newsletter." It reads the saved subscription, so it changes only after you save.
  • Intro. It now reads "If you subscribe, you can unsubscribe at any time, here or from the link in any newsletter."

Tests cover both states.

@Chrystinne

Copy link
Copy Markdown
Contributor

Thanks @Chrystinne, good catch. Done in 77e3fba:

  • Status line. Above the form, the page now shows either "You are subscribed at x@example.com since 18 Nov 2026." or "You are not subscribed to the newsletter." It reads the saved subscription, so it changes only after you save.
  • Intro. It now reads "If you subscribe, you can unsubscribe at any time, here or from the link in any newsletter."

Tests cover both states.

Thanks, @tompollard. Looks great now. I'll approve it once the tests have completed

@tompollard
tompollard added this pull request to the merge queue Nov 19, 2026
Merged via the queue into dev with commit 636ff33 Nov 19, 2026
9 checks passed
@tompollard
tompollard deleted the tp/newsletter-subscription branch November 19, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add communication/newsletter (or similar) page to settings

2 participants