Move "everyone" role and "instance actor" account magic number IDs to constants (#29260)
This commit is contained in:
		@@ -64,6 +64,7 @@ class Account < ApplicationRecord
 | 
				
			|||||||
  )
 | 
					  )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  BACKGROUND_REFRESH_INTERVAL = 1.week.freeze
 | 
					  BACKGROUND_REFRESH_INTERVAL = 1.week.freeze
 | 
				
			||||||
 | 
					  INSTANCE_ACTOR_ID = -99
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  USERNAME_RE   = /[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?/i
 | 
					  USERNAME_RE   = /[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?/i
 | 
				
			||||||
  MENTION_RE    = %r{(?<![=/[:word:]])@((#{USERNAME_RE})(?:@[[:word:].-]+[[:word:]]+)?)}i
 | 
					  MENTION_RE    = %r{(?<![=/[:word:]])@((#{USERNAME_RE})(?:@[[:word:].-]+[[:word:]]+)?)}i
 | 
				
			||||||
@@ -118,7 +119,7 @@ class Account < ApplicationRecord
 | 
				
			|||||||
  scope :sensitized, -> { where.not(sensitized_at: nil) }
 | 
					  scope :sensitized, -> { where.not(sensitized_at: nil) }
 | 
				
			||||||
  scope :without_suspended, -> { where(suspended_at: nil) }
 | 
					  scope :without_suspended, -> { where(suspended_at: nil) }
 | 
				
			||||||
  scope :without_silenced, -> { where(silenced_at: nil) }
 | 
					  scope :without_silenced, -> { where(silenced_at: nil) }
 | 
				
			||||||
  scope :without_instance_actor, -> { where.not(id: -99) }
 | 
					  scope :without_instance_actor, -> { where.not(id: INSTANCE_ACTOR_ID) }
 | 
				
			||||||
  scope :recent, -> { reorder(id: :desc) }
 | 
					  scope :recent, -> { reorder(id: :desc) }
 | 
				
			||||||
  scope :bots, -> { where(actor_type: %w(Application Service)) }
 | 
					  scope :bots, -> { where(actor_type: %w(Application Service)) }
 | 
				
			||||||
  scope :groups, -> { where(actor_type: 'Group') }
 | 
					  scope :groups, -> { where(actor_type: 'Group') }
 | 
				
			||||||
@@ -176,7 +177,7 @@ class Account < ApplicationRecord
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def instance_actor?
 | 
					  def instance_actor?
 | 
				
			||||||
    id == -99
 | 
					    id == INSTANCE_ACTOR_ID
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  alias bot bot?
 | 
					  alias bot bot?
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,11 +13,11 @@ module Account::FinderConcern
 | 
				
			|||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def representative
 | 
					    def representative
 | 
				
			||||||
      actor = Account.find(-99).tap(&:ensure_keys!)
 | 
					      actor = Account.find(Account::INSTANCE_ACTOR_ID).tap(&:ensure_keys!)
 | 
				
			||||||
      actor.update!(username: 'mastodon.internal') if actor.username.include?(':')
 | 
					      actor.update!(username: 'mastodon.internal') if actor.username.include?(':')
 | 
				
			||||||
      actor
 | 
					      actor
 | 
				
			||||||
    rescue ActiveRecord::RecordNotFound
 | 
					    rescue ActiveRecord::RecordNotFound
 | 
				
			||||||
      Account.create!(id: -99, actor_type: 'Application', locked: true, username: 'mastodon.internal')
 | 
					      Account.create!(id: Account::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true, username: 'mastodon.internal')
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def find_local(username)
 | 
					    def find_local(username)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,6 +38,8 @@ class UserRole < ApplicationRecord
 | 
				
			|||||||
    delete_user_data: (1 << 19),
 | 
					    delete_user_data: (1 << 19),
 | 
				
			||||||
  }.freeze
 | 
					  }.freeze
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  EVERYONE_ROLE_ID = -99
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  module Flags
 | 
					  module Flags
 | 
				
			||||||
    NONE = 0
 | 
					    NONE = 0
 | 
				
			||||||
    ALL  = FLAGS.values.reduce(&:|)
 | 
					    ALL  = FLAGS.values.reduce(&:|)
 | 
				
			||||||
@@ -94,7 +96,7 @@ class UserRole < ApplicationRecord
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  before_validation :set_position
 | 
					  before_validation :set_position
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  scope :assignable, -> { where.not(id: -99).order(position: :asc) }
 | 
					  scope :assignable, -> { where.not(id: EVERYONE_ROLE_ID).order(position: :asc) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  has_many :users, inverse_of: :role, foreign_key: 'role_id', dependent: :nullify
 | 
					  has_many :users, inverse_of: :role, foreign_key: 'role_id', dependent: :nullify
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -103,9 +105,9 @@ class UserRole < ApplicationRecord
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def self.everyone
 | 
					  def self.everyone
 | 
				
			||||||
    UserRole.find(-99)
 | 
					    UserRole.find(EVERYONE_ROLE_ID)
 | 
				
			||||||
  rescue ActiveRecord::RecordNotFound
 | 
					  rescue ActiveRecord::RecordNotFound
 | 
				
			||||||
    UserRole.create!(id: -99, permissions: Flags::DEFAULT)
 | 
					    UserRole.create!(id: EVERYONE_ROLE_ID, permissions: Flags::DEFAULT)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def self.that_can(*any_of_privileges)
 | 
					  def self.that_can(*any_of_privileges)
 | 
				
			||||||
@@ -113,7 +115,7 @@ class UserRole < ApplicationRecord
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def everyone?
 | 
					  def everyone?
 | 
				
			||||||
    id == -99
 | 
					    id == EVERYONE_ROLE_ID
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def nobody?
 | 
					  def nobody?
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,8 @@ class AddInstanceActor < ActiveRecord::Migration[5.2]
 | 
				
			|||||||
    # Dummy class, to make migration possible across version changes
 | 
					    # Dummy class, to make migration possible across version changes
 | 
				
			||||||
    validates :username, uniqueness: { scope: :domain, case_sensitive: false }
 | 
					    validates :username, uniqueness: { scope: :domain, case_sensitive: false }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    INSTANCE_ACTOR_ID = -99
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    before_create :generate_keys
 | 
					    before_create :generate_keys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def generate_keys
 | 
					    def generate_keys
 | 
				
			||||||
@@ -15,10 +17,10 @@ class AddInstanceActor < ActiveRecord::Migration[5.2]
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def up
 | 
					  def up
 | 
				
			||||||
    Account.create!(id: -99, actor_type: 'Application', locked: true, username: Rails.configuration.x.local_domain)
 | 
					    Account.create!(id: Account::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true, username: Rails.configuration.x.local_domain)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def down
 | 
					  def down
 | 
				
			||||||
    Account.find_by(id: -99, actor_type: 'Application').destroy!
 | 
					    Account.find_by(id: Account::INSTANCE_ACTOR_ID, actor_type: 'Application').destroy!
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,9 @@
 | 
				
			|||||||
class MigrateSettingsToUserRoles < ActiveRecord::Migration[6.1]
 | 
					class MigrateSettingsToUserRoles < ActiveRecord::Migration[6.1]
 | 
				
			||||||
  disable_ddl_transaction!
 | 
					  disable_ddl_transaction!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  class UserRole < ApplicationRecord; end
 | 
					  class UserRole < ApplicationRecord
 | 
				
			||||||
 | 
					    EVERYONE_ROLE_ID = -99
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def up
 | 
					  def up
 | 
				
			||||||
    process_role_everyone
 | 
					    process_role_everyone
 | 
				
			||||||
@@ -17,7 +19,7 @@ class MigrateSettingsToUserRoles < ActiveRecord::Migration[6.1]
 | 
				
			|||||||
  private
 | 
					  private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def process_role_everyone
 | 
					  def process_role_everyone
 | 
				
			||||||
    everyone_role = UserRole.find_by(id: -99)
 | 
					    everyone_role = UserRole.find_by(id: UserRole::EVERYONE_ROLE_ID)
 | 
				
			||||||
    return unless everyone_role
 | 
					    return unless everyone_role
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    everyone_role.permissions &= ~::UserRole::FLAGS[:invite_users] unless min_invite_role == 'user'
 | 
					    everyone_role.permissions &= ~::UserRole::FLAGS[:invite_users] unless min_invite_role == 'user'
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,3 +1,3 @@
 | 
				
			|||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Account.create_with(actor_type: 'Application', locked: true, username: 'mastodon.internal').find_or_create_by(id: -99)
 | 
					Account.create_with(actor_type: 'Application', locked: true, username: 'mastodon.internal').find_or_create_by(id: Account::INSTANCE_ACTOR_ID)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -50,7 +50,7 @@ namespace :tests do
 | 
				
			|||||||
        exit(1)
 | 
					        exit(1)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if Account.find(-99).private_key.blank?
 | 
					      if Account.find(Account::INSTANCE_ACTOR_ID).private_key.blank?
 | 
				
			||||||
        puts 'Instance actor does not have a private key'
 | 
					        puts 'Instance actor does not have a private key'
 | 
				
			||||||
        exit(1)
 | 
					        exit(1)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -746,13 +746,13 @@ RSpec.describe Account do
 | 
				
			|||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      it 'is valid if we are creating an instance actor account with a period' do
 | 
					      it 'is valid if we are creating an instance actor account with a period' do
 | 
				
			||||||
        account = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
 | 
					        account = Fabricate.build(:account, id: described_class::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true, username: 'example.com')
 | 
				
			||||||
        expect(account.valid?).to be true
 | 
					        expect(account.valid?).to be true
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      it 'is valid if we are creating a possibly-conflicting instance actor account' do
 | 
					      it 'is valid if we are creating a possibly-conflicting instance actor account' do
 | 
				
			||||||
        _account = Fabricate(:account, username: 'examplecom')
 | 
					        _account = Fabricate(:account, username: 'examplecom')
 | 
				
			||||||
        instance_account = Fabricate.build(:account, id: -99, actor_type: 'Application', locked: true, username: 'example.com')
 | 
					        instance_account = Fabricate.build(:account, id: described_class::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true, username: 'example.com')
 | 
				
			||||||
        expect(instance_account.valid?).to be true
 | 
					        expect(instance_account.valid?).to be true
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -164,12 +164,12 @@ RSpec.describe UserRole do
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe '#everyone?' do
 | 
					  describe '#everyone?' do
 | 
				
			||||||
    it 'returns true when id is -99' do
 | 
					    it 'returns true when id matches the everyone id' do
 | 
				
			||||||
      subject.id = -99
 | 
					      subject.id = described_class::EVERYONE_ROLE_ID
 | 
				
			||||||
      expect(subject.everyone?).to be true
 | 
					      expect(subject.everyone?).to be true
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it 'returns false when id is not -99' do
 | 
					    it 'returns false when id does not match the everyone id' do
 | 
				
			||||||
      subject.id = 123
 | 
					      subject.id = 123
 | 
				
			||||||
      expect(subject.everyone?).to be false
 | 
					      expect(subject.everyone?).to be false
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user