Files
Gogs/internal/route/home.go

174 lines
4.0 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 route
2014-02-12 12:49:46 -05:00
2014-02-12 14:54:09 -05:00
import (
gocontext "context"
"fmt"
"net/http"
"github.com/go-macaron/i18n"
"github.com/unknwon/paginater"
"gopkg.in/macaron.v1"
2015-09-01 07:04:35 -04:00
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/route/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 && conf.Auth.RequireEmailConfirmation {
2017-06-03 07:26:09 -04:00
c.Data["Title"] = c.Tr("auth.active_your_account")
c.Success(user.ACTIVATE)
2014-08-09 21:02:00 -07:00
} else {
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.
2020-02-22 20:46:16 +08:00
uname := c.GetCookie(conf.Security.CookieUsername)
if uname != "" {
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
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 := db.SearchRepositoryByName(&db.SearchRepoOptions{
Keyword: keyword,
2017-06-03 07:26:09 -04:00
UserID: c.UserID(),
OrderBy: "updated_unix DESC",
Page: page,
PageSize: conf.UI.ExplorePagingNum,
})
if err != nil {
c.Error(err, "search repository by name")
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), conf.UI.ExplorePagingNum, page, 5)
if err = db.RepositoryList(repos).LoadAttributes(); err != nil {
c.Error(err, "load attributes")
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 db.UserType
Counter func(ctx gocontext.Context) int64
Ranger func(ctx gocontext.Context, page, pageSize int) ([]*db.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 []*db.User
count int64
err error
)
2017-06-03 07:26:09 -04:00
keyword := c.Query("q")
if keyword == "" {
users, err = opts.Ranger(c.Req.Context(), page, opts.PageSize)
if err != nil {
c.Error(err, "ranger")
return
}
count = opts.Counter(c.Req.Context())
} else {
users, count, err = db.SearchUserByName(&db.SearchUserOptions{
Keyword: keyword,
Type: opts.Type,
OrderBy: opts.OrderBy,
Page: page,
PageSize: opts.PageSize,
})
if err != nil {
c.Error(err, "search user by name")
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: db.UserTypeIndividual,
Counter: db.Users.Count,
Ranger: db.Users.List,
PageSize: conf.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: db.UserTypeOrganization,
Counter: func(gocontext.Context) int64 {
return db.CountOrganizations()
},
Ranger: func(_ gocontext.Context, page, pageSize int) ([]*db.User, error) {
return db.Organizations(page, pageSize)
},
PageSize: conf.UI.ExplorePagingNum,
OrderBy: "updated_unix DESC",
TplName: EXPLORE_ORGANIZATIONS,
})
}
func NotFound(c *macaron.Context, l i18n.Locale) {
c.Data["Title"] = l.Tr("status.page_not_found")
c.HTML(http.StatusNotFound, fmt.Sprintf("status/%d", http.StatusNotFound))
2014-03-23 13:48:01 +08:00
}