bookmarks_controller.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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(results.map(&:status), Status)
  16. end
  17. def results
  18. @results ||= account_bookmarks.joins(:status).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_bookmarks
  24. current_account.bookmarks
  25. end
  26. def insert_pagination_headers
  27. set_pagination_headers(next_path, prev_path)
  28. end
  29. def next_path
  30. api_v1_bookmarks_url pagination_params(max_id: pagination_max_id) if records_continue?
  31. end
  32. def prev_path
  33. api_v1_bookmarks_url pagination_params(min_id: pagination_since_id) unless results.empty?
  34. end
  35. def pagination_max_id
  36. results.last.id
  37. end
  38. def pagination_since_id
  39. results.first.id
  40. end
  41. def records_continue?
  42. results.size == limit_param(DEFAULT_STATUSES_LIMIT)
  43. end
  44. def pagination_params(core_params)
  45. params.slice(:limit).permit(:limit).merge(core_params)
  46. end
  47. end