notification.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. # type :string
  14. #
  15. class Notification < ApplicationRecord
  16. self.inheritance_column = nil
  17. include Paginable
  18. LEGACY_TYPE_CLASS_MAP = {
  19. 'Mention' => :mention,
  20. 'Status' => :reblog,
  21. 'Follow' => :follow,
  22. 'FollowRequest' => :follow_request,
  23. 'Favourite' => :favourite,
  24. 'Poll' => :poll,
  25. }.freeze
  26. TYPES = %i(
  27. mention
  28. status
  29. reblog
  30. follow
  31. follow_request
  32. favourite
  33. poll
  34. update
  35. admin.sign_up
  36. admin.report
  37. ).freeze
  38. TARGET_STATUS_INCLUDES_BY_TYPE = {
  39. status: :status,
  40. reblog: [status: :reblog],
  41. mention: [mention: :status],
  42. favourite: [favourite: :status],
  43. poll: [poll: :status],
  44. update: :status,
  45. 'admin.report': [report: :target_account],
  46. }.freeze
  47. belongs_to :account, optional: true
  48. belongs_to :from_account, class_name: 'Account', optional: true
  49. belongs_to :activity, polymorphic: true, optional: true
  50. with_options foreign_key: 'activity_id', optional: true do
  51. belongs_to :mention, inverse_of: :notification
  52. belongs_to :status, inverse_of: :notification
  53. belongs_to :follow, inverse_of: :notification
  54. belongs_to :follow_request, inverse_of: :notification
  55. belongs_to :favourite, inverse_of: :notification
  56. belongs_to :poll, inverse_of: false
  57. belongs_to :report, inverse_of: false
  58. end
  59. validates :type, inclusion: { in: TYPES }
  60. scope :without_suspended, -> { joins(:from_account).merge(Account.without_suspended) }
  61. def type
  62. @type ||= (super || LEGACY_TYPE_CLASS_MAP[activity_type]).to_sym
  63. end
  64. def target_status
  65. case type
  66. when :status, :update
  67. status
  68. when :reblog
  69. status&.reblog
  70. when :favourite
  71. favourite&.status
  72. when :mention
  73. mention&.status
  74. when :poll
  75. poll&.status
  76. end
  77. end
  78. class << self
  79. def browserable(types: [], exclude_types: [], from_account_id: nil)
  80. requested_types = if types.empty?
  81. TYPES
  82. else
  83. types.map(&:to_sym) & TYPES
  84. end
  85. requested_types -= exclude_types.map(&:to_sym)
  86. all.tap do |scope|
  87. scope.merge!(where(from_account_id: from_account_id)) if from_account_id.present?
  88. scope.merge!(where(type: requested_types)) unless requested_types.size == TYPES.size
  89. end
  90. end
  91. def preload_cache_collection_target_statuses(notifications, &_block)
  92. notifications.group_by(&:type).each do |type, grouped_notifications|
  93. associations = TARGET_STATUS_INCLUDES_BY_TYPE[type]
  94. next unless associations
  95. # Instead of using the usual `includes`, manually preload each type.
  96. # If polymorphic associations are loaded with the usual `includes`, other types of associations will be loaded more.
  97. ActiveRecord::Associations::Preloader.new(records: grouped_notifications, associations: associations)
  98. end
  99. unique_target_statuses = notifications.filter_map(&:target_status).uniq
  100. # Call cache_collection in block
  101. cached_statuses_by_id = yield(unique_target_statuses).index_by(&:id)
  102. notifications.each do |notification|
  103. next if notification.target_status.nil?
  104. cached_status = cached_statuses_by_id[notification.target_status.id]
  105. case notification.type
  106. when :status, :update
  107. notification.status = cached_status
  108. when :reblog
  109. notification.status.reblog = cached_status
  110. when :favourite
  111. notification.favourite.status = cached_status
  112. when :mention
  113. notification.mention.status = cached_status
  114. when :poll
  115. notification.poll.status = cached_status
  116. end
  117. end
  118. notifications
  119. end
  120. end
  121. after_initialize :set_from_account
  122. before_validation :set_from_account
  123. private
  124. def set_from_account
  125. return unless new_record?
  126. case activity_type
  127. when 'Status', 'Follow', 'Favourite', 'FollowRequest', 'Poll', 'Report'
  128. self.from_account_id = activity&.account_id
  129. when 'Mention'
  130. self.from_account_id = activity&.status&.account_id
  131. when 'Account'
  132. self.from_account_id = activity&.id
  133. end
  134. end
  135. end