notification_mailer.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. class NotificationMailer < ApplicationMailer
  3. helper :accounts,
  4. :statuses,
  5. :routing
  6. before_action :process_params
  7. before_action :set_status, only: [:mention, :favourite, :reblog]
  8. before_action :set_account, only: [:follow, :favourite, :reblog, :follow_request]
  9. after_action :set_list_headers!
  10. default to: -> { email_address_with_name(@user.email, @me.username) }
  11. layout 'mailer'
  12. def mention
  13. return unless @user.functional? && @status.present?
  14. locale_for_account(@me) do
  15. thread_by_conversation(@status.conversation)
  16. mail subject: default_i18n_subject(name: @status.account.acct)
  17. end
  18. end
  19. def follow
  20. return unless @user.functional?
  21. locale_for_account(@me) do
  22. mail subject: default_i18n_subject(name: @account.acct)
  23. end
  24. end
  25. def favourite
  26. return unless @user.functional? && @status.present?
  27. locale_for_account(@me) do
  28. thread_by_conversation(@status.conversation)
  29. mail subject: default_i18n_subject(name: @account.acct)
  30. end
  31. end
  32. def reblog
  33. return unless @user.functional? && @status.present?
  34. locale_for_account(@me) do
  35. thread_by_conversation(@status.conversation)
  36. mail subject: default_i18n_subject(name: @account.acct)
  37. end
  38. end
  39. def follow_request
  40. return unless @user.functional?
  41. locale_for_account(@me) do
  42. mail subject: default_i18n_subject(name: @account.acct)
  43. end
  44. end
  45. private
  46. def process_params
  47. @notification = params[:notification]
  48. @me = params[:recipient]
  49. @user = @me.user
  50. @type = action_name
  51. @unsubscribe_url = unsubscribe_url(token: @user.to_sgid(for: 'unsubscribe').to_s, type: @type)
  52. end
  53. def set_status
  54. @status = @notification.target_status
  55. end
  56. def set_account
  57. @account = @notification.from_account
  58. end
  59. def set_list_headers!
  60. headers['List-ID'] = "<#{@type}.#{@me.username}.#{Rails.configuration.x.local_domain}>"
  61. headers['List-Unsubscribe'] = "<#{@unsubscribe_url}>"
  62. headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click'
  63. end
  64. def thread_by_conversation(conversation)
  65. return if conversation.nil?
  66. msg_id = "<conversation-#{conversation.id}.#{conversation.created_at.strftime('%Y-%m-%d')}@#{Rails.configuration.x.local_domain}>"
  67. headers['In-Reply-To'] = msg_id
  68. headers['References'] = msg_id
  69. end
  70. end