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.

79 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::RebloggedByAccountsController < Api::BaseController
  3. include Authorization
  4. before_action :authorize_if_got_token
  5. before_action :set_status
  6. after_action :insert_pagination_headers
  7. respond_to :json
  8. def index
  9. @accounts = load_accounts
  10. render 'api/v1/statuses/accounts'
  11. end
  12. private
  13. def load_accounts
  14. default_accounts.merge(paginated_statuses).to_a
  15. end
  16. def default_accounts
  17. Account.includes(:statuses).references(:statuses)
  18. end
  19. def paginated_statuses
  20. Status.where(reblog_of_id: @status.id).paginate_by_max_id(
  21. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  22. params[:max_id],
  23. params[:since_id]
  24. )
  25. end
  26. def insert_pagination_headers
  27. set_pagination_headers(next_path, prev_path)
  28. end
  29. def next_path
  30. if records_continue?
  31. api_v1_status_reblogged_by_index_url pagination_params(max_id: pagination_max_id)
  32. end
  33. end
  34. def prev_path
  35. unless @accounts.empty?
  36. api_v1_status_reblogged_by_index_url pagination_params(since_id: pagination_since_id)
  37. end
  38. end
  39. def pagination_max_id
  40. @accounts.last.statuses.last.id
  41. end
  42. def pagination_since_id
  43. @accounts.first.statuses.first.id
  44. end
  45. def records_continue?
  46. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  47. end
  48. def set_status
  49. @status = Status.find(params[:status_id])
  50. authorize @status, :show?
  51. rescue Mastodon::NotPermittedError
  52. # Reraise in order to get a 404 instead of a 403 error code
  53. raise ActiveRecord::RecordNotFound
  54. end
  55. def authorize_if_got_token
  56. request_token = Doorkeeper::OAuth::Token.from_request(request, *Doorkeeper.configuration.access_token_methods)
  57. doorkeeper_authorize! :read if request_token
  58. end
  59. def pagination_params(core_params)
  60. params.permit(:limit).merge(core_params)
  61. end
  62. end