From e0e58ab9cf3604452f260d5ef6272334aae60c1b Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Tue, 12 Sep 2023 11:03:55 +0900 Subject: [PATCH 1/4] Make statuses that local user replied to also searchable --- app/models/concerns/status/search_concern.rb | 1 + app/models/status.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/concerns/status/search_concern.rb b/app/models/concerns/status/search_concern.rb index 3f31b3b6750..c8ab1fc5e28 100644 --- a/app/models/concerns/status/search_concern.rb +++ b/app/models/concerns/status/search_concern.rb @@ -17,6 +17,7 @@ module Status::SearchConcern ids += local_favorited.pluck(:id) ids += local_reblogged.pluck(:id) ids += local_bookmarked.pluck(:id) + ids += local_replied.pluck(:id) ids += preloadable_poll.local_voters.pluck(:id) if preloadable_poll.present? ids.uniq diff --git a/app/models/status.rb b/app/models/status.rb index 9d09fa5fee1..1f662a8571e 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -83,6 +83,7 @@ class Status < ApplicationRecord has_many :local_favorited, -> { merge(Account.local) }, through: :favourites, source: :account has_many :local_reblogged, -> { merge(Account.local) }, through: :reblogs, source: :account has_many :local_bookmarked, -> { merge(Account.local) }, through: :bookmarks, source: :account + has_many :local_replied, -> { merge(Account.local) }, through: :replies, source: :account has_and_belongs_to_many :tags # rubocop:disable Rails/HasAndBelongsToMany From dded2f01b3b54a1fdb4164b63b22cf3af75dfac2 Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Wed, 13 Sep 2023 10:16:08 +0900 Subject: [PATCH 2/4] Fix N+1 and chewy importer --- app/chewy/statuses_index.rb | 2 +- app/lib/importer/statuses_index_importer.rb | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/chewy/statuses_index.rb b/app/chewy/statuses_index.rb index e739ccecb41..05eb0a9aa6d 100644 --- a/app/chewy/statuses_index.rb +++ b/app/chewy/statuses_index.rb @@ -52,7 +52,7 @@ class StatusesIndex < Chewy::Index }, } - index_scope ::Status.unscoped.kept.without_reblogs.includes(:media_attachments, :local_mentioned, :local_favorited, :local_reblogged, :local_bookmarked, :tags, preview_cards_status: :preview_card, preloadable_poll: :local_voters), delete_if: ->(status) { status.searchable_by.empty? } + index_scope ::Status.unscoped.kept.without_reblogs.includes(:media_attachments, :local_mentioned, :local_favorited, :local_reblogged, :local_bookmarked, :local_replied, :tags, preview_cards_status: :preview_card, preloadable_poll: :local_voters), delete_if: ->(status) { status.searchable_by.empty? } root date_detection: false do field(:id, type: 'long') diff --git a/app/lib/importer/statuses_index_importer.rb b/app/lib/importer/statuses_index_importer.rb index 1922f65f6dc..c54722fb5b4 100644 --- a/app/lib/importer/statuses_index_importer.rb +++ b/app/lib/importer/statuses_index_importer.rb @@ -12,7 +12,7 @@ class Importer::StatusesIndexImporter < Importer::BaseImporter # could end up being a very large array scope.reorder(nil).find_in_batches(batch_size: @batch_size) do |tmp| - in_work_unit(tmp.map(&:status_id)) do |status_ids| + in_work_unit(get_status_ids(tmp)) do |status_ids| deleted = 0 bulk = ActiveRecord::Base.connection_pool.with_connection do @@ -55,9 +55,16 @@ class Importer::StatusesIndexImporter < Importer::BaseImporter local_favourites_scope, local_votes_scope, local_bookmarks_scope, + local_replied_scope, ] end + def get_status_ids(scope) + scope.map(&:status_id) + rescue NoMethodError + scope.map(&:in_reply_to_id) # local_replied_scope + end + def local_mentions_scope Mention.where(account: Account.local, silent: false).select(:id, :status_id) end @@ -77,4 +84,8 @@ class Importer::StatusesIndexImporter < Importer::BaseImporter def local_statuses_scope Status.local.select('"statuses"."id", COALESCE("statuses"."reblog_of_id", "statuses"."id") AS status_id') end + + def local_replied_scope + Status.local.where.not(in_reply_to_id: nil).where('in_reply_to_account_id != account_id').select(:id, :in_reply_to_id) + end end From 8d7ba830f5bbd2bc3b3ce7bcd62026fac45b3d53 Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Wed, 13 Sep 2023 16:39:50 +0900 Subject: [PATCH 3/4] Remove get_status_ids Co-authored-by: Claire --- app/lib/importer/statuses_index_importer.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/app/lib/importer/statuses_index_importer.rb b/app/lib/importer/statuses_index_importer.rb index c54722fb5b4..a92e0ccb3c3 100644 --- a/app/lib/importer/statuses_index_importer.rb +++ b/app/lib/importer/statuses_index_importer.rb @@ -12,7 +12,7 @@ class Importer::StatusesIndexImporter < Importer::BaseImporter # could end up being a very large array scope.reorder(nil).find_in_batches(batch_size: @batch_size) do |tmp| - in_work_unit(get_status_ids(tmp)) do |status_ids| + in_work_unit(tmp.map(&:status_id)) do |status_ids| deleted = 0 bulk = ActiveRecord::Base.connection_pool.with_connection do @@ -59,12 +59,6 @@ class Importer::StatusesIndexImporter < Importer::BaseImporter ] end - def get_status_ids(scope) - scope.map(&:status_id) - rescue NoMethodError - scope.map(&:in_reply_to_id) # local_replied_scope - end - def local_mentions_scope Mention.where(account: Account.local, silent: false).select(:id, :status_id) end @@ -86,6 +80,6 @@ class Importer::StatusesIndexImporter < Importer::BaseImporter end def local_replied_scope - Status.local.where.not(in_reply_to_id: nil).where('in_reply_to_account_id != account_id').select(:id, :in_reply_to_id) + Status.local.where.not(in_reply_to_id: nil).where('in_reply_to_account_id != account_id').select(:id, 'in_reply_to_id AS status_id') end end From 823ed7ad15380ea194cacc9d1dc6cb2a088dc4ed Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Mon, 27 May 2024 22:41:33 +0900 Subject: [PATCH 4/4] Fix ruby lint --- app/chewy/statuses_index.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/chewy/statuses_index.rb b/app/chewy/statuses_index.rb index 05eb0a9aa6d..50d979e24a1 100644 --- a/app/chewy/statuses_index.rb +++ b/app/chewy/statuses_index.rb @@ -52,7 +52,9 @@ class StatusesIndex < Chewy::Index }, } - index_scope ::Status.unscoped.kept.without_reblogs.includes(:media_attachments, :local_mentioned, :local_favorited, :local_reblogged, :local_bookmarked, :local_replied, :tags, preview_cards_status: :preview_card, preloadable_poll: :local_voters), delete_if: ->(status) { status.searchable_by.empty? } + index_scope ::Status.unscoped.kept.without_reblogs.includes( + :media_attachments, :local_mentioned, :local_favorited, :local_reblogged, :local_bookmarked, :local_replied, :tags, preview_cards_status: :preview_card, preloadable_poll: :local_voters + ), delete_if: ->(status) { status.searchable_by.empty? } root date_detection: false do field(:id, type: 'long')