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.

145 lines
4.1 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. ).freeze
  35. TARGET_STATUS_INCLUDES_BY_TYPE = {
  36. status: :status,
  37. reblog: [status: :reblog],
  38. mention: [mention: :status],
  39. favourite: [favourite: :status],
  40. poll: [poll: :status],
  41. }.freeze
  42. belongs_to :account, optional: true
  43. belongs_to :from_account, class_name: 'Account', optional: true
  44. belongs_to :activity, polymorphic: true, optional: true
  45. belongs_to :mention, foreign_key: 'activity_id', optional: true
  46. belongs_to :status, foreign_key: 'activity_id', optional: true
  47. belongs_to :follow, foreign_key: 'activity_id', optional: true
  48. belongs_to :follow_request, foreign_key: 'activity_id', optional: true
  49. belongs_to :favourite, foreign_key: 'activity_id', optional: true
  50. belongs_to :poll, foreign_key: 'activity_id', optional: true
  51. validates :type, inclusion: { in: TYPES }
  52. scope :without_suspended, -> { joins(:from_account).merge(Account.without_suspended) }
  53. scope :browserable, ->(exclude_types = [], account_id = nil) {
  54. types = TYPES - exclude_types.map(&:to_sym)
  55. if account_id.nil?
  56. where(type: types)
  57. else
  58. where(type: types, from_account_id: account_id)
  59. end
  60. }
  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
  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 preload_cache_collection_target_statuses(notifications, &_block)
  80. notifications.group_by(&:type).each do |type, grouped_notifications|
  81. associations = TARGET_STATUS_INCLUDES_BY_TYPE[type]
  82. next unless associations
  83. # Instead of using the usual `includes`, manually preload each type.
  84. # If polymorphic associations are loaded with the usual `includes`, other types of associations will be loaded more.
  85. ActiveRecord::Associations::Preloader.new.preload(grouped_notifications, associations)
  86. end
  87. unique_target_statuses = notifications.map(&:target_status).compact.uniq
  88. # Call cache_collection in block
  89. cached_statuses_by_id = yield(unique_target_statuses).index_by(&:id)
  90. notifications.each do |notification|
  91. next if notification.target_status.nil?
  92. cached_status = cached_statuses_by_id[notification.target_status.id]
  93. case notification.type
  94. when :status
  95. notification.status = cached_status
  96. when :reblog
  97. notification.status.reblog = cached_status
  98. when :favourite
  99. notification.favourite.status = cached_status
  100. when :mention
  101. notification.mention.status = cached_status
  102. when :poll
  103. notification.poll.status = cached_status
  104. end
  105. end
  106. notifications
  107. end
  108. end
  109. after_initialize :set_from_account
  110. before_validation :set_from_account
  111. private
  112. def set_from_account
  113. return unless new_record?
  114. case activity_type
  115. when 'Status', 'Follow', 'Favourite', 'FollowRequest', 'Poll'
  116. self.from_account_id = activity&.account_id
  117. when 'Mention'
  118. self.from_account_id = activity&.status&.account_id
  119. end
  120. end
  121. end