about summary refs log tree commit diff
path: root/app/models/account.rb
diff options
context:
space:
mode:
authorJames Tucker <jftucker@gmail.com>2022-11-09 20:49:30 -0800
committerGitHub <noreply@github.com>2022-11-10 05:49:30 +0100
commit78a6b871fe3dae308380ea88132ddadc86a1431e (patch)
tree355c20f14f76d034c9e2b9e90d2e9c86745d4b87 /app/models/account.rb
parent0cd0786aef140ea41aa229cd52ac67867259a3f5 (diff)
Improve performance by avoiding regex construction (#20215)
```ruby
10.times { p /#{FOO}/.object_id }
10.times { p FOO_RE.object_id }
```
Diffstat (limited to 'app/models/account.rb')
-rw-r--r--app/models/account.rb3
1 files changed, 2 insertions, 1 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index be1968fa6..cc3a8f3df 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -64,6 +64,7 @@ class Account < ApplicationRecord
   USERNAME_RE   = /[a-z0-9_]+([a-z0-9_\.-]+[a-z0-9_]+)?/i
   MENTION_RE    = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[[:word:]\.\-]+[[:word:]]+)?)/i
   URL_PREFIX_RE = /\Ahttp(s?):\/\/[^\/]+/
+  USERNAME_ONLY_RE = /\A#{USERNAME_RE}\z/i
 
   include Attachmentable
   include AccountAssociations
@@ -84,7 +85,7 @@ class Account < ApplicationRecord
   validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? }
 
   # Remote user validations
-  validates :username, format: { with: /\A#{USERNAME_RE}\z/i }, if: -> { !local? && will_save_change_to_username? }
+  validates :username, format: { with: USERNAME_ONLY_RE }, if: -> { !local? && will_save_change_to_username? }
 
   # Local user validations
   validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? && actor_type != 'Application' }