FEATURE: custom colors for default letter avatars (#7167)
This commit is contained in:
parent
22c75e1c04
commit
7e9afdace3
|
@ -743,9 +743,22 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
def self.letter_avatar_color(username)
|
def self.letter_avatar_color(username)
|
||||||
username ||= ""
|
username ||= ""
|
||||||
color = LetterAvatar::COLORS[Digest::MD5.hexdigest(username)[0...15].to_i(16) % LetterAvatar::COLORS.length]
|
if SiteSetting.restrict_letter_avatar_colors.present?
|
||||||
|
hex_length = 6
|
||||||
|
colors = SiteSetting.restrict_letter_avatar_colors
|
||||||
|
length = colors.count("|") + 1
|
||||||
|
num = color_index(username, length)
|
||||||
|
index = (num * hex_length) + num
|
||||||
|
colors[index, hex_length]
|
||||||
|
else
|
||||||
|
color = LetterAvatar::COLORS[color_index(username, LetterAvatar::COLORS.length)]
|
||||||
color.map { |c| c.to_s(16).rjust(2, '0') }.join
|
color.map { |c| c.to_s(16).rjust(2, '0') }.join
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.color_index(username, length)
|
||||||
|
Digest::MD5.hexdigest(username)[0...15].to_i(16) % length
|
||||||
|
end
|
||||||
|
|
||||||
def avatar_template
|
def avatar_template
|
||||||
self.class.avatar_template(username, uploaded_avatar_id)
|
self.class.avatar_template(username, uploaded_avatar_id)
|
||||||
|
|
|
@ -1570,6 +1570,7 @@ en:
|
||||||
|
|
||||||
external_system_avatars_enabled: "Use external system avatars service."
|
external_system_avatars_enabled: "Use external system avatars service."
|
||||||
external_system_avatars_url: "URL of the external system avatars service. Allowed substitutions are {username} {first_letter} {color} {size}"
|
external_system_avatars_url: "URL of the external system avatars service. Allowed substitutions are {username} {first_letter} {color} {size}"
|
||||||
|
restrict_letter_avatar_colors: "A list of 6-digit hexadecimal color values to be used for letter avatar background."
|
||||||
|
|
||||||
selectable_avatars_enabled: "Force users to choose an avatar from the list."
|
selectable_avatars_enabled: "Force users to choose an avatar from the list."
|
||||||
selectable_avatars: "List of avatars users can choose from."
|
selectable_avatars: "List of avatars users can choose from."
|
||||||
|
@ -2023,6 +2024,7 @@ en:
|
||||||
min_username_length_range: "You cannot set the minimum above the maximum."
|
min_username_length_range: "You cannot set the minimum above the maximum."
|
||||||
max_username_length_exists: "You cannot set the maximum username length below the longest username (%{username})."
|
max_username_length_exists: "You cannot set the maximum username length below the longest username (%{username})."
|
||||||
max_username_length_range: "You cannot set the maximum below the minimum."
|
max_username_length_range: "You cannot set the maximum below the minimum."
|
||||||
|
invalid_hex_value: "Color values have to be 6-digit hexadecimal codes."
|
||||||
|
|
||||||
placeholder:
|
placeholder:
|
||||||
sso_provider_secrets:
|
sso_provider_secrets:
|
||||||
|
|
|
@ -1106,6 +1106,11 @@ files:
|
||||||
client: true
|
client: true
|
||||||
regex: '^((https?:)?\/)?\/.+[^\/]'
|
regex: '^((https?:)?\/)?\/.+[^\/]'
|
||||||
shadowed_by_global: true
|
shadowed_by_global: true
|
||||||
|
restrict_letter_avatar_colors:
|
||||||
|
default: ''
|
||||||
|
type: list
|
||||||
|
list_type: compact
|
||||||
|
validator: "ColorListValidator"
|
||||||
selectable_avatars_enabled:
|
selectable_avatars_enabled:
|
||||||
default: false
|
default: false
|
||||||
client: true
|
client: true
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
class ColorListValidator
|
||||||
|
def initialize(opts = {})
|
||||||
|
@opts = opts
|
||||||
|
end
|
||||||
|
|
||||||
|
def valid_value?(value)
|
||||||
|
hex_regex = /\A\h{6}\z/
|
||||||
|
value.split("|").all? { |c| c =~ hex_regex }
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_message
|
||||||
|
I18n.t('site_settings.errors.invalid_hex_value')
|
||||||
|
end
|
||||||
|
end
|
|
@ -1138,6 +1138,20 @@ describe User do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#letter_avatar_color" do
|
||||||
|
before do
|
||||||
|
SiteSetting.restrict_letter_avatar_colors = "2F70AC|ED207B|AAAAAA|77FF33"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns custom color if restrict_letter_avatar_colors site setting is set" do
|
||||||
|
colors = SiteSetting.restrict_letter_avatar_colors.split("|")
|
||||||
|
expect(User.letter_avatar_color("username_one")).to eq("2F70AC")
|
||||||
|
expect(User.letter_avatar_color("username_two")).to eq("ED207B")
|
||||||
|
expect(User.letter_avatar_color("username_three")).to eq("AAAAAA")
|
||||||
|
expect(User.letter_avatar_color("username_four")).to eq("77FF33")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe ".small_avatar_url" do
|
describe ".small_avatar_url" do
|
||||||
|
|
||||||
let(:user) { build(:user, username: 'Sam') }
|
let(:user) { build(:user, username: 'Sam') }
|
||||||
|
|
Loading…
Reference in New Issue