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.

76 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::FavouritedByAccountsController < Api::BaseController
  3. include Authorization
  4. before_action -> { authorize_if_got_token! :read, :'read:accounts' }
  5. before_action :set_status
  6. after_action :insert_pagination_headers
  7. def index
  8. @accounts = load_accounts
  9. render json: @accounts, each_serializer: REST::AccountSerializer
  10. end
  11. private
  12. def load_accounts
  13. scope = default_accounts
  14. scope = scope.where.not(id: current_account.excluded_from_timeline_account_ids) unless current_account.nil?
  15. scope.merge(paginated_favourites).to_a
  16. end
  17. def default_accounts
  18. Account
  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. if records_continue?
  35. api_v1_status_favourited_by_index_url pagination_params(max_id: pagination_max_id)
  36. end
  37. end
  38. def prev_path
  39. unless @accounts.empty?
  40. api_v1_status_favourited_by_index_url pagination_params(since_id: pagination_since_id)
  41. end
  42. end
  43. def pagination_max_id
  44. @accounts.last.favourites.last.id
  45. end
  46. def pagination_since_id
  47. @accounts.first.favourites.first.id
  48. end
  49. def records_continue?
  50. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  51. end
  52. def set_status
  53. @status = Status.find(params[:status_id])
  54. authorize @status, :show?
  55. rescue Mastodon::NotPermittedError
  56. not_found
  57. end
  58. def pagination_params(core_params)
  59. params.slice(:limit).permit(:limit).merge(core_params)
  60. end
  61. end