summaryrefslogtreecommitdiffstats
path: root/models/access.go
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-07-24 14:32:46 +0800
committerUnknwon <u@gogs.io>2016-07-24 14:32:46 +0800
commite74630ae3b635a43a1bdafcf8b80d2f87b3536b6 (patch)
tree63c5fb32b6ca4417c3dfd9c05f532d5598aa4117 /models/access.go
parent1f2e173a745da8e4b57f96b5561a3c10054d3b76 (diff)
downloadgitea-e74630ae3b635a43a1bdafcf8b80d2f87b3536b6.tar.gz
gitea-e74630ae3b635a43a1bdafcf8b80d2f87b3536b6.zip
#1384 add pagination for repositories
Diffstat (limited to 'models/access.go')
-rw-r--r--models/access.go28
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 {