diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-22 23:21:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-22 23:21:55 +0800 |
commit | baed01f24753afb600a2984dcb9bcda0bb8502b6 (patch) | |
tree | 5621ef980b6b0067a21c86be7e4808d83c0538ab /models/repo.go | |
parent | c2ab19888f92fbdec4276a16d224e8de80d1d1dd (diff) | |
download | gitea-baed01f24753afb600a2984dcb9bcda0bb8502b6.tar.gz gitea-baed01f24753afb600a2984dcb9bcda0bb8502b6.zip |
Remove unnecessary attributes of User struct (#17745)
* Remove unnecessary functions of User struct
* Move more database methods out of user struct
* Move more database methods out of user struct
* Fix template failure
* Fix bug
* Remove finished FIXME
* remove unnecessary code
Diffstat (limited to 'models/repo.go')
-rw-r--r-- | models/repo.go | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/models/repo.go b/models/repo.go index 1347de3045..7944149a76 100644 --- a/models/repo.go +++ b/models/repo.go @@ -754,19 +754,20 @@ func (repo *Repository) UpdateSize(ctx context.Context) error { return repo.updateSize(db.GetEngine(ctx)) } -// CanUserFork returns true if specified user can fork repository. -func (repo *Repository) CanUserFork(user *User) (bool, error) { +// CanUserForkRepo returns true if specified user can fork repository. +func CanUserForkRepo(user *User, repo *Repository) (bool, error) { if user == nil { return false, nil } - if repo.OwnerID != user.ID && !user.HasForkedRepo(repo.ID) { + if repo.OwnerID != user.ID && !HasForkedRepo(user.ID, repo.ID) { return true, nil } - if err := user.GetOwnedOrganizations(); err != nil { + ownedOrgs, err := GetOwnedOrgsByUserID(user.ID) + if err != nil { return false, err } - for _, org := range user.OwnedOrgs { - if repo.OwnerID != org.ID && !org.HasForkedRepo(repo.ID) { + for _, org := range ownedOrgs { + if repo.OwnerID != org.ID && !HasForkedRepo(org.ID, repo.ID) { return true, nil } } @@ -2036,13 +2037,25 @@ func (repo *Repository) SetArchiveRepoState(isArchived bool) (err error) { // \___ / \____/|__| |__|_ \ // \/ \/ -// HasForkedRepo checks if given user has already forked a repository with given ID. -func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) { +// GetForkedRepo checks if given user has already forked a repository with given ID. +func GetForkedRepo(ownerID, repoID int64) *Repository { repo := new(Repository) has, _ := db.GetEngine(db.DefaultContext). Where("owner_id=? AND fork_id=?", ownerID, repoID). Get(repo) - return repo, has + if has { + return repo + } + return nil +} + +// HasForkedRepo checks if given user has already forked a repository with given ID. +func HasForkedRepo(ownerID, repoID int64) bool { + has, _ := db.GetEngine(db.DefaultContext). + Table("repository"). + Where("owner_id=? AND fork_id=?", ownerID, repoID). + Exist() + return has } // CopyLFS copies LFS data from one repo to another |