notification.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: notifications
  5. #
  6. # id :bigint(8) not null, primary key
  7. # activity_id :bigint(8) not null
  8. # activity_type :string not null
  9. # created_at :datetime not null
  10. # updated_at :datetime not null
  11. # account_id :bigint(8) not null
  12. # from_account_id :bigint(8) not null
  13. #
  14. class Notification < ApplicationRecord
  15. include Paginable
  16. include Cacheable
  17. TYPE_CLASS_MAP = {
  18. mention: 'Mention',
  19. reblog: 'Status',
  20. follow: 'Follow',
  21. follow_request: 'FollowRequest',
  22. favourite: 'Favourite',
  23. poll: 'Poll',
  24. }.freeze
  25. STATUS_INCLUDES = [:account, :application, :preloadable_poll, :media_attachments, :tags, active_mentions: :account, reblog: [:account, :application, :preloadable_poll, :media_attachments, :tags, active_mentions: :account]].freeze
  26. belongs_to :account, optional: true
  27. belongs_to :from_account, class_name: 'Account', optional: true
  28. belongs_to :activity, polymorphic: true, optional: true
  29. belongs_to :mention, foreign_type: 'Mention', foreign_key: 'activity_id', optional: true
  30. belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id', optional: true
  31. belongs_to :follow, foreign_type: 'Follow', foreign_key: 'activity_id', optional: true
  32. belongs_to :follow_request, foreign_type: 'FollowRequest', foreign_key: 'activity_id', optional: true
  33. belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id', optional: true
  34. belongs_to :poll, foreign_type: 'Poll', foreign_key: 'activity_id', optional: true
  35. validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
  36. validates :activity_type, inclusion: { in: TYPE_CLASS_MAP.values }
  37. scope :browserable, ->(exclude_types = [], account_id = nil) {
  38. types = TYPE_CLASS_MAP.values - activity_types_from_types(exclude_types + [:follow_request])
  39. if account_id.nil?
  40. where(activity_type: types)
  41. else
  42. where(activity_type: types, from_account_id: account_id)
  43. end
  44. }
  45. cache_associated :from_account, status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account, poll: [status: STATUS_INCLUDES]
  46. def type
  47. @type ||= TYPE_CLASS_MAP.invert[activity_type].to_sym
  48. end
  49. def target_status
  50. case type
  51. when :reblog
  52. status&.reblog
  53. when :favourite
  54. favourite&.status
  55. when :mention
  56. mention&.status
  57. when :poll
  58. poll&.status
  59. end
  60. end
  61. def browserable?
  62. type != :follow_request
  63. end
  64. class << self
  65. def cache_ids
  66. select(:id, :updated_at, :activity_type, :activity_id)
  67. end
  68. def reload_stale_associations!(cached_items)
  69. account_ids = (cached_items.map(&:from_account_id) + cached_items.map { |item| item.target_status&.account_id }.compact).uniq
  70. return if account_ids.empty?
  71. accounts = Account.where(id: account_ids).includes(:account_stat).each_with_object({}) { |a, h| h[a.id] = a }
  72. cached_items.each do |item|
  73. item.from_account = accounts[item.from_account_id]
  74. item.target_status.account = accounts[item.target_status.account_id] if item.target_status
  75. end
  76. end
  77. def activity_types_from_types(types)
  78. types.map { |type| TYPE_CLASS_MAP[type.to_sym] }.compact
  79. end
  80. end
  81. after_initialize :set_from_account
  82. before_validation :set_from_account
  83. private
  84. def set_from_account
  85. return unless new_record?
  86. case activity_type
  87. when 'Status', 'Follow', 'Favourite', 'FollowRequest', 'Poll'
  88. self.from_account_id = activity&.account_id
  89. when 'Mention'
  90. self.from_account_id = activity&.status&.account_id
  91. end
  92. end
  93. end