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.

75 lines
1.6 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. end
  9. def authorize
  10. AuthorizeFollowService.new.call(account, current_account)
  11. render_empty
  12. end
  13. def reject
  14. RejectFollowService.new.call(account, current_account)
  15. render_empty
  16. end
  17. private
  18. def account
  19. Account.find(params[:id])
  20. end
  21. def load_accounts
  22. default_accounts.merge(paginated_follow_requests).to_a
  23. end
  24. def default_accounts
  25. Account.includes(:follow_requests).references(:follow_requests)
  26. end
  27. def paginated_follow_requests
  28. FollowRequest.where(target_account: current_account).paginate_by_max_id(
  29. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  30. params[:max_id],
  31. params[:since_id]
  32. )
  33. end
  34. def insert_pagination_headers
  35. set_pagination_headers(next_path, prev_path)
  36. end
  37. def next_path
  38. if records_continue?
  39. api_v1_follow_requests_url pagination_params(max_id: pagination_max_id)
  40. end
  41. end
  42. def prev_path
  43. unless @accounts.empty?
  44. api_v1_follow_requests_url pagination_params(since_id: pagination_since_id)
  45. end
  46. end
  47. def pagination_max_id
  48. @accounts.last.follow_requests.last.id
  49. end
  50. def pagination_since_id
  51. @accounts.first.follow_requests.first.id
  52. end
  53. def records_continue?
  54. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  55. end
  56. def pagination_params(core_params)
  57. params.permit(:limit).merge(core_params)
  58. end
  59. end