statuses_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::StatusesController < Api::BaseController
  3. before_action -> { authorize_if_got_token! :read, :'read:statuses' }
  4. before_action :set_account
  5. after_action :insert_pagination_headers, unless: -> { truthy_param?(:pinned) }
  6. def index
  7. cache_if_unauthenticated!
  8. @statuses = load_statuses
  9. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  10. end
  11. private
  12. def set_account
  13. @account = Account.find(params[:account_id])
  14. end
  15. def load_statuses
  16. @account.unavailable? ? [] : cached_account_statuses
  17. end
  18. def cached_account_statuses
  19. cache_collection_paginated_by_id(
  20. AccountStatusesFilter.new(@account, current_account, params).results,
  21. Status,
  22. limit_param(DEFAULT_STATUSES_LIMIT),
  23. params_slice(:max_id, :since_id, :min_id)
  24. )
  25. end
  26. def pagination_params(core_params)
  27. params.slice(:limit, *AccountStatusesFilter::KEYS).permit(:limit, *AccountStatusesFilter::KEYS).merge(core_params)
  28. end
  29. def insert_pagination_headers
  30. set_pagination_headers(next_path, prev_path)
  31. end
  32. def next_path
  33. api_v1_account_statuses_url pagination_params(max_id: pagination_max_id) if records_continue?
  34. end
  35. def prev_path
  36. api_v1_account_statuses_url pagination_params(min_id: pagination_since_id) unless @statuses.empty?
  37. end
  38. def records_continue?
  39. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  40. end
  41. def pagination_max_id
  42. @statuses.last.id
  43. end
  44. def pagination_since_id
  45. @statuses.first.id
  46. end
  47. end