Move "everyone" role and "instance actor" account magic number IDs to constants (#29260)

This commit is contained in:
Matt Jankowski 2024-02-19 06:09:43 -05:00 committed by GitHub
parent 96ddf1d482
commit 245064bb98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 26 additions and 19 deletions

View file

@ -64,6 +64,7 @@ class Account < ApplicationRecord
)
BACKGROUND_REFRESH_INTERVAL = 1.week.freeze
INSTANCE_ACTOR_ID = -99
USERNAME_RE = /[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?/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 :without_suspended, -> { where(suspended_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 :bots, -> { where(actor_type: %w(Application Service)) }
scope :groups, -> { where(actor_type: 'Group') }
@ -176,7 +177,7 @@ class Account < ApplicationRecord
end
def instance_actor?
id == -99
id == INSTANCE_ACTOR_ID
end
alias bot bot?

View file

@ -13,11 +13,11 @@ module Account::FinderConcern
end
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
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
def find_local(username)

View file

@ -38,6 +38,8 @@ class UserRole < ApplicationRecord
delete_user_data: (1 << 19),
}.freeze
EVERYONE_ROLE_ID = -99
module Flags
NONE = 0
ALL = FLAGS.values.reduce(&:|)
@ -94,7 +96,7 @@ class UserRole < ApplicationRecord
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
@ -103,9 +105,9 @@ class UserRole < ApplicationRecord
end
def self.everyone
UserRole.find(-99)
UserRole.find(EVERYONE_ROLE_ID)
rescue ActiveRecord::RecordNotFound
UserRole.create!(id: -99, permissions: Flags::DEFAULT)
UserRole.create!(id: EVERYONE_ROLE_ID, permissions: Flags::DEFAULT)
end
def self.that_can(*any_of_privileges)
@ -113,7 +115,7 @@ class UserRole < ApplicationRecord
end
def everyone?
id == -99
id == EVERYONE_ROLE_ID
end
def nobody?