2014-02-12 12:49:46 -05: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.
|
|
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
package route
|
2014-02-12 12:49:46 -05:00
|
|
|
|
2014-02-12 14:54:09 -05:00
|
|
|
import (
|
2019-10-23 23:03:17 -07:00
|
|
|
"github.com/unknwon/paginater"
|
2019-10-24 01:51:46 -07:00
|
|
|
user2 "gogs.io/gogs/internal/route/user"
|
2015-09-01 07:04:35 -04:00
|
|
|
|
2020-02-22 09:05:26 +08:00
|
|
|
"gogs.io/gogs/internal/conf"
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/context"
|
|
|
|
|
"gogs.io/gogs/internal/db"
|
2014-02-12 14:54:09 -05:00
|
|
|
)
|
|
|
|
|
|
2014-06-22 13:14:03 -04:00
|
|
|
const (
|
2017-04-05 09:17:21 -04:00
|
|
|
HOME = "home"
|
|
|
|
|
EXPLORE_REPOS = "explore/repos"
|
|
|
|
|
EXPLORE_USERS = "explore/users"
|
|
|
|
|
EXPLORE_ORGANIZATIONS = "explore/organizations"
|
2014-06-22 13:14:03 -04:00
|
|
|
)
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func Home(c *context.Context) {
|
|
|
|
|
if c.IsLogged {
|
2020-02-27 18:06:38 +08:00
|
|
|
if !c.User.IsActive && conf.Auth.RequireEmailConfirmation {
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Data["Title"] = c.Tr("auth.active_your_account")
|
2019-10-24 01:51:46 -07:00
|
|
|
c.Success(user2.ACTIVATE)
|
2014-08-09 21:02:00 -07:00
|
|
|
} else {
|
2019-10-24 01:51:46 -07:00
|
|
|
user2.Dashboard(c)
|
2014-08-09 21:02:00 -07:00
|
|
|
}
|
2014-03-06 21:33:17 +08:00
|
|
|
return
|
|
|
|
|
}
|
2014-03-24 12:43:51 -04:00
|
|
|
|
|
|
|
|
// Check auto-login.
|
2020-02-22 20:46:16 +08:00
|
|
|
uname := c.GetCookie(conf.Security.CookieUsername)
|
2014-07-26 00:24:27 -04:00
|
|
|
if len(uname) != 0 {
|
2020-02-22 09:05:26 +08:00
|
|
|
c.Redirect(conf.Server.Subpath + "/user/login")
|
2014-03-24 12:43:51 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Data["PageIsHome"] = true
|
2017-07-18 22:09:57 -04:00
|
|
|
c.Success(HOME)
|
2014-02-12 12:49:46 -05:00
|
|
|
}
|
2014-03-16 03:41:22 -04:00
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ExploreRepos(c *context.Context) {
|
|
|
|
|
c.Data["Title"] = c.Tr("explore")
|
|
|
|
|
c.Data["PageIsExplore"] = true
|
|
|
|
|
c.Data["PageIsExploreRepositories"] = true
|
2016-03-15 14:23:12 -04:00
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
page := c.QueryInt("page")
|
2016-07-24 14:32:46 +08:00
|
|
|
if page <= 0 {
|
2015-09-01 07:04:35 -04:00
|
|
|
page = 1
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
keyword := c.Query("q")
|
2019-10-24 01:51:46 -07:00
|
|
|
repos, count, err := db.SearchRepositoryByName(&db.SearchRepoOptions{
|
2017-03-17 19:17:40 -04:00
|
|
|
Keyword: keyword,
|
2017-06-03 07:26:09 -04:00
|
|
|
UserID: c.UserID(),
|
2017-03-17 19:17:40 -04:00
|
|
|
OrderBy: "updated_unix DESC",
|
|
|
|
|
Page: page,
|
2020-02-22 09:05:26 +08:00
|
|
|
PageSize: conf.UI.ExplorePagingNum,
|
2017-03-17 19:17:40 -04:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2017-07-18 22:09:57 -04:00
|
|
|
c.ServerError("SearchRepositoryByName", err)
|
2017-03-17 19:17:40 -04:00
|
|
|
return
|
2014-09-05 17:28:09 -04:00
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Data["Keyword"] = keyword
|
|
|
|
|
c.Data["Total"] = count
|
2020-02-22 09:05:26 +08:00
|
|
|
c.Data["Page"] = paginater.New(int(count), conf.UI.ExplorePagingNum, page, 5)
|
2016-03-11 15:33:12 -05:00
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
if err = db.RepositoryList(repos).LoadAttributes(); err != nil {
|
2017-07-18 22:09:57 -04:00
|
|
|
c.ServerError("RepositoryList.LoadAttributes", err)
|
2017-03-17 19:17:40 -04:00
|
|
|
return
|
2014-09-05 17:28:09 -04:00
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Data["Repos"] = repos
|
2014-09-05 17:28:09 -04:00
|
|
|
|
2017-07-18 22:09:57 -04:00
|
|
|
c.Success(EXPLORE_REPOS)
|
2016-03-15 14:23:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UserSearchOptions struct {
|
2019-10-24 01:51:46 -07:00
|
|
|
Type db.UserType
|
2016-03-15 14:23:12 -04:00
|
|
|
Counter func() int64
|
2019-10-24 01:51:46 -07:00
|
|
|
Ranger func(int, int) ([]*db.User, error)
|
2016-03-15 14:23:12 -04:00
|
|
|
PageSize int
|
|
|
|
|
OrderBy string
|
2017-04-05 09:17:21 -04:00
|
|
|
TplName string
|
2016-03-11 15:33:12 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
|
|
|
|
|
page := c.QueryInt("page")
|
2016-03-11 15:33:12 -05:00
|
|
|
if page <= 1 {
|
|
|
|
|
page = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
2019-10-24 01:51:46 -07:00
|
|
|
users []*db.User
|
2016-03-11 15:33:12 -05:00
|
|
|
count int64
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
keyword := c.Query("q")
|
2016-03-11 15:33:12 -05:00
|
|
|
if len(keyword) == 0 {
|
2016-03-15 14:23:12 -04:00
|
|
|
users, err = opts.Ranger(page, opts.PageSize)
|
2016-03-11 15:33:12 -05:00
|
|
|
if err != nil {
|
2017-07-18 22:09:57 -04:00
|
|
|
c.ServerError("Ranger", err)
|
2016-03-11 15:33:12 -05:00
|
|
|
return
|
|
|
|
|
}
|
2016-03-15 14:23:12 -04:00
|
|
|
count = opts.Counter()
|
2016-03-11 15:33:12 -05:00
|
|
|
} else {
|
2019-10-24 01:51:46 -07:00
|
|
|
users, count, err = db.SearchUserByName(&db.SearchUserOptions{
|
2016-03-11 15:33:12 -05:00
|
|
|
Keyword: keyword,
|
2016-03-15 14:23:12 -04:00
|
|
|
Type: opts.Type,
|
|
|
|
|
OrderBy: opts.OrderBy,
|
2016-03-11 15:33:12 -05:00
|
|
|
Page: page,
|
2016-03-15 14:23:12 -04:00
|
|
|
PageSize: opts.PageSize,
|
2016-03-11 15:33:12 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2017-07-18 22:09:57 -04:00
|
|
|
c.ServerError("SearchUserByName", err)
|
2016-03-11 15:33:12 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
c.Data["Keyword"] = keyword
|
|
|
|
|
c.Data["Total"] = count
|
|
|
|
|
c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
|
|
|
|
|
c.Data["Users"] = users
|
2016-03-11 15:33:12 -05:00
|
|
|
|
2017-07-18 22:09:57 -04:00
|
|
|
c.Success(opts.TplName)
|
2016-03-11 15:33:12 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ExploreUsers(c *context.Context) {
|
|
|
|
|
c.Data["Title"] = c.Tr("explore")
|
|
|
|
|
c.Data["PageIsExplore"] = true
|
|
|
|
|
c.Data["PageIsExploreUsers"] = true
|
2016-03-11 15:33:12 -05:00
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
RenderUserSearch(c, &UserSearchOptions{
|
2019-10-24 01:51:46 -07:00
|
|
|
Type: db.USER_TYPE_INDIVIDUAL,
|
|
|
|
|
Counter: db.CountUsers,
|
|
|
|
|
Ranger: db.Users,
|
2020-02-22 09:05:26 +08:00
|
|
|
PageSize: conf.UI.ExplorePagingNum,
|
2016-03-15 14:23:12 -04:00
|
|
|
OrderBy: "updated_unix DESC",
|
|
|
|
|
TplName: EXPLORE_USERS,
|
|
|
|
|
})
|
2014-09-05 17:28:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ExploreOrganizations(c *context.Context) {
|
|
|
|
|
c.Data["Title"] = c.Tr("explore")
|
|
|
|
|
c.Data["PageIsExplore"] = true
|
|
|
|
|
c.Data["PageIsExploreOrganizations"] = true
|
2016-09-01 23:08:05 +10:00
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
RenderUserSearch(c, &UserSearchOptions{
|
2019-10-24 01:51:46 -07:00
|
|
|
Type: db.USER_TYPE_ORGANIZATION,
|
|
|
|
|
Counter: db.CountOrganizations,
|
|
|
|
|
Ranger: db.Organizations,
|
2020-02-22 09:05:26 +08:00
|
|
|
PageSize: conf.UI.ExplorePagingNum,
|
2016-09-01 23:08:05 +10:00
|
|
|
OrderBy: "updated_unix DESC",
|
|
|
|
|
TplName: EXPLORE_ORGANIZATIONS,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func NotFound(c *context.Context) {
|
|
|
|
|
c.Data["Title"] = "Page Not Found"
|
|
|
|
|
c.NotFound()
|
2014-03-23 13:48:01 +08:00
|
|
|
}
|