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.

77 lines
1.8 KiB

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