user_mailer.rb 6.1 KB

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