@@ -421,23 +421,67 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
421
421
return getOrgsByUserID (sess , userID , showAll )
422
422
}
423
423
424
- // queryUserOrgIDs returns a condition to return user's organization id
425
- func queryUserOrgIDs (uid int64 ) * builder.Builder {
426
- return builder .Select ("team.org_id" ).
427
- From ("team_user" ).InnerJoin ("team" , "team.id = team_user.team_id" ).
428
- Where (builder.Eq {"team_user.uid" : uid })
429
- }
430
-
431
424
// MinimalOrg represents a simple orgnization with only needed columns
432
425
type MinimalOrg = User
433
426
434
427
// GetUserOrgsList returns one user's all orgs list
435
- func GetUserOrgsList (uid int64 ) ([]* MinimalOrg , error ) {
436
- var orgs = make ([]* MinimalOrg , 0 , 20 )
437
- return orgs , x .Select ("id, name, full_name, visibility, avatar, avatar_email, use_custom_avatar" ).
428
+ func GetUserOrgsList (user * User ) ([]* MinimalOrg , error ) {
429
+ sess := x .NewSession ()
430
+ defer sess .Close ()
431
+
432
+ schema , err := x .TableInfo (new (User ))
433
+ if err != nil {
434
+ return nil , err
435
+ }
436
+
437
+ outputCols := []string {
438
+ "id" ,
439
+ "name" ,
440
+ "full_name" ,
441
+ "visibility" ,
442
+ "avatar" ,
443
+ "avatar_email" ,
444
+ "use_custom_avatar" ,
445
+ }
446
+
447
+ groupByCols := & strings.Builder {}
448
+ for _ , col := range outputCols {
449
+ fmt .Fprintf (groupByCols , "`%s`.%s," , schema .Name , col )
450
+ }
451
+ groupByStr := groupByCols .String ()
452
+ groupByStr = groupByStr [0 : len (groupByStr )- 1 ]
453
+
454
+ sess .Select (groupByStr + ", count(repo_id) as org_count" ).
438
455
Table ("user" ).
439
- In ("id" , queryUserOrgIDs (uid )).
440
- Find (& orgs )
456
+ Join ("INNER" , "team" , "`team`.org_id = `user`.id" ).
457
+ Join ("INNER" , "team_user" , "`team`.id = `team_user`.team_id" ).
458
+ Join ("LEFT" , builder .
459
+ Select ("id as repo_id, owner_id as repo_owner_id" ).
460
+ From ("repository" ).
461
+ Where (accessibleRepositoryCondition (user )), "`repository`.repo_owner_id = `team`.org_id" ).
462
+ Where ("`team_user`.uid = ?" , user .ID ).
463
+ GroupBy (groupByStr )
464
+
465
+ type OrgCount struct {
466
+ User `xorm:"extends"`
467
+ OrgCount int
468
+ }
469
+
470
+ orgCounts := make ([]* OrgCount , 0 , 10 )
471
+
472
+ if err := sess .
473
+ Asc ("`user`.name" ).
474
+ Find (& orgCounts ); err != nil {
475
+ return nil , err
476
+ }
477
+
478
+ orgs := make ([]* MinimalOrg , len (orgCounts ))
479
+ for i , orgCount := range orgCounts {
480
+ orgCount .User .NumRepos = orgCount .OrgCount
481
+ orgs [i ] = & orgCount .User
482
+ }
483
+
484
+ return orgs , nil
441
485
}
442
486
443
487
func getOwnedOrgsByUserID (sess * xorm.Session , userID int64 ) ([]* User , error ) {
0 commit comments