Skip to content

Commit d7ce773

Browse files
authored
Merge pull request #270 from CaptainFact/chore/move-to-gravatar
Move from Adorable to Gravatar
2 parents b6d9ee1 + 92039f1 commit d7ce773

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

apps/cf/lib/accounts/accounts.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule CF.Accounts do
2323
# 48 hours
2424
@request_validity 48 * 60 * 60
2525

26-
# Configure Fetching of user picture on adorable.io
26+
# Configure Fetching of user picture on Gravatar
2727
@fetch_default_picture Application.get_env(:cf, :fetch_default_user_picture, true)
2828

2929
# ---- User creation ----

apps/db/lib/db_schema/comment.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ defmodule DB.Schema.Comment do
5454
inserted_at: u.inserted_at,
5555
picture_url: u.picture_url,
5656
achievements: u.achievements,
57-
speaker_id: u.speaker_id
57+
speaker_id: u.speaker_id,
58+
# To create pictures with Gravatar
59+
email: u.email
5860
}
5961
})
6062
end

apps/db/lib/db_type/user_picture.ex

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule DB.Type.UserPicture do
77
use Arc.Ecto.Definition
88

99
@versions [:thumb, :mini_thumb]
10+
1011
# TODO @extension_whitelist ~w(.jpg .jpeg .png)
1112

1213
@doc """
@@ -37,17 +38,29 @@ defmodule DB.Type.UserPicture do
3738
"#{user_id}_#{Atom.to_string(version)}"
3839
end
3940

40-
# Use adorable.io as default profile picture provider
41-
def default_url(:thumb, %{id: id}) do
42-
"https://api.adorable.io/avatars/96/#{id}.png"
41+
# Use Gravatar as default profile picture provider
42+
def default_url(:thumb, %{email: email}) do
43+
"https://gravatar.com/avatar/#{gravatar_hash(email)}.jpg?size=94&d=robohash"
4344
end
4445

45-
def default_url(:mini_thumb, %{id: id}) do
46-
"https://api.adorable.io/avatars/24/#{id}.png"
46+
def default_url(:mini_thumb, %{email: email}) do
47+
"https://gravatar.com/avatar/#{gravatar_hash(email)}.jpg?size=24&d=robohash"
4748
end
4849

4950
# Override the storage directory:
5051
def storage_dir(_, {_, _}) do
5152
"resources/users"
5253
end
54+
55+
defp gravatar_hash(email) do
56+
email
57+
|> String.trim()
58+
|> String.downcase()
59+
|> md5()
60+
|> Base.encode16(case: :lower)
61+
end
62+
63+
defp md5(str) do
64+
:crypto.hash(:md5, str)
65+
end
5366
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule DB.Type.UserPictureTest do
2+
use DB.DataCase, async: true
3+
doctest DB.Schema.User
4+
5+
import DB.Factory, only: [insert: 1, insert: 2]
6+
alias DB.Schema.User
7+
8+
test "defaults to gravatar" do
9+
user = insert(:user, picture_url: nil)
10+
email_md5 = :crypto.hash(:md5, user.email) |> Base.encode16(case: :lower)
11+
12+
assert DB.Type.UserPicture.default_url(:thumb, user) ==
13+
"https://gravatar.com/avatar/#{email_md5}.jpg?size=94&d=robohash"
14+
end
15+
end

0 commit comments

Comments
 (0)