logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

favourites_controller.rb (1516B)


  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.reorder(nil).joins(:favourites).merge(results),
  18. Status
  19. )
  20. end
  21. def results
  22. @_results ||= account_favourites.paginate_by_max_id(
  23. limit_param(DEFAULT_STATUSES_LIMIT),
  24. params[:max_id],
  25. params[:since_id]
  26. )
  27. end
  28. def account_favourites
  29. current_account.favourites
  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_favourites_url pagination_params(max_id: pagination_max_id)
  37. end
  38. end
  39. def prev_path
  40. unless results.empty?
  41. api_v1_favourites_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