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.7 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::FollowRequestsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :follow }
  4. before_action :require_user!
  5. after_action :insert_pagination_headers, only: :index
  6. def index
  7. @accounts = load_accounts
  8. render json: @accounts, each_serializer: REST::AccountSerializer
  9. end
  10. def authorize
  11. AuthorizeFollowService.new.call(account, current_account)
  12. render_empty
  13. end
  14. def reject
  15. RejectFollowService.new.call(account, current_account)
  16. render_empty
  17. end
  18. private
  19. def account
  20. Account.find(params[:id])
  21. end
  22. def load_accounts
  23. default_accounts.merge(paginated_follow_requests).to_a
  24. end
  25. def default_accounts
  26. Account.includes(:follow_requests).references(:follow_requests)
  27. end
  28. def paginated_follow_requests
  29. FollowRequest.where(target_account: current_account).paginate_by_max_id(
  30. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  31. params[:max_id],
  32. params[:since_id]
  33. )
  34. end
  35. def insert_pagination_headers
  36. set_pagination_headers(next_path, prev_path)
  37. end
  38. def next_path
  39. if records_continue?
  40. api_v1_follow_requests_url pagination_params(max_id: pagination_max_id)
  41. end
  42. end
  43. def prev_path
  44. unless @accounts.empty?
  45. api_v1_follow_requests_url pagination_params(since_id: pagination_since_id)
  46. end
  47. end
  48. def pagination_max_id
  49. @accounts.last.follow_requests.last.id
  50. end
  51. def pagination_since_id
  52. @accounts.first.follow_requests.first.id
  53. end
  54. def records_continue?
  55. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  56. end
  57. def pagination_params(core_params)
  58. params.permit(:limit).merge(core_params)
  59. end
  60. end