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.

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