accounts_controller.rb 4.2 KB

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