闭社主体 forked from https://github.com/tootsuite/mastodon
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.

64 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::BlocksController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :follow }
  4. before_action :require_user!
  5. after_action :insert_pagination_headers
  6. respond_to :json
  7. def index
  8. @accounts = load_accounts
  9. render json: @accounts, each_serializer: REST::AccountSerializer
  10. end
  11. private
  12. def load_accounts
  13. default_accounts.merge(paginated_blocks).to_a
  14. end
  15. def default_accounts
  16. Account.includes(:blocked_by).references(:blocked_by)
  17. end
  18. def paginated_blocks
  19. Block.where(account: current_account).paginate_by_max_id(
  20. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  21. params[:max_id],
  22. params[:since_id]
  23. )
  24. end
  25. def insert_pagination_headers
  26. set_pagination_headers(next_path, prev_path)
  27. end
  28. def next_path
  29. if records_continue?
  30. api_v1_blocks_url pagination_params(max_id: pagination_max_id)
  31. end
  32. end
  33. def prev_path
  34. unless @accounts.empty?
  35. api_v1_blocks_url pagination_params(since_id: pagination_since_id)
  36. end
  37. end
  38. def pagination_max_id
  39. @accounts.last.blocked_by_ids.last
  40. end
  41. def pagination_since_id
  42. @accounts.first.blocked_by_ids.first
  43. end
  44. def records_continue?
  45. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  46. end
  47. def pagination_params(core_params)
  48. params.permit(:limit).merge(core_params)
  49. end
  50. end