user_mailer.rb 6.0 KB

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