public_feed.rb 2.2 KB

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