notification_mailer.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # frozen_string_literal: true
  2. class NotificationMailer < ApplicationMailer
  3. helper :statuses
  4. add_template_helper RoutingHelper
  5. def mention(recipient, notification)
  6. @me = recipient
  7. @status = notification.target_status
  8. return if @me.user.disabled? || @status.nil?
  9. locale_for_account(@me) do
  10. thread_by_conversation(@status.conversation)
  11. mail to: @me.user.email, subject: I18n.t('notification_mailer.mention.subject', name: @status.account.acct)
  12. end
  13. end
  14. def follow(recipient, notification)
  15. @me = recipient
  16. @account = notification.from_account
  17. return if @me.user.disabled?
  18. locale_for_account(@me) do
  19. mail to: @me.user.email, subject: I18n.t('notification_mailer.follow.subject', name: @account.acct)
  20. end
  21. end
  22. def favourite(recipient, notification)
  23. @me = recipient
  24. @account = notification.from_account
  25. @status = notification.target_status
  26. return if @me.user.disabled? || @status.nil?
  27. locale_for_account(@me) do
  28. thread_by_conversation(@status.conversation)
  29. mail to: @me.user.email, subject: I18n.t('notification_mailer.favourite.subject', name: @account.acct)
  30. end
  31. end
  32. def reblog(recipient, notification)
  33. @me = recipient
  34. @account = notification.from_account
  35. @status = notification.target_status
  36. return if @me.user.disabled? || @status.nil?
  37. locale_for_account(@me) do
  38. thread_by_conversation(@status.conversation)
  39. mail to: @me.user.email, subject: I18n.t('notification_mailer.reblog.subject', name: @account.acct)
  40. end
  41. end
  42. def follow_request(recipient, notification)
  43. @me = recipient
  44. @account = notification.from_account
  45. return if @me.user.disabled?
  46. locale_for_account(@me) do
  47. mail to: @me.user.email, subject: I18n.t('notification_mailer.follow_request.subject', name: @account.acct)
  48. end
  49. end
  50. def digest(recipient, **opts)
  51. return if recipient.user.disabled?
  52. @me = recipient
  53. @since = opts[:since] || [@me.user.last_emailed_at, (@me.user.current_sign_in_at + 1.day)].compact.max
  54. @notifications_count = Notification.where(account: @me, activity_type: 'Mention').where('created_at > ?', @since).count
  55. return if @notifications_count.zero?
  56. @notifications = Notification.where(account: @me, activity_type: 'Mention').where('created_at > ?', @since).limit(40)
  57. @follows_since = Notification.where(account: @me, activity_type: 'Follow').where('created_at > ?', @since).count
  58. locale_for_account(@me) do
  59. mail to: @me.user.email,
  60. subject: I18n.t(:subject, scope: [:notification_mailer, :digest], count: @notifications_count)
  61. end
  62. end
  63. private
  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