accounts_controller.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # frozen_string_literal: true
  2. class Api::V1::AccountsController < Api::BaseController
  3. include RegistrationHelper
  4. before_action -> { authorize_if_got_token! :read, :'read:accounts' }, except: [:create, :follow, :unfollow, :remove_from_followers, :block, :unblock, :mute, :unmute]
  5. before_action -> { doorkeeper_authorize! :follow, :write, :'write:follows' }, only: [:follow, :unfollow, :remove_from_followers]
  6. before_action -> { doorkeeper_authorize! :follow, :write, :'write:mutes' }, only: [:mute, :unmute]
  7. before_action -> { doorkeeper_authorize! :follow, :write, :'write:blocks' }, only: [:block, :unblock]
  8. before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, only: [:create]
  9. before_action :require_user!, except: [:show, :create]
  10. before_action :set_account, except: [:create]
  11. before_action :check_account_approval, except: [:create]
  12. before_action :check_account_confirmation, except: [:create]
  13. before_action :check_enabled_registrations, only: [:create]
  14. skip_before_action :require_authenticated_user!, only: :create
  15. override_rate_limit_headers :follow, family: :follows
  16. def show
  17. cache_if_unauthenticated!
  18. render json: @account, serializer: REST::AccountSerializer
  19. end
  20. def create
  21. token = AppSignUpService.new.call(doorkeeper_token.application, request.remote_ip, account_params)
  22. response = Doorkeeper::OAuth::TokenResponse.new(token)
  23. headers.merge!(response.headers)
  24. self.response_body = Oj.dump(response.body)
  25. self.status = response.status
  26. rescue ActiveRecord::RecordInvalid => e
  27. render json: ValidationErrorFormatter.new(e, 'account.username': :username, 'invite_request.text': :reason).as_json, status: 422
  28. end
  29. def follow
  30. follow = FollowService.new.call(current_user.account, @account, reblogs: params.key?(:reblogs) ? truthy_param?(:reblogs) : nil, notify: params.key?(:notify) ? truthy_param?(:notify) : nil, languages: params.key?(:languages) ? params[:languages] : nil, with_rate_limit: true)
  31. options = @account.locked? || current_user.account.silenced? ? {} : { following_map: { @account.id => { reblogs: follow.show_reblogs?, notify: follow.notify?, languages: follow.languages } }, requested_map: { @account.id => false } }
  32. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships(**options)
  33. end
  34. def block
  35. BlockService.new.call(current_user.account, @account)
  36. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  37. end
  38. def mute
  39. MuteService.new.call(current_user.account, @account, notifications: truthy_param?(:notifications), duration: params[:duration].to_i)
  40. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  41. end
  42. def unfollow
  43. UnfollowService.new.call(current_user.account, @account)
  44. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  45. end
  46. def remove_from_followers
  47. RemoveFromFollowersService.new.call(current_user.account, @account)
  48. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  49. end
  50. def unblock
  51. UnblockService.new.call(current_user.account, @account)
  52. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  53. end
  54. def unmute
  55. UnmuteService.new.call(current_user.account, @account)
  56. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  57. end
  58. private
  59. def set_account
  60. @account = Account.find(params[:id])
  61. end
  62. def check_account_approval
  63. raise(ActiveRecord::RecordNotFound) if @account.local? && @account.user_pending?
  64. end
  65. def check_account_confirmation
  66. raise(ActiveRecord::RecordNotFound) if @account.local? && !@account.user_confirmed?
  67. end
  68. def relationships(**options)
  69. AccountRelationshipsPresenter.new([@account], current_user.account_id, **options)
  70. end
  71. def account_params
  72. params.permit(:username, :email, :password, :agreement, :locale, :reason, :time_zone, :invite_code)
  73. end
  74. def invite
  75. Invite.find_by(code: params[:invite_code]) if params[:invite_code].present?
  76. end
  77. def check_enabled_registrations
  78. forbidden unless allowed_registration?(request.remote_ip, invite)
  79. end
  80. end