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.

90 lines
2.0 KiB

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