notification_mailer.rb 2.3 KB

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