1
0

notification_mailer.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. class NotificationMailer < ApplicationMailer
  3. helper :accounts
  4. helper :statuses
  5. helper RoutingHelper
  6. def mention(recipient, notification)
  7. @me = recipient
  8. @status = notification.target_status
  9. return unless @me.user.functional? && @status.present?
  10. locale_for_account(@me) do
  11. thread_by_conversation(@status.conversation)
  12. mail to: @me.user.email, subject: I18n.t('notification_mailer.mention.subject', name: @status.account.acct)
  13. end
  14. end
  15. def follow(recipient, notification)
  16. @me = recipient
  17. @account = notification.from_account
  18. return unless @me.user.functional?
  19. locale_for_account(@me) do
  20. mail to: @me.user.email, subject: I18n.t('notification_mailer.follow.subject', name: @account.acct)
  21. end
  22. end
  23. def favourite(recipient, notification)
  24. @me = recipient
  25. @account = notification.from_account
  26. @status = notification.target_status
  27. return unless @me.user.functional? && @status.present?
  28. locale_for_account(@me) do
  29. thread_by_conversation(@status.conversation)
  30. mail to: @me.user.email, subject: I18n.t('notification_mailer.favourite.subject', name: @account.acct)
  31. end
  32. end
  33. def reblog(recipient, notification)
  34. @me = recipient
  35. @account = notification.from_account
  36. @status = notification.target_status
  37. return unless @me.user.functional? && @status.present?
  38. locale_for_account(@me) do
  39. thread_by_conversation(@status.conversation)
  40. mail to: @me.user.email, subject: I18n.t('notification_mailer.reblog.subject', name: @account.acct)
  41. end
  42. end
  43. def follow_request(recipient, notification)
  44. @me = recipient
  45. @account = notification.from_account
  46. return unless @me.user.functional?
  47. locale_for_account(@me) do
  48. mail to: @me.user.email, subject: I18n.t('notification_mailer.follow_request.subject', name: @account.acct)
  49. end
  50. end
  51. def digest(recipient, **opts)
  52. return unless recipient.user.functional?
  53. @me = recipient
  54. @since = opts[:since] || [@me.user.last_emailed_at, (@me.user.current_sign_in_at + 1.day)].compact.max
  55. @notifications_count = Notification.where(account: @me, activity_type: 'Mention').where('created_at > ?', @since).count
  56. return if @notifications_count.zero?
  57. @notifications = Notification.where(account: @me, activity_type: 'Mention').where('created_at > ?', @since).limit(40)
  58. @follows_since = Notification.where(account: @me, activity_type: 'Follow').where('created_at > ?', @since).count
  59. locale_for_account(@me) do
  60. mail to: @me.user.email,
  61. subject: I18n.t(:subject, scope: [:notification_mailer, :digest], count: @notifications_count)
  62. end
  63. end
  64. private
  65. def thread_by_conversation(conversation)
  66. return if conversation.nil?
  67. msg_id = "<conversation-#{conversation.id}.#{conversation.created_at.strftime('%Y-%m-%d')}@#{Rails.configuration.x.local_domain}>"
  68. headers['In-Reply-To'] = msg_id
  69. headers['References'] = msg_id
  70. end
  71. end