Fix: タイムライン読み込み時のN+1問題 (#433)

This commit is contained in:
KMY(雪あすか) 2024-01-09 09:57:47 +09:00 committed by GitHub
parent 7ee9efd6f2
commit 616d0c5267
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View file

@ -113,20 +113,25 @@ class CustomFilter < ApplicationRecord
end end
def self.apply_cached_filters(cached_filters, status, following: false) def self.apply_cached_filters(cached_filters, status, following: false)
references_text_cache = nil
references_spoiler_text_cache = nil
cached_filters.filter_map do |filter, rules| cached_filters.filter_map do |filter, rules|
next if filter.exclude_follows && following next if filter.exclude_follows && following
next if filter.exclude_localusers && status.account.local? next if filter.exclude_localusers && status.account.local?
if rules[:keywords].present? if rules[:keywords].present?
match = rules[:keywords].match(status.proper.searchable_text) match = rules[:keywords].match(status.proper.searchable_text)
if match.nil? && filter.with_quote && status.proper.references.exists? if match.nil? && filter.with_quote && status.proper.reference_objects.exists?
match = rules[:keywords].match(status.proper.references.pluck(:text).join("\n\n")) references_text_cache = status.proper.references.pluck(:text).join("\n\n") if references_text_cache.nil?
match = rules[:keywords].match(status.proper.references.pluck(:spoiler_text).join("\n\n")) if match.nil? references_spoiler_text_cache = status.proper.references.pluck(:spoiler_text).join("\n\n") if references_spoiler_text_cache.nil?
match = rules[:keywords].match(references_text_cache)
match = rules[:keywords].match(references_spoiler_text_cache) if match.nil?
end end
end end
keyword_matches = [match.to_s] unless match.nil? keyword_matches = [match.to_s] unless match.nil?
reference_ids = filter.with_quote ? status.proper.references.pluck(:id) : [] reference_ids = filter.with_quote ? status.proper.reference_objects.pluck(:target_status_id) : []
status_matches = ([status.id, status.reblog_of_id] + reference_ids).compact & rules[:status_ids] if rules[:status_ids].present? status_matches = ([status.id, status.reblog_of_id] + reference_ids).compact & rules[:status_ids] if rules[:status_ids].present?
next if keyword_matches.blank? && status_matches.blank? next if keyword_matches.blank? && status_matches.blank?

View file

@ -185,6 +185,7 @@ class Status < ApplicationRecord
:tags, :tags,
:preloadable_poll, :preloadable_poll,
:reference_objects, :reference_objects,
:references,
:scheduled_expiration_status, :scheduled_expiration_status,
preview_cards_status: [:preview_card], preview_cards_status: [:preview_card],
account: [:account_stat, user: :role], account: [:account_stat, user: :role],