From f22a2aab40dab165b2c0872b016e3803c12db972 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 8 Jan 2025 09:28:08 -0500 Subject: [PATCH] Add `Account#remote?` query method (#33508) --- app/models/account.rb | 6 +++++- app/models/poll.rb | 6 +----- spec/models/account_spec.rb | 28 ++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/models/account.rb b/app/models/account.rb index d42da2e9a..fefc40869 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -107,7 +107,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 @@ -186,6 +186,10 @@ class Account < ApplicationRecord domain.nil? end + def remote? + domain.present? + end + def moved? moved_to_account_id.present? end diff --git a/app/models/poll.rb b/app/models/poll.rb index b4e0edcd0..93ef0cc58 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -61,11 +61,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) diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 7755cf961..809ec52cd 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -48,14 +48,34 @@ 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 + 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