Add Account#remote? query method

This commit is contained in:
Matt Jankowski 2024-09-09 11:33:50 -04:00
parent cfb8fc6222
commit a07d95ac9a
3 changed files with 22 additions and 10 deletions

View File

@ -102,7 +102,7 @@ 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: -> { (!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
validates :uri, presence: true, unless: :local?, on: :create
@ -185,6 +185,10 @@ class Account < ApplicationRecord
domain.nil?
end
def remote?
domain.present?
end
def moved?
moved_to_account_id.present?
end

View File

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

View File

@ -106,14 +106,26 @@ RSpec.describe Account do
end
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)
expect(account.local?).to be true
expect(account).to be_local
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')
expect(account.local?).to be false
expect(account).to_not be_local
end
end
describe '#remote?' do
it 'returns false when the domain is null' do
account = Fabricate(:account, domain: nil)
expect(account).to_not be_remote
end
it 'returns true when domain is present' do
account = Fabricate(:account, domain: 'foreign.tld')
expect(account).to be_remote
end
end