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.

73 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::FavouritesController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read }
  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.where(
  18. id: results.map(&:status_id)
  19. ),
  20. Status
  21. )
  22. end
  23. def results
  24. @_results ||= account_favourites.paginate_by_max_id(
  25. limit_param(DEFAULT_STATUSES_LIMIT),
  26. params[:max_id],
  27. params[:since_id]
  28. )
  29. end
  30. def account_favourites
  31. current_account.favourites
  32. end
  33. def insert_pagination_headers
  34. set_pagination_headers(next_path, prev_path)
  35. end
  36. def next_path
  37. if records_continue?
  38. api_v1_favourites_url pagination_params(max_id: pagination_max_id)
  39. end
  40. end
  41. def prev_path
  42. unless results.empty?
  43. api_v1_favourites_url pagination_params(since_id: pagination_since_id)
  44. end
  45. end
  46. def pagination_max_id
  47. results.last.id
  48. end
  49. def pagination_since_id
  50. results.first.id
  51. end
  52. def records_continue?
  53. results.size == limit_param(DEFAULT_STATUSES_LIMIT)
  54. end
  55. def pagination_params(core_params)
  56. params.permit(:limit).merge(core_params)
  57. end
  58. end