diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-10-25 01:36:19 +0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-10-24 20:36:19 +0300 |
commit | 6eeadb2082193f8e3dc5269dfc6da2daef7eb6a1 (patch) | |
tree | d28d3be3451194e98a5c01097347e7eebdb6393f /models/user.go | |
parent | 03900303a9500b33b33ff03fd6dd325147a1117e (diff) | |
download | gitea-6eeadb2082193f8e3dc5269dfc6da2daef7eb6a1.tar.gz gitea-6eeadb2082193f8e3dc5269dfc6da2daef7eb6a1.zip |
Hide unactive on explore users and some refactors (#2741)
* hide unactive on explore users and some refactors
* fix test for removed Organizations
* fix test for removed Organizations
* fix imports
* fix logic bug
* refactor the toConds
* Rename TestOrganizations to TestSearchUsers and add tests for users
* fix other tests
* fix other tests
* fix watchers tests
* fix comments and remove unused code
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 74 |
1 files changed, 32 insertions, 42 deletions
diff --git a/models/user.go b/models/user.go index 835a775ebc..337c39efe4 100644 --- a/models/user.go +++ b/models/user.go @@ -36,6 +36,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" ) // UserType defines the user type @@ -729,22 +730,6 @@ func CountUsers() int64 { return countUsers(x) } -// Users returns number of users in given page. -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) -} - // get user by verify code func getVerifyUser(code string) (user *User) { if len(code) <= base.TimeLimitCodeLength { @@ -1284,45 +1269,50 @@ type SearchUserOptions struct { OrderBy string Page int PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum + IsActive util.OptionalBool } -// SearchUserByName takes keyword and part of user name to search, -// it returns results in given range and number of total results. -func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error) { - if len(opts.Keyword) == 0 { - return users, 0, nil +func (opts *SearchUserOptions) toConds() builder.Cond { + var cond builder.Cond = builder.Eq{"type": opts.Type} + if len(opts.Keyword) > 0 { + lowerKeyword := strings.ToLower(opts.Keyword) + cond = cond.And(builder.Or( + builder.Like{"lower_name", lowerKeyword}, + builder.Like{"LOWER(full_name)", lowerKeyword}, + )) } - opts.Keyword = strings.ToLower(opts.Keyword) - if opts.PageSize <= 0 || opts.PageSize > setting.UI.ExplorePagingNum { - opts.PageSize = setting.UI.ExplorePagingNum - } - if opts.Page <= 0 { - opts.Page = 1 + if !opts.IsActive.IsNone() { + cond = cond.And(builder.Eq{"is_active": opts.IsActive.IsTrue()}) } - users = make([]*User, 0, opts.PageSize) - - // Append conditions - cond := builder.And( - builder.Eq{"type": opts.Type}, - builder.Or( - builder.Like{"lower_name", opts.Keyword}, - builder.Like{"LOWER(full_name)", opts.Keyword}, - ), - ) + return cond +} +// SearchUsers takes options i.e. keyword and part of user name to search, +// it returns results in given range and number of total results. +func SearchUsers(opts *SearchUserOptions) (users []*User, _ int64, _ error) { + cond := opts.toConds() count, err := x.Where(cond).Count(new(User)) if err != nil { return nil, 0, fmt.Errorf("Count: %v", err) } - sess := x.Where(cond). - Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) - if len(opts.OrderBy) > 0 { - sess.OrderBy(opts.OrderBy) + if opts.PageSize <= 0 || opts.PageSize > setting.UI.ExplorePagingNum { + opts.PageSize = setting.UI.ExplorePagingNum + } + if opts.Page <= 0 { + opts.Page = 1 } - return users, count, sess.Find(&users) + if len(opts.OrderBy) == 0 { + opts.OrderBy = "name ASC" + } + + users = make([]*User, 0, opts.PageSize) + return users, count, x.Where(cond). + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + OrderBy(opts.OrderBy). + Find(&users) } // GetStarredRepos returns the repos starred by a particular user |