favourites_controller.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. class Api::V1::FavouritesController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read, :'read:favourites' }
  4. before_action :require_user!
  5. after_action :insert_pagination_headers
  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 load_statuses
  12. cached_favourites
  13. end
  14. def cached_favourites
  15. cache_collection(
  16. Status.reorder(nil).joins(:favourites).merge(results),
  17. Status
  18. )
  19. end
  20. def results
  21. @_results ||= account_favourites.paginate_by_id(
  22. limit_param(DEFAULT_STATUSES_LIMIT),
  23. params_slice(:max_id, :since_id, :min_id)
  24. )
  25. end
  26. def account_favourites
  27. current_account.favourites
  28. end
  29. def insert_pagination_headers
  30. set_pagination_headers(next_path, prev_path)
  31. end
  32. def next_path
  33. if records_continue?
  34. api_v1_favourites_url pagination_params(max_id: pagination_max_id)
  35. end
  36. end
  37. def prev_path
  38. unless results.empty?
  39. api_v1_favourites_url pagination_params(min_id: pagination_since_id)
  40. end
  41. end
  42. def pagination_max_id
  43. results.last.id
  44. end
  45. def pagination_since_id
  46. results.first.id
  47. end
  48. def records_continue?
  49. results.size == limit_param(DEFAULT_STATUSES_LIMIT)
  50. end
  51. def pagination_params(core_params)
  52. params.slice(:limit).permit(:limit).merge(core_params)
  53. end
  54. end