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.

56 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class TagFeed < PublicFeed
  3. LIMIT_PER_MODE = 4
  4. # @param [Tag] tag
  5. # @param [Account] account
  6. # @param [Hash] options
  7. # @option [Enumerable<String>] :any
  8. # @option [Enumerable<String>] :all
  9. # @option [Enumerable<String>] :none
  10. # @option [Boolean] :local
  11. # @option [Boolean] :remote
  12. # @option [Boolean] :only_media
  13. def initialize(tag, account, options = {})
  14. @tag = tag
  15. super(account, options)
  16. end
  17. # @param [Integer] limit
  18. # @param [Integer] max_id
  19. # @param [Integer] since_id
  20. # @param [Integer] min_id
  21. # @return [Array<Status>]
  22. def get(limit, max_id = nil, since_id = nil, min_id = nil)
  23. scope = public_scope
  24. scope.merge!(tagged_with_any_scope)
  25. scope.merge!(tagged_with_all_scope)
  26. scope.merge!(tagged_with_none_scope)
  27. scope.merge!(local_only_scope) if local_only?
  28. scope.merge!(remote_only_scope) if remote_only?
  29. scope.merge!(account_filters_scope) if account?
  30. scope.merge!(media_only_scope) if media_only?
  31. scope.cache_ids.to_a_paginated_by_id(limit, max_id: max_id, since_id: since_id, min_id: min_id)
  32. end
  33. private
  34. def tagged_with_any_scope
  35. Status.group(:id).tagged_with(tags_for(Array(@tag.name) | Array(options[:any])))
  36. end
  37. def tagged_with_all_scope
  38. Status.group(:id).tagged_with_all(tags_for(options[:all]))
  39. end
  40. def tagged_with_none_scope
  41. Status.group(:id).tagged_with_none(tags_for(options[:none]))
  42. end
  43. def tags_for(names)
  44. Tag.matching_name(Array(names).take(LIMIT_PER_MODE)).pluck(:id) if names.present?
  45. end
  46. end