user_mailer.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # frozen_string_literal: true
  2. class UserMailer < Devise::Mailer
  3. layout 'mailer'
  4. helper :accounts
  5. helper :application
  6. helper :instance
  7. helper :statuses
  8. helper :formatting
  9. helper :routing
  10. before_action :set_instance
  11. default to: -> { @resource.email }
  12. def confirmation_instructions(user, token, *, **)
  13. @resource = user
  14. @token = token
  15. return unless @resource.active_for_authentication?
  16. I18n.with_locale(locale) do
  17. mail to: @resource.unconfirmed_email.presence || @resource.email,
  18. subject: I18n.t(@resource.pending_reconfirmation? ? 'devise.mailer.reconfirmation_instructions.subject' : 'devise.mailer.confirmation_instructions.subject', instance: @instance),
  19. template_name: @resource.pending_reconfirmation? ? 'reconfirmation_instructions' : 'confirmation_instructions'
  20. end
  21. end
  22. def reset_password_instructions(user, token, *, **)
  23. @resource = user
  24. @token = token
  25. return unless @resource.active_for_authentication?
  26. I18n.with_locale(locale) do
  27. mail subject: default_devise_subject
  28. end
  29. end
  30. def password_change(user, *, **)
  31. @resource = user
  32. return unless @resource.active_for_authentication?
  33. I18n.with_locale(locale) do
  34. mail subject: default_devise_subject
  35. end
  36. end
  37. def email_changed(user, *, **)
  38. @resource = user
  39. return unless @resource.active_for_authentication?
  40. I18n.with_locale(locale) do
  41. mail subject: default_devise_subject
  42. end
  43. end
  44. def two_factor_enabled(user, *, **)
  45. @resource = user
  46. return unless @resource.active_for_authentication?
  47. I18n.with_locale(locale) do
  48. mail subject: default_devise_subject
  49. end
  50. end
  51. def two_factor_disabled(user, *, **)
  52. @resource = user
  53. return unless @resource.active_for_authentication?
  54. I18n.with_locale(locale) do
  55. mail subject: default_devise_subject
  56. end
  57. end
  58. def two_factor_recovery_codes_changed(user, *, **)
  59. @resource = user
  60. return unless @resource.active_for_authentication?
  61. I18n.with_locale(locale) do
  62. mail subject: default_devise_subject
  63. end
  64. end
  65. def webauthn_enabled(user, *, **)
  66. @resource = user
  67. return unless @resource.active_for_authentication?
  68. I18n.with_locale(locale) do
  69. mail subject: default_devise_subject
  70. end
  71. end
  72. def webauthn_disabled(user, *, **)
  73. @resource = user
  74. return unless @resource.active_for_authentication?
  75. I18n.with_locale(locale) do
  76. mail subject: default_devise_subject
  77. end
  78. end
  79. def webauthn_credential_added(user, webauthn_credential)
  80. @resource = user
  81. @webauthn_credential = webauthn_credential
  82. return unless @resource.active_for_authentication?
  83. I18n.with_locale(locale) do
  84. mail subject: I18n.t('devise.mailer.webauthn_credential.added.subject')
  85. end
  86. end
  87. def webauthn_credential_deleted(user, webauthn_credential)
  88. @resource = user
  89. @webauthn_credential = webauthn_credential
  90. return unless @resource.active_for_authentication?
  91. I18n.with_locale(locale) do
  92. mail subject: I18n.t('devise.mailer.webauthn_credential.deleted.subject')
  93. end
  94. end
  95. def welcome(user)
  96. @resource = user
  97. return unless @resource.active_for_authentication?
  98. I18n.with_locale(locale) do
  99. mail subject: default_i18n_subject
  100. end
  101. end
  102. def backup_ready(user, backup)
  103. @resource = user
  104. @backup = backup
  105. return unless @resource.active_for_authentication?
  106. I18n.with_locale(locale) do
  107. mail subject: default_i18n_subject
  108. end
  109. end
  110. def warning(user, warning)
  111. @resource = user
  112. @warning = warning
  113. @statuses = @warning.statuses.includes(:account, :preloadable_poll, :media_attachments, active_mentions: [:account])
  114. I18n.with_locale(locale) do
  115. mail subject: I18n.t("user_mailer.warning.subject.#{@warning.action}", acct: "@#{user.account.local_username_and_domain}")
  116. end
  117. end
  118. def appeal_approved(user, appeal)
  119. @resource = user
  120. @appeal = appeal
  121. I18n.with_locale(locale) do
  122. mail subject: default_i18n_subject(date: l(@appeal.created_at))
  123. end
  124. end
  125. def appeal_rejected(user, appeal)
  126. @resource = user
  127. @appeal = appeal
  128. I18n.with_locale(locale) do
  129. mail subject: default_i18n_subject(date: l(@appeal.created_at))
  130. end
  131. end
  132. def suspicious_sign_in(user, remote_ip, user_agent, timestamp)
  133. @resource = user
  134. @remote_ip = remote_ip
  135. @user_agent = user_agent
  136. @detection = Browser.new(user_agent)
  137. @timestamp = timestamp.to_time.utc
  138. I18n.with_locale(locale) do
  139. mail subject: default_i18n_subject
  140. end
  141. end
  142. private
  143. def default_devise_subject
  144. I18n.t(:subject, scope: ['devise.mailer', action_name])
  145. end
  146. def set_instance
  147. @instance = Rails.configuration.x.local_domain
  148. end
  149. def locale
  150. @resource.locale.presence || I18n.locale || I18n.default_locale
  151. end
  152. end