public_feed.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. class PublicFeed
  3. # @param [Account] account
  4. # @param [Hash] options
  5. # @option [Boolean] :with_replies
  6. # @option [Boolean] :with_reblogs
  7. # @option [Boolean] :local
  8. # @option [Boolean] :remote
  9. # @option [Boolean] :only_media
  10. def initialize(account, options = {})
  11. @account = account
  12. @options = options
  13. end
  14. # @param [Integer] limit
  15. # @param [Integer] max_id
  16. # @param [Integer] since_id
  17. # @param [Integer] min_id
  18. # @return [Array<Status>]
  19. def get(limit, max_id = nil, since_id = nil, min_id = nil)
  20. scope = public_scope
  21. scope.merge!(without_replies_scope) unless with_replies?
  22. scope.merge!(without_reblogs_scope) unless with_reblogs?
  23. scope.merge!(local_only_scope) if local_only?
  24. scope.merge!(remote_only_scope) if remote_only?
  25. scope.merge!(account_filters_scope) if account?
  26. scope.merge!(media_only_scope) if media_only?
  27. scope.merge!(language_scope) if account&.chosen_languages.present?
  28. scope.cache_ids.to_a_paginated_by_id(limit, max_id: max_id, since_id: since_id, min_id: min_id)
  29. end
  30. private
  31. attr_reader :account, :options
  32. def with_reblogs?
  33. options[:with_reblogs]
  34. end
  35. def with_replies?
  36. options[:with_replies]
  37. end
  38. def local_only?
  39. options[:local] && !options[:remote]
  40. end
  41. def remote_only?
  42. options[:remote] && !options[:local]
  43. end
  44. def account?
  45. account.present?
  46. end
  47. def media_only?
  48. options[:only_media]
  49. end
  50. def public_scope
  51. Status.with_public_visibility.joins(:account).merge(Account.without_suspended.without_silenced)
  52. end
  53. def local_only_scope
  54. Status.local
  55. end
  56. def remote_only_scope
  57. Status.remote
  58. end
  59. def without_replies_scope
  60. Status.without_replies
  61. end
  62. def without_reblogs_scope
  63. Status.without_reblogs
  64. end
  65. def media_only_scope
  66. Status.joins(:media_attachments).group(:id)
  67. end
  68. def language_scope
  69. Status.where(language: account.chosen_languages)
  70. end
  71. def account_filters_scope
  72. Status.not_excluded_by_account(account).tap do |scope|
  73. scope.merge!(Status.not_domain_blocked_by_account(account)) unless local_only?
  74. end
  75. end
  76. end