Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: custom colors for default letter avatars #7167

Merged
merged 1 commit into from Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions app/models/user.rb
Expand Up @@ -738,8 +738,21 @@ def self.system_avatar_template(username)

def self.letter_avatar_color(username)
username ||= ""
color = LetterAvatar::COLORS[Digest::MD5.hexdigest(username)[0...15].to_i(16) % LetterAvatar::COLORS.length]
color.map { |c| c.to_s(16).rjust(2, '0') }.join
if SiteSetting.restrict_letter_avatar_colors.present?
ZogStriP marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
2 changes: 2 additions & 0 deletions config/locales/server.en.yml
Expand Up @@ -1569,6 +1569,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."
Expand Down Expand Up @@ -2019,6 +2020,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:
Expand Down
5 changes: 5 additions & 0 deletions config/site_settings.yml
Expand Up @@ -1100,6 +1100,11 @@ files:
client: true
regex: '^((https?:)?\/)?\/.+[^\/]'
shadowed_by_global: true
restrict_letter_avatar_colors:
ZogStriP marked this conversation as resolved.
Show resolved Hide resolved
default: ''
type: list
list_type: compact
validator: "ColorListValidator"
selectable_avatars_enabled:
default: false
client: true
Expand Down
14 changes: 14 additions & 0 deletions lib/validators/color_list_validator.rb
@@ -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
14 changes: 14 additions & 0 deletions spec/models/user_spec.rb
Expand Up @@ -1138,6 +1138,20 @@ def test_user?(opts = {})

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') }
Expand Down