From 661a1e10f7abd3527d2abc027dec936022db9379 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 2 Sep 2024 01:55:05 +0900 Subject: [PATCH] Fix sort order for organization home and user profile page (#31921) Fix #31916 In #30876, `sortOrder` has been changed into a map, but it is only implemented in explore. ~~But it seems that size sort order has no effect from long long ago,~~ not directly caused by the PR above. I think it is still caused by #29231. In #29231, it merged the sort orders from `templates/explore/repo_search.tmpl` and `templates/admin/repo/search.tmpl`. In `templates/admin/repo/search.tmpl`, it contains size sort orders, but not in `templates/explore/repo_search.tmpl`, which is used in non-admin pages. So `order by size` is added from #29231, but the handler was not added. --------- Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: wxiaoguang --- routers/web/org/home.go | 30 +++++------------------------- routers/web/user/profile.go | 30 +++++------------------------- 2 files changed, 10 insertions(+), 50 deletions(-) diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 366a7b20de..069bd549c1 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -55,32 +55,12 @@ func home(ctx *context.Context, viewRepositories bool) { ctx.Data["Title"] = org.DisplayName() var orderBy db.SearchOrderBy - ctx.Data["SortType"] = ctx.FormString("sort") - switch ctx.FormString("sort") { - case "newest": - orderBy = db.SearchOrderByNewest - case "oldest": - orderBy = db.SearchOrderByOldest - case "recentupdate": - orderBy = db.SearchOrderByRecentUpdated - case "leastupdate": - orderBy = db.SearchOrderByLeastUpdated - case "reversealphabetically": - orderBy = db.SearchOrderByAlphabeticallyReverse - case "alphabetically": - orderBy = db.SearchOrderByAlphabetically - case "moststars": - orderBy = db.SearchOrderByStarsReverse - case "feweststars": - orderBy = db.SearchOrderByStars - case "mostforks": - orderBy = db.SearchOrderByForksReverse - case "fewestforks": - orderBy = db.SearchOrderByForks - default: - ctx.Data["SortType"] = "recentupdate" - orderBy = db.SearchOrderByRecentUpdated + sortOrder := ctx.FormString("sort") + if _, ok := repo_model.OrderByFlatMap[sortOrder]; !ok { + sortOrder = setting.UI.ExploreDefaultSort // TODO: add new default sort order for org home? } + ctx.Data["SortType"] = sortOrder + orderBy = repo_model.OrderByFlatMap[sortOrder] keyword := ctx.FormTrim("q") ctx.Data["Keyword"] = keyword diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 3f91233ee6..39c6d23079 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -110,32 +110,12 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb orderBy db.SearchOrderBy ) - ctx.Data["SortType"] = ctx.FormString("sort") - switch ctx.FormString("sort") { - case "newest": - orderBy = db.SearchOrderByNewest - case "oldest": - orderBy = db.SearchOrderByOldest - case "recentupdate": - orderBy = db.SearchOrderByRecentUpdated - case "leastupdate": - orderBy = db.SearchOrderByLeastUpdated - case "reversealphabetically": - orderBy = db.SearchOrderByAlphabeticallyReverse - case "alphabetically": - orderBy = db.SearchOrderByAlphabetically - case "moststars": - orderBy = db.SearchOrderByStarsReverse - case "feweststars": - orderBy = db.SearchOrderByStars - case "mostforks": - orderBy = db.SearchOrderByForksReverse - case "fewestforks": - orderBy = db.SearchOrderByForks - default: - ctx.Data["SortType"] = "recentupdate" - orderBy = db.SearchOrderByRecentUpdated + sortOrder := ctx.FormString("sort") + if _, ok := repo_model.OrderByFlatMap[sortOrder]; !ok { + sortOrder = setting.UI.ExploreDefaultSort // TODO: add new default sort order for org home? } + ctx.Data["SortType"] = sortOrder + orderBy = repo_model.OrderByFlatMap[sortOrder] keyword := ctx.FormTrim("q") ctx.Data["Keyword"] = keyword