From 5d113bdd1905c73fb8071f420ae2d248202971f9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 14 Jun 2021 20:18:09 +0800 Subject: [PATCH] Improve performance of dashboard list orgs (#16099) * Improve performance of dashboard list orgs * Fix wrong error description * unexport queryUserOrgIDs method * SimpleOrg -> MinimalOrg * . Co-authored-by: 6543 <6543@obermui.de> --- models/org.go | 19 +++++++++++++++++++ routers/web/user/home.go | 7 ++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/models/org.go b/models/org.go index 3474988efc..7f9e3cce5b 100644 --- a/models/org.go +++ b/models/org.go @@ -425,6 +425,25 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) { return getOrgsByUserID(sess, userID, showAll) } +// queryUserOrgIDs returns a condition to return user's organization id +func queryUserOrgIDs(uid int64) *builder.Builder { + return builder.Select("team.org_id"). + From("team_user").InnerJoin("team", "team.id = team_user.team_id"). + Where(builder.Eq{"team_user.uid": uid}) +} + +// MinimalOrg represents a simple orgnization with only needed columns +type MinimalOrg = User + +// GetUserOrgsList returns one user's all orgs list +func GetUserOrgsList(uid int64) ([]*MinimalOrg, error) { + var orgs = make([]*MinimalOrg, 0, 20) + return orgs, x.Select("id, name, full_name, visibility, avatar, avatar_email, use_custom_avatar"). + Table("user"). + In("id", queryUserOrgIDs(uid)). + Find(&orgs) +} + func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) { orgs := make([]*User, 0, 10) return orgs, sess. diff --git a/routers/web/user/home.go b/routers/web/user/home.go index acf73f82fe..d3fc36c730 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -49,11 +49,12 @@ func getDashboardContextUser(ctx *context.Context) *models.User { } ctx.Data["ContextUser"] = ctxUser - if err := ctx.User.GetOrganizations(&models.SearchOrganizationsOptions{All: true}); err != nil { - ctx.ServerError("GetOrganizations", err) + orgs, err := models.GetUserOrgsList(ctx.User.ID) + if err != nil { + ctx.ServerError("GetUserOrgsList", err) return nil } - ctx.Data["Orgs"] = ctx.User.Orgs + ctx.Data["Orgs"] = orgs return ctxUser }