favourited_by_accounts_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::FavouritedByAccountsController < Api::V1::Statuses::BaseController
  3. before_action -> { authorize_if_got_token! :read, :'read:accounts' }
  4. after_action :insert_pagination_headers
  5. def index
  6. cache_if_unauthenticated!
  7. @accounts = load_accounts
  8. render json: @accounts, each_serializer: REST::AccountSerializer
  9. end
  10. private
  11. def load_accounts
  12. scope = default_accounts
  13. scope = scope.where.not(id: current_account.excluded_from_timeline_account_ids) unless current_account.nil?
  14. scope.merge(paginated_favourites).to_a
  15. end
  16. def default_accounts
  17. Account
  18. .without_suspended
  19. .includes(:favourites, :account_stat)
  20. .references(:favourites)
  21. .where(favourites: { status_id: @status.id })
  22. end
  23. def paginated_favourites
  24. Favourite.paginate_by_max_id(
  25. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  26. params[:max_id],
  27. params[:since_id]
  28. )
  29. end
  30. def insert_pagination_headers
  31. set_pagination_headers(next_path, prev_path)
  32. end
  33. def next_path
  34. api_v1_status_favourited_by_index_url pagination_params(max_id: pagination_max_id) if records_continue?
  35. end
  36. def prev_path
  37. api_v1_status_favourited_by_index_url pagination_params(since_id: pagination_since_id) unless @accounts.empty?
  38. end
  39. def pagination_max_id
  40. @accounts.last.favourites.last.id
  41. end
  42. def pagination_since_id
  43. @accounts.first.favourites.first.id
  44. end
  45. def records_continue?
  46. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  47. end
  48. def pagination_params(core_params)
  49. params.slice(:limit).permit(:limit).merge(core_params)
  50. end
  51. end