From 3d9b98fae4782a45edc3f53ad4561fe7e14a4377 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 7 Sep 2015 13:58:23 -0400 Subject: [PATCH] #1585 order owners list by last changed time --- models/org.go | 21 +++++++++++++++++++++ models/user.go | 7 +++++++ routers/repo/repo.go | 7 ++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/models/org.go b/models/org.go index 4076bf15e55..b45dcafb21b 100644 --- a/models/org.go +++ b/models/org.go @@ -9,6 +9,8 @@ import ( "fmt" "os" "strings" + + "github.com/go-xorm/xorm" ) var ( @@ -251,6 +253,25 @@ func IsPublicMembership(orgId, uid int64) bool { return has } +func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) { + orgs := make([]*User, 0, 10) + return orgs, sess.Where("`org_user`.uid=?", userID).And("`org_user`.is_owner=?", true). + Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Find(&orgs) +} + +// GetOwnedOrgsByUserID returns a list of organizations are owned by given user ID. +func GetOwnedOrgsByUserID(userID int64) ([]*User, error) { + sess := x.NewSession() + return getOwnedOrgsByUserID(sess, userID) +} + +// GetOwnedOrganizationsByUserIDDesc returns a list of organizations are owned by +// given user ID and descring order by given condition. +func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) { + sess := x.NewSession() + return getOwnedOrgsByUserID(sess.Desc(desc), userID) +} + // GetOrgUsersByUserId returns all organization-user relations by user ID. func GetOrgUsersByUserId(uid int64) ([]*OrgUser, error) { ous := make([]*OrgUser, 0, 10) diff --git a/models/user.go b/models/user.go index 641e14d0cea..da2f49a1ccc 100644 --- a/models/user.go +++ b/models/user.go @@ -61,6 +61,7 @@ type User struct { LoginSource int64 `xorm:"NOT NULL DEFAULT 0"` LoginName string Type UserType + OwnedOrgs []*User `xorm:"-"` Orgs []*User `xorm:"-"` Repos []*Repository `xorm:"-"` Location string @@ -307,6 +308,12 @@ func (u *User) GetRepositories() (err error) { return err } +// GetOwnedOrganizations returns all organizations that user owns. +func (u *User) GetOwnedOrganizations() (err error) { + u.OwnedOrgs, err = GetOwnedOrgsByUserID(u.Id) + return err +} + // GetOrganizations returns all organizations that user belongs to. func (u *User) GetOrganizations() error { ous, err := GetOrgUsersByUserId(u.Id) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index b9cc98a2dcd..567b717a706 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -28,11 +28,12 @@ const ( ) func checkContextUser(ctx *middleware.Context, uid int64) *models.User { - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "GetOrganizations", err) + orgs, err := models.GetOwnedOrgsByUserIDDesc(ctx.User.Id, "updated") + if err != nil { + ctx.Handle(500, "GetOwnedOrganizationsByUserIDDesc", err) return nil } - ctx.Data["Orgs"] = ctx.User.Orgs + ctx.Data["Orgs"] = orgs // Not equal means current user is an organization. if uid == ctx.User.Id || uid == 0 {