past_interactions_source.rb 787 B

123456789101112131415161718192021222324252627282930313233343536
  1. # frozen_string_literal: true
  2. class AccountSuggestions::PastInteractionsSource < AccountSuggestions::Source
  3. include Redisable
  4. def key
  5. :past_interactions
  6. end
  7. def get(account, skip_account_ids: [], limit: 40)
  8. account_ids = account_ids_for_account(account.id, limit + skip_account_ids.size) - skip_account_ids
  9. as_ordered_suggestions(
  10. scope.where(id: account_ids),
  11. account_ids
  12. ).take(limit)
  13. end
  14. def remove(account, target_account_id)
  15. redis.zrem("interactions:#{account.id}", target_account_id)
  16. end
  17. private
  18. def scope
  19. Account.searchable
  20. end
  21. def account_ids_for_account(account_id, limit)
  22. redis.zrevrange("interactions:#{account_id}", 0, limit).map(&:to_i)
  23. end
  24. def to_ordered_list_key(account)
  25. account.id
  26. end
  27. end