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.

87 lines
3.4 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, 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 unblock
  43. UnblockService.new.call(current_user.account, @account)
  44. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  45. end
  46. def unmute
  47. UnmuteService.new.call(current_user.account, @account)
  48. render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships
  49. end
  50. private
  51. def set_account
  52. @account = Account.find(params[:id])
  53. end
  54. def relationships(**options)
  55. AccountRelationshipsPresenter.new([@account.id], current_user.account_id, **options)
  56. end
  57. def account_params
  58. params.permit(:username, :email, :password, :agreement, :locale, :reason)
  59. end
  60. def check_enabled_registrations
  61. forbidden if single_user_mode? || !allowed_registrations?
  62. end
  63. def allowed_registrations?
  64. Setting.registrations_mode != 'none'
  65. end
  66. end