Add Account#actor_type_application? query method (#33525)

This commit is contained in:
Matt Jankowski 2025-01-09 09:32:48 -05:00 committed by GitHub
parent a8b2b474d7
commit 9b8d1fb6d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

View File

@ -107,14 +107,14 @@ class Account < ApplicationRecord
validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? }
# Remote user validations, also applies to internal actors
validates :username, format: { with: USERNAME_ONLY_RE }, if: -> { (remote? || actor_type == 'Application') && will_save_change_to_username? }
validates :username, format: { with: USERNAME_ONLY_RE }, if: -> { (remote? || actor_type_application?) && will_save_change_to_username? }
# Remote user validations
validates :uri, presence: true, unless: :local?, on: :create
# Local user validations
validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: USERNAME_LENGTH_LIMIT }, if: -> { local? && will_save_change_to_username? && actor_type != 'Application' }
validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? && actor_type != 'Application' }
validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: USERNAME_LENGTH_LIMIT }, if: -> { local? && will_save_change_to_username? && !actor_type_application? }
validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? && !actor_type_application? }
validates :display_name, length: { maximum: DISPLAY_NAME_LENGTH_LIMIT }, if: -> { local? && will_save_change_to_display_name? }
validates :note, note_length: { maximum: NOTE_LENGTH_LIMIT }, if: -> { local? && will_save_change_to_note? }
validates :fields, length: { maximum: DEFAULT_FIELDS_SIZE }, if: -> { local? && will_save_change_to_fields? }
@ -208,6 +208,10 @@ class Account < ApplicationRecord
self.actor_type = ActiveModel::Type::Boolean.new.cast(val) ? 'Service' : 'Person'
end
def actor_type_application?
actor_type == 'Application'
end
def group?
actor_type == 'Group'
end

View File

@ -80,6 +80,20 @@ RSpec.describe Account do
end
end
describe '#actor_type_application?' do
context 'when the actor is not of type application' do
subject { Fabricate.build :account, actor_type: 'Person' }
it { is_expected.to_not be_actor_type_application }
end
context 'when the actor is of type application' do
subject { Fabricate.build :account, actor_type: 'Application' }
it { is_expected.to be_actor_type_application }
end
end
describe 'Local domain user methods' do
subject { Fabricate(:account, domain: nil, username: 'alice') }