mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-03-27 22:12:50 +00:00
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/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. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests (Sorry, not sure how to add a test for this change) - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes I don't mind either way. - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Co-authored-by: John Moon <john.moon@vts-i.com> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6749 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org> Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
169 lines
4.7 KiB
Go
169 lines
4.7 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package explore
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/base"
|
|
"code.gitea.io/gitea/modules/container"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/optional"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/sitemap"
|
|
"code.gitea.io/gitea/modules/structs"
|
|
"code.gitea.io/gitea/services/context"
|
|
)
|
|
|
|
const (
|
|
// tplExploreUsers explore users page template
|
|
tplExploreUsers base.TplName = "explore/users"
|
|
)
|
|
|
|
var nullByte = []byte{0x00}
|
|
|
|
func isKeywordValid(keyword string) bool {
|
|
return !bytes.Contains([]byte(keyword), nullByte)
|
|
}
|
|
|
|
// RenderUserSearch render user search page
|
|
func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, tplName base.TplName) {
|
|
// Sitemap index for sitemap paths
|
|
opts.Page = int(ctx.ParamsInt64("idx"))
|
|
isSitemap := ctx.Params("idx") != ""
|
|
if opts.Page <= 1 {
|
|
opts.Page = ctx.FormInt("page")
|
|
}
|
|
if opts.Page <= 1 {
|
|
opts.Page = 1
|
|
}
|
|
|
|
if isSitemap {
|
|
opts.PageSize = setting.UI.SitemapPagingNum
|
|
}
|
|
|
|
var (
|
|
users []*user_model.User
|
|
count int64
|
|
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")
|
|
if sortOrder == "" {
|
|
sortOrder = setting.UI.ExploreDefaultSort
|
|
}
|
|
ctx.Data["SortType"] = sortOrder
|
|
|
|
switch sortOrder {
|
|
case "newest":
|
|
orderBy = "`user`.id DESC"
|
|
case "oldest":
|
|
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"
|
|
ctx.Data["SortType"] = "recentupdate"
|
|
orderBy = "`user`.updated_unix DESC"
|
|
}
|
|
|
|
if opts.SupportedSortOrders != nil && !opts.SupportedSortOrders.Contains(sortOrder) {
|
|
ctx.NotFound("unsupported sort order", nil)
|
|
return
|
|
}
|
|
|
|
opts.Keyword = ctx.FormTrim("q")
|
|
opts.OrderBy = orderBy
|
|
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
|
|
users, count, err = user_model.SearchUsers(ctx, opts)
|
|
if err != nil {
|
|
ctx.ServerError("SearchUsers", err)
|
|
return
|
|
}
|
|
}
|
|
if isSitemap {
|
|
m := sitemap.NewSitemap()
|
|
for _, item := range users {
|
|
m.Add(sitemap.URL{URL: item.HTMLURL(), LastMod: item.UpdatedUnix.AsTimePtr()})
|
|
}
|
|
ctx.Resp.Header().Set("Content-Type", "text/xml")
|
|
if _, err := m.WriteTo(ctx.Resp); err != nil {
|
|
log.Error("Failed writing sitemap: %v", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx.Data["Keyword"] = opts.Keyword
|
|
ctx.Data["Total"] = count
|
|
ctx.Data["Users"] = users
|
|
ctx.Data["UsersTwoFaStatus"] = user_model.UserList(users).GetTwoFaStatus(ctx)
|
|
ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
|
|
|
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
|
|
pager.SetDefaultParams(ctx)
|
|
for paramKey, paramVal := range opts.ExtraParamStrings {
|
|
pager.AddParamString(paramKey, paramVal)
|
|
}
|
|
ctx.Data["Page"] = pager
|
|
|
|
ctx.HTML(http.StatusOK, tplName)
|
|
}
|
|
|
|
// Users render explore users page
|
|
func Users(ctx *context.Context) {
|
|
if setting.Service.Explore.DisableUsersPage {
|
|
ctx.Redirect(setting.AppSubURL + "/explore")
|
|
return
|
|
}
|
|
ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage
|
|
ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
ctx.Data["PageIsExplore"] = true
|
|
ctx.Data["PageIsExploreUsers"] = true
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
|
|
|
supportedSortOrders := container.SetOf(
|
|
"newest",
|
|
"oldest",
|
|
"alphabetically",
|
|
"reversealphabetically",
|
|
)
|
|
sortOrder := ctx.FormString("sort")
|
|
if sortOrder == "" {
|
|
if supportedSortOrders.Contains(setting.UI.ExploreDefaultSort) {
|
|
sortOrder = setting.UI.ExploreDefaultSort
|
|
} else {
|
|
sortOrder = "newest"
|
|
}
|
|
ctx.SetFormString("sort", sortOrder)
|
|
}
|
|
|
|
RenderUserSearch(ctx, &user_model.SearchUserOptions{
|
|
Actor: ctx.Doer,
|
|
Type: user_model.UserTypeIndividual,
|
|
ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum},
|
|
IsActive: optional.Some(true),
|
|
Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
|
|
|
|
SupportedSortOrders: supportedSortOrders,
|
|
}, tplExploreUsers)
|
|
}
|