mirror of
https://github.com/mastodon/mastodon.git
synced 2025-09-05 17:31:12 +00:00
Use normalizes
api for UsernameBlock
username change (#35606)
This commit is contained in:
parent
ffeb5da991
commit
a968849e9c
|
@ -29,10 +29,12 @@ class UsernameBlock < ApplicationRecord
|
|||
validates :username, presence: true, uniqueness: true
|
||||
|
||||
scope :matches_exactly, ->(str) { where(exact: true).where(normalized_username: str) }
|
||||
scope :matches_partially, ->(str) { where(exact: false).where(Arel::Nodes.build_quoted(str).matches(Arel::Nodes.build_quoted('%').concat(arel_table[:normalized_username]).concat(Arel::Nodes.build_quoted('%')))) }
|
||||
scope :matches_partially, ->(str) { where(exact: false).where(Arel::Nodes.build_quoted(normalize_value_for(:normalized_username, str)).matches(Arel::Nodes.build_quoted('%').concat(arel_table[:normalized_username]).concat(Arel::Nodes.build_quoted('%')))) }
|
||||
|
||||
before_save :set_normalized_username
|
||||
|
||||
normalizes :normalized_username, with: ->(value) { value.downcase.gsub(Regexp.union(HOMOGLYPHS.keys), HOMOGLYPHS) }
|
||||
|
||||
def comparison
|
||||
exact? ? 'equals' : 'contains'
|
||||
end
|
||||
|
@ -42,8 +44,7 @@ class UsernameBlock < ApplicationRecord
|
|||
end
|
||||
|
||||
def self.matches?(str, allow_with_approval: false)
|
||||
normalized_str = str.downcase.gsub(Regexp.union(HOMOGLYPHS.keys), HOMOGLYPHS)
|
||||
matches_exactly(normalized_str).or(matches_partially(normalized_str)).where(allow_with_approval: allow_with_approval).any?
|
||||
matches_exactly(str).or(matches_partially(str)).where(allow_with_approval: allow_with_approval).any?
|
||||
end
|
||||
|
||||
def to_log_human_identifier
|
||||
|
@ -53,10 +54,6 @@ class UsernameBlock < ApplicationRecord
|
|||
private
|
||||
|
||||
def set_normalized_username
|
||||
self.normalized_username = normalize(username)
|
||||
end
|
||||
|
||||
def normalize(str)
|
||||
str.downcase.gsub(Regexp.union(HOMOGLYPHS.keys), HOMOGLYPHS)
|
||||
self.normalized_username = username
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user