glitch-social/app
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
2020-04-15 16:13:44 +02:00
..
chewy Fix bookmarks also searchable (#13293) 2020-03-22 02:40:03 +01:00
controllers Add rate limit for reporting (#13390) 2020-04-05 14:40:08 +02:00
helpers Add ability to filter audit log in admin UI (#13381) 2020-04-03 13:06:34 +02:00
javascript Fix WebUI pagination of following, followers, follow requests, blocks and mutes lists (#13445) 2020-04-12 13:38:00 +02:00
lib Add rate limit for reporting (#13390) 2020-04-05 14:40:08 +02:00
mailers Fix error when sending moderation notification (#13014) 2020-02-01 15:42:12 +01:00
models Fix “Email changed” notification sometimes having wrong e-mail (#13475) 2020-04-15 16:13:44 +02:00
policies Add ability to delete files uploaded for settings in admin UI (#13192) 2020-03-08 16:00:24 +01:00
presenters Fix n+1 query for bookmarks on statuses (#12494) 2019-11-28 04:08:00 +01:00
serializers Change local media attachments to perform heavy processing asynchronously (#13210) 2020-03-08 23:56:18 +01:00
services Fix returning results when searching for URL with non-zero offset (#13377) 2020-04-02 03:39:37 +02:00
validators Improve polls: option lengths & redesign (#13257) 2020-04-02 17:10:55 +02:00
views Fix “Email changed” notification sometimes having wrong e-mail (#13475) 2020-04-15 16:13:44 +02:00
workers Fix trying to delete already deleted file when post-processing (#13406) 2020-04-06 14:11:22 +02:00