闭社主体 forked from https://github.com/tootsuite/mastodon
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.

74 lines
1.4 KiB

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