Skip to content

Commit 44a2fe4

Browse files
lunny6543
authored andcommitted
Improve performance of dashboard list orgs (go-gitea#16099)
* Improve performance of dashboard list orgs * Fix wrong error description * unexport queryUserOrgIDs method * SimpleOrg -> MinimalOrg * . Co-authored-by: 6543 <6543@obermui.de>
1 parent f32d02c commit 44a2fe4

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

models/org.go

+19
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,25 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
425425
return getOrgsByUserID(sess, userID, showAll)
426426
}
427427

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+
// MinimalOrg represents a simple orgnization with only needed columns
436+
type MinimalOrg = User
437+
438+
// 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").
442+
Table("user").
443+
In("id", queryUserOrgIDs(uid)).
444+
Find(&orgs)
445+
}
446+
428447
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
429448
orgs := make([]*User, 0, 10)
430449
return orgs, sess.

routers/web/user/home.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
4949
}
5050
ctx.Data["ContextUser"] = ctxUser
5151

52-
if err := ctx.User.GetOrganizations(&models.SearchOrganizationsOptions{All: true}); err != nil {
53-
ctx.ServerError("GetOrganizations", err)
52+
orgs, err := models.GetUserOrgsList(ctx.User.ID)
53+
if err != nil {
54+
ctx.ServerError("GetUserOrgsList", err)
5455
return nil
5556
}
56-
ctx.Data["Orgs"] = ctx.User.Orgs
57+
ctx.Data["Orgs"] = orgs
5758

5859
return ctxUser
5960
}

0 commit comments

Comments
 (0)