@@ -38,7 +38,7 @@ function ControllerHome(
3838
3939 dataHome . data . hasPass = ! ! $location . search ( ) . password ;
4040
41- verifyUserIsAuth ( ) ;
41+ $scope . goToInstance = verifyUserIsAuth ;
4242
4343 function verifyUserIsAuth ( ) {
4444 async . series ( [
@@ -50,131 +50,18 @@ function ControllerHome(
5050 cb ( ) ;
5151 } ) ;
5252 } ,
53- fetchInstances ,
5453 function sendUserSomewhere ( cb ) {
5554
5655 var thisUser = $scope . user ;
5756
58- if ( ! keypather . get ( $localStorage , 'stateParams.instanceName' ) ) {
59- // no cached previously visited instance
60- goToFirstInstance ( ) ;
61- } else {
62- goToLastVisitedInstance ( ) ;
63- }
64-
65- clean ( ) ;
57+ $state . go ( 'instance.home' , {
58+ userName : keypather . get ( $localStorage , 'stateParams.userName' ) ||
59+ thisUser . oauthName ( )
60+ } ) ;
6661 return cb ( ) ;
6762
68- function getEntityName ( userOrOrg ) {
69- return ( thisUser === userOrOrg ) ?
70- thisUser . attrs . accounts . github . username : // user
71- userOrOrg . attrs . login ; // org
72- }
73-
74- // remove attached instances property from user/org models
75- function clean ( ) {
76- dataHome . data . orgs . forEach ( function ( org ) {
77- delete org . instances ;
78- } ) ;
79- }
80-
81- function goToFirstInstance ( ) {
82- if ( thisUser . instances . models . length === 0 ) {
83- return goToSetup ( ) ;
84- }
85- thisUser . instances . models = $filter ( 'orderBy' ) ( thisUser . instances . models , 'attrs.name' ) ;
86- var firstInstance = thisUser . instances . models [ 0 ] ;
87- var userName = thisUser . attrs . accounts . github . username ;
88- var instanceName = firstInstance . attrs . name ;
89- $state . go ( 'instance.instance' , {
90- userName : userName ,
91- instanceName : instanceName
92- } ) ;
93- }
94-
95- function goToSetup ( ) {
96- $state . go ( 'instance.new' , {
97- userName : thisUser . oauthName ( )
98- } ) ;
99- return cb ( ) ;
100- }
101-
102- function goToLastVisitedInstance ( ) {
103- var instanceName = $localStorage . stateParams . instanceName ;
104- var userOrgName = $localStorage . stateParams . userName ;
105- //verify exists
106- var org = dataHome . data . orgs . find ( function ( org ) {
107- return getEntityName ( org ) === userOrgName ;
108- } ) ;
109- if ( ! org ) {
110- return goToFirstInstance ( ) ;
111- }
112- var instance = org . instances . find ( function ( instance ) {
113- return instance . attrs . name === instanceName ;
114- } ) ;
115- if ( ! instance ) {
116- return goToFirstInstance ( ) ;
117- }
118- // we found the cached org & instance
119- $state . go ( 'instance.instance' , {
120- userName : userOrgName ,
121- instanceName : instanceName
122- } ) ;
123- }
12463 }
12564 ] , errs . handler ) ;
12665 }
12766
128- /**
129- * Fetch all user orgs and all instances for user + user-orgs
130- * temporarily attach 'instances' property to user & org models
131- */
132- function fetchInstances ( cb ) {
133- var thisUser = $scope . user ;
134-
135- if ( ! keypather . get ( $localStorage , 'stateParams.instanceName' ) ) {
136- // dont bother finding all orgs, we're just going to send user to first user-instance
137- dataHome . data . orgs = [ thisUser ] ;
138- fetchAllInstances ( dataHome . data . orgs ) ;
139- } else {
140- var orgs = thisUser . fetchGithubOrgs ( function ( err ) {
141- if ( err ) { throw err ; }
142- dataHome . data . orgs = orgs . models ;
143- dataHome . data . orgs . unshift ( thisUser ) ;
144- fetchAllInstances ( dataHome . data . orgs ) ;
145- } ) ;
146- }
147-
148- function fetchAllInstances ( orgs ) {
149- async . map ( orgs , fetchInstancesForOrg , function ( err ) {
150- if ( err ) { throw err ; }
151- cb ( ) ;
152- } ) ;
153- }
154-
155- function fetchInstancesForOrg ( userOrOrg , cb ) {
156- var userId = ( thisUser === userOrOrg ) ?
157- thisUser . attrs . accounts . github . id : // user
158- userOrOrg . attrs . id ; // org
159-
160- new QueryAssist ( thisUser , cb )
161- . wrapFunc ( 'fetchInstances' )
162- . query ( {
163- owner : {
164- github : userId
165- }
166- } )
167- . cacheFetch ( angular . noop )
168- . resolve ( function ( userOrOrg , err , instances , cb ) {
169- if ( err ) {
170- cb ( err ) ;
171- }
172- userOrOrg . instances = instances ;
173- cb ( null , instances ) ;
174- } . bind ( this , userOrOrg ) )
175- . go ( ) ;
176- }
177-
178- }
179-
18067}
0 commit comments