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.

92 lines
2.0 KiB

  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.cache_ids.to_a_paginated_by_id(limit, max_id: max_id, since_id: since_id, min_id: min_id)
  28. end
  29. private
  30. attr_reader :account, :options
  31. def with_reblogs?
  32. options[:with_reblogs]
  33. end
  34. def with_replies?
  35. options[:with_replies]
  36. end
  37. def local_only?
  38. options[:local]
  39. end
  40. def remote_only?
  41. options[:remote]
  42. end
  43. def account?
  44. account.present?
  45. end
  46. def media_only?
  47. options[:only_media]
  48. end
  49. def public_scope
  50. Status.with_public_visibility.joins(:account).merge(Account.without_suspended.without_silenced)
  51. end
  52. def local_only_scope
  53. Status.local
  54. end
  55. def remote_only_scope
  56. Status.remote
  57. end
  58. def without_replies_scope
  59. Status.without_replies
  60. end
  61. def without_reblogs_scope
  62. Status.without_reblogs
  63. end
  64. def media_only_scope
  65. Status.joins(:media_attachments).group(:id)
  66. end
  67. def account_filters_scope
  68. Status.not_excluded_by_account(account).tap do |scope|
  69. scope.merge!(Status.not_domain_blocked_by_account(account)) unless local_only?
  70. scope.merge!(Status.in_chosen_languages(account)) if account.chosen_languages.present?
  71. end
  72. end
  73. end