notification.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. belongs_to :mention, foreign_key: 'activity_id', optional: true
  51. belongs_to :status, foreign_key: 'activity_id', optional: true
  52. belongs_to :follow, foreign_key: 'activity_id', optional: true
  53. belongs_to :follow_request, foreign_key: 'activity_id', optional: true
  54. belongs_to :favourite, foreign_key: 'activity_id', optional: true
  55. belongs_to :poll, foreign_key: 'activity_id', optional: true
  56. belongs_to :report, foreign_key: 'activity_id', optional: true
  57. validates :type, inclusion: { in: TYPES }
  58. scope :without_suspended, -> { joins(:from_account).merge(Account.without_suspended) }
  59. def type
  60. @type ||= (super || LEGACY_TYPE_CLASS_MAP[activity_type]).to_sym
  61. end
  62. def target_status
  63. case type
  64. when :status, :update
  65. status
  66. when :reblog
  67. status&.reblog
  68. when :favourite
  69. favourite&.status
  70. when :mention
  71. mention&.status
  72. when :poll
  73. poll&.status
  74. end
  75. end
  76. class << self
  77. def browserable(types: [], exclude_types: [], from_account_id: nil)
  78. requested_types = begin
  79. if types.empty?
  80. TYPES
  81. else
  82. types.map(&:to_sym) & TYPES
  83. end
  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.preload(grouped_notifications, associations)
  98. end
  99. unique_target_statuses = notifications.map(&:target_status).compact.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