Use conditions but not repo ids as query condition (#16839)
* Use conditions but not repo ids as query condition * Improve the performance of pulls/issue * Remove duplicated code * fix lint * Fix bug * Fix stats * More fixes * Fix build * Fix lint * Fix test * Fix build * Adjust the logic * Merge * Fix conflicts * improve the performance * Add comments for the query conditions functions * Some improvements
This commit is contained in:
104
models/user.go
104
models/user.go
@ -15,7 +15,6 @@ import (
|
||||
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unit"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
@ -30,109 +29,6 @@ func GetOrganizationCount(ctx context.Context, u *user_model.User) (int64, error
|
||||
Count(new(OrgUser))
|
||||
}
|
||||
|
||||
// GetRepositoryIDs returns repositories IDs where user owned and has unittypes
|
||||
// Caller shall check that units is not globally disabled
|
||||
func GetRepositoryIDs(u *user_model.User, units ...unit.Type) ([]int64, error) {
|
||||
var ids []int64
|
||||
|
||||
sess := db.GetEngine(db.DefaultContext).Table("repository").Cols("repository.id")
|
||||
|
||||
if len(units) > 0 {
|
||||
sess = sess.Join("INNER", "repo_unit", "repository.id = repo_unit.repo_id")
|
||||
sess = sess.In("repo_unit.type", units)
|
||||
}
|
||||
|
||||
return ids, sess.Where("owner_id = ?", u.ID).Find(&ids)
|
||||
}
|
||||
|
||||
// GetActiveRepositoryIDs returns non-archived repositories IDs where user owned and has unittypes
|
||||
// Caller shall check that units is not globally disabled
|
||||
func GetActiveRepositoryIDs(u *user_model.User, units ...unit.Type) ([]int64, error) {
|
||||
var ids []int64
|
||||
|
||||
sess := db.GetEngine(db.DefaultContext).Table("repository").Cols("repository.id")
|
||||
|
||||
if len(units) > 0 {
|
||||
sess = sess.Join("INNER", "repo_unit", "repository.id = repo_unit.repo_id")
|
||||
sess = sess.In("repo_unit.type", units)
|
||||
}
|
||||
|
||||
sess.Where(builder.Eq{"is_archived": false})
|
||||
|
||||
return ids, sess.Where("owner_id = ?", u.ID).GroupBy("repository.id").Find(&ids)
|
||||
}
|
||||
|
||||
// GetOrgRepositoryIDs returns repositories IDs where user's team owned and has unittypes
|
||||
// Caller shall check that units is not globally disabled
|
||||
func GetOrgRepositoryIDs(u *user_model.User, units ...unit.Type) ([]int64, error) {
|
||||
var ids []int64
|
||||
|
||||
if err := db.GetEngine(db.DefaultContext).Table("repository").
|
||||
Cols("repository.id").
|
||||
Join("INNER", "team_user", "repository.owner_id = team_user.org_id").
|
||||
Join("INNER", "team_repo", "(? != ? and repository.is_private != ?) OR (team_user.team_id = team_repo.team_id AND repository.id = team_repo.repo_id)", true, u.IsRestricted, true).
|
||||
Where("team_user.uid = ?", u.ID).
|
||||
GroupBy("repository.id").Find(&ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(units) > 0 {
|
||||
return FilterOutRepoIdsWithoutUnitAccess(u, ids, units...)
|
||||
}
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// GetActiveOrgRepositoryIDs returns non-archived repositories IDs where user's team owned and has unittypes
|
||||
// Caller shall check that units is not globally disabled
|
||||
func GetActiveOrgRepositoryIDs(u *user_model.User, units ...unit.Type) ([]int64, error) {
|
||||
var ids []int64
|
||||
|
||||
if err := db.GetEngine(db.DefaultContext).Table("repository").
|
||||
Cols("repository.id").
|
||||
Join("INNER", "team_user", "repository.owner_id = team_user.org_id").
|
||||
Join("INNER", "team_repo", "(? != ? and repository.is_private != ?) OR (team_user.team_id = team_repo.team_id AND repository.id = team_repo.repo_id)", true, u.IsRestricted, true).
|
||||
Where("team_user.uid = ?", u.ID).
|
||||
Where(builder.Eq{"is_archived": false}).
|
||||
GroupBy("repository.id").Find(&ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(units) > 0 {
|
||||
return FilterOutRepoIdsWithoutUnitAccess(u, ids, units...)
|
||||
}
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// GetAccessRepoIDs returns all repositories IDs where user's or user is a team member organizations
|
||||
// Caller shall check that units is not globally disabled
|
||||
func GetAccessRepoIDs(u *user_model.User, units ...unit.Type) ([]int64, error) {
|
||||
ids, err := GetRepositoryIDs(u, units...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids2, err := GetOrgRepositoryIDs(u, units...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return append(ids, ids2...), nil
|
||||
}
|
||||
|
||||
// GetActiveAccessRepoIDs returns all non-archived repositories IDs where user's or user is a team member organizations
|
||||
// Caller shall check that units is not globally disabled
|
||||
func GetActiveAccessRepoIDs(u *user_model.User, units ...unit.Type) ([]int64, error) {
|
||||
ids, err := GetActiveRepositoryIDs(u, units...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids2, err := GetActiveOrgRepositoryIDs(u, units...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return append(ids, ids2...), nil
|
||||
}
|
||||
|
||||
// deleteBeans deletes all given beans, beans should contain delete conditions.
|
||||
func deleteBeans(e db.Engine, beans ...interface{}) (err error) {
|
||||
for i := range beans {
|
||||
|
Reference in New Issue
Block a user