reblogged_by_accounts_controller.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::RebloggedByAccountsController < 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_statuses).to_a
  16. end
  17. def default_accounts
  18. Account.without_suspended.includes(:statuses, :account_stat).references(:statuses)
  19. end
  20. def paginated_statuses
  21. Status.where(reblog_of_id: @status.id).where(visibility: [:public, :unlisted]).paginate_by_max_id(
  22. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  23. params[:max_id],
  24. params[:since_id]
  25. )
  26. end
  27. def insert_pagination_headers
  28. set_pagination_headers(next_path, prev_path)
  29. end
  30. def next_path
  31. if records_continue?
  32. api_v1_status_reblogged_by_index_url pagination_params(max_id: pagination_max_id)
  33. end
  34. end
  35. def prev_path
  36. unless @accounts.empty?
  37. api_v1_status_reblogged_by_index_url pagination_params(since_id: pagination_since_id)
  38. end
  39. end
  40. def pagination_max_id
  41. @accounts.last.statuses.last.id
  42. end
  43. def pagination_since_id
  44. @accounts.first.statuses.first.id
  45. end
  46. def records_continue?
  47. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  48. end
  49. def set_status
  50. @status = Status.find(params[:status_id])
  51. authorize @status, :show?
  52. rescue Mastodon::NotPermittedError
  53. not_found
  54. end
  55. def pagination_params(core_params)
  56. params.slice(:limit).permit(:limit).merge(core_params)
  57. end
  58. end