diff options
Diffstat (limited to 'models/repo.go')
-rw-r--r-- | models/repo.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/models/repo.go b/models/repo.go index 0f448ba6..925a6f90 100644 --- a/models/repo.go +++ b/models/repo.go @@ -228,6 +228,21 @@ func (repo *Repository) AfterSet(colName string, _ xorm.Cell) { } } +func (repo *Repository) loadAttributes(e Engine) (err error) { + if repo.Owner == nil { + repo.Owner, err = getUserByID(e, repo.OwnerID) + if err != nil { + return fmt.Errorf("getUserByID [%d]: %v", repo.OwnerID, err) + } + } + + return nil +} + +func (repo *Repository) LoadAttributes() error { + return repo.loadAttributes(x) +} + // MustOwner always returns a valid *User object to avoid // conceptually impossible error handling. // It creates a fake object that contains error deftail @@ -1559,6 +1574,24 @@ func GetRecentUpdatedRepositories(page, pageSize int) (repos []*Repository, err Where("is_private=?", false).Limit(pageSize).Desc("updated_unix").Find(&repos) } +// GetUserAndCollaborativeRepositories returns list of repositories the user owns and collaborates. +func GetUserAndCollaborativeRepositories(userID int64) ([]*Repository, error) { + repos := make([]*Repository, 0, 10) + if err := x.Alias("repo"). + Join("INNER", "collaboration", "collaboration.repo_id = repo.id"). + Where("collaboration.user_id = ?", userID). + Find(&repos); err != nil { + return nil, fmt.Errorf("select collaborative repositories: %v", err) + } + + ownRepos := make([]*Repository, 0, 10) + if err := x.Where("owner_id = ?", userID).Find(&ownRepos); err != nil { + return nil, fmt.Errorf("select own repositories: %v", err) + } + + return append(repos, ownRepos...), nil +} + func getRepositoryCount(e Engine, u *User) (int64, error) { return x.Count(&Repository{OwnerID: u.ID}) } |