notification.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 Paginable
  20. include Redisable
  21. LEGACY_TYPE_CLASS_MAP = {
  22. 'Mention' => :mention,
  23. 'Status' => :reblog,
  24. 'Follow' => :follow,
  25. 'FollowRequest' => :follow_request,
  26. 'Favourite' => :favourite,
  27. 'Poll' => :poll,
  28. }.freeze
  29. # `set_group_key!` needs to be updated if this list changes
  30. GROUPABLE_NOTIFICATION_TYPES = %i(favourite reblog follow).freeze
  31. MAXIMUM_GROUP_SPAN_HOURS = 12
  32. # Please update app/javascript/api_types/notification.ts if you change this
  33. PROPERTIES = {
  34. mention: {
  35. filterable: true,
  36. }.freeze,
  37. status: {
  38. filterable: false,
  39. }.freeze,
  40. reblog: {
  41. filterable: true,
  42. }.freeze,
  43. follow: {
  44. filterable: true,
  45. }.freeze,
  46. follow_request: {
  47. filterable: true,
  48. }.freeze,
  49. favourite: {
  50. filterable: true,
  51. }.freeze,
  52. poll: {
  53. filterable: false,
  54. }.freeze,
  55. update: {
  56. filterable: false,
  57. }.freeze,
  58. severed_relationships: {
  59. filterable: false,
  60. }.freeze,
  61. moderation_warning: {
  62. filterable: false,
  63. }.freeze,
  64. annual_report: {
  65. filterable: false,
  66. }.freeze,
  67. 'admin.sign_up': {
  68. filterable: false,
  69. }.freeze,
  70. 'admin.report': {
  71. filterable: false,
  72. }.freeze,
  73. }.freeze
  74. TYPES = PROPERTIES.keys.freeze
  75. TARGET_STATUS_INCLUDES_BY_TYPE = {
  76. status: :status,
  77. reblog: [status: :reblog],
  78. mention: [mention: :status],
  79. favourite: [favourite: :status],
  80. poll: [poll: :status],
  81. update: :status,
  82. 'admin.report': [report: :target_account],
  83. }.freeze
  84. belongs_to :account, optional: true
  85. belongs_to :from_account, class_name: 'Account', optional: true
  86. belongs_to :activity, polymorphic: true, optional: true
  87. with_options foreign_key: 'activity_id', optional: true do
  88. belongs_to :mention, inverse_of: :notification
  89. belongs_to :status, inverse_of: :notification
  90. belongs_to :follow, inverse_of: :notification
  91. belongs_to :follow_request, inverse_of: :notification
  92. belongs_to :favourite, inverse_of: :notification
  93. belongs_to :poll, inverse_of: false
  94. belongs_to :report, inverse_of: false
  95. belongs_to :account_relationship_severance_event, inverse_of: false
  96. belongs_to :account_warning, inverse_of: false
  97. belongs_to :generated_annual_report, inverse_of: false
  98. end
  99. validates :type, inclusion: { in: TYPES }
  100. scope :without_suspended, -> { joins(:from_account).merge(Account.without_suspended) }
  101. def type
  102. @type ||= (super || LEGACY_TYPE_CLASS_MAP[activity_type]).to_sym
  103. end
  104. def target_status
  105. case type
  106. when :status, :update
  107. status
  108. when :reblog
  109. status&.reblog
  110. when :favourite
  111. favourite&.status
  112. when :mention
  113. mention&.status
  114. when :poll
  115. poll&.status
  116. end
  117. end
  118. def set_group_key!
  119. return if filtered? || Notification::GROUPABLE_NOTIFICATION_TYPES.exclude?(type)
  120. type_prefix = case type
  121. when :favourite, :reblog
  122. [type, target_status&.id].join('-')
  123. when :follow
  124. type
  125. else
  126. raise NotImplementedError
  127. end
  128. redis_key = "notif-group/#{account.id}/#{type_prefix}"
  129. hour_bucket = activity.created_at.utc.to_i / 1.hour.to_i
  130. # Reuse previous group if it does not span too large an amount of time
  131. previous_bucket = redis.get(redis_key).to_i
  132. hour_bucket = previous_bucket if hour_bucket < previous_bucket + MAXIMUM_GROUP_SPAN_HOURS
  133. # We do not concern ourselves with race conditions since we use hour buckets
  134. redis.set(redis_key, hour_bucket, ex: MAXIMUM_GROUP_SPAN_HOURS.hours.to_i)
  135. self.group_key = "#{type_prefix}-#{hour_bucket}"
  136. end
  137. class << self
  138. def browserable(types: [], exclude_types: [], from_account_id: nil, include_filtered: false)
  139. requested_types = if types.empty?
  140. TYPES
  141. else
  142. types.map(&:to_sym) & TYPES
  143. end
  144. requested_types -= exclude_types.map(&:to_sym)
  145. all.tap do |scope|
  146. scope.merge!(where(filtered: false)) unless include_filtered || from_account_id.present?
  147. scope.merge!(where(from_account_id: from_account_id)) if from_account_id.present?
  148. scope.merge!(where(type: requested_types)) unless requested_types.size == TYPES.size
  149. end
  150. end
  151. def paginate_groups(limit, pagination_order, grouped_types: nil)
  152. raise ArgumentError unless %i(asc desc).include?(pagination_order)
  153. query = reorder(id: pagination_order)
  154. # Ideally `:types` would be a bind rather than part of the SQL itself, but that does not
  155. # seem to be possible to do with Rails, considering that the expression would occur in
  156. # multiple places, including in a `select`
  157. group_key_sql = begin
  158. if grouped_types.present?
  159. # Normalize `grouped_types` so the number of different SQL query shapes remains small, and
  160. # the queries can be analyzed in monitoring/telemetry tools
  161. grouped_types = (grouped_types.map(&:to_sym) & GROUPABLE_NOTIFICATION_TYPES).sort
  162. sanitize_sql_array([<<~SQL.squish, { types: grouped_types }])
  163. COALESCE(
  164. CASE
  165. WHEN notifications.type IN (:types) THEN notifications.group_key
  166. ELSE NULL
  167. END,
  168. 'ungrouped-' || notifications.id
  169. )
  170. SQL
  171. else
  172. "COALESCE(notifications.group_key, 'ungrouped-' || notifications.id)"
  173. end
  174. end
  175. unscoped
  176. .with_recursive(
  177. grouped_notifications: [
  178. # Base case: fetching one notification and annotating it with visited groups
  179. query
  180. .select('notifications.*', "ARRAY[#{group_key_sql}] AS groups")
  181. .limit(1),
  182. # Recursive case, always yielding at most one annotated notification
  183. unscoped
  184. .from(
  185. [
  186. # Expose the working table as `wt`, but quit early if we've reached the limit
  187. unscoped
  188. .select('id', 'groups')
  189. .from('grouped_notifications')
  190. .where('array_length(grouped_notifications.groups, 1) < :limit', limit: limit)
  191. .arel.as('wt'),
  192. # Recursive query, using `LATERAL` so we can refer to `wt`
  193. query
  194. .where(pagination_order == :desc ? 'notifications.id < wt.id' : 'notifications.id > wt.id')
  195. .where.not("#{group_key_sql} = ANY(wt.groups)")
  196. .limit(1)
  197. .arel.lateral('notifications'),
  198. ]
  199. )
  200. .select('notifications.*', "array_append(wt.groups, #{group_key_sql}) AS groups"),
  201. ]
  202. )
  203. .from('grouped_notifications AS notifications')
  204. .order(id: pagination_order)
  205. .limit(limit)
  206. end
  207. # This returns notifications from the request page, but with at most one notification per group.
  208. # Notifications that have no `group_key` each count as a separate group.
  209. def paginate_groups_by_max_id(limit, max_id: nil, since_id: nil, grouped_types: nil)
  210. query = reorder(id: :desc)
  211. query = query.where(id: ...(max_id.to_i)) if max_id.present?
  212. query = query.where(id: (since_id.to_i + 1)...) if since_id.present?
  213. query.paginate_groups(limit, :desc, grouped_types: grouped_types)
  214. end
  215. # Differs from :paginate_groups_by_max_id in that it gives the results immediately following min_id,
  216. # whereas since_id gives the items with largest id, but with since_id as a cutoff.
  217. # Results will be in ascending order by id.
  218. def paginate_groups_by_min_id(limit, max_id: nil, min_id: nil, grouped_types: nil)
  219. query = reorder(id: :asc)
  220. query = query.where(id: (min_id.to_i + 1)...) if min_id.present?
  221. query = query.where(id: ...(max_id.to_i)) if max_id.present?
  222. query.paginate_groups(limit, :asc, grouped_types: grouped_types)
  223. end
  224. def to_a_grouped_paginated_by_id(limit, options = {})
  225. if options[:min_id].present?
  226. paginate_groups_by_min_id(limit, min_id: options[:min_id], max_id: options[:max_id], grouped_types: options[:grouped_types]).reverse
  227. else
  228. paginate_groups_by_max_id(limit, max_id: options[:max_id], since_id: options[:since_id], grouped_types: options[:grouped_types]).to_a
  229. end
  230. end
  231. def preload_cache_collection_target_statuses(notifications, &_block)
  232. notifications.group_by(&:type).each do |type, grouped_notifications|
  233. associations = TARGET_STATUS_INCLUDES_BY_TYPE[type]
  234. next unless associations
  235. # Instead of using the usual `includes`, manually preload each type.
  236. # If polymorphic associations are loaded with the usual `includes`, other types of associations will be loaded more.
  237. ActiveRecord::Associations::Preloader.new(records: grouped_notifications, associations: associations).call
  238. end
  239. unique_target_statuses = notifications.filter_map(&:target_status).uniq
  240. # Call cache_collection in block
  241. cached_statuses_by_id = yield(unique_target_statuses).index_by(&:id)
  242. notifications.each do |notification|
  243. next if notification.target_status.nil?
  244. cached_status = cached_statuses_by_id[notification.target_status.id]
  245. case notification.type
  246. when :status, :update
  247. notification.status = cached_status
  248. when :reblog
  249. notification.status.reblog = cached_status
  250. when :favourite
  251. notification.favourite.status = cached_status
  252. when :mention
  253. notification.mention.status = cached_status
  254. when :poll
  255. notification.poll.status = cached_status
  256. end
  257. end
  258. notifications
  259. end
  260. end
  261. after_initialize :set_from_account
  262. before_validation :set_from_account
  263. after_destroy :remove_from_notification_request
  264. private
  265. def set_from_account
  266. return unless new_record?
  267. case activity_type
  268. when 'Status', 'Follow', 'Favourite', 'FollowRequest', 'Poll', 'Report'
  269. self.from_account_id = activity&.account_id
  270. when 'Mention'
  271. self.from_account_id = activity&.status&.account_id
  272. when 'Account'
  273. self.from_account_id = activity&.id
  274. when 'AccountRelationshipSeveranceEvent', 'AccountWarning', 'GeneratedAnnualReport'
  275. # These do not really have an originating account, but this is mandatory
  276. # in the data model, and the recipient's account will by definition
  277. # always exist
  278. self.from_account_id = account_id
  279. end
  280. end
  281. def remove_from_notification_request
  282. notification_request = NotificationRequest.find_by(account_id: account_id, from_account_id: from_account_id)
  283. notification_request&.reconsider_existence!
  284. end
  285. end