Files
Gogs/routes/home.go

164 lines
3.7 KiB
Go
Raw Normal View History

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.
package routes
2014-02-12 12:49:46 -05:00
2014-02-12 14:54:09 -05:00
import (
"github.com/unknwon/paginater"
2015-09-01 07:04:35 -04:00
"gogs.io/gogs/models"
"gogs.io/gogs/pkg/context"
"gogs.io/gogs/pkg/setting"
"gogs.io/gogs/routes/user"
2014-02-12 14:54:09 -05:00
)
const (
2017-04-05 09:17:21 -04:00
HOME = "home"
EXPLORE_REPOS = "explore/repos"
EXPLORE_USERS = "explore/users"
EXPLORE_ORGANIZATIONS = "explore/organizations"
)
2017-06-03 07:26:09 -04:00
func Home(c *context.Context) {
if c.IsLogged {
if !c.User.IsActive && setting.Service.RegisterEmailConfirm {
c.Data["Title"] = c.Tr("auth.active_your_account")
2017-07-18 22:09:57 -04:00
c.Success(user.ACTIVATE)
2014-08-09 21:02:00 -07:00
} else {
2017-06-03 07:26:09 -04:00
user.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.
2017-06-03 07:26:09 -04:00
uname := c.GetCookie(setting.CookieUserName)
2014-07-26 00:24:27 -04:00
if len(uname) != 0 {
2017-06-03 07:26:09 -04:00
c.Redirect(setting.AppSubURL + "/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
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")
repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword,
2017-06-03 07:26:09 -04:00
UserID: c.UserID(),
OrderBy: "updated_unix DESC",
Page: page,
PageSize: setting.UI.ExplorePagingNum,
})
if err != nil {
2017-07-18 22:09:57 -04:00
c.ServerError("SearchRepositoryByName", err)
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
c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
2017-07-18 22:09:57 -04:00
c.ServerError("RepositoryList.LoadAttributes", err)
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)
}
type UserSearchOptions struct {
Type models.UserType
Counter func() int64
Ranger func(int, int) ([]*models.User, error)
PageSize int
OrderBy string
2017-04-05 09:17:21 -04:00
TplName string
}
2017-06-03 07:26:09 -04:00
func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
page := c.QueryInt("page")
if page <= 1 {
page = 1
}
var (
users []*models.User
count int64
err error
)
2017-06-03 07:26:09 -04:00
keyword := c.Query("q")
if len(keyword) == 0 {
users, err = opts.Ranger(page, opts.PageSize)
if err != nil {
2017-07-18 22:09:57 -04:00
c.ServerError("Ranger", err)
return
}
count = opts.Counter()
} else {
users, count, err = models.SearchUserByName(&models.SearchUserOptions{
Keyword: keyword,
Type: opts.Type,
OrderBy: opts.OrderBy,
Page: page,
PageSize: opts.PageSize,
})
if err != nil {
2017-07-18 22:09:57 -04:00
c.ServerError("SearchUserByName", err)
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
2017-07-18 22:09:57 -04:00
c.Success(opts.TplName)
}
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
2017-06-03 07:26:09 -04:00
RenderUserSearch(c, &UserSearchOptions{
Type: models.USER_TYPE_INDIVIDUAL,
Counter: models.CountUsers,
Ranger: models.Users,
2016-07-24 00:23:54 +08:00
PageSize: setting.UI.ExplorePagingNum,
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
2017-06-03 07:26:09 -04:00
RenderUserSearch(c, &UserSearchOptions{
Type: models.USER_TYPE_ORGANIZATION,
Counter: models.CountOrganizations,
Ranger: models.Organizations,
PageSize: setting.UI.ExplorePagingNum,
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
}