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.

57 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. @account = account
  16. @options = options
  17. end
  18. # @param [Integer] limit
  19. # @param [Integer] max_id
  20. # @param [Integer] since_id
  21. # @param [Integer] min_id
  22. # @return [Array<Status>]
  23. def get(limit, max_id = nil, since_id = nil, min_id = nil)
  24. scope = public_scope
  25. scope.merge!(tagged_with_any_scope)
  26. scope.merge!(tagged_with_all_scope)
  27. scope.merge!(tagged_with_none_scope)
  28. scope.merge!(local_only_scope) if local_only?
  29. scope.merge!(remote_only_scope) if remote_only?
  30. scope.merge!(account_filters_scope) if account?
  31. scope.merge!(media_only_scope) if media_only?
  32. scope.cache_ids.to_a_paginated_by_id(limit, max_id: max_id, since_id: since_id, min_id: min_id)
  33. end
  34. private
  35. def tagged_with_any_scope
  36. Status.group(:id).tagged_with(tags_for(Array(@tag.name) | Array(@options[:any])))
  37. end
  38. def tagged_with_all_scope
  39. Status.group(:id).tagged_with_all(tags_for(@options[:all]))
  40. end
  41. def tagged_with_none_scope
  42. Status.group(:id).tagged_with_none(tags_for(@options[:none]))
  43. end
  44. def tags_for(names)
  45. Tag.matching_name(Array(names).take(LIMIT_PER_MODE)) if names.present?
  46. end
  47. end