Browse Source

models/org: reduce to 2 SQL executions for GetOrgIDsByUserID

This also addresses #4231. It is now ignoring nonexistent
organizations returned from 'org_user' table.
This was a bug caused in older version that didn't cleanup
'org_user' table when delete an organization.
pull/4240/head
Unknwon 8 years ago
parent
commit
ebd95dd082
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 2
      gogs.go
  2. 19
      models/org.go
  3. 15
      models/user.go
  4. 2
      templates/.VERSION

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/modules/setting"
)
const APP_VER = "0.10.4.0302"
const APP_VER = "0.10.4.0303"
func init() {
setting.AppVer = APP_VER

19
models/org.go

@ -303,16 +303,15 @@ func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
return getOwnedOrgsByUserID(sess.Desc(desc), userID)
}
// GetOrgUsersByUserID returns all organization-user relations by user ID.
func GetOrgUsersByUserID(uid int64, all bool) ([]*OrgUser, error) {
ous := make([]*OrgUser, 0, 10)
sess := x.Where("uid=?", uid)
if !all {
// Only show public organizations
sess.And("is_public=?", true)
}
err := sess.Find(&ous)
return ous, err
// GetOrgIDsByUserID returns a list of organization IDs that user belongs to.
// The showPrivate indicates whether to include private memberships.
func GetOrgIDsByUserID(userID int64, showPrivate bool) ([]int64, error) {
orgIDs := make([]int64, 0, 5)
sess := x.Table("org_user").Where("uid = ?", userID)
if !showPrivate {
sess.And("is_public = ?", true)
}
return orgIDs, sess.Distinct("org_id").Find(&orgIDs)
}
func getOrgUsersByOrgID(e Engine, orgID int64) ([]*OrgUser, error) {

15
models/user.go

@ -448,18 +448,15 @@ func (u *User) GetOwnedOrganizations() (err error) {
}
// GetOrganizations returns all organizations that user belongs to.
func (u *User) GetOrganizations(all bool) error {
ous, err := GetOrgUsersByUserID(u.ID, all)
func (u *User) GetOrganizations(showPrivate bool) error {
orgIDs, err := GetOrgIDsByUserID(u.ID, showPrivate)
if err != nil {
return err
return fmt.Errorf("GetOrgIDsByUserID: %v", err)
}
u.Orgs = make([]*User, len(ous))
for i, ou := range ous {
u.Orgs[i], err = GetUserByID(ou.OrgID)
if err != nil {
return err
}
u.Orgs = make([]*User, 0, len(orgIDs))
if err = x.In("id", orgIDs).Find(&u.Orgs); err != nil {
return err
}
return nil
}

2
templates/.VERSION

@ -1 +1 @@
0.10.4.0302
0.10.4.0303
Loading…
Cancel
Save