1
0

accounts_controller.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, :block, :unblock, :mute, :unmute]
  4. before_action -> { doorkeeper_authorize! :follow, :'write:follows' }, only: [:follow, :unfollow]
  5. before_action -> { doorkeeper_authorize! :follow, :'write:mutes' }, only: [:mute, :unmute]
  6. before_action -> { doorkeeper_authorize! :follow, :'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_suspension, only: [:show]
  11. before_action :check_enabled_registrations, only: [:create]
  12. respond_to :json
  13. def show
  14. render json: @account, serializer: REST::AccountSerializer
  15. end
  16. def create
  17. token = AppSignUpService.new.call(doorkeeper_token.application, account_params)
  18. response = Doorkeeper::OAuth::TokenResponse.new(token)
  19. headers.merge!(response.headers)
  20. self.response_body = Oj.dump(response.body)
  21. self.status = response.status
  22. end
  23. def follow
  24. FollowService.new.call(current_user.account, @account, reblogs: truthy_param?(:reblogs))
  25. options = @account.locked? ? {} : { following_map: { @account.id => { reblogs: truthy_param?(:reblogs) } }, requested_map: { @account.id => false } }
  26. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships(options)
  27. end
  28. def block
  29. BlockService.new.call(current_user.account, @account)
  30. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  31. end
  32. def mute
  33. MuteService.new.call(current_user.account, @account, notifications: truthy_param?(:notifications))
  34. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  35. end
  36. def unfollow
  37. UnfollowService.new.call(current_user.account, @account)
  38. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  39. end
  40. def unblock
  41. UnblockService.new.call(current_user.account, @account)
  42. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  43. end
  44. def unmute
  45. UnmuteService.new.call(current_user.account, @account)
  46. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  47. end
  48. private
  49. def set_account
  50. @account = Account.find(params[:id])
  51. end
  52. def relationships(**options)
  53. AccountRelationshipsPresenter.new([@account.id], current_user.account_id, options)
  54. end
  55. def check_account_suspension
  56. gone if @account.suspended?
  57. end
  58. def account_params
  59. params.permit(:username, :email, :password, :agreement, :locale)
  60. end
  61. def check_enabled_registrations
  62. forbidden if single_user_mode? || !allowed_registrations?
  63. end
  64. def allowed_registrations?
  65. Setting.registrations_mode != 'none'
  66. end
  67. end