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.

71 lines
1.5 KiB

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