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.

65 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. 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(results.map(&:status), Status)
  16. end
  17. def results
  18. @_results ||= account_favourites.eager_load(:status).to_a_paginated_by_id(
  19. limit_param(DEFAULT_STATUSES_LIMIT),
  20. params_slice(:max_id, :since_id, :min_id)
  21. )
  22. end
  23. def account_favourites
  24. current_account.favourites
  25. end
  26. def insert_pagination_headers
  27. set_pagination_headers(next_path, prev_path)
  28. end
  29. def next_path
  30. if records_continue?
  31. api_v1_favourites_url pagination_params(max_id: pagination_max_id)
  32. end
  33. end
  34. def prev_path
  35. unless results.empty?
  36. api_v1_favourites_url pagination_params(min_id: pagination_since_id)
  37. end
  38. end
  39. def pagination_max_id
  40. results.last.id
  41. end
  42. def pagination_since_id
  43. results.first.id
  44. end
  45. def records_continue?
  46. results.size == limit_param(DEFAULT_STATUSES_LIMIT)
  47. end
  48. def pagination_params(core_params)
  49. params.slice(:limit).permit(:limit).merge(core_params)
  50. end
  51. end