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.

86 lines
3.1 KiB

  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_enabled_registrations, only: [:create]
  11. skip_before_action :require_authenticated_user!, only: :create
  12. override_rate_limit_headers :follow, family: :follows
  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), with_rate_limit: true)
  25. options = @account.locked? || current_user.account.silenced? ? {} : { 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 account_params
  56. params.permit(:username, :email, :password, :agreement, :locale, :reason)
  57. end
  58. def check_enabled_registrations
  59. forbidden if single_user_mode? || !allowed_registrations?
  60. end
  61. def allowed_registrations?
  62. Setting.registrations_mode != 'none'
  63. end
  64. end