Skip to content

Commit 1ac6000

Browse files
committed
Experiment with even more optimizations
1 parent ff46b8d commit 1ac6000

File tree

7 files changed

+66
-9
lines changed

7 files changed

+66
-9
lines changed

Diff for: lib/code_corps_web/controllers/project_controller.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ defmodule CodeCorpsWeb.ProjectController do
4545
end
4646

4747
@preloads [
48-
:donation_goals, [organization: :stripe_connect_account],
48+
:donation_goals, [organization: [:organization_github_app_installations, :projects, :slugged_route, :stripe_connect_account]],
4949
:project_categories, :project_github_repos, :project_skills,
50-
:project_users, :stripe_connect_plan, :task_lists, :tasks
50+
[project_users: :user], :stripe_connect_plan, :task_lists, :tasks
5151
]
5252

5353
def preload(data) do

Diff for: lib/code_corps_web/controllers/task_list_controller.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule CodeCorpsWeb.TaskListController do
2828
end
2929
end
3030

31-
@preloads [tasks: [:github_issue, :github_pull_request, :task_skills, :user, :user_task]]
31+
@preloads [tasks: [:github_issue, :github_pull_request, :task_skills, :user, [user_task: :user]]]
3232

3333
def preload(data) do
3434
Repo.preload(data, @preloads)

Diff for: lib/code_corps_web/views/project_user_view.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ defmodule CodeCorpsWeb.ProjectUserView do
66
attributes [:role, :inserted_at, :updated_at]
77

88
has_one :project, type: "project", field: :project_id
9-
has_one :user, type: "user", field: :user_id
9+
has_one :user, serializer: CodeCorpsWeb.UserSlimView, include: true
1010
end

Diff for: lib/code_corps_web/views/project_view.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ defmodule CodeCorpsWeb.ProjectView do
1414
:total_monthly_donated, :updated_at, :website
1515
]
1616

17-
has_one :organization, type: "organization", field: :organization_id
17+
has_one :organization, serializer: CodeCorpsWeb.OrganizationView, include: true
1818
has_one :stripe_connect_plan, serializer: CodeCorpsWeb.StripeConnectPlanView
1919

2020
has_many :donation_goals, serializer: CodeCorpsWeb.DonationGoalView, identifiers: :always
2121
has_many :project_categories, serializer: CodeCorpsWeb.ProjectCategoryView, identifiers: :always
2222
has_many :project_github_repos, serializer: CodeCorpsWeb.ProjectGithubRepoView, identifiers: :always
2323
has_many :project_skills, serializer: CodeCorpsWeb.ProjectSkillView, identifiers: :always
24-
has_many :project_users, serializer: CodeCorpsWeb.ProjectUserView, identifiers: :always
24+
has_many :project_users, serializer: CodeCorpsWeb.ProjectUserView, include: true
2525
has_many :tasks, serializer: CodeCorpsWeb.TaskView, identifiers: :always
2626
has_many :task_lists, serializer: CodeCorpsWeb.TaskListView, identifiers: :always
2727

Diff for: lib/code_corps_web/views/task_view.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ defmodule CodeCorpsWeb.TaskView do
1313
has_one :github_repo, type: "github-repo", field: :github_repo_id
1414
has_one :project, type: "project", field: :project_id
1515
has_one :task_list, type: "task-list", field: :task_list_id
16-
has_one :user, type: "user", field: :user_id
17-
has_one :user_task, serializer: CodeCorpsWeb.UserTaskView, identifiers: :always
16+
has_one :user, serializer: CodeCorpsWeb.UserSlimView, include: true
17+
has_one :user_task, serializer: CodeCorpsWeb.UserTaskView, include: true
1818

1919
has_many :comments, serializer: CodeCorpsWeb.CommentView, identifiers: :always
2020
has_many :task_skills, serializer: CodeCorpsWeb.TaskSkillView, identifiers: :always

Diff for: lib/code_corps_web/views/user_slim_view.ex

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
defmodule CodeCorpsWeb.UserSlimView do
2+
@moduledoc false
3+
alias CodeCorps.Presenters.ImagePresenter
4+
5+
use CodeCorpsWeb, :view
6+
use JaSerializer.PhoenixView
7+
8+
def type, do: "user"
9+
10+
attributes [
11+
:biography, :cloudinary_public_id, :email, :first_name,
12+
:github_avatar_url, :github_id, :github_username, :intercom_user_hash,
13+
:inserted_at, :last_name, :name, :photo_large_url, :photo_thumb_url,
14+
:sign_up_context, :state, :state_transition, :twitter, :username,
15+
:website, :updated_at
16+
]
17+
18+
def photo_large_url(user, _conn), do: ImagePresenter.large(user)
19+
20+
def photo_thumb_url(user, _conn), do: ImagePresenter.thumbnail(user)
21+
22+
@doc """
23+
Returns the user email or an empty string, depending on the user
24+
being rendered is the authenticated user, or some other user.
25+
26+
Users can only see their own emails. Everyone else's are private.
27+
"""
28+
def email(user, %Plug.Conn{assigns: %{current_user: current_user}}) do
29+
if user.id == current_user.id, do: user.email, else: ""
30+
end
31+
def email(_user, _conn), do: ""
32+
33+
@intercom_secret_key Application.get_env(:code_corps, :intercom_identity_secret_key) || "RANDOM_KEY"
34+
35+
def intercom_user_hash(%{id: id}, _conn) when is_number(id) do
36+
id |> Integer.to_string |> do_intercom_user_hash
37+
end
38+
# def intercom_user_hash(_user, _conn), do: nil
39+
40+
defp do_intercom_user_hash(id_string) do
41+
:crypto.hmac(:sha256, @intercom_secret_key, id_string)
42+
|> Base.encode16
43+
|> String.downcase
44+
end
45+
46+
@doc """
47+
Returns the user's full name when both first and last name are present.
48+
Returns the only user's first name or last name when the other is missing,
49+
otherwise returns nil.
50+
"""
51+
def name(%{first_name: first_name, last_name: last_name}, _conn) do
52+
"#{first_name} #{last_name}" |> String.trim |> normalize_name
53+
end
54+
55+
defp normalize_name(name) when name in ["", nil], do: nil
56+
defp normalize_name(name), do: name
57+
end

Diff for: lib/code_corps_web/views/user_task_view.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ defmodule CodeCorpsWeb.UserTaskView do
44
use JaSerializer.PhoenixView
55

66
has_one :task, type: "task", field: :task_id
7-
has_one :user, type: "user", field: :user_id
7+
has_one :user, serializer: CodeCorpsWeb.UserSlimView, include: true
88
end

0 commit comments

Comments
 (0)