Commit 05b72368 authored by alpaca-tc's avatar alpaca-tc Committed by Eugen Rochko

Fixes slow query in notifications api (#2851)

* Add inclusion validation for activity_type

* When excluding all types with `WHERE NOT IN`, the query search all rows.
parent 6f71cfef
...@@ -16,6 +16,16 @@ class Notification < ApplicationRecord ...@@ -16,6 +16,16 @@ class Notification < ApplicationRecord
include Paginable include Paginable
include Cacheable include Cacheable
TYPE_CLASS_MAP = {
mention: 'Mention',
reblog: 'Status',
follow: 'Follow',
follow_request: 'FollowRequest',
favourite: 'Favourite',
}.freeze
STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
belongs_to :account belongs_to :account
belongs_to :from_account, class_name: 'Account' belongs_to :from_account, class_name: 'Account'
belongs_to :activity, polymorphic: true belongs_to :activity, polymorphic: true
...@@ -27,19 +37,15 @@ class Notification < ApplicationRecord ...@@ -27,19 +37,15 @@ class Notification < ApplicationRecord
belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id' belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id'
validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] } validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
validates :activity_type, inclusion: { in: TYPE_CLASS_MAP.values }
TYPE_CLASS_MAP = {
mention: 'Mention',
reblog: 'Status',
follow: 'Follow',
follow_request: 'FollowRequest',
favourite: 'Favourite',
}.freeze
STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
scope :cache_ids, -> { select(:id, :updated_at, :activity_type, :activity_id) } scope :cache_ids, -> { select(:id, :updated_at, :activity_type, :activity_id) }
scope :browserable, ->(exclude_types = []) {
types = TYPE_CLASS_MAP.values - activity_types_from_types(exclude_types + [:follow_request])
where(activity_type: types)
}
cache_associated :from_account, status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account cache_associated :from_account, status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account
def activity(eager_loaded = true) def activity(eager_loaded = true)
...@@ -64,11 +70,6 @@ class Notification < ApplicationRecord ...@@ -64,11 +70,6 @@ class Notification < ApplicationRecord
end end
class << self class << self
def browserable(types = [])
types.concat([:follow_request])
where.not(activity_type: activity_types_from_types(types))
end
def reload_stale_associations!(cached_items) def reload_stale_associations!(cached_items)
account_ids = cached_items.map(&:from_account_id).uniq account_ids = cached_items.map(&:from_account_id).uniq
accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h
......
Fabricator(:notification) do Fabricator(:notification) do
activity_id 1 activity_id 1
activity_type "MyString" activity_type 'Favourite'
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment