1
0
mirror of https://github.com/mastodon/mastodon synced 2025-01-10 20:03:22 +09:00

Extricate Invite constants for code generation (#33472)

This commit is contained in:
Matt Jankowski 2025-01-06 13:23:05 -05:00 committed by GitHub
parent 1eb752fb38
commit 4cf031ee13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,6 +20,9 @@ class Invite < ApplicationRecord
include Expireable include Expireable
COMMENT_SIZE_LIMIT = 420 COMMENT_SIZE_LIMIT = 420
ELIGIBLE_CODE_CHARACTERS = [*('a'..'z'), *('A'..'Z'), *('0'..'9')].freeze
HOMOGLYPHS = %w(0 1 I l O).freeze
VALID_CODE_CHARACTERS = ELIGIBLE_CODE_CHARACTERS - HOMOGLYPHS
belongs_to :user, inverse_of: :invites belongs_to :user, inverse_of: :invites
has_many :users, inverse_of: :invite, dependent: nil has_many :users, inverse_of: :invite, dependent: nil
@ -38,7 +41,7 @@ class Invite < ApplicationRecord
def set_code def set_code
loop do loop do
self.code = ([*('a'..'z'), *('A'..'Z'), *('0'..'9')] - %w(0 1 I l O)).sample(8).join self.code = VALID_CODE_CHARACTERS.sample(8).join
break if Invite.find_by(code: code).nil? break if Invite.find_by(code: code).nil?
end end
end end