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.

70 lines
1.5 KiB

  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. respond_to :json
  7. def index
  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 load_statuses
  13. cached_favourites
  14. end
  15. def cached_favourites
  16. cache_collection(
  17. Status.reorder(nil).joins(:favourites).merge(results),
  18. Status
  19. )
  20. end
  21. def results
  22. @_results ||= account_favourites.paginate_by_id(
  23. limit_param(DEFAULT_STATUSES_LIMIT),
  24. params_slice(:max_id, :since_id, :min_id)
  25. )
  26. end
  27. def account_favourites
  28. current_account.favourites
  29. end
  30. def insert_pagination_headers
  31. set_pagination_headers(next_path, prev_path)
  32. end
  33. def next_path
  34. if records_continue?
  35. api_v1_favourites_url pagination_params(max_id: pagination_max_id)
  36. end
  37. end
  38. def prev_path
  39. unless results.empty?
  40. api_v1_favourites_url pagination_params(min_id: pagination_since_id)
  41. end
  42. end
  43. def pagination_max_id
  44. results.last.id
  45. end
  46. def pagination_since_id
  47. results.first.id
  48. end
  49. def records_continue?
  50. results.size == limit_param(DEFAULT_STATUSES_LIMIT)
  51. end
  52. def pagination_params(core_params)
  53. params.slice(:limit).permit(:limit).merge(core_params)
  54. end
  55. end