You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.6 KiB

  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 = if types.empty?
  79. TYPES
  80. else
  81. types.map(&:to_sym) & TYPES
  82. end
  83. requested_types -= exclude_types.map(&:to_sym)
  84. all.tap do |scope|
  85. scope.merge!(where(from_account_id: from_account_id)) if from_account_id.present?
  86. scope.merge!(where(type: requested_types)) unless requested_types.size == TYPES.size
  87. end
  88. end
  89. def preload_cache_collection_target_statuses(notifications, &_block)
  90. notifications.group_by(&:type).each do |type, grouped_notifications|
  91. associations = TARGET_STATUS_INCLUDES_BY_TYPE[type]
  92. next unless associations
  93. # Instead of using the usual `includes`, manually preload each type.
  94. # If polymorphic associations are loaded with the usual `includes`, other types of associations will be loaded more.
  95. ActiveRecord::Associations::Preloader.new.preload(grouped_notifications, associations)
  96. end
  97. unique_target_statuses = notifications.map(&:target_status).compact.uniq
  98. # Call cache_collection in block
  99. cached_statuses_by_id = yield(unique_target_statuses).index_by(&:id)
  100. notifications.each do |notification|
  101. next if notification.target_status.nil?
  102. cached_status = cached_statuses_by_id[notification.target_status.id]
  103. case notification.type
  104. when :status, :update
  105. notification.status = cached_status
  106. when :reblog
  107. notification.status.reblog = cached_status
  108. when :favourite
  109. notification.favourite.status = cached_status
  110. when :mention
  111. notification.mention.status = cached_status
  112. when :poll
  113. notification.poll.status = cached_status
  114. end
  115. end
  116. notifications
  117. end
  118. end
  119. after_initialize :set_from_account
  120. before_validation :set_from_account
  121. private
  122. def set_from_account
  123. return unless new_record?
  124. case activity_type
  125. when 'Status', 'Follow', 'Favourite', 'FollowRequest', 'Poll', 'Report'
  126. self.from_account_id = activity&.account_id
  127. when 'Mention'
  128. self.from_account_id = activity&.status&.account_id
  129. when 'Account'
  130. self.from_account_id = activity&.id
  131. end
  132. end
  133. end