public_feed.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if account?
  26. scope.merge!(account_filters_scope)
  27. else
  28. scope.merge!(instance_only_statuses_scope)
  29. end
  30. scope.merge!(media_only_scope) if media_only?
  31. scope.merge!(language_scope) if account&.chosen_languages.present?
  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. attr_reader :account, :options
  36. def with_reblogs?
  37. options[:with_reblogs]
  38. end
  39. def with_replies?
  40. options[:with_replies]
  41. end
  42. def local_only?
  43. options[:local]
  44. end
  45. def without_local_only?
  46. options[:without_local_only]
  47. end
  48. def remote_only?
  49. options[:remote]
  50. end
  51. def account?
  52. account.present?
  53. end
  54. def media_only?
  55. options[:only_media]
  56. end
  57. def public_scope
  58. Status.with_public_visibility.joins(:account).merge(Account.without_suspended.without_silenced)
  59. end
  60. def local_only_scope
  61. Status.local
  62. end
  63. def remote_only_scope
  64. Status.remote
  65. end
  66. def without_local_only_scope
  67. Status.without_local_only
  68. end
  69. def without_replies_scope
  70. Status.without_replies
  71. end
  72. def without_reblogs_scope
  73. Status.without_reblogs
  74. end
  75. def media_only_scope
  76. Status.joins(:media_attachments).group(:id)
  77. end
  78. def instance_only_statuses_scope
  79. Status.where(local_only: [false, nil])
  80. end
  81. def language_scope
  82. Status.where(language: account.chosen_languages)
  83. end
  84. def account_filters_scope
  85. Status.not_excluded_by_account(account).tap do |scope|
  86. scope.merge!(Status.not_domain_blocked_by_account(account)) unless local_only?
  87. end
  88. end
  89. end