2014-02-12 23:19:46 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-02-18 21:30:27 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2014-02-12 23:19:46 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package routers
|
|
|
|
|
2014-02-13 01:24:09 +05:30
|
|
|
import (
|
2017-02-11 09:30:01 +05:30
|
|
|
"bytes"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2017-02-11 09:30:01 +05:30
|
|
|
"strings"
|
2014-09-06 02:58:09 +05:30
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-12-23 18:01:16 +05:30
|
|
|
code_indexer "code.gitea.io/gitea/modules/indexer/code"
|
2019-02-19 12:49:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-01-12 21:13:44 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2017-10-24 23:06:19 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-03-07 13:42:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/routers/user"
|
2014-02-13 01:24:09 +05:30
|
|
|
)
|
|
|
|
|
2014-06-22 22:44:03 +05:30
|
|
|
const (
|
2016-11-18 08:33:03 +05:30
|
|
|
// tplHome home page template
|
|
|
|
tplHome base.TplName = "home"
|
|
|
|
// tplExploreRepos explore repositories page template
|
|
|
|
tplExploreRepos base.TplName = "explore/repos"
|
|
|
|
// tplExploreUsers explore users page template
|
|
|
|
tplExploreUsers base.TplName = "explore/users"
|
|
|
|
// tplExploreOrganizations explore organizations page template
|
|
|
|
tplExploreOrganizations base.TplName = "explore/organizations"
|
2018-03-16 19:34:33 +05:30
|
|
|
// tplExploreCode explore code page template
|
|
|
|
tplExploreCode base.TplName = "explore/code"
|
2014-06-22 22:44:03 +05:30
|
|
|
)
|
|
|
|
|
2016-11-18 08:33:03 +05:30
|
|
|
// Home render home page
|
2016-03-11 22:26:52 +05:30
|
|
|
func Home(ctx *context.Context) {
|
2014-03-15 20:04:33 +05:30
|
|
|
if ctx.IsSigned {
|
2014-08-10 09:32:00 +05:30
|
|
|
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, user.TplActivate)
|
2019-02-19 12:49:28 +05:30
|
|
|
} else if !ctx.User.IsActive || ctx.User.ProhibitLogin {
|
|
|
|
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
|
2019-02-28 13:31:42 +05:30
|
|
|
} else if ctx.User.MustChangePassword {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.must_change_password")
|
|
|
|
ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password"
|
2021-03-07 13:42:43 +05:30
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
|
2019-02-28 13:31:42 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/change_password")
|
2014-08-10 09:32:00 +05:30
|
|
|
} else {
|
|
|
|
user.Dashboard(ctx)
|
|
|
|
}
|
2014-03-06 19:03:17 +05:30
|
|
|
return
|
2018-06-15 09:12:46 +05:30
|
|
|
// Check non-logged users landing page.
|
|
|
|
} else if setting.LandingPageURL != setting.LandingPageHome {
|
|
|
|
ctx.Redirect(setting.AppSubURL + string(setting.LandingPageURL))
|
|
|
|
return
|
2014-03-06 19:03:17 +05:30
|
|
|
}
|
2014-03-24 22:13:51 +05:30
|
|
|
|
|
|
|
// Check auto-login.
|
2014-07-26 09:54:27 +05:30
|
|
|
uname := ctx.GetCookie(setting.CookieUserName)
|
|
|
|
if len(uname) != 0 {
|
2016-11-27 15:44:25 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login")
|
2014-03-24 22:13:51 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-25 00:58:31 +05:30
|
|
|
ctx.Data["PageIsHome"] = true
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplHome)
|
2014-02-12 23:19:46 +05:30
|
|
|
}
|
2014-03-16 13:11:22 +05:30
|
|
|
|
2016-11-18 08:33:03 +05:30
|
|
|
// RepoSearchOptions when calling search repositories
|
2016-03-15 23:53:12 +05:30
|
|
|
type RepoSearchOptions struct {
|
2020-01-13 23:03:46 +05:30
|
|
|
OwnerID int64
|
|
|
|
Private bool
|
|
|
|
Restricted bool
|
|
|
|
PageSize int
|
|
|
|
TplName base.TplName
|
2016-03-15 23:53:12 +05:30
|
|
|
}
|
|
|
|
|
2016-12-01 16:22:57 +05:30
|
|
|
var (
|
|
|
|
nullByte = []byte{0x00}
|
|
|
|
)
|
|
|
|
|
|
|
|
func isKeywordValid(keyword string) bool {
|
|
|
|
return !bytes.Contains([]byte(keyword), nullByte)
|
|
|
|
}
|
|
|
|
|
2016-11-18 08:33:03 +05:30
|
|
|
// RenderRepoSearch render repositories search page
|
2016-03-15 23:53:12 +05:30
|
|
|
func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
2015-09-01 16:34:35 +05:30
|
|
|
page := ctx.QueryInt("page")
|
2016-07-24 12:02:46 +05:30
|
|
|
if page <= 0 {
|
2015-09-01 16:34:35 +05:30
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2016-03-12 02:03:12 +05:30
|
|
|
var (
|
2016-12-24 20:12:26 +05:30
|
|
|
repos []*models.Repository
|
|
|
|
count int64
|
|
|
|
err error
|
2017-09-22 18:23:21 +05:30
|
|
|
orderBy models.SearchOrderBy
|
2016-03-12 02:03:12 +05:30
|
|
|
)
|
2016-12-24 20:12:26 +05:30
|
|
|
|
2017-10-05 10:32:43 +05:30
|
|
|
ctx.Data["SortType"] = ctx.Query("sort")
|
2016-12-24 20:12:26 +05:30
|
|
|
switch ctx.Query("sort") {
|
2017-10-05 10:32:43 +05:30
|
|
|
case "newest":
|
|
|
|
orderBy = models.SearchOrderByNewest
|
2016-12-24 20:12:26 +05:30
|
|
|
case "oldest":
|
2017-09-22 18:23:21 +05:30
|
|
|
orderBy = models.SearchOrderByOldest
|
2016-12-24 20:12:26 +05:30
|
|
|
case "recentupdate":
|
2017-09-22 18:23:21 +05:30
|
|
|
orderBy = models.SearchOrderByRecentUpdated
|
2016-12-24 20:12:26 +05:30
|
|
|
case "leastupdate":
|
2017-09-22 18:23:21 +05:30
|
|
|
orderBy = models.SearchOrderByLeastUpdated
|
2016-12-24 20:12:26 +05:30
|
|
|
case "reversealphabetically":
|
2017-09-22 18:23:21 +05:30
|
|
|
orderBy = models.SearchOrderByAlphabeticallyReverse
|
2016-12-24 20:12:26 +05:30
|
|
|
case "alphabetically":
|
2017-09-22 18:23:21 +05:30
|
|
|
orderBy = models.SearchOrderByAlphabetically
|
2017-05-02 14:04:28 +05:30
|
|
|
case "reversesize":
|
2017-09-22 18:23:21 +05:30
|
|
|
orderBy = models.SearchOrderBySizeReverse
|
2017-05-02 14:04:28 +05:30
|
|
|
case "size":
|
2017-09-22 18:23:21 +05:30
|
|
|
orderBy = models.SearchOrderBySize
|
2018-05-24 06:33:42 +05:30
|
|
|
case "moststars":
|
|
|
|
orderBy = models.SearchOrderByStarsReverse
|
|
|
|
case "feweststars":
|
|
|
|
orderBy = models.SearchOrderByStars
|
|
|
|
case "mostforks":
|
|
|
|
orderBy = models.SearchOrderByForksReverse
|
|
|
|
case "fewestforks":
|
|
|
|
orderBy = models.SearchOrderByForks
|
2016-12-24 20:12:26 +05:30
|
|
|
default:
|
2017-10-05 10:32:43 +05:30
|
|
|
ctx.Data["SortType"] = "recentupdate"
|
|
|
|
orderBy = models.SearchOrderByRecentUpdated
|
2016-12-24 20:12:26 +05:30
|
|
|
}
|
2015-09-01 16:34:35 +05:30
|
|
|
|
2017-02-11 09:30:01 +05:30
|
|
|
keyword := strings.Trim(ctx.Query("q"), " ")
|
2018-09-13 08:03:48 +05:30
|
|
|
topicOnly := ctx.QueryBool("topic")
|
2019-11-20 14:37:09 +05:30
|
|
|
ctx.Data["TopicOnly"] = topicOnly
|
2017-10-11 02:07:18 +05:30
|
|
|
|
2019-08-25 22:36:36 +05:30
|
|
|
repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
|
2020-01-25 00:30:29 +05:30
|
|
|
ListOptions: models.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: opts.PageSize,
|
|
|
|
},
|
2020-01-13 23:03:46 +05:30
|
|
|
Actor: ctx.User,
|
2019-08-25 22:36:36 +05:30
|
|
|
OrderBy: orderBy,
|
|
|
|
Private: opts.Private,
|
|
|
|
Keyword: keyword,
|
|
|
|
OwnerID: opts.OwnerID,
|
|
|
|
AllPublic: true,
|
2020-01-06 00:18:47 +05:30
|
|
|
AllLimited: true,
|
2019-08-25 22:36:36 +05:30
|
|
|
TopicOnly: topicOnly,
|
|
|
|
IncludeDescription: setting.UI.SearchRepoDescription,
|
2017-10-11 02:07:18 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
2019-08-25 22:36:36 +05:30
|
|
|
ctx.ServerError("SearchRepository", err)
|
2017-10-11 02:07:18 +05:30
|
|
|
return
|
2014-09-06 02:58:09 +05:30
|
|
|
}
|
2016-03-12 02:03:12 +05:30
|
|
|
ctx.Data["Keyword"] = keyword
|
|
|
|
ctx.Data["Total"] = count
|
2014-09-06 02:58:09 +05:30
|
|
|
ctx.Data["Repos"] = repos
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
2014-09-06 02:58:09 +05:30
|
|
|
|
2019-04-20 09:45:19 +05:30
|
|
|
pager := context.NewPagination(int(count), opts.PageSize, page, 5)
|
|
|
|
pager.SetDefaultParams(ctx)
|
2019-11-20 14:37:09 +05:30
|
|
|
pager.AddParam(ctx, "topic", "TopicOnly")
|
2019-04-20 09:45:19 +05:30
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, opts.TplName)
|
2016-03-12 02:03:12 +05:30
|
|
|
}
|
|
|
|
|
2016-11-18 08:33:03 +05:30
|
|
|
// ExploreRepos render explore repositories page
|
2016-03-12 02:03:12 +05:30
|
|
|
func ExploreRepos(ctx *context.Context) {
|
2021-03-11 19:10:54 +05:30
|
|
|
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
|
2016-03-12 02:03:12 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreRepositories"] = true
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
2016-03-12 02:03:12 +05:30
|
|
|
|
2017-10-11 02:07:18 +05:30
|
|
|
var ownerID int64
|
|
|
|
if ctx.User != nil && !ctx.User.IsAdmin {
|
|
|
|
ownerID = ctx.User.ID
|
|
|
|
}
|
|
|
|
|
2016-03-15 23:53:12 +05:30
|
|
|
RenderRepoSearch(ctx, &RepoSearchOptions{
|
2016-07-23 21:53:54 +05:30
|
|
|
PageSize: setting.UI.ExplorePagingNum,
|
2017-10-11 02:07:18 +05:30
|
|
|
OwnerID: ownerID,
|
|
|
|
Private: ctx.User != nil,
|
2016-11-18 08:33:03 +05:30
|
|
|
TplName: tplExploreRepos,
|
2016-03-15 23:53:12 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-18 08:33:03 +05:30
|
|
|
// RenderUserSearch render user search page
|
2017-10-24 23:06:19 +05:30
|
|
|
func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplName base.TplName) {
|
|
|
|
opts.Page = ctx.QueryInt("page")
|
|
|
|
if opts.Page <= 1 {
|
|
|
|
opts.Page = 1
|
2016-03-12 02:03:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-12-24 20:12:26 +05:30
|
|
|
users []*models.User
|
|
|
|
count int64
|
|
|
|
err error
|
2018-05-24 06:33:42 +05:30
|
|
|
orderBy models.SearchOrderBy
|
2016-03-12 02:03:12 +05:30
|
|
|
)
|
|
|
|
|
2016-12-24 20:12:26 +05:30
|
|
|
ctx.Data["SortType"] = ctx.Query("sort")
|
|
|
|
switch ctx.Query("sort") {
|
2017-10-05 10:32:43 +05:30
|
|
|
case "newest":
|
2018-05-24 06:33:42 +05:30
|
|
|
orderBy = models.SearchOrderByIDReverse
|
2016-12-24 20:12:26 +05:30
|
|
|
case "oldest":
|
2018-05-24 06:33:42 +05:30
|
|
|
orderBy = models.SearchOrderByID
|
2016-12-24 20:12:26 +05:30
|
|
|
case "recentupdate":
|
2018-05-24 06:33:42 +05:30
|
|
|
orderBy = models.SearchOrderByRecentUpdated
|
2016-12-24 20:12:26 +05:30
|
|
|
case "leastupdate":
|
2018-05-24 06:33:42 +05:30
|
|
|
orderBy = models.SearchOrderByLeastUpdated
|
2016-12-24 20:12:26 +05:30
|
|
|
case "reversealphabetically":
|
2018-05-24 06:33:42 +05:30
|
|
|
orderBy = models.SearchOrderByAlphabeticallyReverse
|
2016-12-24 20:12:26 +05:30
|
|
|
case "alphabetically":
|
2018-05-24 06:33:42 +05:30
|
|
|
orderBy = models.SearchOrderByAlphabetically
|
2016-12-24 20:12:26 +05:30
|
|
|
default:
|
2017-10-05 10:32:43 +05:30
|
|
|
ctx.Data["SortType"] = "alphabetically"
|
2018-05-24 06:33:42 +05:30
|
|
|
orderBy = models.SearchOrderByAlphabetically
|
2016-12-24 20:12:26 +05:30
|
|
|
}
|
|
|
|
|
2017-10-24 23:06:19 +05:30
|
|
|
opts.Keyword = strings.Trim(ctx.Query("q"), " ")
|
|
|
|
opts.OrderBy = orderBy
|
|
|
|
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
|
|
|
|
users, count, err = models.SearchUsers(opts)
|
2016-03-12 02:03:12 +05:30
|
|
|
if err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("SearchUsers", err)
|
2016-03-12 02:03:12 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 23:06:19 +05:30
|
|
|
ctx.Data["Keyword"] = opts.Keyword
|
2016-03-12 02:03:12 +05:30
|
|
|
ctx.Data["Total"] = count
|
|
|
|
ctx.Data["Users"] = users
|
2020-08-20 07:23:49 +05:30
|
|
|
ctx.Data["UsersTwoFaStatus"] = models.UserList(users).GetTwoFaStatus()
|
2017-01-01 08:21:10 +05:30
|
|
|
ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
2016-03-12 02:03:12 +05:30
|
|
|
|
2019-04-20 09:45:19 +05:30
|
|
|
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
|
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplName)
|
2016-03-12 02:03:12 +05:30
|
|
|
}
|
|
|
|
|
2016-11-18 08:33:03 +05:30
|
|
|
// ExploreUsers render explore users page
|
2016-03-12 02:03:12 +05:30
|
|
|
func ExploreUsers(ctx *context.Context) {
|
2021-03-11 19:10:54 +05:30
|
|
|
if setting.Service.Explore.DisableUsersPage {
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/explore/repos")
|
|
|
|
return
|
|
|
|
}
|
2016-03-12 02:03:12 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreUsers"] = true
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
2016-03-12 02:03:12 +05:30
|
|
|
|
2017-10-24 23:06:19 +05:30
|
|
|
RenderUserSearch(ctx, &models.SearchUserOptions{
|
2020-05-18 04:57:54 +05:30
|
|
|
Actor: ctx.User,
|
2020-01-25 00:30:29 +05:30
|
|
|
Type: models.UserTypeIndividual,
|
|
|
|
ListOptions: models.ListOptions{PageSize: setting.UI.ExplorePagingNum},
|
|
|
|
IsActive: util.OptionalBoolTrue,
|
|
|
|
Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
|
2017-10-24 23:06:19 +05:30
|
|
|
}, tplExploreUsers)
|
2014-09-06 02:58:09 +05:30
|
|
|
}
|
|
|
|
|
2016-11-18 08:33:03 +05:30
|
|
|
// ExploreOrganizations render explore organizations page
|
2016-09-01 18:38:05 +05:30
|
|
|
func ExploreOrganizations(ctx *context.Context) {
|
2021-03-11 19:10:54 +05:30
|
|
|
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
|
2016-09-01 18:38:05 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreOrganizations"] = true
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
2016-09-01 18:38:05 +05:30
|
|
|
|
2020-01-13 23:03:46 +05:30
|
|
|
visibleTypes := []structs.VisibleType{structs.VisibleTypePublic}
|
|
|
|
if ctx.User != nil {
|
|
|
|
visibleTypes = append(visibleTypes, structs.VisibleTypeLimited, structs.VisibleTypePrivate)
|
2019-02-18 21:30:27 +05:30
|
|
|
}
|
|
|
|
|
2020-01-13 23:03:46 +05:30
|
|
|
RenderUserSearch(ctx, &models.SearchUserOptions{
|
2020-05-18 04:57:54 +05:30
|
|
|
Actor: ctx.User,
|
2020-01-25 00:30:29 +05:30
|
|
|
Type: models.UserTypeOrganization,
|
|
|
|
ListOptions: models.ListOptions{PageSize: setting.UI.ExplorePagingNum},
|
|
|
|
Visible: visibleTypes,
|
2020-01-13 23:03:46 +05:30
|
|
|
}, tplExploreOrganizations)
|
2016-09-01 18:38:05 +05:30
|
|
|
}
|
|
|
|
|
2018-03-16 19:34:33 +05:30
|
|
|
// ExploreCode render explore code page
|
|
|
|
func ExploreCode(ctx *context.Context) {
|
|
|
|
if !setting.Indexer.RepoIndexerEnabled {
|
2019-10-24 02:34:22 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL+"/explore", 302)
|
2018-03-16 19:34:33 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-11 19:10:54 +05:30
|
|
|
ctx.Data["UsersIsDisabled"] = setting.Service.Explore.DisableUsersPage
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreCode"] = true
|
|
|
|
|
2020-02-21 01:23:55 +05:30
|
|
|
language := strings.TrimSpace(ctx.Query("l"))
|
2018-03-16 19:34:33 +05:30
|
|
|
keyword := strings.TrimSpace(ctx.Query("q"))
|
|
|
|
page := ctx.QueryInt("page")
|
|
|
|
if page <= 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:30:35 +05:30
|
|
|
queryType := strings.TrimSpace(ctx.Query("t"))
|
|
|
|
isMatch := queryType == "match"
|
|
|
|
|
2018-03-16 19:34:33 +05:30
|
|
|
var (
|
|
|
|
repoIDs []int64
|
|
|
|
err error
|
|
|
|
isAdmin bool
|
|
|
|
)
|
|
|
|
if ctx.User != nil {
|
|
|
|
isAdmin = ctx.User.IsAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
// guest user or non-admin user
|
|
|
|
if ctx.User == nil || !isAdmin {
|
2020-01-13 23:03:46 +05:30
|
|
|
repoIDs, err = models.FindUserAccessibleRepoIDs(ctx.User)
|
2018-03-16 19:34:33 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchResults", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-02-21 01:23:55 +05:30
|
|
|
total int
|
|
|
|
searchResults []*code_indexer.Result
|
|
|
|
searchResultLanguages []*code_indexer.SearchResultLanguages
|
2018-03-16 19:34:33 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// if non-admin login user, we need check UnitTypeCode at first
|
|
|
|
if ctx.User != nil && len(repoIDs) > 0 {
|
|
|
|
repoMaps, err := models.GetRepositoriesMapByIDs(repoIDs)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchResults", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rightRepoMap = make(map[int64]*models.Repository, len(repoMaps))
|
|
|
|
repoIDs = make([]int64, 0, len(repoMaps))
|
|
|
|
for id, repo := range repoMaps {
|
2020-09-16 05:19:34 +05:30
|
|
|
if repo.CheckUnitUser(ctx.User, models.UnitTypeCode) {
|
2018-03-16 19:34:33 +05:30
|
|
|
rightRepoMap[id] = repo
|
|
|
|
repoIDs = append(repoIDs, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["RepoMaps"] = rightRepoMap
|
|
|
|
|
2021-01-27 15:30:35 +05:30
|
|
|
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum, isMatch)
|
2018-03-16 19:34:33 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchResults", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if non-login user or isAdmin, no need to check UnitTypeCode
|
|
|
|
} else if (ctx.User == nil && len(repoIDs) > 0) || isAdmin {
|
2021-01-27 15:30:35 +05:30
|
|
|
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum, isMatch)
|
2018-03-16 19:34:33 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchResults", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var loadRepoIDs = make([]int64, 0, len(searchResults))
|
|
|
|
for _, result := range searchResults {
|
|
|
|
var find bool
|
|
|
|
for _, id := range loadRepoIDs {
|
|
|
|
if id == result.RepoID {
|
|
|
|
find = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !find {
|
|
|
|
loadRepoIDs = append(loadRepoIDs, result.RepoID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoMaps, err := models.GetRepositoriesMapByIDs(loadRepoIDs)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchResults", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["RepoMaps"] = repoMaps
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["Keyword"] = keyword
|
2020-02-21 01:23:55 +05:30
|
|
|
ctx.Data["Language"] = language
|
2021-01-27 15:30:35 +05:30
|
|
|
ctx.Data["queryType"] = queryType
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["SearchResults"] = searchResults
|
2020-02-21 01:23:55 +05:30
|
|
|
ctx.Data["SearchResultLanguages"] = searchResultLanguages
|
2018-03-16 19:34:33 +05:30
|
|
|
ctx.Data["RequireHighlightJS"] = true
|
|
|
|
ctx.Data["PageIsViewCode"] = true
|
2019-04-20 09:45:19 +05:30
|
|
|
|
|
|
|
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
|
|
|
|
pager.SetDefaultParams(ctx)
|
2020-02-21 01:23:55 +05:30
|
|
|
pager.AddParam(ctx, "l", "Language")
|
2019-04-20 09:45:19 +05:30
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplExploreCode)
|
2018-03-16 19:34:33 +05:30
|
|
|
}
|
|
|
|
|
2016-11-18 08:33:03 +05:30
|
|
|
// NotFound render 404 page
|
2016-03-11 22:26:52 +05:30
|
|
|
func NotFound(ctx *context.Context) {
|
2014-03-23 18:10:40 +05:30
|
|
|
ctx.Data["Title"] = "Page Not Found"
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.NotFound("home.NotFound", nil)
|
2014-03-23 11:18:01 +05:30
|
|
|
}
|