diff options
author | Unknwon <u@gogs.io> | 2016-07-24 14:32:46 +0800 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-07-24 14:32:46 +0800 |
commit | e74630ae3b635a43a1bdafcf8b80d2f87b3536b6 (patch) | |
tree | 63c5fb32b6ca4417c3dfd9c05f532d5598aa4117 /models/access.go | |
parent | 1f2e173a745da8e4b57f96b5561a3c10054d3b76 (diff) | |
download | gitea-e74630ae3b635a43a1bdafcf8b80d2f87b3536b6.tar.gz gitea-e74630ae3b635a43a1bdafcf8b80d2f87b3536b6.zip |
#1384 add pagination for repositories
Diffstat (limited to 'models/access.go')
-rw-r--r-- | models/access.go | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/models/access.go b/models/access.go index 32e9b6ea8b..b3be9b5bb6 100644 --- a/models/access.go +++ b/models/access.go @@ -121,23 +121,17 @@ func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) { return repos, nil } -// GetAccessibleRepositories finds all repositories where a user has access but does not own. -func (u *User) GetAccessibleRepositories() ([]*Repository, error) { - accesses := make([]*Access, 0, 10) - if err := x.Find(&accesses, &Access{UserID: u.ID}); err != nil { - return nil, err - } - - if len(accesses) == 0 { - return []*Repository{}, nil - } - - repoIDs := make([]int64, 0, len(accesses)) - for _, access := range accesses { - repoIDs = append(repoIDs, access.RepoID) - } - repos := make([]*Repository, 0, len(repoIDs)) - return repos, x.Where("owner_id != ?", u.ID).In("id", repoIDs).Desc("updated_unix").Find(&repos) +// GetAccessibleRepositories finds repositories which the user has access but does not own. +// If limit is smaller than 1 means returns all found results. +func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) { + sess := x.Where("owner_id !=? ", user.ID).Desc("updated_unix") + if limit > 0 { + sess.Limit(limit) + repos = make([]*Repository, 0, limit) + } else { + repos = make([]*Repository, 0, 10) + } + return repos, sess.Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).Find(&repos) } func maxAccessMode(modes ...AccessMode) AccessMode { |