When organization grouping is enabled, modifies sort order to group by group_header first before applying user-selected ordering. This prevents organizations in the same group from being split across pagination boundaries. Adds CASE expression to sort ungrouped orgs (null/empty group_header) last, then groups alphabetically, then applies the requested orderBy within each group.
202 lines
6.1 KiB
Go
202 lines
6.1 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package explore
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"sort"
|
|
|
|
"code.gitcaddy.com/server/v3/models/db"
|
|
user_model "code.gitcaddy.com/server/v3/models/user"
|
|
"code.gitcaddy.com/server/v3/modules/container"
|
|
"code.gitcaddy.com/server/v3/modules/log"
|
|
"code.gitcaddy.com/server/v3/modules/optional"
|
|
"code.gitcaddy.com/server/v3/modules/setting"
|
|
"code.gitcaddy.com/server/v3/modules/sitemap"
|
|
"code.gitcaddy.com/server/v3/modules/structs"
|
|
"code.gitcaddy.com/server/v3/modules/templates"
|
|
"code.gitcaddy.com/server/v3/modules/util"
|
|
"code.gitcaddy.com/server/v3/services/context"
|
|
)
|
|
|
|
const (
|
|
// tplExploreUsers explore users page template
|
|
tplExploreUsers templates.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 templates.TplName) {
|
|
// Sitemap index for sitemap paths
|
|
opts.Page = ctx.PathParamInt("idx")
|
|
isSitemap := ctx.PathParam("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(nil)
|
|
return
|
|
}
|
|
|
|
opts.Keyword = ctx.FormTrim("q")
|
|
// When grouping is enabled, order by group first so groups aren't split across pages
|
|
if ctx.Data["PageIsExploreOrganizations"] == true && ctx.Data["ShowGrouping"] == true {
|
|
opts.OrderBy = "CASE WHEN `user`.group_header = '' OR `user`.group_header IS NULL THEN 1 ELSE 0 END, `user`.group_header ASC, " + orderBy
|
|
} else {
|
|
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(ctx), 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
|
|
|
|
// Group organizations by GroupHeader if grouping is enabled (for org explore page)
|
|
if ctx.Data["PageIsExploreOrganizations"] == true && ctx.Data["ShowGrouping"] == true {
|
|
groupedOrgs := make(map[string][]*user_model.User)
|
|
var headers []string
|
|
headerSeen := make(map[string]bool)
|
|
|
|
for _, user := range users {
|
|
header := user.GroupHeader
|
|
if !headerSeen[header] {
|
|
headerSeen[header] = true
|
|
headers = append(headers, header)
|
|
}
|
|
groupedOrgs[header] = append(groupedOrgs[header], user)
|
|
}
|
|
|
|
// Sort headers alphabetically, empty string last
|
|
sort.Slice(headers, func(i, j int) bool {
|
|
if headers[i] == "" {
|
|
return false
|
|
}
|
|
if headers[j] == "" {
|
|
return true
|
|
}
|
|
return headers[i] < headers[j]
|
|
})
|
|
|
|
ctx.Data["GroupedOrgs"] = groupedOrgs
|
|
ctx.Data["OrgHeaders"] = headers
|
|
}
|
|
|
|
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
|
|
pager.AddParamFromRequest(ctx.Req)
|
|
ctx.Data["Page"] = pager
|
|
|
|
ctx.HTML(http.StatusOK, tplName)
|
|
}
|
|
|
|
// Users render explore users page
|
|
func Users(ctx *context.Context) {
|
|
if setting.Service.Explore.DisableUsersPage || setting.Config().Theme.HideExploreUsers.Value(ctx) {
|
|
ctx.Redirect(setting.AppSubURL + "/explore")
|
|
return
|
|
}
|
|
ctx.Data["OrganizationsPageIsDisabled"] = setting.Service.Explore.DisableOrganizationsPage
|
|
ctx.Data["CodePageIsDisabled"] = setting.Service.Explore.DisableCodePage
|
|
ctx.Data["PackagesPageIsEnabled"] = setting.Service.Explore.EnablePackagesPage || setting.Config().Theme.EnableExplorePackages.Value(ctx)
|
|
ctx.Data["BlogsPageIsEnabled"] = setting.Config().Theme.EnableBlogs.Value(ctx)
|
|
ctx.Data["Title"] = ctx.Tr("explore_title")
|
|
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 == "" {
|
|
sortOrder = util.Iif(supportedSortOrders.Contains(setting.UI.ExploreDefaultSort), setting.UI.ExploreDefaultSort, "newest")
|
|
ctx.SetFormString("sort", sortOrder)
|
|
}
|
|
|
|
RenderUserSearch(ctx, user_model.SearchUserOptions{
|
|
Actor: ctx.Doer,
|
|
Types: []user_model.UserType{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)
|
|
}
|