Skip to content

Commit 4ef00fe

Browse files
JohnMoon-VTSGusted
authored andcommitted
fix: set explore pages to configurable default sort (go-gitea#6708)
- Currently, the explore/organizations page always defaults to using "newest" as its sort. Instead, use the pre-existing config option (`setting.UI.ExploreDefaultSort`) so server administrators can change the default sort order. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6708 Reviewed-by: Gusted <[email protected]> Co-authored-by: John Moon <[email protected]> Co-committed-by: John Moon <[email protected]>
1 parent 226994a commit 4ef00fe

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

routers/web/explore/org.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ func Organizations(ctx *context.Context) {
3939
)
4040
sortOrder := ctx.FormString("sort")
4141
if sortOrder == "" {
42-
sortOrder = "newest"
42+
if supportedSortOrders.Contains(setting.UI.ExploreDefaultSort) {
43+
sortOrder = setting.UI.ExploreDefaultSort
44+
} else {
45+
sortOrder = "newest"
46+
}
4347
ctx.SetFormString("sort", sortOrder)
4448
}
4549

routers/web/explore/user.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ func Users(ctx *context.Context) {
151151
)
152152
sortOrder := ctx.FormString("sort")
153153
if sortOrder == "" {
154-
sortOrder = "newest"
154+
if supportedSortOrders.Contains(setting.UI.ExploreDefaultSort) {
155+
sortOrder = setting.UI.ExploreDefaultSort
156+
} else {
157+
sortOrder = "newest"
158+
}
155159
ctx.SetFormString("sort", sortOrder)
156160
}
157161

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"net/http"
8+
"testing"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
"code.gitea.io/gitea/modules/test"
12+
"code.gitea.io/gitea/tests"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestExploreOrg(t *testing.T) {
18+
defer tests.PrepareTestEnv(t)()
19+
20+
// Set the default sort order
21+
defer test.MockVariableValue(&setting.UI.ExploreDefaultSort, "alphabetically")()
22+
23+
cases := []struct{ sortOrder, expected string }{
24+
{"", "?sort=" + setting.UI.ExploreDefaultSort + "&q="},
25+
{"newest", "?sort=newest&q="},
26+
{"oldest", "?sort=oldest&q="},
27+
{"alphabetically", "?sort=alphabetically&q="},
28+
{"reversealphabetically", "?sort=reversealphabetically&q="},
29+
}
30+
for _, c := range cases {
31+
req := NewRequest(t, "GET", "/explore/organizations?sort="+c.sortOrder)
32+
resp := MakeRequest(t, req, http.StatusOK)
33+
h := NewHTMLParser(t, resp.Body)
34+
href, _ := h.Find(`.ui.dropdown .menu a.active.item[href^="?sort="]`).Attr("href")
35+
assert.Equal(t, c.expected, href)
36+
}
37+
38+
// these sort orders shouldn't be supported, to avoid leaking user activity
39+
cases404 := []string{
40+
"/explore/organizations?sort=mostMembers",
41+
"/explore/organizations?sort=leastGroups",
42+
"/explore/organizations?sort=leastupdate",
43+
"/explore/organizations?sort=reverseleastupdate",
44+
}
45+
for _, c := range cases404 {
46+
req := NewRequest(t, "GET", c).SetHeader("Accept", "text/html")
47+
MakeRequest(t, req, http.StatusNotFound)
48+
}
49+
}

tests/integration/explore_user_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"net/http"
88
"testing"
99

10+
"code.gitea.io/gitea/modules/setting"
11+
"code.gitea.io/gitea/modules/test"
1012
"code.gitea.io/gitea/tests"
1113

1214
"github.com/stretchr/testify/assert"
@@ -15,8 +17,11 @@ import (
1517
func TestExploreUser(t *testing.T) {
1618
defer tests.PrepareTestEnv(t)()
1719

20+
// Set the default sort order
21+
defer test.MockVariableValue(&setting.UI.ExploreDefaultSort, "reversealphabetically")()
22+
1823
cases := []struct{ sortOrder, expected string }{
19-
{"", "?sort=newest&q="},
24+
{"", "?sort=" + setting.UI.ExploreDefaultSort + "&q="},
2025
{"newest", "?sort=newest&q="},
2126
{"oldest", "?sort=oldest&q="},
2227
{"alphabetically", "?sort=alphabetically&q="},

0 commit comments

Comments
 (0)