@@ -74,52 +74,32 @@ InstanceSchema.statics.getGithubUsernamesForInstances = function (sessionUser, i
7474 var instancesByCreatedByGithubId = groupBy ( instances , 'createdBy.github' ) ;
7575 var createdByGithubIds = Object . keys ( instancesByCreatedByGithubId ) ;
7676 async . parallel ( [
77- function fetchGithubOwners ( cb ) {
78- var count = createCount ( ownerGithubIds . length , cb ) ;
79- ownerGithubIds . map ( toInt ) . forEach ( function ( githubId ) {
80- sessionUser . findGithubUserByGithubId ( githubId , function ( err , user ) {
81- var username = null ;
82- var gravatar = null ;
83- if ( err ) {
84- // log error, and continue
85- error . logIfErr ( err ) ;
86- }
87- else {
88- username = user . login ;
89- gravatar = user . avatar_url ;
90- }
91- instancesByOwnerGithubId [ githubId ] . forEach ( function ( instance ) {
92- instance . owner . username = username ;
93- instance . owner . gravatar = gravatar ;
94- } ) ;
95- count . next ( ) ; // don't pass error
96- } ) ;
97- } ) ;
98- } ,
99- function fetchGithubCreatedBy ( cb ) {
100- var count = createCount ( createdByGithubIds . length , cb ) ;
101- createdByGithubIds . map ( toInt ) . forEach ( function ( githubId ) {
102- sessionUser . findGithubUserByGithubId ( githubId , function ( err , user ) {
103- var username = null ;
104- var gravatar = null ;
105- if ( err ) {
106- // log error, and continue
107- error . logIfErr ( err ) ;
108- }
109- else {
110- username = user . login ;
111- gravatar = user . avatar_url ;
112- }
113- instancesByCreatedByGithubId [ githubId ] . forEach ( function ( instance ) {
114- instance . createdBy . username = username ;
115- instance . createdBy . gravatar = gravatar ;
116- } ) ;
117- count . next ( ) ; // don't pass error
118- } ) ;
119- } ) ;
120- }
77+ populateField . bind ( null , ownerGithubIds , instancesByOwnerGithubId , 'owner' ) ,
78+ populateField . bind ( null , createdByGithubIds , instancesByCreatedByGithubId , 'createdBy' )
12179 ] , done ) ;
12280 }
81+
82+ function populateField ( keyIds , mapToUpdateList , fieldPath , cb ) {
83+ async . each ( keyIds . map ( toInt ) , function ( githubId , asyncCb ) {
84+ sessionUser . findGithubUserByGithubId ( githubId , function ( err , user ) {
85+ var username = null ;
86+ var gravatar = null ;
87+ if ( err ) {
88+ // log error, and continue
89+ error . logIfErr ( err ) ;
90+ }
91+ else {
92+ username = user . login ;
93+ gravatar = user . avatar_url ;
94+ }
95+ mapToUpdateList [ githubId ] . forEach ( function ( instance ) {
96+ keypather . set ( instance , fieldPath + '.username' , username ) ;
97+ keypather . set ( instance , fieldPath + '.gravatar' , gravatar ) ;
98+ } ) ;
99+ asyncCb ( ) ; // don't pass error
100+ } ) ;
101+ } , cb ) ;
102+ }
123103 function done ( err ) {
124104 cb ( err , instances ) ;
125105 }
0 commit comments