follow_requests_controller.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # frozen_string_literal: true
  2. class Api::V1::FollowRequestsController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :follow, :read, :'read:follows' }, only: :index
  4. before_action -> { doorkeeper_authorize! :follow, :write, :'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. LocalNotificationWorker.perform_async(current_account.id, Follow.find_by(account: account, target_account: current_account).id, 'Follow', 'follow')
  14. render json: account, serializer: REST::RelationshipSerializer, relationships: relationships
  15. end
  16. def reject
  17. RejectFollowService.new.call(account, current_account)
  18. render json: account, serializer: REST::RelationshipSerializer, relationships: relationships
  19. end
  20. private
  21. def account
  22. Account.find(params[:id])
  23. end
  24. def relationships(**options)
  25. AccountRelationshipsPresenter.new([params[:id]], current_user.account_id, **options)
  26. end
  27. def load_accounts
  28. default_accounts.merge(paginated_follow_requests).to_a
  29. end
  30. def default_accounts
  31. Account.without_suspended.includes(:follow_requests, :account_stat).references(:follow_requests)
  32. end
  33. def paginated_follow_requests
  34. FollowRequest.where(target_account: current_account).paginate_by_max_id(
  35. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  36. params[:max_id],
  37. params[:since_id]
  38. )
  39. end
  40. def insert_pagination_headers
  41. set_pagination_headers(next_path, prev_path)
  42. end
  43. def next_path
  44. if records_continue?
  45. api_v1_follow_requests_url pagination_params(max_id: pagination_max_id)
  46. end
  47. end
  48. def prev_path
  49. unless @accounts.empty?
  50. api_v1_follow_requests_url pagination_params(since_id: pagination_since_id)
  51. end
  52. end
  53. def pagination_max_id
  54. @accounts.last.follow_requests.last.id
  55. end
  56. def pagination_since_id
  57. @accounts.first.follow_requests.first.id
  58. end
  59. def records_continue?
  60. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  61. end
  62. def pagination_params(core_params)
  63. params.slice(:limit).permit(:limit).merge(core_params)
  64. end
  65. end