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.

96 lines
3.8 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, :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_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, request.remote_ip, 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. rescue ActiveRecord::RecordInvalid => e
  23. render json: ValidationErrorFormatter.new(e, :'account.username' => :username, :'invite_request.text' => :reason).as_json, status: :unprocessable_entity
  24. end
  25. def follow
  26. follow = FollowService.new.call(current_user.account, @account, reblogs: params.key?(:reblogs) ? truthy_param?(:reblogs) : nil, notify: params.key?(:notify) ? truthy_param?(:notify) : nil, with_rate_limit: true)
  27. options = @account.locked? || current_user.account.silenced? ? {} : { following_map: { @account.id => { reblogs: follow.show_reblogs?, notify: follow.notify? } }, requested_map: { @account.id => false } }
  28. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships(**options)
  29. end
  30. def block
  31. BlockService.new.call(current_user.account, @account)
  32. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  33. end
  34. def mute
  35. MuteService.new.call(current_user.account, @account, notifications: truthy_param?(:notifications), duration: (params[:duration]&.to_i || 0))
  36. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  37. end
  38. def unfollow
  39. UnfollowService.new.call(current_user.account, @account)
  40. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  41. end
  42. def remove_from_followers
  43. RemoveFromFollowersService.new.call(current_user.account, @account)
  44. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  45. end
  46. def unblock
  47. UnblockService.new.call(current_user.account, @account)
  48. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  49. end
  50. def unmute
  51. UnmuteService.new.call(current_user.account, @account)
  52. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  53. end
  54. private
  55. def set_account
  56. @account = Account.find(params[:id])
  57. end
  58. def relationships(**options)
  59. AccountRelationshipsPresenter.new([@account.id], current_user.account_id, **options)
  60. end
  61. def account_params
  62. params.permit(:username, :email, :password, :agreement, :locale, :reason)
  63. end
  64. def check_enabled_registrations
  65. forbidden if single_user_mode? || omniauth_only? || !allowed_registrations?
  66. end
  67. def allowed_registrations?
  68. Setting.registrations_mode != 'none'
  69. end
  70. def omniauth_only?
  71. ENV['OMNIAUTH_ONLY'] == 'true'
  72. end
  73. end