user_mailer.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # frozen_string_literal: true
  2. class UserMailer < Devise::Mailer
  3. layout 'mailer'
  4. helper :accounts
  5. helper :application
  6. helper :formatting
  7. helper :instance
  8. helper :routing
  9. helper :statuses
  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. @suggestions = AccountSuggestions.new(@resource.account).get(5)
  99. @tags = Trends.tags.query.allowed.limit(5)
  100. @has_account_fields = @resource.account.display_name.present? || @resource.account.note.present? || @resource.account.avatar.present?
  101. @has_active_relationships = @resource.account.active_relationships.exists?
  102. @has_statuses = @resource.account.statuses.exists?
  103. I18n.with_locale(locale) do
  104. mail subject: default_i18n_subject
  105. end
  106. end
  107. def backup_ready(user, backup)
  108. @resource = user
  109. @backup = backup
  110. return unless @resource.active_for_authentication?
  111. I18n.with_locale(locale) do
  112. mail subject: default_i18n_subject
  113. end
  114. end
  115. def warning(user, warning)
  116. @resource = user
  117. @warning = warning
  118. @statuses = @warning.statuses.includes(:account, :preloadable_poll, :media_attachments, active_mentions: [:account])
  119. I18n.with_locale(locale) do
  120. mail subject: I18n.t("user_mailer.warning.subject.#{@warning.action}", acct: "@#{user.account.local_username_and_domain}")
  121. end
  122. end
  123. def appeal_approved(user, appeal)
  124. @resource = user
  125. @appeal = appeal
  126. I18n.with_locale(locale) do
  127. mail subject: default_i18n_subject(date: l(@appeal.created_at))
  128. end
  129. end
  130. def appeal_rejected(user, appeal)
  131. @resource = user
  132. @appeal = appeal
  133. I18n.with_locale(locale) do
  134. mail subject: default_i18n_subject(date: l(@appeal.created_at))
  135. end
  136. end
  137. def suspicious_sign_in(user, remote_ip, user_agent, timestamp)
  138. @resource = user
  139. @remote_ip = remote_ip
  140. @user_agent = user_agent
  141. @detection = Browser.new(user_agent)
  142. @timestamp = timestamp.to_time.utc
  143. I18n.with_locale(locale) do
  144. mail subject: default_i18n_subject
  145. end
  146. end
  147. def failed_2fa(user, remote_ip, user_agent, timestamp)
  148. @resource = user
  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 subject: default_i18n_subject
  155. end
  156. end
  157. private
  158. def default_devise_subject
  159. I18n.t(:subject, scope: ['devise.mailer', action_name])
  160. end
  161. def set_instance
  162. @instance = Rails.configuration.x.local_domain
  163. end
  164. def locale
  165. @resource.locale.presence || I18n.locale || I18n.default_locale
  166. end
  167. end