statuses_search.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # frozen_string_literal: true
  2. module Account::StatusesSearch
  3. extend ActiveSupport::Concern
  4. included do
  5. after_update_commit :enqueue_update_public_statuses_index, if: :saved_change_to_indexable?
  6. after_destroy_commit :enqueue_remove_from_public_statuses_index, if: :indexable?
  7. end
  8. def enqueue_update_public_statuses_index
  9. if indexable?
  10. enqueue_add_to_public_statuses_index
  11. else
  12. enqueue_remove_from_public_statuses_index
  13. end
  14. end
  15. def enqueue_add_to_public_statuses_index
  16. return unless Chewy.enabled?
  17. AddToPublicStatusesIndexWorker.perform_async(id)
  18. end
  19. def enqueue_remove_from_public_statuses_index
  20. return unless Chewy.enabled?
  21. RemoveFromPublicStatusesIndexWorker.perform_async(id)
  22. end
  23. def add_to_public_statuses_index!
  24. return unless Chewy.enabled?
  25. statuses.without_reblogs.where(visibility: :public).reorder(nil).find_in_batches do |batch|
  26. PublicStatusesIndex.import(batch)
  27. end
  28. end
  29. def remove_from_public_statuses_index!
  30. return unless Chewy.enabled?
  31. PublicStatusesIndex.filter(term: { account_id: id }).delete_all
  32. end
  33. end