You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5 KiB

  1. # frozen_string_literal: true
  2. class Importer::StatusesIndexImporter < Importer::BaseImporter
  3. def import!
  4. # The idea is that instead of iterating over all statuses in the database
  5. # and calculating the searchable_by for each of them (majority of which
  6. # would be empty), we approach the index from the other end
  7. scopes.each do |scope|
  8. # We could be tempted to keep track of status IDs we have already processed
  9. # from a different scope to avoid indexing them multiple times, but that
  10. # could end up being a very large array
  11. scope.find_in_batches(batch_size: @batch_size) do |tmp|
  12. in_work_unit(tmp.map(&:status_id)) do |status_ids|
  13. bulk = ActiveRecord::Base.connection_pool.with_connection do
  14. Chewy::Index::Import::BulkBuilder.new(index, to_index: Status.includes(:media_attachments, :preloadable_poll).where(id: status_ids)).bulk_body
  15. end
  16. indexed = 0
  17. deleted = 0
  18. # We can't use the delete_if proc to do the filtering because delete_if
  19. # is called before rendering the data and we need to filter based
  20. # on the results of the filter, so this filtering happens here instead
  21. bulk.map! do |entry|
  22. new_entry = if entry[:index] && entry.dig(:index, :data, 'searchable_by').blank?
  23. { delete: entry[:index].except(:data) }
  24. else
  25. entry
  26. end
  27. if new_entry[:index]
  28. indexed += 1
  29. else
  30. deleted += 1
  31. end
  32. new_entry
  33. end
  34. Chewy::Index::Import::BulkRequest.new(index).perform(bulk)
  35. [indexed, deleted]
  36. end
  37. end
  38. end
  39. wait!
  40. end
  41. private
  42. def index
  43. StatusesIndex
  44. end
  45. def scopes
  46. [
  47. local_statuses_scope,
  48. local_mentions_scope,
  49. local_favourites_scope,
  50. local_votes_scope,
  51. local_bookmarks_scope,
  52. ]
  53. end
  54. def local_mentions_scope
  55. Mention.where(account: Account.local, silent: false).select(:id, :status_id)
  56. end
  57. def local_favourites_scope
  58. Favourite.where(account: Account.local).select(:id, :status_id)
  59. end
  60. def local_bookmarks_scope
  61. Bookmark.select(:id, :status_id)
  62. end
  63. def local_votes_scope
  64. Poll.joins(:votes).where(votes: { account: Account.local }).select('polls.id, polls.status_id')
  65. end
  66. def local_statuses_scope
  67. Status.local.select('"statuses"."id", COALESCE("statuses"."reblog_of_id", "statuses"."id") AS status_id')
  68. end
  69. end