Skip to content

Commit

Permalink
Improve performance of dashboard list orgs (go-gitea#16099)
Browse files Browse the repository at this point in the history
* Improve performance of dashboard list orgs

* Fix wrong error description

* unexport queryUserOrgIDs method

* SimpleOrg -> MinimalOrg

* .

Co-authored-by: 6543 <6543@obermui.de>
  • Loading branch information
2 people authored and AbdulrhmnGhanem committed Aug 10, 2021
1 parent f32d02c commit 44a2fe4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 19 additions & 0 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 44a2fe4

Please sign in to comment.