Skip to content

Commit

Permalink
Fix default avatar configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jun 17, 2024
1 parent 7a0b7aa commit b7f8c86
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 43 deletions.
4 changes: 2 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ config :asciinema, Asciinema.FileStore.Local, path: "uploads/"

config :asciinema, Asciinema.FileCache, path: "cache/"

config :asciinema, :default_avatar, :identicon

config :asciinema, Asciinema.PngGenerator, adapter: Asciinema.PngGenerator.Rsvg

config :asciinema, Asciinema.PngGenerator.Rsvg,
pool_size: 2,
font_family: "monospace"

config :asciinema, AsciinemaWeb.DefaultAvatar, adapter: AsciinemaWeb.DefaultAvatar.Identicon

config :asciinema, Oban,
repo: Asciinema.Repo,
queues: [default: 10, emails: 10, upgrades: 1],
Expand Down
11 changes: 8 additions & 3 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,15 @@ if config_env() in [:prod, :dev] do
config :asciinema, Asciinema.Accounts, sign_up_enabled?: false
end

avatar = env.("DEFAULT_AVATAR")
case env.("DEFAULT_AVATAR") do
"identicon" ->
config :asciinema, AsciinemaWeb.DefaultAvatar, adapter: AsciinemaWeb.DefaultAvatar.Identicon

if avatar in ["gravatar", "identicon"] do
config :asciinema, :default_avatar, String.to_existing_atom(avatar)
"gravatar" ->
config :asciinema, AsciinemaWeb.DefaultAvatar, adapter: AsciinemaWeb.DefaultAvatar.Gravatar

nil ->
:ok
end

if dsn = env.("SENTRY_DSN") do
Expand Down
13 changes: 0 additions & 13 deletions lib/asciinema/gravatar.ex

This file was deleted.

14 changes: 3 additions & 11 deletions lib/asciinema_web/controllers/user_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule AsciinemaWeb.UserHTML do
use AsciinemaWeb, :html
import AsciinemaWeb.ErrorHelpers
import Scrivener.HTML
alias Asciinema.{Fonts, Gravatar}
alias AsciinemaWeb.{MediaView, RecordingHTML}
alias Asciinema.Fonts
alias AsciinemaWeb.{DefaultAvatar, MediaView, RecordingHTML}

embed_templates "user_html/*"

Expand All @@ -12,15 +12,7 @@ defmodule AsciinemaWeb.UserHTML do
defdelegate default_font_display_name, to: Fonts

def avatar_url(user) do
avatar_url(user, Application.fetch_env!(:asciinema, :default_avatar))
end

def avatar_url(user, :identicon), do: ~p"/u/#{user}/avatar"

def avatar_url(user, :gravatar) do
email = user.email || "#{user.id}@asciinema"

Gravatar.gravatar_url(email)
DefaultAvatar.url(user)
end

def username(user) do
Expand Down
10 changes: 10 additions & 0 deletions lib/asciinema_web/default_avatar.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule AsciinemaWeb.DefaultAvatar do
@type url :: String.t()
@type user :: Asciinema.User.t()

@callback url(user) :: url

def url(user) do
Keyword.fetch!(Application.fetch_env!(:asciinema, __MODULE__), :adapter).url(user)
end
end
18 changes: 18 additions & 0 deletions lib/asciinema_web/default_avatar/gravatar.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule AsciinemaWeb.DefaultAvatar.Gravatar do
@behaviour AsciinemaWeb.DefaultAvatar

@size 128
@style "retro"

@impl true
def url(user) do
email = user.email || "#{user.id}@asciinema"

hash =
email
|> String.downcase()
|> Crypto.md5()

"//gravatar.com/avatar/#{hash}?s=#{@size}&d=#{@style}"
end
end
12 changes: 12 additions & 0 deletions lib/asciinema_web/default_avatar/identicon.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule AsciinemaWeb.DefaultAvatar.Identicon do
use Phoenix.VerifiedRoutes,
endpoint: AsciinemaWeb.Endpoint,
router: AsciinemaWeb.Router

@behaviour AsciinemaWeb.DefaultAvatar

@impl true
def url(user) do
~p"/u/#{user}/avatar"
end
end
14 changes: 0 additions & 14 deletions test/asciinema/gravatar_test.exs

This file was deleted.

0 comments on commit b7f8c86

Please sign in to comment.