directories_controller.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # frozen_string_literal: true
  2. class Api::V1::DirectoriesController < Api::BaseController
  3. before_action :require_enabled!
  4. before_action :set_accounts
  5. def show
  6. cache_if_unauthenticated!
  7. render json: @accounts, each_serializer: REST::AccountSerializer
  8. end
  9. private
  10. def require_enabled!
  11. return not_found unless Setting.profile_directory
  12. end
  13. def set_accounts
  14. with_read_replica do
  15. @accounts = accounts_scope.offset(params[:offset]).limit(limit_param(DEFAULT_ACCOUNTS_LIMIT))
  16. end
  17. end
  18. def accounts_scope
  19. Account.discoverable.tap do |scope|
  20. scope.merge!(account_order_scope)
  21. scope.merge!(local_account_scope) if local_accounts?
  22. scope.merge!(account_exclusion_scope) if current_account
  23. scope.merge!(account_domain_block_scope) if current_account && !local_accounts?
  24. end
  25. end
  26. def local_accounts?
  27. truthy_param?(:local)
  28. end
  29. def account_order_scope
  30. case params[:order]
  31. when 'new'
  32. Account.order(id: :desc)
  33. when 'active', nil
  34. Account.by_recent_status
  35. end
  36. end
  37. def local_account_scope
  38. Account.local
  39. end
  40. def account_exclusion_scope
  41. Account.not_excluded_by_account(current_account)
  42. end
  43. def account_domain_block_scope
  44. Account.not_domain_blocked_by_account(current_account)
  45. end
  46. end