logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

reblogged_by_accounts_controller.rb (1903B)


  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 json: @accounts, each_serializer: REST::AccountSerializer
  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.slice(:limit).permit(:limit).merge(core_params)
  61. end
  62. end