]> git.openstreetmap.org Git - rails.git/blob - app/helpers/user_helper.rb
Merge branch 'pull/5182'
[rails.git] / app / helpers / user_helper.rb
1 module UserHelper
2   # User images
3
4   def user_image(user, options = {})
5     options[:class] ||= "user_image border border-secondary-subtle bg-body"
6     options[:alt] ||= ""
7
8     if user.image_use_gravatar
9       user_gravatar_tag(user, options)
10     elsif user.avatar.attached?
11       user_avatar_variant_tag(user, { :resize_to_limit => [100, 100] }, options)
12     else
13       image_tag "avatar.svg", options.merge(:width => 100, :height => 100)
14     end
15   end
16
17   def user_thumbnail(user, options = {})
18     options[:class] ||= "user_thumbnail border border-secondary-subtle bg-body"
19     options[:alt] ||= ""
20
21     if user.image_use_gravatar
22       user_gravatar_tag(user, options.merge(:size => 50))
23     elsif user.avatar.attached?
24       user_avatar_variant_tag(user, { :resize_to_limit => [50, 50] }, options)
25     else
26       image_tag "avatar.svg", options.merge(:width => 50, :height => 50)
27     end
28   end
29
30   def user_thumbnail_tiny(user, options = {})
31     options[:class] ||= "user_thumbnail_tiny border border-secondary-subtle bg-body"
32     options[:alt] ||= ""
33
34     if user.image_use_gravatar
35       user_gravatar_tag(user, options.merge(:size => 50))
36     elsif user.avatar.attached?
37       user_avatar_variant_tag(user, { :resize_to_limit => [50, 50] }, options)
38     else
39       image_tag "avatar.svg", options.merge(:width => 50, :height => 50)
40     end
41   end
42
43   def user_image_url(user, options = {})
44     if user.image_use_gravatar
45       user_gravatar_url(user, options)
46     elsif user.avatar.attached?
47       polymorphic_url(user_avatar_variant(user, :resize_to_limit => [100, 100]), :host => Settings.server_url)
48     else
49       image_url("avatar.svg")
50     end
51   end
52
53   # External authentication support
54
55   def auth_button(provider, options = {})
56     link_to(
57       image_tag("auth_providers/#{provider}.svg",
58                 :alt => t("application.auth_providers.#{provider}.alt"),
59                 :class => "rounded-1",
60                 :size => "36"),
61       auth_path(options.merge(:provider => provider)),
62       :method => :post,
63       :class => "auth_button btn btn-light p-2",
64       :title => t("application.auth_providers.#{provider}.title")
65     )
66   end
67
68   def auth_button_preferred(provider, options = {})
69     link_to(
70       image_tag("auth_providers/#{provider}.svg",
71                 :alt => t("application.auth_providers.#{provider}.alt"),
72                 :class => "rounded-1 me-3",
73                 :size => "36") + t("application.auth_providers.#{provider}.title"),
74       auth_path(options.merge(:provider => provider)),
75       :method => :post,
76       :class => "auth_button btn btn-outline-secondary border py-2 px-4 d-flex justify-content-center align-items-center",
77       :title => t("application.auth_providers.#{provider}.title")
78     )
79   end
80
81   private
82
83   # Local avatar support
84   def user_avatar_variant_tag(user, variant_options, options)
85     if user.avatar.variable?
86       variant = user.avatar.variant(variant_options)
87       # https://stackoverflow.com/questions/61893089/get-metadata-of-active-storage-variant/67228171
88       if variant.send(:processed?)
89         metadata = variant.processed.send(:record).image.blob.metadata
90         if metadata["width"]
91           options[:width] = metadata["width"]
92           options[:height] = metadata["height"]
93         end
94       end
95       image_tag variant, options
96     else
97       image_tag user.avatar, options
98     end
99   end
100
101   def user_avatar_variant(user, options)
102     if user.avatar.variable?
103       user.avatar.variant(options)
104     else
105       user.avatar
106     end
107   end
108
109   # Gravatar support
110
111   # See http://en.gravatar.com/site/implement/images/ for details.
112   def user_gravatar_url(user, options = {})
113     size = options[:size] || 100
114     hash = Digest::MD5.hexdigest(user.email.downcase)
115     default_image_url = image_url("avatar_large.png")
116     "#{request.protocol}www.gravatar.com/avatar/#{hash}.jpg?s=#{size}&d=#{u(default_image_url)}"
117   end
118
119   def user_gravatar_tag(user, options = {})
120     url = user_gravatar_url(user, options)
121     options[:height] = options[:width] = options.delete(:size) || 100
122     image_tag url, options
123   end
124 end