bookmarks_controller.rb 1.4 KB

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