2018-08-18 07:19:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-08 20:47:05 -04:00
|
|
|
module AvatarsHelper
|
2018-09-28 06:58:30 -04:00
|
|
|
def project_icon(project, options = {})
|
|
|
|
source_icon(project, options)
|
2018-04-25 11:32:47 -04:00
|
|
|
end
|
|
|
|
|
2018-09-28 06:58:30 -04:00
|
|
|
def group_icon(group, options = {})
|
|
|
|
source_icon(group, options)
|
2018-04-25 11:32:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Takes both user and email and returns the avatar_icon by
|
|
|
|
# user (preferred) or email.
|
|
|
|
def avatar_icon_for(user = nil, email = nil, size = nil, scale = 2, only_path: true)
|
|
|
|
if user
|
|
|
|
avatar_icon_for_user(user, size, scale, only_path: only_path)
|
|
|
|
elsif email
|
|
|
|
avatar_icon_for_email(email, size, scale, only_path: only_path)
|
|
|
|
else
|
|
|
|
default_avatar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_icon_for_email(email = nil, size = nil, scale = 2, only_path: true)
|
2021-03-03 01:11:13 -05:00
|
|
|
return gravatar_icon(email, size, scale) if email.nil?
|
|
|
|
|
|
|
|
if Feature.enabled?(:avatar_cache_for_email, @current_user, type: :development)
|
|
|
|
Gitlab::AvatarCache.by_email(email, size, scale, only_path) do
|
|
|
|
avatar_icon_by_user_email_or_gravatar(email, size, scale, only_path: only_path)
|
|
|
|
end
|
2018-04-25 11:32:47 -04:00
|
|
|
else
|
2021-03-03 01:11:13 -05:00
|
|
|
avatar_icon_by_user_email_or_gravatar(email, size, scale, only_path: only_path)
|
2018-04-25 11:32:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_icon_for_user(user = nil, size = nil, scale = 2, only_path: true)
|
2021-02-01 13:09:17 -05:00
|
|
|
if user
|
|
|
|
user.avatar_url(size: size, only_path: only_path) || default_avatar
|
|
|
|
else
|
|
|
|
gravatar_icon(nil, size, scale)
|
|
|
|
end
|
2018-04-25 11:32:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def gravatar_icon(user_email = '', size = nil, scale = 2)
|
|
|
|
GravatarService.new.execute(user_email, size, scale) ||
|
|
|
|
default_avatar
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_avatar
|
|
|
|
ActionController::Base.helpers.image_path('no_avatar.png')
|
|
|
|
end
|
|
|
|
|
2016-07-08 20:47:05 -04:00
|
|
|
def author_avatar(commit_or_event, options = {})
|
|
|
|
user_avatar(options.merge({
|
|
|
|
user: commit_or_event.author,
|
|
|
|
user_name: commit_or_event.author_name,
|
|
|
|
user_email: commit_or_event.author_email,
|
2019-07-24 17:28:26 -04:00
|
|
|
css_class: 'd-none d-sm-inline-block'
|
2016-07-08 20:47:05 -04:00
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2017-06-02 03:42:18 -04:00
|
|
|
def user_avatar_without_link(options = {})
|
2016-07-08 20:47:05 -04:00
|
|
|
avatar_size = options[:size] || 16
|
|
|
|
user_name = options[:user].try(:name) || options[:user_name]
|
2018-02-09 07:07:19 -05:00
|
|
|
|
2020-09-29 14:09:52 -04:00
|
|
|
avatar_url = user_avatar_url_for(**options.merge(size: avatar_size))
|
2018-02-09 07:07:19 -05:00
|
|
|
|
2017-08-22 17:41:17 -04:00
|
|
|
has_tooltip = options[:has_tooltip].nil? ? true : options[:has_tooltip]
|
2017-09-22 11:52:50 -04:00
|
|
|
data_attributes = options[:data] || {}
|
2017-08-22 17:41:17 -04:00
|
|
|
css_class = %W[avatar s#{avatar_size}].push(*options[:css_class])
|
2019-08-30 13:26:10 -04:00
|
|
|
alt_text = user_name ? "#{user_name}'s avatar" : "default avatar"
|
2017-08-22 17:41:17 -04:00
|
|
|
|
|
|
|
if has_tooltip
|
|
|
|
css_class.push('has-tooltip')
|
2017-09-22 11:52:50 -04:00
|
|
|
data_attributes[:container] = 'body'
|
2017-08-22 17:41:17 -04:00
|
|
|
end
|
2017-06-02 03:42:18 -04:00
|
|
|
|
2017-09-22 11:52:50 -04:00
|
|
|
if options[:lazy]
|
|
|
|
css_class << 'lazy'
|
|
|
|
data_attributes[:src] = avatar_url
|
|
|
|
avatar_url = LazyImageTagHelper.placeholder_image
|
|
|
|
end
|
|
|
|
|
|
|
|
image_options = {
|
2019-08-30 13:26:10 -04:00
|
|
|
alt: alt_text,
|
2017-09-22 11:52:50 -04:00
|
|
|
src: avatar_url,
|
|
|
|
data: data_attributes,
|
2017-08-22 17:41:17 -04:00
|
|
|
class: css_class,
|
2017-09-22 11:52:50 -04:00
|
|
|
title: user_name
|
|
|
|
}
|
|
|
|
|
|
|
|
tag(:img, image_options)
|
2017-06-02 03:42:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_avatar(options = {})
|
|
|
|
avatar = user_avatar_without_link(options)
|
2016-07-08 20:47:05 -04:00
|
|
|
|
|
|
|
if options[:user]
|
|
|
|
link_to(avatar, user_path(options[:user]))
|
|
|
|
elsif options[:user_email]
|
|
|
|
mail_to(options[:user_email], avatar)
|
|
|
|
end
|
|
|
|
end
|
2018-08-09 09:47:47 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-03-03 01:11:13 -05:00
|
|
|
def avatar_icon_by_user_email_or_gravatar(email, size, scale, only_path:)
|
|
|
|
user = User.find_by_any_email(email)
|
2019-10-14 14:06:24 -04:00
|
|
|
|
|
|
|
if user
|
2021-03-03 01:11:13 -05:00
|
|
|
avatar_icon_for_user(user, size, scale, only_path: only_path)
|
2019-10-14 14:06:24 -04:00
|
|
|
else
|
2021-03-03 01:11:13 -05:00
|
|
|
gravatar_icon(email, size, scale)
|
2019-10-14 14:06:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-03 01:11:13 -05:00
|
|
|
def user_avatar_url_for(only_path: true, **options)
|
|
|
|
return options[:url] if options[:url]
|
|
|
|
return avatar_icon_for_user(options[:user], options[:size], only_path: only_path) if options[:user]
|
|
|
|
|
|
|
|
avatar_icon_for_email(options[:user_email], options[:size], only_path: only_path)
|
|
|
|
end
|
|
|
|
|
2018-09-28 06:58:30 -04:00
|
|
|
def source_icon(source, options = {})
|
|
|
|
avatar_url = source.try(:avatar_url)
|
2018-08-09 09:47:47 -04:00
|
|
|
|
2018-09-28 06:58:30 -04:00
|
|
|
if avatar_url
|
|
|
|
image_tag avatar_url, options
|
2018-08-09 09:47:47 -04:00
|
|
|
else
|
|
|
|
source_identicon(source, options)
|
|
|
|
end
|
2020-01-30 01:08:49 -05:00
|
|
|
|
|
|
|
rescue GRPC::Unavailable, GRPC::DeadlineExceeded => e
|
|
|
|
# Handle Gitaly connection issues gracefully
|
|
|
|
Gitlab::ErrorTracking
|
|
|
|
.track_exception(e, source_type: source.class.name, source_id: source.id)
|
|
|
|
|
|
|
|
source_identicon(source, options)
|
2018-08-09 09:47:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def source_identicon(source, options = {})
|
|
|
|
bg_key = (source.id % 7) + 1
|
2018-08-18 07:19:57 -04:00
|
|
|
|
|
|
|
options[:class] =
|
|
|
|
[*options[:class], "identicon bg#{bg_key}"].join(' ')
|
2018-08-09 09:47:47 -04:00
|
|
|
|
|
|
|
content_tag(:div, class: options[:class].strip) do
|
|
|
|
source.name[0, 1].upcase
|
|
|
|
end
|
|
|
|
end
|
2016-07-08 20:47:05 -04:00
|
|
|
end
|