diff --git a/models/org.go b/models/org.go index 9908386f0d..b302288ca5 100644 --- a/models/org.go +++ b/models/org.go @@ -195,12 +195,19 @@ func CountOrganizations() int64 { } // Organizations returns number of organizations in given page. -func Organizations(page, pageSize int) ([]*User, error) { - orgs := make([]*User, 0, pageSize) - return orgs, x. - Limit(pageSize, (page-1)*pageSize). - Where("type=1"). - Asc("name"). +func Organizations(opts *SearchUserOptions) ([]*User, error) { + orgs := make([]*User, 0, opts.PageSize) + + if len(opts.OrderBy) == 0 { + opts.OrderBy = "name ASC" + } + + sess := x. + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + Where("type=1") + + return orgs, sess. + OrderBy(opts.OrderBy). Find(&orgs) } diff --git a/models/repo.go b/models/repo.go index 05e4f0b510..f6e1af55cd 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1105,14 +1105,18 @@ func CountUserRepositories(userID int64, private bool) int64 { } // Repositories returns all repositories -func Repositories(page, pageSize int) (_ []*Repository, err error) { - repos := make([]*Repository, 0, pageSize) - return repos, x.Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&repos) +func Repositories(opts *SearchRepoOptions) (_ []*Repository, err error) { + if len(opts.OrderBy) == 0 { + opts.OrderBy = "id ASC" + } + + repos := make([]*Repository, 0, opts.PageSize) + return repos, x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(opts.OrderBy).Find(&repos) } // RepositoriesWithUsers returns number of repos in given page. -func RepositoriesWithUsers(page, pageSize int) (_ []*Repository, err error) { - repos, err := Repositories(page, pageSize) +func RepositoriesWithUsers(opts *SearchRepoOptions) (_ []*Repository, err error) { + repos, err := Repositories(opts) if err != nil { return nil, fmt.Errorf("Repositories: %v", err) } @@ -1565,12 +1569,31 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) { } // GetRecentUpdatedRepositories returns the list of repositories that are recently updated. -func GetRecentUpdatedRepositories(page, pageSize int) (repos []*Repository, err error) { - return repos, x. - Limit(pageSize, (page-1)*pageSize). - Where("is_private=?", false). - Limit(pageSize). - Desc("updated_unix"). +func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository, err error) { + if len(opts.OrderBy) == 0 { + opts.OrderBy = "updated_unix DESC" + } + + sess := x.Where("is_private=?", false). + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + Limit(opts.PageSize) + + if opts.Searcher != nil { + sess.Or("owner_id = ?", opts.Searcher.ID) + + err := opts.Searcher.GetOrganizations(true) + + if err != nil { + return nil, fmt.Errorf("Organization: %v", err) + } + + for _, org := range opts.Searcher.Orgs { + sess.Or("owner_id = ?", org.ID) + } + } + + return repos, sess. + OrderBy(opts.OrderBy). Find(&repos) } @@ -1587,6 +1610,7 @@ func GetRepositoryCount(u *User) (int64, error) { type SearchRepoOptions struct { Keyword string OwnerID int64 + Searcher *User //ID of the person who's seeking OrderBy string Private bool // Include private repositories in results Page int @@ -1616,6 +1640,25 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int sess.And("is_private=?", false) } + if opts.Searcher != nil { + + sess.Or("owner_id = ?", opts.Searcher.ID) + + err := opts.Searcher.GetOrganizations(true) + + if err != nil { + return nil, 0, fmt.Errorf("Organization: %v", err) + } + + for _, org := range opts.Searcher.Orgs { + sess.Or("owner_id = ?", org.ID) + } + } + + if len(opts.OrderBy) == 0 { + opts.OrderBy = "name ASC" + } + var countSess xorm.Session countSess = *sess count, err := countSess.Count(new(Repository)) @@ -1623,10 +1666,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int return nil, 0, fmt.Errorf("Count: %v", err) } - if len(opts.OrderBy) > 0 { - sess.OrderBy(opts.OrderBy) - } - return repos, count, sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).Find(&repos) + return repos, count, sess. + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + OrderBy(opts.OrderBy). + Find(&repos) } // DeleteRepositoryArchives deletes all repositories' archives. diff --git a/models/user.go b/models/user.go index 1b645a5285..803cb3b03e 100644 --- a/models/user.go +++ b/models/user.go @@ -641,12 +641,18 @@ func CountUsers() int64 { } // Users returns number of users in given page. -func Users(page, pageSize int) ([]*User, error) { - users := make([]*User, 0, pageSize) - return users, x. - Limit(pageSize, (page-1)*pageSize). - Where("type=0"). - Asc("name"). +func Users(opts *SearchUserOptions) ([]*User, error) { + if len(opts.OrderBy) == 0 { + opts.OrderBy = "name ASC" + } + + users := make([]*User, 0, opts.PageSize) + sess := x. + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + Where("type=0") + + return users, sess. + OrderBy(opts.OrderBy). Find(&users) } diff --git a/routers/admin/orgs.go b/routers/admin/orgs.go index 88d88d1c05..eeb9002bc2 100644 --- a/routers/admin/orgs.go +++ b/routers/admin/orgs.go @@ -27,7 +27,6 @@ func Organizations(ctx *context.Context) { Counter: models.CountOrganizations, Ranger: models.Organizations, PageSize: setting.UI.Admin.OrgPagingNum, - OrderBy: "id ASC", TplName: tplOrgs, }) } diff --git a/routers/admin/repos.go b/routers/admin/repos.go index 86a9ea9405..f2f402db76 100644 --- a/routers/admin/repos.go +++ b/routers/admin/repos.go @@ -28,7 +28,6 @@ func Repos(ctx *context.Context) { Ranger: models.Repositories, Private: true, PageSize: setting.UI.Admin.RepoPagingNum, - OrderBy: "owner_id ASC, name ASC, id ASC", TplName: tplRepos, }) } diff --git a/routers/admin/users.go b/routers/admin/users.go index fa61a46938..782012d95d 100644 --- a/routers/admin/users.go +++ b/routers/admin/users.go @@ -35,7 +35,6 @@ func Users(ctx *context.Context) { Counter: models.CountUsers, Ranger: models.Users, PageSize: setting.UI.Admin.UserPagingNum, - OrderBy: "id ASC", TplName: tplUsers, }) } diff --git a/routers/home.go b/routers/home.go index 3b51148762..d3c48f7aae 100644 --- a/routers/home.go +++ b/routers/home.go @@ -55,10 +55,10 @@ func Home(ctx *context.Context) { // RepoSearchOptions when calling search repositories type RepoSearchOptions struct { Counter func(bool) int64 - Ranger func(int, int) ([]*models.Repository, error) + Ranger func(*models.SearchRepoOptions) ([]*models.Repository, error) + Searcher *models.User Private bool PageSize int - OrderBy string TplName base.TplName } @@ -78,14 +78,36 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { } var ( - repos []*models.Repository - count int64 - err error + repos []*models.Repository + count int64 + err error + orderBy string ) + ctx.Data["SortType"] = ctx.Query("sort") + + switch ctx.Query("sort") { + case "oldest": + orderBy = "created_unix ASC" + case "recentupdate": + orderBy = "updated_unix DESC" + case "leastupdate": + orderBy = "updated_unix ASC" + case "reversealphabetically": + orderBy = "name DESC" + case "alphabetically": + orderBy = "name ASC" + default: + orderBy = "created_unix DESC" + } keyword := ctx.Query("q") if len(keyword) == 0 { - repos, err = opts.Ranger(page, opts.PageSize) + repos, err = opts.Ranger(&models.SearchRepoOptions{ + Page: page, + PageSize: opts.PageSize, + Searcher: ctx.User, + OrderBy: orderBy, + }) if err != nil { ctx.Handle(500, "opts.Ranger", err) return @@ -95,10 +117,11 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { if isKeywordValid(keyword) { repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ Keyword: keyword, - OrderBy: opts.OrderBy, + OrderBy: orderBy, Private: opts.Private, Page: page, PageSize: opts.PageSize, + Searcher: ctx.User, }) if err != nil { ctx.Handle(500, "SearchRepositoryByName", err) @@ -131,7 +154,7 @@ func ExploreRepos(ctx *context.Context) { Counter: models.CountRepositories, Ranger: models.GetRecentUpdatedRepositories, PageSize: setting.UI.ExplorePagingNum, - OrderBy: "updated_unix DESC", + Searcher: ctx.User, TplName: tplExploreRepos, }) } @@ -140,9 +163,8 @@ func ExploreRepos(ctx *context.Context) { type UserSearchOptions struct { Type models.UserType Counter func() int64 - Ranger func(int, int) ([]*models.User, error) + Ranger func(*models.SearchUserOptions) ([]*models.User, error) PageSize int - OrderBy string TplName base.TplName } @@ -154,14 +176,35 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) { } var ( - users []*models.User - count int64 - err error + users []*models.User + count int64 + err error + orderBy string ) + ctx.Data["SortType"] = ctx.Query("sort") + //OrderBy: "id ASC", + switch ctx.Query("sort") { + case "oldest": + orderBy = "id ASC" + case "recentupdate": + orderBy = "updated_unix DESC" + case "leastupdate": + orderBy = "updated_unix ASC" + case "reversealphabetically": + orderBy = "name DESC" + case "alphabetically": + orderBy = "name ASC" + default: + orderBy = "id DESC" + } + keyword := ctx.Query("q") if len(keyword) == 0 { - users, err = opts.Ranger(page, opts.PageSize) + users, err = opts.Ranger(&models.SearchUserOptions{OrderBy: orderBy, + Page: page, + PageSize: opts.PageSize, + }) if err != nil { ctx.Handle(500, "opts.Ranger", err) return @@ -172,7 +215,7 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) { users, count, err = models.SearchUserByName(&models.SearchUserOptions{ Keyword: keyword, Type: opts.Type, - OrderBy: opts.OrderBy, + OrderBy: orderBy, Page: page, PageSize: opts.PageSize, }) @@ -201,7 +244,6 @@ func ExploreUsers(ctx *context.Context) { Counter: models.CountUsers, Ranger: models.Users, PageSize: setting.UI.ExplorePagingNum, - OrderBy: "name ASC", TplName: tplExploreUsers, }) } @@ -217,7 +259,6 @@ func ExploreOrganizations(ctx *context.Context) { Counter: models.CountOrganizations, Ranger: models.Organizations, PageSize: setting.UI.ExplorePagingNum, - OrderBy: "name ASC", TplName: tplExploreOrganizations, }) } diff --git a/templates/admin/base/search.tmpl b/templates/admin/base/search.tmpl index 22634354b8..9e450ee367 100644 --- a/templates/admin/base/search.tmpl +++ b/templates/admin/base/search.tmpl @@ -1,4 +1,21 @@ -