notification.rb 4.4 KB

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