Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement identicon-based avatars, make it a default #440

Merged
merged 5 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/css/_recording_card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ div.asciicast-card {
width: 20px;
height: 20px;

img {
img, svg {
width: 100%;
height: 100%;
border-radius: 2px;
Expand Down
10 changes: 0 additions & 10 deletions assets/css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,3 @@ $primary: #06989a;
max-width: 200px;
}
}

// temporary overrides, until it gets included in the released player CSS

div.ap-control-bar * {
box-sizing: inherit;
}

.ap-overlay-help kbd {
font-family: inherit;
}
8 changes: 4 additions & 4 deletions assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"devDependencies": {
"@babel/core": "^7.21.0",
"@babel/preset-env": "^7.20.2",
"asciinema-player": "3.7.2-rc.7",
"asciinema-player": "3.8.0",
"babel-loader": "^8.3.0",
"bootstrap": "^4.5.0",
"copy-webpack-plugin": "^11.0.0",
Expand Down
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ config :asciinema, Asciinema.FileStore.Local, path: "uploads/"

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

config :asciinema, :default_avatar, :identicon

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

config :asciinema, Asciinema.PngGenerator.Rsvg,
Expand Down
6 changes: 6 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ if config_env() in [:prod, :dev] do
config :asciinema, :sign_up_enabled?, false
end

avatar = env.("DEFAULT_AVATAR")

if avatar in ["gravatar", "identicon"] do
config :asciinema, :default_avatar, String.to_existing_atom(avatar)
end

if dsn = env.("SENTRY_DSN") do
config :sentry, dsn: dsn
else
Expand Down
14 changes: 14 additions & 0 deletions lib/asciinema_web/controllers/avatar_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule AsciinemaWeb.AvatarController do
use AsciinemaWeb, :controller
alias Asciinema.Accounts

@one_day 24 * 60 * 60

def show(conn, %{"id" => id}) do
with {:ok, user} <- Accounts.fetch_user(id) do
conn
|> put_resp_header("cache-control", "public, max-age=#{@one_day}")
|> render("show.svg", user: user)
end
end
end
7 changes: 7 additions & 0 deletions lib/asciinema_web/controllers/avatar_svg.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule AsciinemaWeb.AvatarSVG do
def show(%{user: user}) do
email = user.email || "#{user.id}@asciinema"

{:safe, IdenticonSvg.generate(email, 7, :split2, 1.0, 1)}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<span class="author-avatar">
<.link href={author_profile_path(@asciicast)} title={author_username(@asciicast)}>
<img src={author_avatar_url(@asciicast)} class="avatar" />
<img src={author_avatar_url(@asciicast)} />
</.link>
</span>

Expand Down
10 changes: 8 additions & 2 deletions lib/asciinema_web/controllers/user_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ defmodule AsciinemaWeb.UserHTML do
defdelegate default_font_display_name, to: Fonts

def avatar_url(user) do
username = username(user)
email = user.email || "#{username}+#{user.id}@asciinema.org"
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)
end

Expand Down
1 change: 1 addition & 0 deletions lib/asciinema_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ defmodule AsciinemaWeb.Router do
resources "/users", UserController, as: :users, only: [:new, :create]
get "/u/:id", UserController, :show
get "/~:username", UserController, :show
get "/u/:id/avatar", AvatarController, :show
get "/user/delete", UserController, :delete, as: :user_deletion

resources "/username", UsernameController, only: [:new, :create], singleton: true
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ defmodule Asciinema.MixProject do
{:hackney, "~> 1.18"},
{:horde, "~> 0.8.7"},
{:html_sanitize_ex, "~> 1.4"},
{:identicon_svg, "~> 0.9.2"},
{:inflex, "~> 2.0"},
{:jason, "~> 1.2"},
{:libcluster, "~> 3.3"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"horde": {:hex, :horde, "0.8.7", "e51ab8e0e5bc7dcd0caa85d84b144cccfde97994bd865d822c7e489746b87e7f", [:mix], [{:delta_crdt, "~> 0.6.2", [hex: :delta_crdt, repo: "hexpm", optional: false]}, {:libring, "~> 1.4", [hex: :libring, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_poller, "~> 0.5.0 or ~> 1.0", [hex: :telemetry_poller, repo: "hexpm", optional: false]}], "hexpm", "835aede887d777542f85e0a88293c18113abcc1356006050ec216da16aa5e0e3"},
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
"html_sanitize_ex": {:hex, :html_sanitize_ex, "1.4.2", "c479398b6de798c03eb5d04a0a9a9159d73508f83f6590a00b8eacba3619cf4c", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "aef6c28585d06a9109ad591507e508854c5559561f950bbaea773900dd369b0e"},
"identicon_svg": {:hex, :identicon_svg, "0.9.2", "b877efb629240647a04b5e15e727b3835772aa129ae5cd2f44590e514726208e", [:mix], [], "hexpm", "024536e9cdc4babbae894319be3226ca2d8c2cbb3d6aca8e0e9136e63c26f6b6"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"inflex": {:hex, :inflex, "2.1.0", "a365cf0821a9dacb65067abd95008ca1b0bb7dcdd85ae59965deef2aa062924c", [:mix], [], "hexpm", "14c17d05db4ee9b6d319b0bff1bdf22aa389a25398d1952c7a0b5f3d93162dd8"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
Expand Down
13 changes: 13 additions & 0 deletions test/controllers/avatar_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Asciinema.AvatarControllerTest do
use AsciinemaWeb.ConnCase
import Asciinema.Factory

test "image response", %{conn: conn} do
user = insert(:user)

conn = get(conn, ~p"/u/#{user}/avatar")

assert response(conn, 200)
assert List.first(get_resp_header(conn, "content-type")) =~ ~r|image/.+|
end
end
Loading