2014-02-12 17:49:46 +00:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package routers
|
|
|
|
|
2014-02-12 19:54:09 +00:00
|
|
|
import (
|
2017-02-11 04:00:01 +00:00
|
|
|
"bytes"
|
|
|
|
"strings"
|
2014-09-05 21:28:09 +00:00
|
|
|
|
2016-11-10 16:24:48 +00:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-10-24 17:36:19 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2016-11-10 16:24:48 +00:00
|
|
|
"code.gitea.io/gitea/routers/user"
|
2017-02-15 06:01:50 +00:00
|
|
|
|
|
|
|
"github.com/Unknwon/paginater"
|
2014-02-12 19:54:09 +00:00
|
|
|
)
|
|
|
|
|
2014-06-22 17:14:03 +00:00
|
|
|
const (
|
2016-11-18 03:03:03 +00:00
|
|
|
// 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"
|
2014-06-22 17:14:03 +00:00
|
|
|
)
|
|
|
|
|
2016-11-18 03:03:03 +00:00
|
|
|
// Home render home page
|
2016-03-11 16:56:52 +00:00
|
|
|
func Home(ctx *context.Context) {
|
2014-03-15 14:34:33 +00:00
|
|
|
if ctx.IsSigned {
|
2014-08-10 04:02:00 +00:00
|
|
|
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
2016-11-18 03:03:03 +00:00
|
|
|
ctx.HTML(200, user.TplActivate)
|
2014-08-10 04:02:00 +00:00
|
|
|
} else {
|
|
|
|
user.Dashboard(ctx)
|
|
|
|
}
|
2014-03-06 13:33:17 +00:00
|
|
|
return
|
|
|
|
}
|
2014-03-24 16:43:51 +00:00
|
|
|
|
|
|
|
// Check auto-login.
|
2014-07-26 04:24:27 +00:00
|
|
|
uname := ctx.GetCookie(setting.CookieUserName)
|
|
|
|
if len(uname) != 0 {
|
2016-11-27 10:14:25 +00:00
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login")
|
2014-03-24 16:43:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-24 19:28:31 +00:00
|
|
|
ctx.Data["PageIsHome"] = true
|
2016-11-18 03:03:03 +00:00
|
|
|
ctx.HTML(200, tplHome)
|
2014-02-12 17:49:46 +00:00
|
|
|
}
|
2014-03-16 07:41:22 +00:00
|
|
|
|
2016-11-18 03:03:03 +00:00
|
|
|
// RepoSearchOptions when calling search repositories
|
2016-03-15 18:23:12 +00:00
|
|
|
type RepoSearchOptions struct {
|
2017-10-10 20:37:18 +00:00
|
|
|
OwnerID int64
|
2016-03-15 18:23:12 +00:00
|
|
|
Private bool
|
|
|
|
PageSize int
|
|
|
|
TplName base.TplName
|
|
|
|
}
|
|
|
|
|
2016-12-01 10:52:57 +00:00
|
|
|
var (
|
|
|
|
nullByte = []byte{0x00}
|
|
|
|
)
|
|
|
|
|
|
|
|
func isKeywordValid(keyword string) bool {
|
|
|
|
return !bytes.Contains([]byte(keyword), nullByte)
|
|
|
|
}
|
|
|
|
|
2016-11-18 03:03:03 +00:00
|
|
|
// RenderRepoSearch render repositories search page
|
2016-03-15 18:23:12 +00:00
|
|
|
func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
2015-09-01 11:04:35 +00:00
|
|
|
page := ctx.QueryInt("page")
|
2016-07-24 06:32:46 +00:00
|
|
|
if page <= 0 {
|
2015-09-01 11:04:35 +00:00
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2016-03-11 20:33:12 +00:00
|
|
|
var (
|
2016-12-24 14:42:26 +00:00
|
|
|
repos []*models.Repository
|
|
|
|
count int64
|
|
|
|
err error
|
2017-09-22 12:53:21 +00:00
|
|
|
orderBy models.SearchOrderBy
|
2016-03-11 20:33:12 +00:00
|
|
|
)
|
2016-12-24 14:42:26 +00:00
|
|
|
|
2017-10-05 05:02:43 +00:00
|
|
|
ctx.Data["SortType"] = ctx.Query("sort")
|
2016-12-24 14:42:26 +00:00
|
|
|
switch ctx.Query("sort") {
|
2017-10-05 05:02:43 +00:00
|
|
|
case "newest":
|
|
|
|
orderBy = models.SearchOrderByNewest
|
2016-12-24 14:42:26 +00:00
|
|
|
case "oldest":
|
2017-09-22 12:53:21 +00:00
|
|
|
orderBy = models.SearchOrderByOldest
|
2016-12-24 14:42:26 +00:00
|
|
|
case "recentupdate":
|
2017-09-22 12:53:21 +00:00
|
|
|
orderBy = models.SearchOrderByRecentUpdated
|
2016-12-24 14:42:26 +00:00
|
|
|
case "leastupdate":
|
2017-09-22 12:53:21 +00:00
|
|
|
orderBy = models.SearchOrderByLeastUpdated
|
2016-12-24 14:42:26 +00:00
|
|
|
case "reversealphabetically":
|
2017-09-22 12:53:21 +00:00
|
|
|
orderBy = models.SearchOrderByAlphabeticallyReverse
|
2016-12-24 14:42:26 +00:00
|
|
|
case "alphabetically":
|
2017-09-22 12:53:21 +00:00
|
|
|
orderBy = models.SearchOrderByAlphabetically
|
2017-05-02 08:34:28 +00:00
|
|
|
case "reversesize":
|
2017-09-22 12:53:21 +00:00
|
|
|
orderBy = models.SearchOrderBySizeReverse
|
2017-05-02 08:34:28 +00:00
|
|
|
case "size":
|
2017-09-22 12:53:21 +00:00
|
|
|
orderBy = models.SearchOrderBySize
|
2016-12-24 14:42:26 +00:00
|
|
|
default:
|
2017-10-05 05:02:43 +00:00
|
|
|
ctx.Data["SortType"] = "recentupdate"
|
|
|
|
orderBy = models.SearchOrderByRecentUpdated
|
2016-12-24 14:42:26 +00:00
|
|
|
}
|
2015-09-01 11:04:35 +00:00
|
|
|
|
2017-02-11 04:00:01 +00:00
|
|
|
keyword := strings.Trim(ctx.Query("q"), " ")
|
2017-10-10 20:37:18 +00:00
|
|
|
|
|
|
|
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
|
2017-10-26 21:16:13 +00:00
|
|
|
Page: page,
|
|
|
|
PageSize: opts.PageSize,
|
|
|
|
OrderBy: orderBy,
|
|
|
|
Private: opts.Private,
|
|
|
|
Keyword: keyword,
|
|
|
|
OwnerID: opts.OwnerID,
|
|
|
|
AllPublic: true,
|
2017-10-10 20:37:18 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 21:34:17 +00:00
|
|
|
ctx.ServerError("SearchRepositoryByName", err)
|
2017-10-10 20:37:18 +00:00
|
|
|
return
|
2014-09-05 21:28:09 +00:00
|
|
|
}
|
2016-03-11 20:33:12 +00:00
|
|
|
ctx.Data["Keyword"] = keyword
|
|
|
|
ctx.Data["Total"] = count
|
2016-03-15 18:23:12 +00:00
|
|
|
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
|
2014-09-05 21:28:09 +00:00
|
|
|
ctx.Data["Repos"] = repos
|
|
|
|
|
2016-03-15 18:23:12 +00:00
|
|
|
ctx.HTML(200, opts.TplName)
|
2016-03-11 20:33:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 03:03:03 +00:00
|
|
|
// ExploreRepos render explore repositories page
|
2016-03-11 20:33:12 +00:00
|
|
|
func ExploreRepos(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreRepositories"] = true
|
|
|
|
|
2017-10-10 20:37:18 +00:00
|
|
|
var ownerID int64
|
|
|
|
if ctx.User != nil && !ctx.User.IsAdmin {
|
|
|
|
ownerID = ctx.User.ID
|
|
|
|
}
|
|
|
|
|
2016-03-15 18:23:12 +00:00
|
|
|
RenderRepoSearch(ctx, &RepoSearchOptions{
|
2016-07-23 16:23:54 +00:00
|
|
|
PageSize: setting.UI.ExplorePagingNum,
|
2017-10-10 20:37:18 +00:00
|
|
|
OwnerID: ownerID,
|
|
|
|
Private: ctx.User != nil,
|
2016-11-18 03:03:03 +00:00
|
|
|
TplName: tplExploreRepos,
|
2016-03-15 18:23:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-18 03:03:03 +00:00
|
|
|
// RenderUserSearch render user search page
|
2017-10-24 17:36:19 +00:00
|
|
|
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-11 20:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-12-24 14:42:26 +00:00
|
|
|
users []*models.User
|
|
|
|
count int64
|
|
|
|
err error
|
|
|
|
orderBy string
|
2016-03-11 20:33:12 +00:00
|
|
|
)
|
|
|
|
|
2016-12-24 14:42:26 +00:00
|
|
|
ctx.Data["SortType"] = ctx.Query("sort")
|
|
|
|
switch ctx.Query("sort") {
|
2017-10-05 05:02:43 +00:00
|
|
|
case "newest":
|
|
|
|
orderBy = "id DESC"
|
2016-12-24 14:42:26 +00:00
|
|
|
case "oldest":
|
|
|
|
orderBy = "id ASC"
|
|
|
|
case "recentupdate":
|
|
|
|
orderBy = "updated_unix DESC"
|
|
|
|
case "leastupdate":
|
|
|
|
orderBy = "updated_unix ASC"
|
|
|
|
case "reversealphabetically":
|
|
|
|
orderBy = "name DESC"
|
|
|
|
case "alphabetically":
|
|
|
|
orderBy = "name ASC"
|
|
|
|
default:
|
2017-10-05 05:02:43 +00:00
|
|
|
ctx.Data["SortType"] = "alphabetically"
|
|
|
|
orderBy = "name ASC"
|
2016-12-24 14:42:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 17:36:19 +00:00
|
|
|
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-11 20:33:12 +00:00
|
|
|
if err != nil {
|
2018-01-10 21:34:17 +00:00
|
|
|
ctx.ServerError("SearchUsers", err)
|
2016-03-11 20:33:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 17:36:19 +00:00
|
|
|
ctx.Data["Keyword"] = opts.Keyword
|
2016-03-11 20:33:12 +00:00
|
|
|
ctx.Data["Total"] = count
|
2017-10-24 17:36:19 +00:00
|
|
|
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, opts.Page, 5)
|
2016-03-11 20:33:12 +00:00
|
|
|
ctx.Data["Users"] = users
|
2017-01-01 02:51:10 +00:00
|
|
|
ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
|
2016-03-11 20:33:12 +00:00
|
|
|
|
2017-10-24 17:36:19 +00:00
|
|
|
ctx.HTML(200, tplName)
|
2016-03-11 20:33:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 03:03:03 +00:00
|
|
|
// ExploreUsers render explore users page
|
2016-03-11 20:33:12 +00:00
|
|
|
func ExploreUsers(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreUsers"] = true
|
|
|
|
|
2017-10-24 17:36:19 +00:00
|
|
|
RenderUserSearch(ctx, &models.SearchUserOptions{
|
2016-11-07 16:53:22 +00:00
|
|
|
Type: models.UserTypeIndividual,
|
2016-07-23 16:23:54 +00:00
|
|
|
PageSize: setting.UI.ExplorePagingNum,
|
2017-10-24 17:36:19 +00:00
|
|
|
IsActive: util.OptionalBoolTrue,
|
|
|
|
}, tplExploreUsers)
|
2014-09-05 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 03:03:03 +00:00
|
|
|
// ExploreOrganizations render explore organizations page
|
2016-09-01 13:08:05 +00:00
|
|
|
func ExploreOrganizations(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("explore")
|
|
|
|
ctx.Data["PageIsExplore"] = true
|
|
|
|
ctx.Data["PageIsExploreOrganizations"] = true
|
|
|
|
|
2017-10-24 17:36:19 +00:00
|
|
|
RenderUserSearch(ctx, &models.SearchUserOptions{
|
2016-11-07 16:53:22 +00:00
|
|
|
Type: models.UserTypeOrganization,
|
2016-09-01 13:08:05 +00:00
|
|
|
PageSize: setting.UI.ExplorePagingNum,
|
2017-10-24 17:36:19 +00:00
|
|
|
}, tplExploreOrganizations)
|
2016-09-01 13:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 03:03:03 +00:00
|
|
|
// NotFound render 404 page
|
2016-03-11 16:56:52 +00:00
|
|
|
func NotFound(ctx *context.Context) {
|
2014-03-23 12:40:40 +00:00
|
|
|
ctx.Data["Title"] = "Page Not Found"
|
2018-01-10 21:34:17 +00:00
|
|
|
ctx.NotFound("home.NotFound", nil)
|
2014-03-23 05:48:01 +00:00
|
|
|
}
|