Skip to content

Commit 4daf405

Browse files
Sort users and orgs on explore by recency by default (#24279)
This gives more "freshness" to the explore page. So it's not just the same X users on the explore page by default, now it matches the same sort as the repos on the explore page. --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent 4667955 commit 4daf405

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

modules/context/form.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ func (ctx *Context) FormOptionalBool(key string) util.OptionalBool {
6565
v = v || strings.EqualFold(s, "on")
6666
return util.OptionalBoolOf(v)
6767
}
68+
69+
func (ctx *Context) SetFormString(key, value string) {
70+
_ = ctx.Req.FormValue(key) // force parse form
71+
ctx.Req.Form.Set(key, value)
72+
}

routers/web/admin/orgs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func Organizations(ctx *context.Context) {
2323
ctx.Data["Title"] = ctx.Tr("admin.organizations")
2424
ctx.Data["PageIsAdminOrganizations"] = true
2525

26+
if ctx.FormString("sort") == "" {
27+
ctx.SetFormString("sort", explore.UserSearchDefaultAdminSort)
28+
}
29+
2630
explore.RenderUserSearch(ctx, &user_model.SearchUserOptions{
2731
Actor: ctx.Doer,
2832
Type: user_model.UserTypeOrganization,

routers/web/admin/users.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func Users(ctx *context.Context) {
5353

5454
sortType := ctx.FormString("sort")
5555
if sortType == "" {
56-
sortType = explore.UserSearchDefaultSortType
56+
sortType = explore.UserSearchDefaultAdminSort
57+
ctx.SetFormString("sort", sortType)
5758
}
5859
ctx.PageData["adminUserListSearchForm"] = map[string]interface{}{
5960
"StatusFilterMap": statusFilterMap,

routers/web/explore/org.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func Organizations(ctx *context.Context) {
3030
visibleTypes = append(visibleTypes, structs.VisibleTypeLimited, structs.VisibleTypePrivate)
3131
}
3232

33+
if ctx.FormString("sort") == "" {
34+
ctx.SetFormString("sort", UserSearchDefaultSortType)
35+
}
36+
3337
RenderUserSearch(ctx, &user_model.SearchUserOptions{
3438
Actor: ctx.Doer,
3539
Type: user_model.UserTypeOrganization,

routers/web/explore/user.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ const (
2424
)
2525

2626
// UserSearchDefaultSortType is the default sort type for user search
27-
const UserSearchDefaultSortType = "alphabetically"
27+
const (
28+
UserSearchDefaultSortType = "recentupdate"
29+
UserSearchDefaultAdminSort = "alphabetically"
30+
)
2831

2932
var nullByte = []byte{0x00}
3033

@@ -56,14 +59,13 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
5659
)
5760

5861
// we can not set orderBy to `models.SearchOrderByXxx`, because there may be a JOIN in the statement, different tables may have the same name columns
62+
5963
ctx.Data["SortType"] = ctx.FormString("sort")
6064
switch ctx.FormString("sort") {
6165
case "newest":
6266
orderBy = "`user`.id DESC"
6367
case "oldest":
6468
orderBy = "`user`.id ASC"
65-
case "recentupdate":
66-
orderBy = "`user`.updated_unix DESC"
6769
case "leastupdate":
6870
orderBy = "`user`.updated_unix ASC"
6971
case "reversealphabetically":
@@ -72,10 +74,14 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
7274
orderBy = "`user`.last_login_unix ASC"
7375
case "reverselastlogin":
7476
orderBy = "`user`.last_login_unix DESC"
75-
case UserSearchDefaultSortType: // "alphabetically"
76-
default:
77+
case "alphabetically":
7778
orderBy = "`user`.name ASC"
78-
ctx.Data["SortType"] = UserSearchDefaultSortType
79+
case "recentupdate":
80+
fallthrough
81+
default:
82+
// in case the sortType is not valid, we set it to recentupdate
83+
ctx.Data["SortType"] = "recentupdate"
84+
orderBy = "`user`.updated_unix DESC"
7985
}
8086

8187
opts.Keyword = ctx.FormTrim("q")
@@ -127,6 +133,10 @@ func Users(ctx *context.Context) {
127133
ctx.Data["PageIsExploreUsers"] = true
128134
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
129135

136+
if ctx.FormString("sort") == "" {
137+
ctx.SetFormString("sort", UserSearchDefaultSortType)
138+
}
139+
130140
RenderUserSearch(ctx, &user_model.SearchUserOptions{
131141
Actor: ctx.Doer,
132142
Type: user_model.UserTypeIndividual,

tests/integration/setting_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestSettingShowUserEmailExplore(t *testing.T) {
2020
setting.UI.ShowUserEmail = true
2121

2222
session := loginUser(t, "user2")
23-
req := NewRequest(t, "GET", "/explore/users")
23+
req := NewRequest(t, "GET", "/explore/users?sort=alphabetically")
2424
resp := session.MakeRequest(t, req, http.StatusOK)
2525
htmlDoc := NewHTMLParser(t, resp.Body)
2626
assert.Contains(t,
@@ -30,7 +30,7 @@ func TestSettingShowUserEmailExplore(t *testing.T) {
3030

3131
setting.UI.ShowUserEmail = false
3232

33-
req = NewRequest(t, "GET", "/explore/users")
33+
req = NewRequest(t, "GET", "/explore/users?sort=alphabetically")
3434
resp = session.MakeRequest(t, req, http.StatusOK)
3535
htmlDoc = NewHTMLParser(t, resp.Body)
3636
assert.NotContains(t,

0 commit comments

Comments
 (0)