FEATURE: custom colors for default letter avatars (#7167)

This commit is contained in:
Maja Komel 2019-03-18 16:24:21 +01:00 committed by Régis Hanol
parent 22c75e1c04
commit 7e9afdace3
5 changed files with 50 additions and 2 deletions

View File

@ -743,9 +743,22 @@ class User < ActiveRecord::Base
def self.letter_avatar_color(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
end
end
def self.color_index(username, length)
Digest::MD5.hexdigest(username)[0...15].to_i(16) % length
end
def avatar_template
self.class.avatar_template(username, uploaded_avatar_id)

View File

@ -1570,6 +1570,7 @@ en:
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}"
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: "List of avatars users can choose from."
@ -2023,6 +2024,7 @@ en:
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_range: "You cannot set the maximum below the minimum."
invalid_hex_value: "Color values have to be 6-digit hexadecimal codes."
placeholder:
sso_provider_secrets:

View File

@ -1106,6 +1106,11 @@ files:
client: true
regex: '^((https?:)?\/)?\/.+[^\/]'
shadowed_by_global: true
restrict_letter_avatar_colors:
default: ''
type: list
list_type: compact
validator: "ColorListValidator"
selectable_avatars_enabled:
default: false
client: true

View File

@ -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

View File

@ -1138,6 +1138,20 @@ describe User do
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
let(:user) { build(:user, username: 'Sam') }