Skip to content

Commit

Permalink
Introduce Speaker#avatar_url and rework avatar rendering with image…
Browse files Browse the repository at this point in the history
… fallbacks (#463)
  • Loading branch information
marcoroth authored Nov 28, 2024
1 parent f5aca21 commit 8af5efb
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 28 deletions.
31 changes: 30 additions & 1 deletion app/models/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,41 @@ def create_suggestion_from(params:, user: Current.user)
super
end

def avatar_url(...)
bsky_avatar_url(...) || github_avatar_url(...) || fallback_avatar_url(...)
end

def avatar_rank
return 1 if bsky_avatar_url.present?
return 2 if github_avatar_url.present?

3
end

def custom_avatar?
bsky_avatar_url.present? || github_avatar_url.present?
end

def bsky_avatar_url(...)
bsky_metadata.dig("avatar")
end

def github_avatar_url(size: 200)
return "" unless github.present?
return nil unless github.present?

"https://github.com/#{github}.png?size=#{size}"
end

def fallback_avatar_url(size: 200)
url_safe_initials = name.split(" ").map(&:first).join("+")

"https://ui-avatars.com/api/?name=#{url_safe_initials}&size=#{size}&background=DC133C&color=fff"
end

def fetch_bsky_metadata!
Speaker::FetchBskyMetadata.new.perform(speaker: self)
end

def broadcast_about
broadcast_update_to self, target: dom_id(self, :about), partial: "speakers/about", locals: {speaker: self}
end
Expand Down
8 changes: 4 additions & 4 deletions app/views/events/_featured.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
<% shown_speakers = [] %>
<% all_speakers = event.speakers.to_a %>
<% speakers_with_avatars = all_speakers.select { |speaker| speaker.github.present? } %>
<% speakers_with_avatars = all_speakers.select { |speaker| speaker.avatar_url.present? }.sort_by(&:avatar_rank) %>
<% event.keynote_speakers.each do |keynote_speaker| %>
<% shown_speakers << keynote_speaker %>

<div class="avatar bg-white">
<div class="w-12 lg:w-28 xl:w-40">
<img src="<%= keynote_speaker.github_avatar_url %>" onerror="this.parentElement.parentElement.remove()" loading="lazy">
<img src="<%= keynote_speaker.avatar_url %>" onerror="this.parentElement.parentElement.remove()" loading="lazy">
</div>
</div>
<% end %>
Expand All @@ -39,7 +39,7 @@

<div class="avatar bg-white">
<div class="w-12 lg:w-28 xl:w-40">
<img src="<%= speaker.github_avatar_url %>" loading="lazy">
<img src="<%= speaker.avatar_url %>" loading="lazy">
</div>
</div>
<% end %>
Expand All @@ -57,7 +57,7 @@

<div class="avatar bg-white">
<div class="w-8 xl:w-12">
<img src="<%= speaker.github_avatar_url %>" loading="lazy">
<img src="<%= speaker.avatar_url %>" loading="lazy">
</div>
</div>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/leaderboard/_speaker.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<td style="view-transition-name: <%= dom_id(speaker, :Leaderboard) if speaker_counter < 10 %>">
<%= link_to speaker_path(speaker), class: "link link-hover" do %>
<div class="flex items-center gap-4">
<% if speaker.github.present? %>
<%= image_tag speaker.github_avatar_url(size: 128), class: "rounded-full w-10 h-10", alt: "GitHub picture profile of #{speaker.name}", loading: :lazy %>
<% if speaker.custom_avatar? %>
<%= image_tag speaker.avatar_url(size: 128), class: "rounded-full w-10 h-10", alt: "GitHub picture profile of #{speaker.name}", loading: :lazy %>
<% else %>
<div class="rounded-full w-10 h-10 bg-gray-200"></div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/speakers/_card.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= link_to speaker_path(speaker), class: "flex w-full relative bg-white" do %>
<div class="card card-compact card-side border h-full w-full">
<figure>
<%= image_tag speaker.github_avatar_url(size: 250),
<%= image_tag speaker.avatar_url(size: 250),
alt: "picture profile of #{speaker.name}",
class: "w-40 lg:w-full shrink-0 h-full",
loading: "lazy",
Expand Down
2 changes: 1 addition & 1 deletion app/views/speakers/_speaker.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= link_to speaker_path(speaker), id: dom_id(speaker), class: "flex justify-between" do %>
<div class="flex items-center gap-2">
<span><%= speaker.name %></span>
<%#= image_tag speaker.github_avatar_url(size: 64), class: "rounded-full h-8 w-8" if speaker.github_avatar_url.present? %>
<%#= image_tag speaker.avatar_url(size: 64), class: "rounded-full h-8 w-8" if speaker.avatar_url.present? %>
</div>

<%= ui_badge(speaker.talks_count, kind: :secondary, outline: true, size: :lg, class: "min-w-10") %>
Expand Down
25 changes: 12 additions & 13 deletions app/views/speakers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
<% end %>
</div>

<% if @speaker.github.present? %>
<div class="relative w-fit">
<%= image_tag @speaker.github_avatar_url(size: 200),
class: "rounded-full mb-4",
height: 200,
width: 200,
alt: "GitHub picture profile of #{@speaker.github}",
loading: :lazy %>
<div class="relative w-fit">
<%= image_tag @speaker.avatar_url(size: 200),
class: "rounded-full mt-4",
height: 200,
width: 200,
alt: "GitHub picture profile of #{@speaker.github}",
loading: :lazy %>
<% if @speaker.verified? %>
<div class="absolute right-0 top-0 badge badge-accent">Verified</div>
<% end %>
</div>

<% if @speaker.verified? %>
<div class="absolute right-0 top-0 badge badge-accent">Verified</div>
<% end %>
</div>
<% end %>
<%= render "speakers/about", speaker: @speaker %>
<%= render "speakers/actions", speaker: @speaker %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/spotlight/speakers/_speaker.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="avatar placeholder">
<div class="w-8 rounded-full bg-gray-300">
<span class="hidden only:block text-primary"><%= speaker.name.split.map(&:first).then { |initials| [initials.first, initials.last].join }.upcase %></span>
<img src="<%= speaker.github_avatar_url %>" loading="lazy" alt="<%= speaker.name %>" height="200" width="200" onerror="this.remove()">
<img src="<%= speaker.avatar_url %>" loading="lazy" alt="<%= speaker.name %>" height="200" width="200" onerror="this.remove()">
</div>
</div>
<%= speaker.name %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/talks/_explore_event.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<% speakers_with_avatars.each do |speaker| %>
<div class="avatar bg-white border-2">
<div class="w-8">
<img src="<%= speaker.github_avatar_url %>" loading="lazy">
<img src="<%= speaker.avatar_url %>" loading="lazy">
</div>
</div>
<% end %>
Expand Down
6 changes: 4 additions & 2 deletions app/views/talks/_speaker.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<div class="flex items-center gap-4">
<div class="avatar placeholder">
<div class="w-12 rounded-full bg-primary text-neutral-content">
<% if speaker.github.present? %>
<%= image_tag speaker.github_avatar_url(size: 48) %>
<% if speaker.custom_avatar? %>
<%= image_tag speaker.avatar_url(size: 48) %>
<% else %>
<span class="text-lg"><%= speaker.name.split(" ").map(&:first).join %></span>
<% end %>
Expand All @@ -22,6 +22,8 @@
<div class="text-xs text-gray-500 line-clamp-1">
<% if speaker.github.present? %>
<%= "@#{speaker.github}" %>
<% elsif speaker.bsky.present? %>
<%= "@#{speaker.bsky}" %>
<% else %>
<%= pluralize(speaker.talks.count, "talk") %>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/talks/_talk.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@
<% talk.speakers.to_a.from(speaker_preview_count).each do |speaker| %>
<div class="avatar placeholder border-none">
<%= content_tag :div, class: "w-12 rounded-full bg-primary text-neutral-content" do %>
<% if speaker.github.present? %>
<%= image_tag speaker.github_avatar_url(size: 48) %>
<% if speaker.custom_avatar? %>
<%= image_tag speaker.avatar_url(size: 48) %>
<% else %>
<span class="text-lg"><%= speaker.name.split(" ").map(&:first).join %></span>
<% end %>
Expand Down

0 comments on commit 8af5efb

Please sign in to comment.