2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
module AvatarsHelper
|
2022-04-04 11:22:00 +05:30
|
|
|
DEFAULT_AVATAR_PATH = 'no_avatar.png'
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
def project_icon(project, options = {})
|
|
|
|
source_icon(project, options)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
def group_icon(group, options = {})
|
|
|
|
source_icon(group, options)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def topic_icon(topic, options = {})
|
|
|
|
source_icon(topic, options)
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
# 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-04-17 20:07:23 +05:30
|
|
|
return gravatar_icon(email, size, scale) if email.nil?
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
Gitlab::AvatarCache.by_email(email, size, scale, only_path) do
|
2021-04-17 20:07:23 +05:30
|
|
|
avatar_icon_by_user_email_or_gravatar(email, size, scale, only_path: only_path)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def avatar_icon_for_user(user = nil, size = nil, scale = 2, only_path: true, current_user: nil)
|
|
|
|
return gravatar_icon(nil, size, scale) unless user
|
|
|
|
return default_avatar if blocked_or_unconfirmed?(user) && !can_admin?(current_user)
|
|
|
|
|
|
|
|
user_avatar = user.avatar_url(size: size, only_path: only_path)
|
|
|
|
user_avatar || default_avatar
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def gravatar_icon(user_email = '', size = nil, scale = 2)
|
|
|
|
GravatarService.new.execute(user_email, size, scale) ||
|
|
|
|
default_avatar
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_avatar
|
2022-04-04 11:22:00 +05:30
|
|
|
ActionController::Base.helpers.image_path(DEFAULT_AVATAR_PATH)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
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-10-12 21:52:04 +05:30
|
|
|
css_class: 'd-none d-sm-inline-block'
|
2016-08-24 12:49:21 +05:30
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def user_avatar_without_link(options = {})
|
2016-08-24 12:49:21 +05:30
|
|
|
avatar_size = options[:size] || 16
|
|
|
|
user_name = options[:user].try(:name) || options[:user_name]
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
avatar_url = user_avatar_url_for(**options.merge(size: avatar_size))
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
has_tooltip = options[:has_tooltip].nil? ? true : options[:has_tooltip]
|
2018-03-17 18:26:18 +05:30
|
|
|
data_attributes = options[:data] || {}
|
2017-09-10 17:25:29 +05:30
|
|
|
css_class = %W[avatar s#{avatar_size}].push(*options[:css_class])
|
2019-12-04 20:38:33 +05:30
|
|
|
alt_text = user_name ? "#{user_name}'s avatar" : "default avatar"
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
if has_tooltip
|
|
|
|
css_class.push('has-tooltip')
|
2018-03-17 18:26:18 +05:30
|
|
|
data_attributes[:container] = 'body'
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if options[:lazy]
|
|
|
|
css_class << 'lazy'
|
|
|
|
data_attributes[:src] = avatar_url
|
|
|
|
avatar_url = LazyImageTagHelper.placeholder_image
|
|
|
|
end
|
|
|
|
|
|
|
|
image_options = {
|
2022-08-27 11:52:29 +05:30
|
|
|
alt: alt_text,
|
|
|
|
src: avatar_url,
|
|
|
|
data: data_attributes,
|
2017-09-10 17:25:29 +05:30
|
|
|
class: css_class,
|
2018-03-17 18:26:18 +05:30
|
|
|
title: user_name
|
|
|
|
}
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
tag.img(**image_options)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def user_avatar(options = {})
|
|
|
|
avatar = user_avatar_without_link(options)
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
if options[:user]
|
|
|
|
link_to(avatar, user_path(options[:user]))
|
|
|
|
elsif options[:user_email]
|
|
|
|
mail_to(options[:user_email], avatar)
|
|
|
|
end
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def avatar_without_link(resource, options = {})
|
2023-01-13 00:05:48 +05:30
|
|
|
case resource
|
|
|
|
when Namespaces::UserNamespace
|
2022-04-04 11:22:00 +05:30
|
|
|
user_avatar_without_link(options.merge(user: resource.first_owner))
|
2023-01-13 00:05:48 +05:30
|
|
|
when Group
|
2021-06-08 01:23:25 +05:30
|
|
|
group_icon(resource, options.merge(class: 'avatar'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
private
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def avatar_icon_by_user_email_or_gravatar(email, size, scale, only_path:)
|
|
|
|
user = User.find_by_any_email(email)
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
if user
|
2021-04-17 20:07:23 +05:30
|
|
|
avatar_icon_for_user(user, size, scale, only_path: only_path)
|
2019-12-21 20:55:43 +05:30
|
|
|
else
|
2021-04-17 20:07:23 +05:30
|
|
|
gravatar_icon(email, size, scale)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
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-11-20 20:47:30 +05:30
|
|
|
def source_icon(source, options = {})
|
|
|
|
avatar_url = source.try(:avatar_url)
|
|
|
|
|
|
|
|
if avatar_url
|
|
|
|
image_tag avatar_url, options
|
|
|
|
else
|
|
|
|
source_identicon(source, options)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
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-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def source_identicon(source, options = {})
|
|
|
|
bg_key = (source.id % 7) + 1
|
2021-06-08 01:23:25 +05:30
|
|
|
size_class = "s#{options[:size]}" if options[:size]
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
options[:class] =
|
2021-06-08 01:23:25 +05:30
|
|
|
[*options[:class], "identicon bg#{bg_key}", size_class].compact.join(' ')
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
content_tag(:span, class: options[:class].strip) do
|
2018-11-20 20:47:30 +05:30
|
|
|
source.name[0, 1].upcase
|
|
|
|
end
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
def blocked_or_unconfirmed?(user)
|
|
|
|
user.blocked? || !user.confirmed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_admin?(user)
|
|
|
|
return false unless user
|
|
|
|
|
|
|
|
user.can_admin_all_resources?
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|