@@ -5,14 +5,15 @@ defmodule CodeCorps.Accounts do
55 All actions to accounts should go through here.
66 """
77
8+ alias Task.Supervisor , as: TaskSupervisor
89 alias CodeCorps . {
910 Accounts.Changesets ,
1011 Comment ,
1112 GitHub.Adapters ,
1213 GithubAppInstallation ,
14+ Task ,
1315 User ,
14- Repo ,
15- Task
16+ Repo
1617 }
1718 alias Ecto . { Changeset , Multi }
1819
@@ -23,6 +24,16 @@ defmodule CodeCorps.Accounts do
2324 """
2425 @ spec create_from_github ( map ) :: { :ok , User . t } | { :error , Changeset . t }
2526 def create_from_github ( % { } = attrs ) do
27+ with { :ok , user } <- do_create_from_github ( attrs ) do
28+ user |> upload_github_photo_async
29+ { :ok , user }
30+ else
31+ error -> error
32+ end
33+ end
34+
35+ @ spec do_create_from_github ( map ) :: { :ok , User . t } | { :error , Changeset . t }
36+ defp do_create_from_github ( % { } = attrs ) do
2637 % User { }
2738 |> Changesets . create_from_github_changeset ( attrs )
2839 |> Repo . insert
@@ -56,6 +67,20 @@ defmodule CodeCorps.Accounts do
5667 end
5768 end
5869
70+ defp upload_github_photo_async ( % User { } = user ) do
71+ TaskSupervisor . start_child ( :background_processor , fn -> upload_github_photo ( user ) end )
72+ end
73+
74+ defp upload_github_photo ( % User { github_avatar_url: github_avatar_url } = user ) do
75+ [ ok: % Cloudex.UploadedImage { public_id: cloudinary_public_id } ] =
76+ github_avatar_url
77+ |> CodeCorps.Cloudex.Uploader . upload ( )
78+
79+ user
80+ |> Changeset . change ( % { cloudinary_public_id: cloudinary_public_id } )
81+ |> Repo . update!
82+ end
83+
5984 @ spec associate_installations ( User . t ) :: { :ok , list ( GithubAppInstallation . t ) }
6085 defp associate_installations ( % User { id: user_id , github_id: github_id } ) do
6186 updates = [ set: [ user_id: user_id ] ]
0 commit comments