accounts_controller.rb 3.2 KB

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