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

  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. api_v1_follow_requests_url pagination_params(max_id: pagination_max_id) if records_continue?
  45. end
  46. def prev_path
  47. api_v1_follow_requests_url pagination_params(since_id: pagination_since_id) unless @accounts.empty?
  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