notification.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. # filtered :boolean default(FALSE), not null
  15. # group_key :string
  16. #
  17. class Notification < ApplicationRecord
  18. self.inheritance_column = nil
  19. include Notification::Groups
  20. include Paginable
  21. include Redisable
  22. LEGACY_TYPE_CLASS_MAP = {
  23. 'Mention' => :mention,
  24. 'Status' => :reblog,
  25. 'Follow' => :follow,
  26. 'FollowRequest' => :follow_request,
  27. 'Favourite' => :favourite,
  28. 'Poll' => :poll,
  29. }.freeze
  30. # Please update app/javascript/api_types/notification.ts if you change this
  31. PROPERTIES = {
  32. mention: {
  33. filterable: true,
  34. }.freeze,
  35. status: {
  36. filterable: false,
  37. }.freeze,
  38. reblog: {
  39. filterable: true,
  40. }.freeze,
  41. follow: {
  42. filterable: true,
  43. }.freeze,
  44. follow_request: {
  45. filterable: true,
  46. }.freeze,
  47. favourite: {
  48. filterable: true,
  49. }.freeze,
  50. poll: {
  51. filterable: false,
  52. }.freeze,
  53. update: {
  54. filterable: false,
  55. }.freeze,
  56. severed_relationships: {
  57. filterable: false,
  58. }.freeze,
  59. moderation_warning: {
  60. filterable: false,
  61. }.freeze,
  62. annual_report: {
  63. filterable: false,
  64. }.freeze,
  65. 'admin.sign_up': {
  66. filterable: false,
  67. }.freeze,
  68. 'admin.report': {
  69. filterable: false,
  70. }.freeze,
  71. }.freeze
  72. TYPES = PROPERTIES.keys.freeze
  73. TARGET_STATUS_INCLUDES_BY_TYPE = {
  74. status: :status,
  75. reblog: [status: :reblog],
  76. mention: [mention: :status],
  77. favourite: [favourite: :status],
  78. poll: [poll: :status],
  79. update: :status,
  80. 'admin.report': [report: :target_account],
  81. }.freeze
  82. belongs_to :account, optional: true
  83. belongs_to :from_account, class_name: 'Account', optional: true
  84. belongs_to :activity, polymorphic: true, optional: true
  85. with_options foreign_key: 'activity_id', optional: true do
  86. belongs_to :mention, inverse_of: :notification
  87. belongs_to :status, inverse_of: :notification
  88. belongs_to :follow, inverse_of: :notification
  89. belongs_to :follow_request, inverse_of: :notification
  90. belongs_to :favourite, inverse_of: :notification
  91. belongs_to :poll, inverse_of: false
  92. belongs_to :report, inverse_of: false
  93. belongs_to :account_relationship_severance_event, inverse_of: false
  94. belongs_to :account_warning, inverse_of: false
  95. belongs_to :generated_annual_report, inverse_of: false
  96. end
  97. validates :type, inclusion: { in: TYPES }
  98. scope :without_suspended, -> { joins(:from_account).merge(Account.without_suspended) }
  99. def type
  100. @type ||= (super || LEGACY_TYPE_CLASS_MAP[activity_type]).to_sym
  101. end
  102. def target_status
  103. case type
  104. when :status, :update
  105. status
  106. when :reblog
  107. status&.reblog
  108. when :favourite
  109. favourite&.status
  110. when :mention
  111. mention&.status
  112. when :poll
  113. poll&.status
  114. end
  115. end
  116. class << self
  117. def browserable(types: [], exclude_types: [], from_account_id: nil, include_filtered: false)
  118. requested_types = if types.empty?
  119. TYPES
  120. else
  121. types.map(&:to_sym) & TYPES
  122. end
  123. requested_types -= exclude_types.map(&:to_sym)
  124. all.tap do |scope|
  125. scope.merge!(where(filtered: false)) unless include_filtered || from_account_id.present?
  126. scope.merge!(where(from_account_id: from_account_id)) if from_account_id.present?
  127. scope.merge!(where(type: requested_types)) unless requested_types.size == TYPES.size
  128. end
  129. end
  130. def preload_cache_collection_target_statuses(notifications, &_block)
  131. notifications.group_by(&:type).each do |type, grouped_notifications|
  132. associations = TARGET_STATUS_INCLUDES_BY_TYPE[type]
  133. next unless associations
  134. # Instead of using the usual `includes`, manually preload each type.
  135. # If polymorphic associations are loaded with the usual `includes`, other types of associations will be loaded more.
  136. ActiveRecord::Associations::Preloader.new(records: grouped_notifications, associations: associations).call
  137. end
  138. unique_target_statuses = notifications.filter_map(&:target_status).uniq
  139. # Call cache_collection in block
  140. cached_statuses_by_id = yield(unique_target_statuses).index_by(&:id)
  141. notifications.each do |notification|
  142. next if notification.target_status.nil?
  143. cached_status = cached_statuses_by_id[notification.target_status.id]
  144. case notification.type
  145. when :status, :update
  146. notification.status = cached_status
  147. when :reblog
  148. notification.status.reblog = cached_status
  149. when :favourite
  150. notification.favourite.status = cached_status
  151. when :mention
  152. notification.mention.status = cached_status
  153. when :poll
  154. notification.poll.status = cached_status
  155. end
  156. end
  157. notifications
  158. end
  159. end
  160. after_initialize :set_from_account
  161. before_validation :set_from_account
  162. after_destroy :remove_from_notification_request
  163. private
  164. def set_from_account
  165. return unless new_record?
  166. case activity_type
  167. when 'Status', 'Follow', 'Favourite', 'FollowRequest', 'Poll', 'Report'
  168. self.from_account_id = activity&.account_id
  169. when 'Mention'
  170. self.from_account_id = activity&.status&.account_id
  171. when 'Account'
  172. self.from_account_id = activity&.id
  173. when 'AccountRelationshipSeveranceEvent', 'AccountWarning', 'GeneratedAnnualReport'
  174. # These do not really have an originating account, but this is mandatory
  175. # in the data model, and the recipient's account will by definition
  176. # always exist
  177. self.from_account_id = account_id
  178. end
  179. end
  180. def remove_from_notification_request
  181. notification_request = NotificationRequest.find_by(account_id: account_id, from_account_id: from_account_id)
  182. notification_request&.reconsider_existence!
  183. end
  184. end