mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-07 10:55:22 +02:00
fix(ui): Improve chronological sorting of user (#7596)
This PR changes `newest` and `oldest` sorting under *Explore/User* and *Explore/Organization* to refer to the `created_unix` column rather than `id`. Fixes: #7595 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7596 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Gabriel Bjørnager Jensen <gabriel@achernar.io> Co-committed-by: Gabriel Bjørnager Jensen <gabriel@achernar.io>
This commit is contained in:
parent
dbfc24001f
commit
f07456286e
2 changed files with 69 additions and 30 deletions
|
@ -20,7 +20,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// tplExploreUsers explore users page template
|
// `tplExploreUsers` explore users page template.
|
||||||
tplExploreUsers base.TplName = "explore/users"
|
tplExploreUsers base.TplName = "explore/users"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,9 +30,9 @@ func isKeywordValid(keyword string) bool {
|
||||||
return !bytes.Contains([]byte(keyword), nullByte)
|
return !bytes.Contains([]byte(keyword), nullByte)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderUserSearch render user search page
|
// `RenderUserSearch` render user search page.
|
||||||
func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, tplName base.TplName) {
|
func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, tplName base.TplName) {
|
||||||
// Sitemap index for sitemap paths
|
// Sitemap index for sitemap paths.
|
||||||
opts.Page = int(ctx.ParamsInt64("idx"))
|
opts.Page = int(ctx.ParamsInt64("idx"))
|
||||||
isSitemap := ctx.Params("idx") != ""
|
isSitemap := ctx.Params("idx") != ""
|
||||||
if opts.Page <= 1 {
|
if opts.Page <= 1 {
|
||||||
|
@ -50,39 +50,21 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
|
||||||
users []*user_model.User
|
users []*user_model.User
|
||||||
count int64
|
count int64
|
||||||
err error
|
err error
|
||||||
orderBy db.SearchOrderBy
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
sortOrder := ctx.FormString("sort")
|
sortOrder := ctx.FormString("sort")
|
||||||
if sortOrder == "" {
|
if sortOrder == "" {
|
||||||
sortOrder = setting.UI.ExploreDefaultSort
|
sortOrder = setting.UI.ExploreDefaultSort
|
||||||
}
|
}
|
||||||
ctx.Data["SortType"] = sortOrder
|
ctx.Data["SortType"] = sortOrder
|
||||||
|
|
||||||
switch sortOrder {
|
orderBy := MapSortOrder(sortOrder)
|
||||||
case "newest":
|
|
||||||
orderBy = "`user`.id DESC"
|
if orderBy == "" {
|
||||||
case "oldest":
|
// In case the `sortType` is not valid, we set it to `recentupdate`.
|
||||||
orderBy = "`user`.id ASC"
|
|
||||||
case "leastupdate":
|
|
||||||
orderBy = "`user`.updated_unix ASC"
|
|
||||||
case "reversealphabetically":
|
|
||||||
orderBy = "`user`.name DESC"
|
|
||||||
case "lastlogin":
|
|
||||||
orderBy = "`user`.last_login_unix ASC"
|
|
||||||
case "reverselastlogin":
|
|
||||||
orderBy = "`user`.last_login_unix DESC"
|
|
||||||
case "alphabetically":
|
|
||||||
orderBy = "`user`.name ASC"
|
|
||||||
case "recentupdate":
|
|
||||||
fallthrough
|
|
||||||
default:
|
|
||||||
// in case the sortType is not valid, we set it to recentupdate
|
|
||||||
sortOrder = "recentupdate"
|
sortOrder = "recentupdate"
|
||||||
ctx.Data["SortType"] = "recentupdate"
|
ctx.Data["SortType"] = "recentupdate"
|
||||||
orderBy = "`user`.updated_unix DESC"
|
orderBy = MapSortOrder(sortOrder)
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.SupportedSortOrders != nil && !opts.SupportedSortOrders.Contains(sortOrder) {
|
if opts.SupportedSortOrders != nil && !opts.SupportedSortOrders.Contains(sortOrder) {
|
||||||
|
@ -130,7 +112,7 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
|
||||||
ctx.HTML(http.StatusOK, tplName)
|
ctx.HTML(http.StatusOK, tplName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Users render explore users page
|
// Users render explore users page.
|
||||||
func Users(ctx *context.Context) {
|
func Users(ctx *context.Context) {
|
||||||
if setting.Service.Explore.DisableUsersPage {
|
if setting.Service.Explore.DisableUsersPage {
|
||||||
ctx.Redirect(setting.AppSubURL + "/explore")
|
ctx.Redirect(setting.AppSubURL + "/explore")
|
||||||
|
@ -169,3 +151,37 @@ func Users(ctx *context.Context) {
|
||||||
SupportedSortOrders: supportedSortOrders,
|
SupportedSortOrders: supportedSortOrders,
|
||||||
}, tplExploreUsers)
|
}, tplExploreUsers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Maps a sort query to a database search order.
|
||||||
|
//
|
||||||
|
// We cannot use `models.SearchOrderByXxx`, because there may be a JOIN in the statement, different tables may have the same name columns.
|
||||||
|
func MapSortOrder(sortOrder string) db.SearchOrderBy {
|
||||||
|
switch sortOrder {
|
||||||
|
case "newest":
|
||||||
|
return "`user`.created_unix DESC"
|
||||||
|
|
||||||
|
case "oldest":
|
||||||
|
return "`user`.created_unix ASC"
|
||||||
|
|
||||||
|
case "leastupdate":
|
||||||
|
return "`user`.updated_unix ASC"
|
||||||
|
|
||||||
|
case "reversealphabetically":
|
||||||
|
return "`user`.name DESC"
|
||||||
|
|
||||||
|
case "lastlogin":
|
||||||
|
return "`user`.last_login_unix ASC"
|
||||||
|
|
||||||
|
case "reverselastlogin":
|
||||||
|
return "`user`.last_login_unix DESC"
|
||||||
|
|
||||||
|
case "alphabetically":
|
||||||
|
return "`user`.name ASC"
|
||||||
|
|
||||||
|
case "recentupdate":
|
||||||
|
return "`user`.updated_unix DESC"
|
||||||
|
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
23
routers/web/explore/user_test.go
Normal file
23
routers/web/explore/user_test.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package explore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"forgejo.org/models/db"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMapSortOrder(t *testing.T) {
|
||||||
|
assert.Equal(t, MapSortOrder("newest"), db.SearchOrderBy("`user`.created_unix DESC"))
|
||||||
|
assert.Equal(t, MapSortOrder("oldest"), db.SearchOrderBy("`user`.created_unix ASC"))
|
||||||
|
assert.Equal(t, MapSortOrder("leastupdate"), db.SearchOrderBy("`user`.updated_unix ASC"))
|
||||||
|
assert.Equal(t, MapSortOrder("reversealphabetically"), db.SearchOrderBy("`user`.name DESC"))
|
||||||
|
assert.Equal(t, MapSortOrder("lastlogin"), db.SearchOrderBy("`user`.last_login_unix ASC"))
|
||||||
|
assert.Equal(t, MapSortOrder("reverselastlogin"), db.SearchOrderBy("`user`.last_login_unix DESC"))
|
||||||
|
assert.Equal(t, MapSortOrder("alphabetically"), db.SearchOrderBy("`user`.name ASC"))
|
||||||
|
assert.Equal(t, MapSortOrder("recentupdate"), db.SearchOrderBy("`user`.updated_unix DESC"))
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue