2021-06-09 02:33:54 +03:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-09 02:33:54 +03:00
|
|
|
|
|
|
|
package explore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
|
2021-09-24 14:32:56 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-09 02:33:54 +03:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-06-25 20:06:01 +03:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-06-09 02:33:54 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-25 20:06:01 +03:00
|
|
|
"code.gitea.io/gitea/modules/sitemap"
|
2021-06-09 02:33:54 +03:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// tplExploreUsers explore users page template
|
|
|
|
tplExploreUsers base.TplName = "explore/users"
|
|
|
|
)
|
|
|
|
|
2021-10-12 21:11:35 +03:00
|
|
|
// UserSearchDefaultSortType is the default sort type for user search
|
|
|
|
const UserSearchDefaultSortType = "alphabetically"
|
|
|
|
|
2022-01-20 20:46:10 +03:00
|
|
|
var nullByte = []byte{0x00}
|
2021-06-09 02:33:54 +03:00
|
|
|
|
|
|
|
func isKeywordValid(keyword string) bool {
|
|
|
|
return !bytes.Contains([]byte(keyword), nullByte)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderUserSearch render user search page
|
2021-11-24 12:49:20 +03:00
|
|
|
func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, tplName base.TplName) {
|
2022-06-25 20:06:01 +03:00
|
|
|
// Sitemap index for sitemap paths
|
|
|
|
opts.Page = int(ctx.ParamsInt64("idx"))
|
|
|
|
isSitemap := ctx.Params("idx") != ""
|
|
|
|
if opts.Page <= 1 {
|
|
|
|
opts.Page = ctx.FormInt("page")
|
|
|
|
}
|
2021-06-09 02:33:54 +03:00
|
|
|
if opts.Page <= 1 {
|
|
|
|
opts.Page = 1
|
|
|
|
}
|
|
|
|
|
2022-06-25 20:06:01 +03:00
|
|
|
if isSitemap {
|
|
|
|
opts.PageSize = setting.UI.SitemapPagingNum
|
|
|
|
}
|
|
|
|
|
2021-06-09 02:33:54 +03:00
|
|
|
var (
|
2021-11-24 12:49:20 +03:00
|
|
|
users []*user_model.User
|
2021-06-09 02:33:54 +03:00
|
|
|
count int64
|
|
|
|
err error
|
2021-11-24 12:49:20 +03:00
|
|
|
orderBy db.SearchOrderBy
|
2021-06-09 02:33:54 +03:00
|
|
|
)
|
|
|
|
|
2021-10-12 21:11:35 +03:00
|
|
|
// 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
|
2021-08-11 03:31:13 +03:00
|
|
|
ctx.Data["SortType"] = ctx.FormString("sort")
|
|
|
|
switch ctx.FormString("sort") {
|
2021-06-09 02:33:54 +03:00
|
|
|
case "newest":
|
2021-10-12 21:11:35 +03:00
|
|
|
orderBy = "`user`.id DESC"
|
2021-06-09 02:33:54 +03:00
|
|
|
case "oldest":
|
2021-10-12 21:11:35 +03:00
|
|
|
orderBy = "`user`.id ASC"
|
2021-06-09 02:33:54 +03:00
|
|
|
case "recentupdate":
|
2021-10-12 21:11:35 +03:00
|
|
|
orderBy = "`user`.updated_unix DESC"
|
2021-06-09 02:33:54 +03:00
|
|
|
case "leastupdate":
|
2021-10-12 21:11:35 +03:00
|
|
|
orderBy = "`user`.updated_unix ASC"
|
2021-06-09 02:33:54 +03:00
|
|
|
case "reversealphabetically":
|
2021-10-12 21:11:35 +03:00
|
|
|
orderBy = "`user`.name DESC"
|
2022-12-12 03:00:17 +03:00
|
|
|
case "lastlogin":
|
|
|
|
orderBy = "`user`.last_login_unix ASC"
|
|
|
|
case "reverselastlogin":
|
|
|
|
orderBy = "`user`.last_login_unix DESC"
|
2021-10-12 21:11:35 +03:00
|
|
|
case UserSearchDefaultSortType: // "alphabetically"
|
2021-06-09 02:33:54 +03:00
|
|
|
default:
|
2021-10-12 21:11:35 +03:00
|
|
|
orderBy = "`user`.name ASC"
|
|
|
|
ctx.Data["SortType"] = UserSearchDefaultSortType
|
2021-06-09 02:33:54 +03:00
|
|
|
}
|
|
|
|
|
2021-08-11 18:08:52 +03:00
|
|
|
opts.Keyword = ctx.FormTrim("q")
|
2021-06-09 02:33:54 +03:00
|
|
|
opts.OrderBy = orderBy
|
|
|
|
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
|
2021-11-24 12:49:20 +03:00
|
|
|
users, count, err = user_model.SearchUsers(opts)
|
2021-06-09 02:33:54 +03:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchUsers", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 20:06:01 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-09 02:33:54 +03:00
|
|
|
ctx.Data["Keyword"] = opts.Keyword
|
|
|
|
ctx.Data["Total"] = count
|
|
|
|
ctx.Data["Users"] = users
|
2021-11-28 17:11:58 +03:00
|
|
|
ctx.Data["UsersTwoFaStatus"] = user_model.UserList(users).GetTwoFaStatus()
|
2021-06-09 02:33:54 +03:00
|
|
|
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)
|
2022-03-02 18:30:14 +03:00
|
|
|
for paramKey, paramVal := range opts.ExtraParamStrings {
|
|
|
|
pager.AddParamString(paramKey, paramVal)
|
|
|
|
}
|
2021-06-09 02:33:54 +03:00
|
|
|
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/repos")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreUsers"] = true
|
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
RenderUserSearch(ctx, &user_model.SearchUserOptions{
|
2022-03-22 10:03:22 +03:00
|
|
|
Actor: ctx.Doer,
|
2021-11-24 12:49:20 +03:00
|
|
|
Type: user_model.UserTypeIndividual,
|
2021-09-24 14:32:56 +03:00
|
|
|
ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum},
|
2021-06-09 02:33:54 +03:00
|
|
|
IsActive: util.OptionalBoolTrue,
|
|
|
|
Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
|
|
|
|
}, tplExploreUsers)
|
|
|
|
}
|