You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mastodon/app/views/user_mailer
ThibG 5524258da9
Fix “Email changed” notification sometimes having wrong e-mail (#13475)
* Fix “Email changed” notification sometimes having wrong e-mail

Fixes #6778

The root of the issue is that `send_devise_notification` was called before
the changes were properly commited to the database, causing the mailer to
pick previous values if running too early.

Devise's documentation provides guidance on how to handle that[1][2], however,
I have found it to not be working, as the following happens, in that order:
- `send_devise_notification` is called for the `email_changed` notification.
  In that case, `changed?` is false and `saved_changes?` is true, so
  if we use the former, we have the same issue.
- the `after_commit` hook is called
- `send_devise_notification` is called for the `confirmation_instructions`
  notification.
  In that case, `changed?` is still false, and `saved_changes?` still true,
  so if we use the latter, that second notification email is simply not
  going to be sent (as we would be queuing the notification *after*
  executing the after_commit hook).

This is because it may be called from either an `after_update` or
`after_commit` hook, the difference not being a call to `save` but the
transaction actually being committed to the database. This may arguably
be a bug in Devise, or Devise's notification.

The proposed workaround is inspired by Devise's documentation but checks
whether a transaction is open to make the call whether to immediately
send the notification or defer it to the `after_commit` hook.

[1]: https://www.rubydoc.info/github/plataformatec/devise/Devise%2FModels%2FAuthenticatable:send_devise_notification
[2]: 406915cb78/lib/devise/models/authenticatable.rb (L133-L194)

* Fix cases when sending notifications without changing the model

* Defer sending if and only if in transaction including current record
5 years ago
..
backup_ready.html.haml Bump webpacker from 3.5.5 to 4.0.2 (#10277) 6 years ago
backup_ready.text.erb Account archive download (#6460) 7 years ago
confirmation_instructions.html.haml Bump webpacker from 3.5.5 to 4.0.2 (#10277) 6 years ago
confirmation_instructions.text.erb Admission-based registrations mode (#10250) 6 years ago
email_changed.html.haml Fix “Email changed” notification sometimes having wrong e-mail (#13475) 5 years ago
email_changed.text.erb Fix “Email changed” notification sometimes having wrong e-mail (#13475) 5 years ago
password_change.html.haml Bump webpacker from 3.5.5 to 4.0.2 (#10277) 6 years ago
password_change.text.erb Make text e-mails consistent with HTML ones in UserMailer (#6291) 7 years ago
reconfirmation_instructions.html.haml Bump webpacker from 3.5.5 to 4.0.2 (#10277) 6 years ago
reconfirmation_instructions.text.erb Make text e-mails consistent with HTML ones in UserMailer (#6291) 7 years ago
reset_password_instructions.html.haml Bump webpacker from 3.5.5 to 4.0.2 (#10277) 6 years ago
reset_password_instructions.text.erb Make text e-mails consistent with HTML ones in UserMailer (#6291) 7 years ago
two_factor_disabled.html.haml Add password challenge to 2FA settings, e-mail notifications (#11878) 5 years ago
two_factor_disabled.text.erb Add password challenge to 2FA settings, e-mail notifications (#11878) 5 years ago
two_factor_enabled.html.haml Add password challenge to 2FA settings, e-mail notifications (#11878) 5 years ago
two_factor_enabled.text.erb Add password challenge to 2FA settings, e-mail notifications (#11878) 5 years ago
two_factor_recovery_codes_changed.html.haml Add password challenge to 2FA settings, e-mail notifications (#11878) 5 years ago
two_factor_recovery_codes_changed.text.erb Add password challenge to 2FA settings, e-mail notifications (#11878) 5 years ago
warning.html.haml Fix nil error in warning e-mail template (#11812) 5 years ago
warning.text.erb Fix error in user warning template (#11741) 5 years ago
welcome.html.haml Bump webpacker from 3.5.5 to 4.0.2 (#10277) 6 years ago
welcome.text.erb Remove links to bridge.joinmastodon.org (non-functional) (#9608) 6 years ago