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