Port session info in navbar
parent
017a3d683d
commit
acf44345eb
@ -0,0 +1,14 @@
|
||||
defmodule Asciinema.Auth do
|
||||
alias Asciinema.{Repo, User}
|
||||
alias Plug.Conn
|
||||
|
||||
def init(opts) do
|
||||
opts
|
||||
end
|
||||
|
||||
def call(conn, _opts) do
|
||||
user_id = Conn.get_session(conn, "warden.user.user.key")
|
||||
user = user_id && Repo.get(User, user_id)
|
||||
Conn.assign(conn, :current_user, user)
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
defmodule Crypto do
|
||||
def md5(data) do
|
||||
Base.encode16(:erlang.md5(data), case: :lower)
|
||||
end
|
||||
end
|
@ -0,0 +1,14 @@
|
||||
defmodule Asciinema.Gravatar do
|
||||
|
||||
def gravatar_url(email, opts \\ []) do
|
||||
hash = email
|
||||
|> String.downcase
|
||||
|> Crypto.md5
|
||||
|
||||
size = Keyword.get(opts, :size, 128)
|
||||
default = Keyword.get(opts, :default, "retro")
|
||||
|
||||
"//gravatar.com/avatar/#{hash}?s=#{size}&d=#{default}"
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,12 @@
|
||||
defmodule Asciinema.GravatarTest do
|
||||
use ExUnit.Case, async: true
|
||||
alias Asciinema.Gravatar
|
||||
|
||||
test "default options" do
|
||||
assert Gravatar.gravatar_url("test@example.com") == "//gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=128&d=retro"
|
||||
end
|
||||
|
||||
test "custom options" do
|
||||
assert Gravatar.gravatar_url("test@example.com", size: 256, default: "mm") == "//gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=256&d=mm"
|
||||
end
|
||||
end
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -0,0 +1,21 @@
|
||||
defmodule Asciinema.UserView do
|
||||
alias Asciinema.{Gravatar, User}
|
||||
|
||||
def avatar_url(user) do
|
||||
username = user_username(user)
|
||||
email = user.email || "#{username}+#{user.id}@asciinema.org"
|
||||
Gravatar.gravatar_url(email)
|
||||
end
|
||||
|
||||
def profile_path(%User{id: id, username: username}) do
|
||||
if username do
|
||||
"/~#{username}"
|
||||
else
|
||||
"/u/#{id}"
|
||||
end
|
||||
end
|
||||
|
||||
defp user_username(user) do
|
||||
user.username || user.temporary_username || "user:#{user.id}"
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue