Add Account#remote? query method (#33508)

This commit is contained in:
Matt Jankowski 2025-01-08 09:28:08 -05:00 committed by GitHub
parent 15669fcf75
commit f22a2aab40
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 10 deletions

View file

@ -107,7 +107,7 @@ class Account < ApplicationRecord
validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? } validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? }
# Remote user validations, also applies to internal actors # Remote user validations, also applies to internal actors
validates :username, format: { with: USERNAME_ONLY_RE }, if: -> { (!local? || 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 # Remote user validations
validates :uri, presence: true, unless: :local?, on: :create validates :uri, presence: true, unless: :local?, on: :create
@ -186,6 +186,10 @@ class Account < ApplicationRecord
domain.nil? domain.nil?
end end
def remote?
domain.present?
end
def moved? def moved?
moved_to_account_id.present? moved_to_account_id.present?
end end

View file

@ -61,11 +61,7 @@ class Poll < ApplicationRecord
votes.where(account: account).pluck(:choice) votes.where(account: account).pluck(:choice)
end end
delegate :local?, to: :account delegate :local?, :remote?, to: :account
def remote?
!local?
end
def emojis def emojis
@emojis ||= CustomEmoji.from_text(options.join(' '), account.domain) @emojis ||= CustomEmoji.from_text(options.join(' '), account.domain)

View file

@ -48,14 +48,34 @@ RSpec.describe Account do
end end
describe '#local?' do describe '#local?' do
it 'returns true when the account is local' do it 'returns true when domain is null' do
account = Fabricate(:account, domain: nil) account = Fabricate(:account, domain: nil)
expect(account.local?).to be true expect(account).to be_local
end end
it 'returns false when the account is on a different domain' do it 'returns false when domain is present' do
account = Fabricate(:account, domain: 'foreign.tld') account = Fabricate(:account, domain: 'foreign.tld')
expect(account.local?).to be false expect(account).to_not be_local
end
end
describe '#remote?' do
context 'when the domain is null' do
subject { Fabricate.build :account, domain: nil }
it { is_expected.to_not be_remote }
end
context 'when the domain is blank' do
subject { Fabricate.build :account, domain: '' }
it { is_expected.to_not be_remote }
end
context 'when the domain is present' do
subject { Fabricate.build :account, domain: 'host.example' }
it { is_expected.to be_remote }
end end
end end