statuses_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. @statuses = load_statuses
  8. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  9. end
  10. private
  11. def set_account
  12. @account = Account.find(params[:account_id])
  13. end
  14. def load_statuses
  15. @account.suspended? ? [] : cached_account_statuses
  16. end
  17. def cached_account_statuses
  18. cache_collection_paginated_by_id(
  19. AccountStatusesFilter.new(@account, current_account, params).results,
  20. Status,
  21. limit_param(DEFAULT_STATUSES_LIMIT),
  22. params_slice(:max_id, :since_id, :min_id)
  23. )
  24. end
  25. def pagination_params(core_params)
  26. params.slice(:limit, *AccountStatusesFilter::KEYS).permit(:limit, *AccountStatusesFilter::KEYS).merge(core_params)
  27. end
  28. def insert_pagination_headers
  29. set_pagination_headers(next_path, prev_path)
  30. end
  31. def next_path
  32. if records_continue?
  33. api_v1_account_statuses_url pagination_params(max_id: pagination_max_id)
  34. end
  35. end
  36. def prev_path
  37. unless @statuses.empty?
  38. api_v1_account_statuses_url pagination_params(min_id: pagination_since_id)
  39. end
  40. end
  41. def records_continue?
  42. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  43. end
  44. def pagination_max_id
  45. @statuses.last.id
  46. end
  47. def pagination_since_id
  48. @statuses.first.id
  49. end
  50. end